text
stringlengths
0
828
frame_local = sys._getframe(1).f_locals
if '__name__' in frame_local and frame_local['__name__'] == '__main__':
argv = sys.argv[1:]
parser = signature_parser(func)
try:
kwargs = parser.parse_args(argv).__dict__
# special cli flags
# --version is handled by ArgParse
# if kwargs.get('version'):
# print module_version(func)
# return
if 'version' in kwargs.keys():
del kwargs['version']
# --debug
FORMAT = '%(asctime)-6s: %(name)s - %(levelname)s - %(message)s'
if kwargs.get('debug'):
logging.basicConfig(
level=logging.DEBUG,
format=FORMAT,
)
del kwargs['debug']
if ""__args"" in kwargs:
return func(*_correct_args(func, kwargs))
else:
return func(**kwargs)
except UsageError, e:
parser.error(e.message)
return func"
523,"def autorun(func, _depth=1):
""""""
Runs the function if the module in which it is declared is being run
directly from the commandline. Putting the following after the function
definition would be similar:
if __name__ == '__main__':
func()
NOTE: This will work most expectedly as the outermost decorator, as it
will call the function before any more outwards decorators have been
applied.
""""""
frame_local = sys._getframe(_depth).f_locals
if '__name__' in frame_local and frame_local['__name__'] == '__main__':
func(argv=sys.argv[1:])
return func"
524,"def acceptargv(func):
""""""
Transforms the signature of the function, and it's associated __doc__
into an argparse-parser, then calls the function with the results of
using said parser.
The function returned takes an optional argument, which is the list of
parameters, if they are not given, sys.argv[1:] is used instead.
The function may raise a UsageError() if it wants to signal an error
that the user has made with the parameters, this is done by
@withuserfile for example.
CAVEAT: this relies on the argument signature of the function, if that
has been destroyed, perhaps by a badly behaved decorator, this
won't work as expected.
CAVEAT2: this destroys the argument signature of the function ;)
""""""
parser = signature_parser(func)
def main(*args, **kw):
argv = kw.get('argv', None)
if argv == None:
return func(*args, **kw)
else:
try:
kwargs = parser.parse_args(argv).__dict__
# special cli flags
# --version is handled by ArgParse
# if kwargs.get('version'):
# print module_version(func)
# return
if 'version' in kwargs.keys():
del kwargs['version']
# --debug
if kwargs.get('debug'):
logging.basicConfig(level=logging.DEBUG)
del kwargs['debug']
if ""__args"" in kwargs:
return func(*_correct_args(func, kwargs))