Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
- 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
"""
# the level should always be the last argument passed
level = args[-1]
use_json = 'as-json' in args
throw = 'throw' in args
percent_autoformat = '%-autoformat' in args
if 'enable' in args:
log_helper.enable_logging(use_json, level, stream=sys.stdout)
_log()
if percent_autoformat:
logger.info('The %s is %d.', 'answer', 47)
if throw:
raise Exception('An exception occurred before ending the logging')
log_helper.end_logging()
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
def test():
""" Main function, called when running file as script
see module doc for more info
"""
log_helper.enable_logging(False, 'debug')
if len(sys.argv) != 2:
print(u'To test this code, give me a single file as arg')
return 2
# test get_type
print('Detected type: ' + get_type(sys.argv[1]))
# test complete parsing
parser = XmlParser(sys.argv[1])
for subfile, elem, depth in parser.iter_xml():
if depth < 4:
print(u'{0} {1}{2}'.format(subfile, ' ' * depth, debug_str(elem)))
for index, (subfile, content_type, _) in enumerate(parser.iter_non_xml()):
print(u'Non-XML subfile: {0} of type {1}'
.format(subfile, content_type or u'unknown'))
if index > 100:
def main(cmd_line_args=None):
""" Main function, called if this file is called as a script
Optional argument: command line arguments to be forwarded to ArgumentParser
in process_args. Per default (cmd_line_args=None), sys.argv is used. Option
mainly added for unit-testing
"""
args = process_args(cmd_line_args)
# Setup logging to the console:
# here we use stdout instead of stderr by default, so that the output
# can be redirected properly.
log_helper.enable_logging(args.json, args.loglevel, stream=sys.stdout)
if args.nounquote:
global NO_QUOTES
NO_QUOTES = True
logger.print_str(BANNER)
logger.print_str('Opening file: %s' % args.filepath)
text = ''
return_code = 1
try:
text = process_maybe_encrypted(
args.filepath, args.password,
field_filter_mode=args.field_filter_mode)
return_code = 0
except Exception as exc: