Skip to content

Commit

Permalink
Refactor Commands to Handle Default Structure Paths (#13)
Browse files Browse the repository at this point in the history
#### 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.
  • Loading branch information
httpdss authored Sep 23, 2024
1 parent ee05a0c commit fa7c0b8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
9 changes: 7 additions & 2 deletions struct_module/commands/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
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):
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')
Expand Down Expand Up @@ -39,7 +40,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(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
if not os.path.exists(file_path):
self.logger.error(f"File not found: {file_path}")
Expand Down
15 changes: 12 additions & 3 deletions struct_module/commands/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,29 @@
import os
import yaml
from struct_module.file_item import FileItem
from struct_module.utils import project_path

# List command class
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(project_path, "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]}")
4 changes: 3 additions & 1 deletion struct_module/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import yaml

import os

def read_config_file(file_path):
with open(file_path, 'r') as f:
Expand All @@ -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__)), "..")

0 comments on commit fa7c0b8

Please sign in to comment.