Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
""" Test log_helpers """
import sys
from tests.common.log_helper import log_helper_test_imported
from oletools.common.log_helper import log_helper
DEBUG_MESSAGE = 'main: debug log'
INFO_MESSAGE = 'main: info log'
WARNING_MESSAGE = 'main: warning log'
ERROR_MESSAGE = 'main: error log'
CRITICAL_MESSAGE = 'main: critical log'
RESULT_MESSAGE = 'main: result log'
RESULT_TYPE = 'main: result'
logger = log_helper.get_or_create_silent_logger('test_main')
def init_logging_and_log(args):
"""
Try to cover possible logging scenarios. For each scenario covered, here's the expected args and outcome:
- Log without enabling: ['']
* logging when being imported - should never print
- Log as JSON without enabling: ['as-json', '']
* logging as JSON when being imported - should never print
- Enable and log: ['enable', '']
* logging when being run as script - should log messages
- Enable and log as JSON: ['as-json', 'enable', '']
* logging as JSON when being run as script - should log messages as JSON
- Enable, log as JSON and throw: ['enable', 'as-json', 'throw', '']
* should produce JSON-compatible output, even after an unhandled exception
"""
def run_print():
"""This is called from test_read* tests as script. Prints & logs unicode"""
from oletools.common.io_encoding import ensure_stdout_handles_unicode
from oletools.common.log_helper import log_helper
ensure_stdout_handles_unicode()
print(u'Check: \u2713') # print check mark
# check logging as well
logger = log_helper.get_or_create_silent_logger('test_encoding_handler')
log_helper.enable_logging(False, 'debug', stream=sys.stdout)
logger.info(u'Check: \u2713')
return 0
Dummy file that logs messages, meant to be imported
by the main test file
"""
from oletools.common.log_helper import log_helper
import logging
DEBUG_MESSAGE = 'imported: debug log'
INFO_MESSAGE = 'imported: info log'
WARNING_MESSAGE = 'imported: warning log'
ERROR_MESSAGE = 'imported: error log'
CRITICAL_MESSAGE = 'imported: critical log'
RESULT_MESSAGE = 'imported: result log'
RESULT_TYPE = 'imported: result'
logger = log_helper.get_or_create_silent_logger('test_imported', logging.ERROR)
def log():
logger.debug(DEBUG_MESSAGE)
logger.info(INFO_MESSAGE)
logger.warning(WARNING_MESSAGE)
logger.error(ERROR_MESSAGE)
logger.critical(CRITICAL_MESSAGE)
logger.info(RESULT_MESSAGE, type=RESULT_TYPE)
FIELD_FILTER_ALL = 'keep all'
FIELD_FILTER_DEFAULT = FIELD_FILTER_BLACKLIST
# banner to be printed at program start
BANNER = """msodde %s - http://decalage.info/python/oletools
THIS IS WORK IN PROGRESS - Check updates regularly!
Please report any issue at https://github.com/decalage2/oletools/issues
""" % __version__
# === LOGGING =================================================================
DEFAULT_LOG_LEVEL = "warning" # Default log level
# a global logger object used for debugging:
logger = log_helper.get_or_create_silent_logger('msodde')
# === ARGUMENT PARSING =======================================================
class ArgParserWithBanner(argparse.ArgumentParser):
""" Print banner before showing any error """
def error(self, message):
print(BANNER)
super(ArgParserWithBanner, self).error(message)
def existing_file(filename):
""" called by argument parser to see whether given file exists """
if not os.path.exists(filename):
raise argparse.ArgumentTypeError('File {0} does not exist.'
.format(filename))
import sys
from zipfile import ZipFile, BadZipfile, is_zipfile
from os.path import splitext
import io
import re
from oletools.common.log_helper import log_helper
# import lxml or ElementTree for XML parsing:
try:
# lxml: best performance for XML processing
import lxml.etree as ET
except ImportError:
import xml.etree.cElementTree as ET
logger = log_helper.get_or_create_silent_logger('ooxml')
#: subfiles that have to be part of every ooxml file
FILE_CONTENT_TYPES = '[Content_Types].xml'
FILE_RELATIONSHIPS = '_rels/.rels'
#: start of content type attributes
CONTENT_TYPES_EXCEL = (
'application/vnd.openxmlformats-officedocument.spreadsheetml.',
'application/vnd.ms-excel.',
)
CONTENT_TYPES_WORD = (
'application/vnd.openxmlformats-officedocument.wordprocessingml.',
)
CONTENT_TYPES_PPT = (
'application/vnd.openxmlformats-officedocument.presentationml.',
)