def autocomplete (self):
try:
import argcomplete
argcomplete.autocomplete(self.parser)
except ImportError:
# no auto completion
pass
python类autocomplete()的实例源码
def create_parser(self, prog, func_stack=(), parent=None):
"""
Creates an ArgumentParser instance from options returned
by get_options(), and subparser for the given commands.
"""
prog = os.path.basename(prog)
func_stack=func_stack+(self,)
options_parser = argparse.ArgumentParser(add_help=False)
for option in self.get_options():
options_parser.add_argument(*option.args, **option.kwargs)
parser = argparse.ArgumentParser(prog=prog, usage=self.usage,
description=self.description,
parents=[options_parser],
add_help=False)
add_help(parser, self.help_args)
self._patch_argparser(parser)
subparsers = parser.add_subparsers()
for name, command in self._commands.items():
usage = getattr(command, 'usage', None)
help = getattr(command, 'help', None)
if help is None: help = command.__doc__
description = getattr(command, 'description', None)
if description is None: description = command.__doc__
command_parser = command.create_parser(name, func_stack=func_stack, parent=self)
subparser = subparsers.add_parser(name, usage=usage, help=help,
description=description,
parents=[command_parser],
add_help=False)
if isinstance(command, Manager):
self._patch_argparser(subparser)
## enable autocomplete only for parent parser when argcomplete is
## imported and it is NOT disabled in constructor
if parent is None and ARGCOMPLETE_IMPORTED \
and not self.disable_argcomplete:
argcomplete.autocomplete(parser, always_complete_options=True)
self.parser = parser
return parser
# def foo(self, app, *args, **kwargs):
# print(args)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-v', '--verbose', action='count', default=0, dest='verbosity')
parser.add_argument('-V', '--version', action='store_true', dest='version', help='Show version and exit')
input_group = parser.add_mutually_exclusive_group()
input_group.add_argument('file', nargs='?', metavar='FILE', help='The path to the markdown file')
input_group.add_argument('--stdin', dest='stdin', action='store_true', help='Read Markdown from stdin')
parser.add_argument('-t', '--tab-spaces', dest='tab_spaces', default=4, type=int, help='Number of spaces in a tab (defaults to 4)')
try:
import argcomplete
argcomplete.autocomplete(parser)
except Exception:
pass # Optional argcomplete module not installed
args = parser.parse_args()
if args.version:
print('vmd {}'.format(VERSION))
sys.exit(0)
import logging
if args.verbosity != 0:
logging_level = 10 * max(0, 3 - args.verbosity)
logging.basicConfig(level=logging_level)
mdparser = build_parser(args)
config = load_config()
writer = create_display_writer(sys.stdout)
renderer = build_render(writer, config)
if args.stdin:
doc = mdparser.parse(sys.stdin.read())
elif args.file is not None:
with open(args.file) as f:
doc = mdparser.parse(f.read())
else:
parser.print_help()
sys.exit(1)
renderer.render_document(doc)
sys.stdout.write('\n')
sys.stdout.flush()
sys.stdout.close()
def parseArguments():
"Argument parser for standalone hdlcc"
if ('--version' in sys.argv[1:]) or ('-V' in sys.argv[1:]): # pragma: no cover
print(hdlcc.__version__)
sys.exit(0)
parser = argparse.ArgumentParser()
# Options
parser.add_argument('--version', action='store_true',
help="Shows hdlcc version and exit")
parser.add_argument('--verbose', '-v', action='append_const', const=1,
help="""Increases verbose level. Use multiple times to
increase more""")
parser.add_argument('--clean', '-c', action='store_true',
help="Cleans the project before building")
parser.add_argument('--sources', '-s', action='append', nargs='*', default=[],
help="""Source(s) file(s) to build individually""") \
.completer = _fileExtentensionCompleter('vhd')
parser.add_argument('--debug-print-sources', action='store_true')
parser.add_argument('--debug-parse-source-file', action='store_true')
parser.add_argument('--debug-run-static-check', action='store_true')
parser.add_argument('--debug-profiling', action='store', nargs='?',
metavar='OUTPUT_FILENAME', const='hdlcc.pstats')
# Mandatory arguments
parser.add_argument('project_file', action='store', nargs=1,
help="""Configuration file that defines what should be
built (lists sources, libraries, build flags and so on""")
if _HAS_ARGCOMPLETE: # pragma: no cover
argcomplete.autocomplete(parser)
args = parser.parse_args()
# PYTHON_ARGCOMPLETE_OK
args.project_file = args.project_file[0]
args.log_level = logging.FATAL
if args.verbose:
if len(args.verbose) == 1:
args.log_level = logging.WARNING
elif len(args.verbose) == 2:
args.log_level = logging.INFO
else:
args.log_level = logging.DEBUG
# Planify source list if supplied
if args.sources:
args.sources = [source for sublist in args.sources for source in sublist]
return args
def create_parser(self, prog, parents=None):
"""
Creates an ArgumentParser instance from options returned
by get_options(), and a subparser for the given command.
"""
prog = os.path.basename(prog)
options_parser = argparse.ArgumentParser(add_help=False)
for option in self.get_options():
options_parser.add_argument(*option.args, **option.kwargs)
# parser_parents = parents if parents else [option_parser]
# parser_parents = [options_parser]
parser = argparse.ArgumentParser(prog=prog, usage=self.usage,
description=self.description,
parents=[options_parser])
subparsers = parser.add_subparsers()
for name, command in self._commands.items():
usage = getattr(command, 'usage', None)
help = getattr(command, 'help', command.__doc__)
description = getattr(command, 'description', command.__doc__)
# Only pass `parents` argument for commands that support it
try:
command_parser = command.create_parser(name, parents=[options_parser])
except TypeError:
warnings.warn("create_parser for {0} command should accept a `parents` argument".format(name), DeprecationWarning)
command_parser = command.create_parser(name)
subparser = subparsers.add_parser(name, usage=usage, help=help,
description=description,
parents=[command_parser], add_help=False)
## enable autocomplete only for parent parser when argcomplete is
## imported and it is NOT disabled in constructor
if parents is None and ARGCOMPLETE_IMPORTED \
and not self.disable_argcomplete:
argcomplete.autocomplete(parser, always_complete_options=True)
return parser
# def foo(self, app, *args, **kwargs):
# print(args)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('degree')
parser.add_argument('prec', type=int)
parser.add_argument('--division', type=int)
parser.add_argument('--c', type=float)
parser.add_argument('--maxsteps', type=int)
parser.add_argument('--max-loops', type=int)
parser.add_argument('--convergence-value', type=mpmath.mpf)
parser.add_argument('--tol', type=mpmath.mpf)
parser.add_argument('--nsolve-type', default=None,
choices=['points', 'intervals'])
parser.add_argument('--solver', default=None)
parser.add_argument('--D-scale', default=None, type=float)
parser.add_argument('--scale', default=None, type=bool)
parser.add_argument('--plot', default=True, type=bool)
parser.add_argument('--seed', default=None, type=int)
parser.add_argument('--initial-points', default=None,
choices=['chebyshev', 'random'])
parser.add_argument('--log-to-file', default=True, type=bool,
help="Log output to a file (in the logs/ directory)")
parser.add_argument('--log-level', default='info',
choices=['debug', 'info', 'warning', 'error', 'critical'])
parser.add_argument('--save-cache', action="store_true", default=False,
help="""Save the computed CRAM expression to the local cache.""")
try:
import argcomplete
argcomplete.autocomplete(parser)
except ImportError:
pass
args = parser.parse_args()
try:
args.degree = ast.literal_eval(args.degree)
except (ValueError, SyntaxError) as e:
parser.error("Invalid degree: " + str(e))
arguments = args.__dict__.copy()
for i in arguments.copy():
if not arguments[i]:
del arguments[i]
if args.log_level:
logger.setLevel(getattr(logging, args.log_level.upper()))
del arguments['log_level']
expr = CRAM_exp(**arguments)
if args.save_cache:
local_cache_file = get_local_CRAM_cache_file(args.degree, args.prec)
print("Saving to", local_cache_file)
with open(local_cache_file, 'w') as f:
f.write(srepr(expr))
def create_parser(self, prog, func_stack=(), parent=None):
"""
Creates an ArgumentParser instance from options returned
by get_options(), and subparser for the given commands.
"""
prog = os.path.basename(prog)
func_stack=func_stack+(self,)
options_parser = argparse.ArgumentParser(add_help=False)
for option in self.get_options():
options_parser.add_argument(*option.args, **option.kwargs)
parser = argparse.ArgumentParser(prog=prog, usage=self.usage,
description=self.description,
parents=[options_parser],
add_help=False)
add_help(parser, self.help_args)
self._patch_argparser(parser)
subparsers = parser.add_subparsers()
for name, command in self._commands.items():
usage = getattr(command, 'usage', None)
help = getattr(command, 'help', None)
if help is None: help = command.__doc__
description = getattr(command, 'description', None)
if description is None: description = command.__doc__
command_parser = command.create_parser(name, func_stack=func_stack, parent=self)
subparser = subparsers.add_parser(name, usage=usage, help=help,
description=description,
parents=[command_parser],
add_help=False)
if isinstance(command, Manager):
self._patch_argparser(subparser)
## enable autocomplete only for parent parser when argcomplete is
## imported and it is NOT disabled in constructor
if parent is None and ARGCOMPLETE_IMPORTED \
and not self.disable_argcomplete:
argcomplete.autocomplete(parser, always_complete_options=True)
self.parser = parser
return parser
# def foo(self, app, *args, **kwargs):
# print(args)
def create_parser(self, prog, func_stack=(), parent=None):
"""
Creates an ArgumentParser instance from options returned
by get_options(), and subparser for the given commands.
"""
prog = os.path.basename(prog)
func_stack=func_stack+(self,)
options_parser = argparse.ArgumentParser(add_help=False)
for option in self.get_options():
options_parser.add_argument(*option.args, **option.kwargs)
parser = argparse.ArgumentParser(prog=prog, usage=self.usage,
description=self.description,
parents=[options_parser],
add_help=False)
add_help(parser, self.help_args)
self._patch_argparser(parser)
subparsers = parser.add_subparsers()
for name, command in self._commands.items():
usage = getattr(command, 'usage', None)
help = getattr(command, 'help', None)
if help is None: help = command.__doc__
description = getattr(command, 'description', None)
if description is None: description = command.__doc__
command_parser = command.create_parser(name, func_stack=func_stack, parent=self)
subparser = subparsers.add_parser(name, usage=usage, help=help,
description=description,
parents=[command_parser],
add_help=False)
if isinstance(command, Manager):
self._patch_argparser(subparser)
## enable autocomplete only for parent parser when argcomplete is
## imported and it is NOT disabled in constructor
if parent is None and ARGCOMPLETE_IMPORTED \
and not self.disable_argcomplete:
argcomplete.autocomplete(parser, always_complete_options=True)
self.parser = parser
return parser
# def foo(self, app, *args, **kwargs):
# print(args)
def create_parser(self, prog, func_stack=(), parent=None):
"""
Creates an ArgumentParser instance from options returned
by get_options(), and subparser for the given commands.
"""
prog = os.path.basename(prog)
func_stack=func_stack+(self,)
options_parser = argparse.ArgumentParser(add_help=False)
for option in self.get_options():
options_parser.add_argument(*option.args, **option.kwargs)
parser = argparse.ArgumentParser(prog=prog, usage=self.usage,
description=self.description,
parents=[options_parser],
add_help=False)
add_help(parser, self.help_args)
self._patch_argparser(parser)
subparsers = parser.add_subparsers()
for name, command in self._commands.items():
usage = getattr(command, 'usage', None)
help = getattr(command, 'help', None)
if help is None: help = command.__doc__
description = getattr(command, 'description', None)
if description is None: description = command.__doc__
command_parser = command.create_parser(name, func_stack=func_stack, parent=self)
subparser = subparsers.add_parser(name, usage=usage, help=help,
description=description,
parents=[command_parser],
add_help=False)
if isinstance(command, Manager):
self._patch_argparser(subparser)
## enable autocomplete only for parent parser when argcomplete is
## imported and it is NOT disabled in constructor
if parent is None and ARGCOMPLETE_IMPORTED \
and not self.disable_argcomplete:
argcomplete.autocomplete(parser, always_complete_options=True)
self.parser = parser
return parser
# def foo(self, app, *args, **kwargs):
# print(args)
def create_parser(self, prog, parents=None):
"""
Creates an ArgumentParser instance from options returned
by get_options(), and a subparser for the given command.
"""
prog = os.path.basename(prog)
options_parser = argparse.ArgumentParser(add_help=False)
for option in self.get_options():
options_parser.add_argument(*option.args, **option.kwargs)
# parser_parents = parents if parents else [option_parser]
# parser_parents = [options_parser]
parser = argparse.ArgumentParser(prog=prog, usage=self.usage,
description=self.description,
parents=[options_parser])
subparsers = parser.add_subparsers()
for name, command in self._commands.items():
usage = getattr(command, 'usage', None)
help = getattr(command, 'help', command.__doc__)
description = getattr(command, 'description', command.__doc__)
# Only pass `parents` argument for commands that support it
try:
command_parser = command.create_parser(name, parents=[options_parser])
except TypeError:
warnings.warn("create_parser for {0} command should accept a `parents` argument".format(name), DeprecationWarning)
command_parser = command.create_parser(name)
subparser = subparsers.add_parser(name, usage=usage, help=help,
description=description,
parents=[command_parser], add_help=False)
## enable autocomplete only for parent parser when argcomplete is
## imported and it is NOT disabled in constructor
if parents is None and ARGCOMPLETE_IMPORTED \
and not self.disable_argcomplete:
argcomplete.autocomplete(parser, always_complete_options=True)
return parser
# def foo(self, app, *args, **kwargs):
# print(args)
def create_parser(self, prog, func_stack=(), parent=None):
"""
Creates an ArgumentParser instance from options returned
by get_options(), and subparser for the given commands.
"""
prog = os.path.basename(prog)
func_stack=func_stack+(self,)
options_parser = argparse.ArgumentParser(add_help=False)
for option in self.get_options():
options_parser.add_argument(*option.args, **option.kwargs)
parser = argparse.ArgumentParser(prog=prog, usage=self.usage,
description=self.description,
parents=[options_parser],
add_help=False)
add_help(parser, self.help_args)
self._patch_argparser(parser)
subparsers = parser.add_subparsers()
for name, command in self._commands.items():
usage = getattr(command, 'usage', None)
help = getattr(command, 'help', None)
if help is None: help = command.__doc__
description = getattr(command, 'description', None)
if description is None: description = command.__doc__
command_parser = command.create_parser(name, func_stack=func_stack, parent=self)
subparser = subparsers.add_parser(name, usage=usage, help=help,
description=description,
parents=[command_parser],
add_help=False)
if isinstance(command, Manager):
self._patch_argparser(subparser)
## enable autocomplete only for parent parser when argcomplete is
## imported and it is NOT disabled in constructor
if parent is None and ARGCOMPLETE_IMPORTED \
and not self.disable_argcomplete:
argcomplete.autocomplete(parser, always_complete_options=True)
self.parser = parser
return parser
# def foo(self, app, *args, **kwargs):
# print(args)
tweets_on_LDA.py 文件源码
项目:twitter_LDA_topic_modeling
作者: kenneth-orton
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def main():
parser = argparse.ArgumentParser(description='Create a corpus from a collection of tweets and/or build an LDA model')
parser.add_argument('-t', '--topology_file', required=True, action='store', dest='top_file', help='Location of topology file')
parser.add_argument('-p', '--dir_prefix', choices=['clique', 'community'], required=True, action='store', dest='dir_prefix', help='Select whether the topology contains cliques or communities')
parser.add_argument('-w', '--working_dir', required=True, action='store', dest='working_dir', help='Name of the directory you want to direct output to')
parser.add_argument('-l', '--lda_loc', required=True, action='store', dest='lda_loc', help='Location of the saved LDA model')
parser.add_argument('-d', '--dict_loc', required=True, action='store', dest='dict_loc', help='Location of dictionary for the model')
parser.add_argument('-u', '--unseen_docs', required=True, action='store', dest='unseen_docs', help='Directory containing unseen documents')
parser.add_argument('-m', '--lemma', action='store_true', dest='lemma', help='Use this option to lemmatize words')
argcomplete.autocomplete(parser)
args = parser.parse_args()
output_dir = args.working_dir + '/'
if not os.path.exists(os.path.dirname(output_dir)):
os.makedirs(os.path.dirname(output_dir), 0o755)
# load dictionary
model_dict = corpora.Dictionary.load(args.dict_loc)
# load trained model from file
lda = models.LdaModel.load(args.lda_loc)
write_topn_words(output_dir, lda)
with open(args.top_file, 'r') as inp_file:
users = set(str(user) for community in inp_file for user in ast.literal_eval(community))
try:
with open(output_dir + 'document_vectors.json', 'r') as all_community_file:
document_vectors = json.load(all_community_file)
except:
document_vectors = {}
pool = multiprocessing.Pool(max(1, multiprocessing.cpu_count() - 1))
func = partial(get_document_vectors,
tweets_dir=args.unseen_docs,
document_vectors=document_vectors,
dictionary=model_dict,
lda_model=lda,
lemma=args.lemma)
doc_vecs = pool.map(func, users)
doc_vecs = [item for item in doc_vecs if item is not None]
pool.close()
pool.join()
doc_vecs = dict(doc_vecs)
document_vectors.update(doc_vecs)
with open(output_dir + 'document_vectors.json', 'w') as document_vectors_file:
json.dump(document_vectors, document_vectors_file, sort_keys=True, indent=4)
print('Building directories')
with open(args.top_file, 'r') as topology_file:
for i, community in enumerate(topology_file):
community_dir = output_dir + args.dir_prefix + '_' + str(i) + '/'
if not os.path.exists(os.path.dirname(community_dir)):
os.makedirs(os.path.dirname(community_dir), 0o755)
comm_doc_vecs = community_document_vectors(doc_vecs, community)
with open(community_dir + 'community_doc_vecs.json', 'w') as comm_docs_file:
json.dump(comm_doc_vecs, comm_docs_file, sort_keys=True, indent=4)
create_LDA_model.py 文件源码
项目:twitter_LDA_topic_modeling
作者: kenneth-orton
项目源码
文件源码
阅读 30
收藏 0
点赞 0
评论 0
def main():
parser = argparse.ArgumentParser(description='Create a corpus from a collection of tweets and/or build an LDA model')
subparsers = parser.add_subparsers(dest='mode')
text_corpus_parser = subparsers.add_parser('text', help='Build corpus from directory of text files')
text_corpus_parser.add_argument('-d', '--docs_loc', required=True, action='store', dest='docs_loc', help='Directory where tweet documents stored')
text_corpus_parser.add_argument('-c', '--corp_loc', required=True, action='store', dest='corp_loc', help='Location and name to save corpus')
text_corpus_parser.add_argument('-m', '--lemma', action='store_true', dest='lemma', help='Use this option to lemmatize words')
wiki_corpus_parser = subparsers.add_parser('wiki', help='Build corpus from compressed Wikipedia articles')
wiki_corpus_parser.add_argument('-w', '--wiki_loc', required=True, action='store', dest='wiki_loc', help='Location of compressed Wikipedia dump')
wiki_corpus_parser.add_argument('-c', '--corp_loc', required=True, action='store', dest='corp_loc', help='Location and name to save corpus')
wiki_corpus_parser.add_argument('-m', '--lemma', action='store_true', dest='lemma', help='Use this option to lemmatize words')
lda_model_parser = subparsers.add_parser('lda', help='Create LDA model from saved corpus')
lda_model_parser.add_argument('-c', '--corp_loc', required=True, action='store', dest='corp_loc', help='Location of corpus')
lda_model_parser.add_argument('-d', '--dict_loc', required=True, action='store', dest='dict_loc', help='Location of dictionary')
lda_model_parser.add_argument('-n', '--num_topics', required=True, action='store', dest='num_topics', help='Number of topics to assign to LDA model')
lda_model_parser.add_argument('-p', '--num_pass', required=True, action='store', dest='num_pass', help='Number of passes through corpus when training the LDA model')
lda_model_parser.add_argument('-l', '--lda_loc', required=True, action='store', dest='lda_loc', help='Location and name to save LDA model')
lda_vis_parser = subparsers.add_parser('ldavis', help='Create visualization of LDA model')
lda_vis_parser.add_argument('-c', '--corp_loc', required=True, action='store', dest='corp_loc', help='Location of corpus')
lda_vis_parser.add_argument('-d', '--dict_loc', required=True, action='store', dest='dict_loc', help='Location of dictionary')
lda_vis_parser.add_argument('-l', '--lda_loc', required=True, action='store', dest='lda_loc', help='Location of LDA model')
argcomplete.autocomplete(parser)
args = parser.parse_args()
if args.mode == 'text':
doc_corpus = DocCorpus(args.docs_loc, args.lemma)
doc_corpus.dictionary.filter_extremes(no_below=1, no_above=0.5, keep_n=DEFAULT_DICT_SIZE)
MmCorpus.serialize(args.corp_loc + '.mm', doc_corpus)
doc_corpus.dictionary.save(args.corp_loc + '.dict')
if args.mode == 'wiki':
if args.lemma:
wiki_corpus = WikiCorpus(args.wiki_loc, lemmatize=True, tokenizer_func=wiki_tokenizer, article_min_tokens=100, token_min_len=3, token_max_len=15)
else:
wiki_corpus = WikiCorpus(args.wiki_loc, lemmatize=False, tokenizer_func=wiki_tokenizer, article_min_tokens=100, token_min_len=3, token_max_len=15)
wiki_corpus.dictionary.filter_extremes(no_below=5, no_above=0.5, keep_n=DEFAULT_DICT_SIZE)
MmCorpus.serialize(args.corp_loc + '.mm', wiki_corpus)
wiki_corpus.dictionary.save(args.corp_loc + '.dict')
if args.mode == 'lda':
build_LDA_model(args.corp_loc, args.dict_loc, args.num_topics, args.num_pass, args.lda_loc)
if args.mode == 'ldavis':
build_pyLDAvis_output(args.corp_loc, args.dict_loc, args.lda_loc)
def main():
hc.setup()
opt = "stdin_buffer_lines"
buffer = 0
buffering = False
#if opt in CONFIG:
# buffering = True
# buffer = int(CONFIG[opt])
if not sys.stdin.isatty():
db = get_database()
cbuffer = 0
# FIXME: should handle g-code & stuff
while True:
line = sys.stdin.readline()
if not line:
break
path, value = line.split(' ', 1)
if ' ' in value: # timestamp present
dt_in, value = value.split()
dt = parse_timestamp(dt_in)
else:
dt = now()
#print(o)
cbuffer += 1
sys.exit(0)
#logging.basicConfig(level=logging.DEBUG)
#logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
logging.basicConfig(level=logging.INFO)
parser = ArghParser()
parser.add_commands([
send,
shell,
server,
monitor,
uimon,
uipcb,
uiprobe,
])
argcomplete.autocomplete(parser)
try:
parser.dispatch()
except KeyboardInterrupt:
sys.exit(1)
def create_parser(self, prog, func_stack=(), parent=None):
"""
Creates an ArgumentParser instance from options returned
by get_options(), and subparser for the given commands.
"""
prog = os.path.basename(prog)
func_stack=func_stack+(self,)
options_parser = argparse.ArgumentParser(add_help=False)
for option in self.get_options():
options_parser.add_argument(*option.args, **option.kwargs)
parser = argparse.ArgumentParser(prog=prog, usage=self.usage,
description=self.description,
parents=[options_parser],
add_help=False)
add_help(parser, self.help_args)
self._patch_argparser(parser)
subparsers = parser.add_subparsers()
for name, command in self._commands.items():
usage = getattr(command, 'usage', None)
help = getattr(command, 'help', None)
if help is None: help = command.__doc__
description = getattr(command, 'description', None)
if description is None: description = command.__doc__
command_parser = command.create_parser(name, func_stack=func_stack, parent=self)
subparser = subparsers.add_parser(name, usage=usage, help=help,
description=description,
parents=[command_parser],
add_help=False)
if isinstance(command, Manager):
self._patch_argparser(subparser)
## enable autocomplete only for parent parser when argcomplete is
## imported and it is NOT disabled in constructor
if parent is None and ARGCOMPLETE_IMPORTED \
and not self.disable_argcomplete:
argcomplete.autocomplete(parser, always_complete_options=True)
self.parser = parser
return parser
# def foo(self, app, *args, **kwargs):
# print(args)
def create_parser(self, prog, func_stack=(), parent=None):
"""
Creates an ArgumentParser instance from options returned
by get_options(), and subparser for the given commands.
"""
prog = os.path.basename(prog)
func_stack=func_stack+(self,)
options_parser = argparse.ArgumentParser(add_help=False)
for option in self.get_options():
options_parser.add_argument(*option.args, **option.kwargs)
parser = argparse.ArgumentParser(prog=prog, usage=self.usage,
description=self.description,
parents=[options_parser],
add_help=False)
add_help(parser, self.help_args)
self._patch_argparser(parser)
subparsers = parser.add_subparsers()
for name, command in self._commands.items():
usage = getattr(command, 'usage', None)
help = getattr(command, 'help', None)
if help is None: help = command.__doc__
description = getattr(command, 'description', None)
if description is None: description = command.__doc__
command_parser = command.create_parser(name, func_stack=func_stack, parent=self)
subparser = subparsers.add_parser(name, usage=usage, help=help,
description=description,
parents=[command_parser],
add_help=False)
if isinstance(command, Manager):
self._patch_argparser(subparser)
## enable autocomplete only for parent parser when argcomplete is
## imported and it is NOT disabled in constructor
if parent is None and ARGCOMPLETE_IMPORTED \
and not self.disable_argcomplete:
argcomplete.autocomplete(parser, always_complete_options=True)
self.parser = parser
return parser
# def foo(self, app, *args, **kwargs):
# print(args)
def create_parser(self, prog, func_stack=(), parent=None):
"""
Creates an ArgumentParser instance from options returned
by get_options(), and subparser for the given commands.
"""
prog = os.path.basename(prog)
func_stack=func_stack+(self,)
options_parser = argparse.ArgumentParser(add_help=False)
for option in self.get_options():
options_parser.add_argument(*option.args, **option.kwargs)
parser = argparse.ArgumentParser(prog=prog, usage=self.usage,
description=self.description,
parents=[options_parser],
add_help=False)
add_help(parser, self.help_args)
self._patch_argparser(parser)
subparsers = parser.add_subparsers()
for name, command in self._commands.items():
usage = getattr(command, 'usage', None)
help = getattr(command, 'help', None)
if help is None: help = command.__doc__
description = getattr(command, 'description', None)
if description is None: description = command.__doc__
command_parser = command.create_parser(name, func_stack=func_stack, parent=self)
subparser = subparsers.add_parser(name, usage=usage, help=help,
description=description,
parents=[command_parser],
add_help=False)
if isinstance(command, Manager):
self._patch_argparser(subparser)
## enable autocomplete only for parent parser when argcomplete is
## imported and it is NOT disabled in constructor
if parent is None and ARGCOMPLETE_IMPORTED \
and not self.disable_argcomplete:
argcomplete.autocomplete(parser, always_complete_options=True)
self.parser = parser
return parser
# def foo(self, app, *args, **kwargs):
# print(args)
def create_parser(self, prog, func_stack=(), parent=None):
"""
Creates an ArgumentParser instance from options returned
by get_options(), and subparser for the given commands.
"""
prog = os.path.basename(prog)
func_stack=func_stack+(self,)
options_parser = argparse.ArgumentParser(add_help=False)
for option in self.get_options():
options_parser.add_argument(*option.args, **option.kwargs)
parser = argparse.ArgumentParser(prog=prog, usage=self.usage,
description=self.description,
parents=[options_parser],
add_help=False)
add_help(parser, self.help_args)
self._patch_argparser(parser)
subparsers = parser.add_subparsers()
for name, command in self._commands.items():
usage = getattr(command, 'usage', None)
help = getattr(command, 'help', None)
if help is None: help = command.__doc__
description = getattr(command, 'description', None)
if description is None: description = command.__doc__
command_parser = command.create_parser(name, func_stack=func_stack, parent=self)
subparser = subparsers.add_parser(name, usage=usage, help=help,
description=description,
parents=[command_parser],
add_help=False)
if isinstance(command, Manager):
self._patch_argparser(subparser)
## enable autocomplete only for parent parser when argcomplete is
## imported and it is NOT disabled in constructor
if parent is None and ARGCOMPLETE_IMPORTED \
and not self.disable_argcomplete:
argcomplete.autocomplete(parser, always_complete_options=True)
self.parser = parser
return parser
# def foo(self, app, *args, **kwargs):
# print(args)
def __init__(self, name, description, commandline_args=[]):
"""Command line arguments can be a list of shortcuts from
`predefined_args`, or a list of dictionaries. Arguments can also
be put in a file named SCRIPTNAME_args.py, e.g. `harvest_args.py`.
"""
self.parser = argparse.ArgumentParser(description)
# Add one ubiqitous command line arguments
commandline_args += ["loglevel"]
# Check for FILENAME_args.py file
import __main__
import os
try:
filename = os.path.basename(__main__.__file__)
filename = os.path.splitext(filename)[0]
args_from_file = __import__(filename + "_args")
commandline_args += args_from_file.args
except ImportError:
pass
# Add all the command line arguments
for c in commandline_args:
# cCheck for shortcuts used
if isinstance(c, str):
c = self.predefined_args[c]
self.parser.add_argument(
c.pop("short", None),
c.pop("long", None),
**c)
argcomplete.autocomplete(self.parser)
self.args = self.parser.parse_args()
self.logger = logging.getLogger(name)
# https://docs.python.org/2/library/logging.html#levels
self.logger.setLevel(self.args.loglevel * 10)
self.executionMode = self.NORMAL_MODE
# Convenience shortcuts to logger methods
def _parse_args(self):
"""Setup parser and parse cli arguments.
NB! counter-intuitively, this function also messes around with logging levels.
"""
# We want some of our options to take effect as early as possible, as they affect command line parsing.
# For these options we resort to some ugly, basic argv spotting
if '--debug' in sys.argv:
logging.getLogger().setLevel(logging.DEBUG)
logging.debug('Early debug enabled')
if '--verbose' in sys.argv or '-v' in sys.argv:
logging.getLogger().setLevel(logging.INFO)
autodiscover = False
if '--autodiscover' in sys.argv:
logging.debug('Autodiscover enabled')
autodiscover = True
parser = SmartCommandMapParser(prog=self.tool_shortname,
description="Cligraphy command line tools",
formatter_class=CustomDescriptionFormatter)
self.parser = parser # expose to eg. ctx
parser.add_argument('--version', action=_VersionAction, nargs=0, dest="_version")
parser.add_argument("--debug", help="enable debuging output", dest="_level", action="store_const", const=logging.DEBUG)
parser.add_argument("--pdb", help="run pdb on exceptions", dest="_pdb", action="store_true")
parser.add_argument("--no-capture", help="disable input/output capture", dest="_capture", action="store_false", default=True)
parser.add_argument("--no-reporting", help="disable reporting", dest="_reporting", action="store_false", default=True)
parser.add_argument("--profile", help="enable profiling", dest="_profile", action="store_true", default=False)
parser.add_argument("--autodiscover", help="re-discover commands and refresh cache (default: read cached commands list)", dest="_autodiscover", action="store_true")
parser.add_argument("-v", "--verbose", help="enable informational output", dest="_level", action="store_const", const=logging.INFO)
for namespace, command_map in self.get_command_maps(autodiscover):
parser.add_command_map(namespace, command_map)
argcomplete.autocomplete(parser)
_warn_about_bad_non_ascii_chars(sys.argv)
_warn_about_bad_path(os.getenv('VIRTUAL_ENV'), os.getenv('PATH'))
args = parser.parse_args()
args._parser = parser # deprecated
# pylint:disable=protected-access
if args._level is not None:
logging.getLogger().setLevel(args._level)
return args