Skip to content

Commit fa7c0b8

Browse files
authored
Refactor Commands to Handle Default Structure Paths (#13)
#### Overview: This pull request refactors the command modules in `generate.py` and `list.py` to handle cases where the `structures_path` argument is not provided, falling back to a default path. #### Changes: - **`generate.py`**: - Removed the default value for `--structures-path` argument. - Added logic to fallback to a default path (`contribs`) if no path is provided. - **`list.py`**: - Modified the list command to handle optional `structures-path`. - Implemented fallback logic to default `contribs` directory when no path is specified. #### Justification: This refactor allows for greater flexibility when using the commands by enabling the user to provide custom paths for structure definitions or rely on the default path if none is supplied. #### Impact: - Improves the robustness of the commands by ensuring default paths are used if the user does not explicitly specify a path. - Simplifies the interface for users running commands without additional configuration.
1 parent ee05a0c commit fa7c0b8

3 files changed

Lines changed: 22 additions & 6 deletions

File tree

struct_module/commands/generate.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
import argparse
55
from struct_module.file_item import FileItem
66
from struct_module.completers import file_strategy_completer
7+
from struct_module.utils import project_path
78

89
# Generate command class
910
class GenerateCommand(Command):
1011
def __init__(self, parser):
1112
super().__init__(parser)
1213
parser.add_argument('structure_definition', type=str, help='Path to the YAML configuration file')
1314
parser.add_argument('base_path', type=str, help='Base path where the structure will be created')
14-
parser.add_argument('-s', '--structures-path', type=str, help='Path to structure definitions', default='contribs')
15+
parser.add_argument('-s', '--structures-path', type=str, help='Path to structure definitions')
1516
parser.add_argument('-d', '--dry-run', action='store_true', help='Perform a dry run without creating any files or directories')
1617
parser.add_argument('-v', '--vars', type=str, help='Template variables in the format KEY1=value1,KEY2=value2')
1718
parser.add_argument('-b', '--backup', type=str, help='Path to the backup folder')
@@ -39,7 +40,11 @@ def _create_structure(self, args):
3940
with open(args.structure_definition[7:], 'r') as f:
4041
config = yaml.safe_load(f)
4142
else:
42-
file_path = os.path.join(args.structures_path, f"{args.structure_definition}.yaml")
43+
if args.structures_path is None:
44+
this_file = os.path.dirname(os.path.realpath(__file__))
45+
file_path = os.path.join(project_path, "contribs", f"{args.structure_definition}.yaml")
46+
else:
47+
file_path = os.path.join(args.structures_path, f"{args.structure_definition}.yaml")
4348
# show error if file is not found
4449
if not os.path.exists(file_path):
4550
self.logger.error(f"File not found: {file_path}")

struct_module/commands/list.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,29 @@
22
import os
33
import yaml
44
from struct_module.file_item import FileItem
5+
from struct_module.utils import project_path
56

67
# List command class
78
class ListCommand(Command):
89
def __init__(self, parser):
910
super().__init__(parser)
11+
parser.add_argument('-s', '--structures-path', type=str, help='Path to structure definitions')
1012
parser.set_defaults(func=self.execute)
1113

1214
def execute(self, args):
1315
self.logger.info(f"Listing available structures")
14-
self._list_structures()
16+
self._list_structures(args)
17+
18+
def _list_structures(self, args):
19+
20+
if args.structures_path is None:
21+
this_file = os.path.dirname(os.path.realpath(__file__))
22+
final_path = os.path.join(project_path, "contribs")
23+
else:
24+
final_path = os.path.join(args.structures_path)
1525

16-
def _list_structures(self):
1726
print("Listing available structures")
18-
sorted_list = [structure for structure in os.listdir('contribs') if structure.endswith('.yaml')]
27+
sorted_list = [structure for structure in os.listdir(final_path) if structure.endswith('.yaml')]
1928
sorted_list.sort()
2029
for structure in sorted_list:
2130
print(f" - {structure[:-5]}")

struct_module/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import yaml
2-
2+
import os
33

44
def read_config_file(file_path):
55
with open(file_path, 'r') as f:
@@ -12,3 +12,5 @@ def merge_configs(file_config, args):
1212
if key in args_dict and args_dict[key] is None:
1313
args_dict[key] = value
1414
return args_dict
15+
16+
project_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")

0 commit comments

Comments
 (0)