Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _handle_fresh_imports(self, name, import_level_prefix, level):
"""Check which modules were imported since last point we checked and add them to the queue
"""
new_imported_modules = set(sys.modules.keys()) - self._processed_modules - self.__queue_to_process
if new_imported_modules:
lgr.log(4, "%s%d new modules were detected upon import of %s (level=%s)",
import_level_prefix, len(new_imported_modules), name, level)
# lgr.log(2, "%s%d new modules were detected: %s, upon import of %s (level=%s)",
# import_level_prefix, len(new_imported_modules), new_imported_modules, name, level)
for imported_mod in new_imported_modules:
if imported_mod in self.__queue_to_process:
# we saw it already
continue
# lgr.log(1, "Name %r was imported as %r (path: %s). fromlist: %s, level: %s",
# name, mod.__name__, getattr(mod, '__path__', None), fromlist, level)
# package
package = imported_mod.split('.', 1)[0]
if package != imported_mod \
and package not in self._processed_modules \
and package not in self.__queue_to_process:
# if its parent package wasn't yet imported before
lgr.log(3, "%sParent of %s, %s wasn't yet processed, adding to the queue",
parser = setup_parser()
# parse cmd args
args = parser.parse_args(args)
# run the function associated with the selected command
if args.common_debug:
# So we could see/stop clearly at the point of failure
setup_exceptionhook()
args.func(args)
else:
# Otherwise - guard and only log the summary. Postmortem is not
# as convenient if being caught in this ultimate except
try:
args.func(args)
except Exception as exc:
lgr.error('%s (%s)' % (str(exc), exc.__class__.__name__))
sys.exit(1)
try:
with open(progname) as fp:
code = compile(fp.read(), progname, 'exec')
# try to emulate __main__ namespace as much as possible
globs = {
'__file__': progname,
'__name__': '__main__',
'__package__': None,
'__cached__': None,
}
# Since used explicitly -- activate the beast
due.activate(True)
runctx(code, globs, globs)
# TODO: see if we could hide our presence from the final tracebacks if execution fails
except IOError as err:
lgr.error("Cannot run file %r because: %s" % (sys.argv[0], err))
sys.exit(1)
except SystemExit:
pass
if 'parser_args' in subcmdmod.__dict__:
parser_args = subcmdmod.parser_args
else:
parser_args = dict()
# use module description, if no explicit description is available
if not 'description' in parser_args:
parser_args['description'] = subcmdmod.__doc__
# create subparser, use module suffix as cmd name
subparser = subparsers.add_parser(cmd_name, add_help=False, **parser_args)
# all subparser can report the version
helpers.parser_add_common_opt(
subparser, 'version',
version='duecredit %s %s\n\n%s' % (cmd_name, __version__,
_license_info()))
# our own custom help for all commands
helpers.parser_add_common_opt(subparser, 'help')
helpers.parser_add_common_opt(subparser, 'log_level')
# let module configure the parser
subcmdmod.setup_parser(subparser)
# logger for command
# configure 'run' function for this command
subparser.set_defaults(func=subcmdmod.run,
logger=logging.getLogger('duecredit.%s' % cmd))
# store short description for later
sdescr = getattr(subcmdmod, 'short_description',
parser_args['description'].split('\n')[0])
cmd_short_description.append((cmd_name, sdescr))
# create command summary
cmd_summary = []
for cd in cmd_short_description:
def setup_parser():
# setup cmdline args parser
# main parser
parser = argparse.ArgumentParser(
fromfile_prefix_chars='@',
# usage="%(prog)s ...",
description="""\
DueCredit simplifies citation of papers describing methods, software and data used by any given analysis script/pipeline.
""",
epilog='"Your Credit is Due"',
formatter_class=argparse.RawDescriptionHelpFormatter,
add_help=False
)
# common options
helpers.parser_add_common_opt(parser, 'help')
helpers.parser_add_common_opt(parser, 'log_level')
helpers.parser_add_common_opt(parser,
'version',
version='duecredit %s\n\n%s' % (__version__,
_license_info()))
if __debug__:
parser.add_argument(
'--dbg', action='store_true', dest='common_debug',
help="do not catch exceptions and show exception traceback")
# yoh: atm we only dump to console. Might adopt the same separation later on
# and for consistency will call it --verbose-level as well for now
# log-level is set via common_opts ATM
# parser.add_argument('--log-level',
# choices=('critical', 'error', 'warning', 'info', 'debug'),
# dest='common_log_level',
# main parser
parser = argparse.ArgumentParser(
fromfile_prefix_chars='@',
# usage="%(prog)s ...",
description="""\
DueCredit simplifies citation of papers describing methods, software and data used by any given analysis script/pipeline.
""",
epilog='"Your Credit is Due"',
formatter_class=argparse.RawDescriptionHelpFormatter,
add_help=False
)
# common options
helpers.parser_add_common_opt(parser, 'help')
helpers.parser_add_common_opt(parser, 'log_level')
helpers.parser_add_common_opt(parser,
'version',
version='duecredit %s\n\n%s' % (__version__,
_license_info()))
if __debug__:
parser.add_argument(
'--dbg', action='store_true', dest='common_debug',
help="do not catch exceptions and show exception traceback")
# yoh: atm we only dump to console. Might adopt the same separation later on
# and for consistency will call it --verbose-level as well for now
# log-level is set via common_opts ATM
# parser.add_argument('--log-level',
# choices=('critical', 'error', 'warning', 'info', 'debug'),
# dest='common_log_level',
# help="""level of verbosity in log files. By default
# everything, including debug messages is logged.""")
parser_args = subcmdmod.parser_args
else:
parser_args = dict()
# use module description, if no explicit description is available
if not 'description' in parser_args:
parser_args['description'] = subcmdmod.__doc__
# create subparser, use module suffix as cmd name
subparser = subparsers.add_parser(cmd_name, add_help=False, **parser_args)
# all subparser can report the version
helpers.parser_add_common_opt(
subparser, 'version',
version='duecredit %s %s\n\n%s' % (cmd_name, __version__,
_license_info()))
# our own custom help for all commands
helpers.parser_add_common_opt(subparser, 'help')
helpers.parser_add_common_opt(subparser, 'log_level')
# let module configure the parser
subcmdmod.setup_parser(subparser)
# logger for command
# configure 'run' function for this command
subparser.set_defaults(func=subcmdmod.run,
logger=logging.getLogger('duecredit.%s' % cmd))
# store short description for later
sdescr = getattr(subcmdmod, 'short_description',
parser_args['description'].split('\n')[0])
cmd_short_description.append((cmd_name, sdescr))
# create command summary
cmd_summary = []
for cd in cmd_short_description:
cmd_summary.append('%s\n%s\n\n' \
# setup cmdline args parser
# main parser
parser = argparse.ArgumentParser(
fromfile_prefix_chars='@',
# usage="%(prog)s ...",
description="""\
DueCredit simplifies citation of papers describing methods, software and data used by any given analysis script/pipeline.
""",
epilog='"Your Credit is Due"',
formatter_class=argparse.RawDescriptionHelpFormatter,
add_help=False
)
# common options
helpers.parser_add_common_opt(parser, 'help')
helpers.parser_add_common_opt(parser, 'log_level')
helpers.parser_add_common_opt(parser,
'version',
version='duecredit %s\n\n%s' % (__version__,
_license_info()))
if __debug__:
parser.add_argument(
'--dbg', action='store_true', dest='common_debug',
help="do not catch exceptions and show exception traceback")
# yoh: atm we only dump to console. Might adopt the same separation later on
# and for consistency will call it --verbose-level as well for now
# log-level is set via common_opts ATM
# parser.add_argument('--log-level',
# choices=('critical', 'error', 'warning', 'info', 'debug'),
# dest='common_log_level',
# help="""level of verbosity in log files. By default
subcmdmod = getattr(__import__('duecredit.cmdline',
globals(), locals(),
[cmd], 0),
cmd)
# deal with optional parser args
if 'parser_args' in subcmdmod.__dict__:
parser_args = subcmdmod.parser_args
else:
parser_args = dict()
# use module description, if no explicit description is available
if not 'description' in parser_args:
parser_args['description'] = subcmdmod.__doc__
# create subparser, use module suffix as cmd name
subparser = subparsers.add_parser(cmd_name, add_help=False, **parser_args)
# all subparser can report the version
helpers.parser_add_common_opt(
subparser, 'version',
version='duecredit %s %s\n\n%s' % (cmd_name, __version__,
_license_info()))
# our own custom help for all commands
helpers.parser_add_common_opt(subparser, 'help')
helpers.parser_add_common_opt(subparser, 'log_level')
# let module configure the parser
subcmdmod.setup_parser(subparser)
# logger for command
# configure 'run' function for this command
subparser.set_defaults(func=subcmdmod.run,
logger=logging.getLogger('duecredit.%s' % cmd))
# store short description for later
sdescr = getattr(subcmdmod, 'short_description',
parser_args['description'].split('\n')[0])
def __init__(self, collector, outputs="stdout,pickle", fn=DUECREDIT_FILE):
self._due = collector
self.fn = fn
# for now decide on output "format" right here
self._outputs = [
self._get_output_handler(
type_.lower().strip(), collector, fn=fn)
for type_ in os.environ.get('DUECREDIT_OUTPUTS', outputs).split(',')
if type_
]