Skip to content

Commit

Permalink
[recipes.py] move doc arg parsing to its module
Browse files Browse the repository at this point in the history
R=dnj@chromium.org, phajdan.jr@chromium.org
BUG=

Review-Url: https://codereview.chromium.org/2847623002
  • Loading branch information
riannucci authored and Commit bot committed Apr 29, 2017
1 parent bcf9286 commit b682966
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 34 deletions.
35 changes: 27 additions & 8 deletions recipe_engine/doc.py
Expand Up @@ -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
Expand Down Expand Up @@ -456,30 +457,48 @@ 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()
base_dir = universe_view.package.repo_root
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)
28 changes: 2 additions & 26 deletions recipes.py
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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'):
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b682966

Please sign in to comment.