From a5510e2b2030603915190b9f60f70c649fd5e427 Mon Sep 17 00:00:00 2001 From: Kenneth Belitzky Date: Thu, 12 Sep 2024 15:07:51 -0300 Subject: [PATCH 1/3] introduce list commands and generate structures from name --- struct_module/commands/generate.py | 18 ++++++++++++++---- struct_module/commands/list.py | 21 +++++++++++++++++++++ struct_module/main.py | 2 ++ 3 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 struct_module/commands/list.py diff --git a/struct_module/commands/generate.py b/struct_module/commands/generate.py index 61ff538..ceea998 100644 --- a/struct_module/commands/generate.py +++ b/struct_module/commands/generate.py @@ -7,8 +7,9 @@ class GenerateCommand(Command): def __init__(self, parser): super().__init__(parser) - parser.add_argument('yaml_file', type=str, help='Path to the YAML configuration file') + parser.add_argument('structure_definition', type=str, help='Path to the YAML configuration file') parser.add_argument('base_path', type=str, help='Base path where the structure will be created') + parser.add_argument('-s', '--structures-path', type=str, help='Path to structure definitions', default='contribs') parser.add_argument('-d', '--dry-run', action='store_true', help='Perform a dry run without creating any files or directories') parser.add_argument('-v', '--vars', type=str, help='Template variables in the format KEY1=value1,KEY2=value2') parser.add_argument('-b', '--backup', type=str, help='Path to the backup folder') @@ -17,7 +18,7 @@ def __init__(self, parser): parser.set_defaults(func=self.execute) def execute(self, args): - self.logger.info(f"Generating structure at {args.base_path} with config {args.yaml_file}") + self.logger.info(f"Generating structure at {args.base_path} with config {args.structure_definition}") if args.backup and not os.path.exists(args.backup): os.makedirs(args.backup) @@ -30,8 +31,17 @@ def execute(self, args): def _create_structure(self, args): - with open(args.yaml_file, 'r') as f: - config = yaml.safe_load(f) + if args.structure_definition.startswith("file://") and args.structure_definition.endswith(".yaml"): + with open(args.structure_definition[7:], 'r') as f: + config = yaml.safe_load(f) + else: + file_path = os.path.join(args.structures_path, f"{args.structure_definition}.yaml") + # show error if file is not found + if not os.path.exists(file_path): + self.logger.error(f"File not found: {file_path}") + return + with open(file_path, 'r') as f: + config = yaml.safe_load(f) template_vars = dict(item.split('=') for item in args.vars.split(',')) if args.vars else None config_structure = config.get('structure', []) diff --git a/struct_module/commands/list.py b/struct_module/commands/list.py new file mode 100644 index 0000000..d9527ba --- /dev/null +++ b/struct_module/commands/list.py @@ -0,0 +1,21 @@ +from struct_module.commands import Command +import os +import yaml +from struct_module.file_item import FileItem + +# List command class +class ListCommand(Command): + def __init__(self, parser): + super().__init__(parser) + parser.set_defaults(func=self.execute) + + def execute(self, args): + self.logger.info(f"Listing available structures") + self._list_structures() + + def _list_structures(self): + print("Listing available structures") + sorted_list = [structure for structure in os.listdir('contribs') if structure.endswith('.yaml')] + sorted_list.sort() + for structure in sorted_list: + print(f" - {structure[:-5]}") diff --git a/struct_module/main.py b/struct_module/main.py index 63077c5..53df028 100644 --- a/struct_module/main.py +++ b/struct_module/main.py @@ -5,6 +5,7 @@ from struct_module.commands.generate import GenerateCommand from struct_module.commands.info import InfoCommand from struct_module.commands.validate import ValidateCommand +from struct_module.commands.list import ListCommand @@ -23,6 +24,7 @@ def main(): InfoCommand(subparsers.add_parser('info', help='Show information about the package')) ValidateCommand(subparsers.add_parser('validate', help='Validate the YAML configuration file')) GenerateCommand(subparsers.add_parser('generate', help='Generate the project structure')) + ListCommand(subparsers.add_parser('list', help='List available structures')) args = parser.parse_args() From 0e4f4c7bc0359e806de8313e624b6d11da3a7db1 Mon Sep 17 00:00:00 2001 From: Kenneth Belitzky Date: Mon, 23 Sep 2024 11:14:14 -0300 Subject: [PATCH 2/3] Refactor structure definition file path handling in generate command --- struct_module/commands/generate.py | 8 ++++++-- struct_module/commands/list.py | 14 +++++++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/struct_module/commands/generate.py b/struct_module/commands/generate.py index ceea998..7f1a2af 100644 --- a/struct_module/commands/generate.py +++ b/struct_module/commands/generate.py @@ -9,7 +9,7 @@ def __init__(self, parser): super().__init__(parser) parser.add_argument('structure_definition', type=str, help='Path to the YAML configuration file') parser.add_argument('base_path', type=str, help='Base path where the structure will be created') - parser.add_argument('-s', '--structures-path', type=str, help='Path to structure definitions', default='contribs') + parser.add_argument('-s', '--structures-path', type=str, help='Path to structure definitions') parser.add_argument('-d', '--dry-run', action='store_true', help='Perform a dry run without creating any files or directories') parser.add_argument('-v', '--vars', type=str, help='Template variables in the format KEY1=value1,KEY2=value2') parser.add_argument('-b', '--backup', type=str, help='Path to the backup folder') @@ -35,7 +35,11 @@ def _create_structure(self, args): with open(args.structure_definition[7:], 'r') as f: config = yaml.safe_load(f) else: - file_path = os.path.join(args.structures_path, f"{args.structure_definition}.yaml") + if args.structures_path is None: + this_file = os.path.dirname(os.path.realpath(__file__)) + file_path = os.path.join(this_file, "..", "..", "contribs", f"{args.structure_definition}.yaml") + else: + file_path = os.path.join(args.structures_path, f"{args.structure_definition}.yaml") # show error if file is not found if not os.path.exists(file_path): self.logger.error(f"File not found: {file_path}") diff --git a/struct_module/commands/list.py b/struct_module/commands/list.py index d9527ba..c64c79f 100644 --- a/struct_module/commands/list.py +++ b/struct_module/commands/list.py @@ -7,15 +7,23 @@ class ListCommand(Command): def __init__(self, parser): super().__init__(parser) + parser.add_argument('-s', '--structures-path', type=str, help='Path to structure definitions') parser.set_defaults(func=self.execute) def execute(self, args): self.logger.info(f"Listing available structures") - self._list_structures() + self._list_structures(args) + + def _list_structures(self, args): + + if args.structures_path is None: + this_file = os.path.dirname(os.path.realpath(__file__)) + final_path = os.path.join(this_file, "..", "..", "contribs") + else: + final_path = os.path.join(args.structures_path) - def _list_structures(self): print("Listing available structures") - sorted_list = [structure for structure in os.listdir('contribs') if structure.endswith('.yaml')] + sorted_list = [structure for structure in os.listdir(final_path) if structure.endswith('.yaml')] sorted_list.sort() for structure in sorted_list: print(f" - {structure[:-5]}") From c84e4739a44bf9e11b4e0dc85a6a081c003a17d9 Mon Sep 17 00:00:00 2001 From: Kenneth Belitzky Date: Mon, 23 Sep 2024 11:55:47 -0300 Subject: [PATCH 3/3] Refactor file path handling in generate and list commands --- struct_module/commands/generate.py | 3 ++- struct_module/commands/list.py | 3 ++- struct_module/utils.py | 4 +++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/struct_module/commands/generate.py b/struct_module/commands/generate.py index 3974e16..acd5000 100644 --- a/struct_module/commands/generate.py +++ b/struct_module/commands/generate.py @@ -4,6 +4,7 @@ import argparse from struct_module.file_item import FileItem from struct_module.completers import file_strategy_completer +from struct_module.utils import project_path # Generate command class class GenerateCommand(Command): @@ -41,7 +42,7 @@ def _create_structure(self, args): else: if args.structures_path is None: this_file = os.path.dirname(os.path.realpath(__file__)) - file_path = os.path.join(this_file, "..", "..", "contribs", f"{args.structure_definition}.yaml") + file_path = os.path.join(project_path, "contribs", f"{args.structure_definition}.yaml") else: file_path = os.path.join(args.structures_path, f"{args.structure_definition}.yaml") # show error if file is not found diff --git a/struct_module/commands/list.py b/struct_module/commands/list.py index c64c79f..89c3a3c 100644 --- a/struct_module/commands/list.py +++ b/struct_module/commands/list.py @@ -2,6 +2,7 @@ import os import yaml from struct_module.file_item import FileItem +from struct_module.utils import project_path # List command class class ListCommand(Command): @@ -18,7 +19,7 @@ def _list_structures(self, args): if args.structures_path is None: this_file = os.path.dirname(os.path.realpath(__file__)) - final_path = os.path.join(this_file, "..", "..", "contribs") + final_path = os.path.join(project_path, "contribs") else: final_path = os.path.join(args.structures_path) diff --git a/struct_module/utils.py b/struct_module/utils.py index d56294c..bf0c97e 100644 --- a/struct_module/utils.py +++ b/struct_module/utils.py @@ -1,5 +1,5 @@ import yaml - +import os def read_config_file(file_path): with open(file_path, 'r') as f: @@ -12,3 +12,5 @@ def merge_configs(file_config, args): if key in args_dict and args_dict[key] is None: args_dict[key] = value return args_dict + +project_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")