aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2023-06-16 12:45:23 +0200
committerHugo Hörnquist <hugo@lysator.liu.se>2023-06-16 12:45:23 +0200
commitb3e244a87fcd7dc24b97b4f47b2e30aefd47af11 (patch)
tree4d7cfa33da5aba2c1ea580e889756edc6c6eff88
parentDocumentation formatting fixes. (diff)
downloadmuppet-strings-b3e244a87fcd7dc24b97b4f47b2e30aefd47af11.tar.gz
muppet-strings-b3e244a87fcd7dc24b97b4f47b2e30aefd47af11.tar.xz
Allow specifying modules directly on the command line.
-rw-r--r--muppet/__main__.py27
-rw-r--r--muppet/gather.py35
2 files changed, 43 insertions, 19 deletions
diff --git a/muppet/__main__.py b/muppet/__main__.py
index a71363c..e642010 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 a214507..3f326e3 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