From b6829668a6408ae319f98651864da96c59a8762c Mon Sep 17 00:00:00 2001 From: iannucci Date: Sat, 29 Apr 2017 01:10:22 -0700 Subject: [PATCH] [recipes.py] move doc arg parsing to its module R=dnj@chromium.org, phajdan.jr@chromium.org BUG= Review-Url: https://codereview.chromium.org/2847623002 --- recipe_engine/doc.py | 35 +++++++++++++++++++++++++++-------- recipes.py | 28 ++-------------------------- 2 files changed, 29 insertions(+), 34 deletions(-) diff --git a/recipe_engine/doc.py b/recipe_engine/doc.py index 8bdd715fd..eeeeec31f 100755 --- a/recipe_engine/doc.py +++ b/recipe_engine/doc.py @@ -18,6 +18,7 @@ from . import config from . import doc_pb2 as doc +from . import loader from . import recipe_api from . import types from . import util @@ -456,7 +457,25 @@ def _set_known_objects(base): raise ValueError('could not find %r in %r' % (k, relpath)) -def main(universe_view, recipe, kind): +def add_subparser(parser): + doc_kinds=('binarypb', 'jsonpb', 'textpb', 'markdown(github)', + 'markdown(gitiles)') + doc_p = parser.add_parser( + 'doc', + description='List all known modules reachable from the current package, ' + 'with their documentation') + doc_p.add_argument('recipe', nargs='?', + help='Restrict documentation to this recipe') + doc_p.add_argument('--kind', default='jsonpb', choices=doc_kinds, + help='Output this kind of documentation') + + doc_p.set_defaults(command='doc', func=main) + + +def main(package_deps, args): + universe = loader.RecipeUniverse(package_deps, args.package) + universe_view = loader.UniverseView(universe, package_deps.root_package) + logging.basicConfig() spec = universe_view.package.repo_spec.spec_pb() @@ -464,22 +483,22 @@ def main(universe_view, recipe, kind): if spec.recipes_path: base_dir = join(base_dir, spec.recipes_path) - if recipe: - recipe_fullpath = universe_view.find_recipe(recipe) + if args.recipe: + recipe_fullpath = universe_view.find_recipe(args.recipe) relpath = _to_posix(os.path.relpath(recipe_fullpath, base_dir)) - node = parse_recipe(universe_view, base_dir, relpath, recipe) + node = parse_recipe(universe_view, base_dir, relpath, args.recipe) else: node = parse_package(universe_view, base_dir, spec) _set_known_objects(node) - if kind == 'jsonpb': + if args.kind == 'jsonpb': sys.stdout.write(jsonpb.MessageToJson( node, including_default_value_fields=True, preserving_proto_field_name=True)) - elif kind == 'binarypb': + elif args.kind == 'binarypb': sys.stdout.write(node.SerializeToString()) - elif kind == 'textpb': + elif args.kind == 'textpb': sys.stdout.write(textpb.MessageToString(node)) else: - raise NotImplementedError('--kind=%s' % kind) + raise NotImplementedError('--kind=%s' % args.kind) diff --git a/recipes.py b/recipes.py index f7a508921..7765eff79 100755 --- a/recipes.py +++ b/recipes.py @@ -241,16 +241,6 @@ def __call__(self, parser, namespace, values, option_string=None): v[project_id] = path -def doc(config_file, package_deps, args): - from recipe_engine import doc - from recipe_engine import loader - - universe = loader.RecipeUniverse(package_deps, config_file) - universe_view = loader.UniverseView(universe, package_deps.root_package) - - doc.main(universe_view, args.recipe, args.kind) - - # Map of arguments_pb2.Property "value" oneof conversion functions. # # The fields here should be kept in sync with the "value" oneof field names in @@ -375,8 +365,8 @@ def main(): common_postprocess_func = add_common_args(parser) from recipe_engine import fetch, lint_test, bundle, depgraph, autoroll - from recipe_engine import remote, refs - to_add = [fetch, lint_test, bundle, depgraph, autoroll, remote, refs] + from recipe_engine import remote, refs, doc + to_add = [fetch, lint_test, bundle, depgraph, autoroll, remote, refs, doc] subp = parser.add_subparsers() for module in to_add: @@ -459,18 +449,6 @@ def properties_type(value): 'issue=12345. The property value will be decoded as JSON, but if ' 'this decoding fails the value will be interpreted as a string.') - doc_kinds=('binarypb', 'jsonpb', 'textpb', 'markdown(github)', - 'markdown(gitiles)') - doc_p = subp.add_parser( - 'doc', - description='List all known modules reachable from the current package, ' - 'with their documentation') - doc_p.add_argument('recipe', nargs='?', - help='Restrict documentation to this recipe') - doc_p.add_argument('--kind', default='jsonpb', choices=doc_kinds, - help='Output this kind of documentation') - doc_p.set_defaults(command='doc') - args = parser.parse_args() common_postprocess_func(parser, args) if hasattr(args, 'postprocess_func'): @@ -551,8 +529,6 @@ def _real_main(args): return test(config_file, package_deps, args) elif args.command == 'run': return run(config_file, package_deps, args) - elif args.command == 'doc': - return doc(config_file, package_deps, args) else: print """Dear sir or madam, It has come to my attention that a quite impossible condition has come