diff --git a/muppet/__main__.py b/muppet/__main__.py index a71363c13b4cde5501f66376d593cb8ca0b55f76..e6420101873a4dca45cc81b6dc4ecd6edd0fc957 100644 --- a/muppet/__main__.py +++ b/muppet/__main__.py @@ -1,25 +1,34 @@ """New, better, entry point.""" import argparse +import pathlib from .cache import Cache -from .gather import get_modules +from .gather import get_module, get_modules, ModuleEntry from .output import setup_index, setup_module -parser = argparse.ArgumentParser( - prog='puppet-doc configure', - description='Sets up puppet doc') -parser.add_argument('--env', action='store') +def __main() -> None: + parser = argparse.ArgumentParser( + prog='puppet-doc configure', + description='Sets up puppet doc') -args = parser.parse_args() + parser.add_argument('--env', action='store') + parser.add_argument('modules', nargs='*', type=pathlib.Path) -env = args.env or '/etc/puppetlabs/code/modules' + args = parser.parse_args() + env = args.env or '/etc/puppetlabs/code/modules' -def __main() -> None: cache = Cache('/home/hugo/.cache/puppet-doc') - modules = get_modules(cache, env) + + modules: list[ModuleEntry] + if args.modules != []: + modules = [get_module(cache, mod) + for mod in args.modules] + else: + modules = get_modules(cache, env) + setup_index('output', modules) for module in modules: diff --git a/muppet/gather.py b/muppet/gather.py index a2145075696b0b391fb5eabe954eec3795c3cb3d..3f326e3b577f8088df9f39775e8ad16867b4a3c4 100644 --- a/muppet/gather.py +++ b/muppet/gather.py @@ -76,9 +76,32 @@ def get_puppet_strings(cache: Cache, path: str) -> bytes: # pass +def get_module(cache: Cache, + path: str) -> ModuleEntry: + """ + Return the metadata of a given module. + + :param cache: + Cache objcet for modules, see python module configuration. + + :param path: + Path of the given module. + """ + name = os.path.basename(path) + strings_data = get_puppet_strings(cache, path) + + try: + with open(os.path.join(path, 'metadata.json')) as f: + metadata = json.load(f) + except FileNotFoundError: + metadata = {} + + return ModuleEntry(name, path, strings_data, metadata) + + def get_modules(cache: Cache, dir: str) -> list[ModuleEntry]: """ - Enumerate modules in directory. + Return all modules present in a given directory. The directory should be the modules subdirectory of an environment, e.g. /etc/puppetlabs/code/environments/production/modules. @@ -88,16 +111,8 @@ def get_modules(cache: Cache, dir: str) -> list[ModuleEntry]: for entry in sorted(list(os.scandir(dir)), key=lambda d: d.name): # TODO Logging # print('- entry', entry, file=sys.stderr) - name = entry.name path = os.path.join(dir, entry) - strings_data = get_puppet_strings(cache, path) - - try: - with open(os.path.join(path, 'metadata.json')) as f: - metadata = json.load(f) - except FileNotFoundError: - metadata = {} - modules.append(ModuleEntry(name, path, strings_data, metadata)) + modules.append(get_module(cache, path)) return modules