Skip to content

Commit 671de90

Browse files
authored
Improve Path Handling for Structure Commands (#26)
#### **Overview** This PR improves the handling of file paths within the `generate` and `list` commands of the `struct_module`. #### **Changes** - Introduced consistent path resolution for `contribs` directory using `os.path.dirname` and `os.path.realpath`. - Simplified and clarified logic for determining `file_path` in `generate.py`. - Enhanced logic in `list.py` to aggregate structures from multiple directories (`structures_path` and `contribs`). - Ensured error handling and path existence checks are more robust and better logged. #### **Justification** - Resolving paths centrally reduces redundancy and the risk of bugs. - Aggregating structures from multiple sources improves functionality and user experience. #### **Impact** - Commands `generate` and `list` now handle paths more reliably. - Users benefit from more informative error messages and a broader set of structure files in the listing command.
1 parent 9d565e0 commit 671de90

2 files changed

Lines changed: 20 additions & 11 deletions

File tree

struct_module/commands/generate.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,21 @@ def execute(self, args):
3939
def _create_structure(self, args):
4040
if isinstance(args, dict):
4141
args = argparse.Namespace(**args)
42+
this_file = os.path.dirname(os.path.realpath(__file__))
43+
contribs_path = os.path.join(this_file, "..", "contribs")
44+
4245
if args.structure_definition.startswith("file://") and args.structure_definition.endswith(".yaml"):
4346
with open(args.structure_definition[7:], 'r') as f:
4447
config = yaml.safe_load(f)
4548
else:
46-
if args.structures_path is None:
47-
this_file = os.path.dirname(os.path.realpath(__file__))
48-
file_path = os.path.join(this_file, "..", "contribs", f"{args.structure_definition}.yaml")
49-
else:
49+
file_path = os.path.join(contribs_path, f"{args.structure_definition}.yaml")
50+
if args.structures_path:
5051
file_path = os.path.join(args.structures_path, f"{args.structure_definition}.yaml")
51-
# show error if file is not found
52+
5253
if not os.path.exists(file_path):
5354
self.logger.error(f"File not found: {file_path}")
5455
return
56+
5557
with open(file_path, 'r') as f:
5658
config = yaml.safe_load(f)
5759

struct_module/commands/list.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,22 @@ def execute(self, args):
1616
self._list_structures(args)
1717

1818
def _list_structures(self, args):
19+
this_file = os.path.dirname(os.path.realpath(__file__))
20+
contribs_path = os.path.join(this_file, "..", "contribs")
1921

20-
if args.structures_path is None:
21-
this_file = os.path.dirname(os.path.realpath(__file__))
22-
final_path = os.path.join(this_file, "..", "contribs")
22+
if args.structures_path:
23+
final_path = args.structures_path
24+
paths_to_list = [final_path, contribs_path]
2325
else:
24-
final_path = os.path.join(args.structures_path)
26+
paths_to_list = [contribs_path]
2527

2628
print("Listing available structures")
27-
sorted_list = [structure for structure in os.listdir(final_path) if structure.endswith('.yaml')]
28-
sorted_list.sort()
29+
all_structures = set()
30+
for path in paths_to_list:
31+
if os.path.exists(path):
32+
structures = [structure for structure in os.listdir(path) if structure.endswith('.yaml')]
33+
all_structures.update(structures)
34+
35+
sorted_list = sorted(all_structures)
2936
for structure in sorted_list:
3037
print(f" - {structure[:-5]}")

0 commit comments

Comments
 (0)