From 263d68f9c3d230e1a412b59967ba401232d8fdc6 Mon Sep 17 00:00:00 2001 From: matt2930 Date: Mon, 19 Sep 2022 13:04:16 -0400 Subject: [PATCH] feat: Add CommandExtractor --- README.md | 17 +++++++++++++++++ setup.py | 6 +++++- tmpl8/cli.py | 13 +++++++++++-- tmpl8/extractor.py | 21 +++++++++++++++++++++ 4 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 tmpl8/extractor.py diff --git a/README.md b/README.md index 4a6229a..4d3f4f4 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,20 @@ # tmpl8 This repository hosts code for the future of templating + + +## Usage +``` +$ tmpl8 + +usage: tmpl8 [-h] [-v] ... + +tmpl8 wrapper for cli commands + +positional arguments: + command command to run + +optional arguments: + -h, --help show this help message and exit + -v, --version show program's version number and exit +``` diff --git a/setup.py b/setup.py index fbb4523..be9df64 100644 --- a/setup.py +++ b/setup.py @@ -1,12 +1,16 @@ -from importlib.metadata import entry_points from setuptools import setup from tmpl8 import __version__ +install_requires = [ + 'pyyaml' + ] + setup_options = dict( name='tmpl8', version=__version__, description="Templating wrapper for CLI commands", author="Matthew Altberg", + install_requires=install_requires, entry_points={ 'console_scripts': [ 'tmpl8=tmpl8.cli:main' diff --git a/tmpl8/cli.py b/tmpl8/cli.py index be469d8..cbbdc4d 100755 --- a/tmpl8/cli.py +++ b/tmpl8/cli.py @@ -1,10 +1,19 @@ import argparse + from tmpl8 import __version__ +from tmpl8.extractor import CommandExtractor def main(): parser = argparse.ArgumentParser(prog='tmpl8', description="tmpl8 wrapper for cli commands") parser.add_argument('command', help='command to run', nargs=argparse.REMAINDER) - parser.add_argument('-v', action='version', version=__version__) + parser.add_argument('-v', '--version', action='version', version=__version__) args = parser.parse_args() - print(args.command) + + if args.command == []: + parser.print_help() + parser.exit(status=1, message='error: missing command\n') + + command = CommandExtractor(args.command) + + print(command.files) diff --git a/tmpl8/extractor.py b/tmpl8/extractor.py new file mode 100644 index 0000000..23c466d --- /dev/null +++ b/tmpl8/extractor.py @@ -0,0 +1,21 @@ +import glob +import os + +class CommandExtractor: + files: dict = {} + + def __init__(self, command: list): + self.command = command + self._extractFiles() + + def _extractFiles(self) -> set: + for i, arg in enumerate(self.command): + for path in glob.glob(arg): + if os.path.isfile(path): + self.files[path] = {'ind': i, 'files': os.path.abspath(path)} + elif os.path.isdir(path): + dir_files = set() + for file in glob.glob(f'{path}/**/*'): + if os.path.isfile(file): + dir_files.add(os.path.abspath(file)) + self.files[path] = {'ind': i, 'files': dir_files}