Skip to content

Commit bac9b62

Browse files
authored
Merge branch 'autogen' into refactor/rename-variables-edit-docstrings
2 parents eb6b61c + 8fdc651 commit bac9b62

File tree

3 files changed

+26
-22
lines changed

3 files changed

+26
-22
lines changed

scripts/microgenerator/generate.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import argparse
2828
import glob
2929
import logging
30+
import re
3031
from collections import defaultdict
3132
from pathlib import Path
3233
from typing import List, Dict, Any
@@ -505,7 +506,6 @@ def analyze_source_files(
505506
# Make the pattern absolute
506507
absolute_pattern = os.path.join(project_root, pattern)
507508
source_files.extend(glob.glob(absolute_pattern, recursive=True))
508-
509509
# PASS 1: Build the request argument schema from the types files.
510510
request_arg_schema = _build_request_arg_schema(source_files, project_root)
511511

@@ -566,14 +566,14 @@ def generate_code(config: Dict[str, Any], analysis_results: tuple) -> None:
566566
Generates source code files using Jinja2 templates.
567567
"""
568568
data, all_imports, all_types, request_arg_schema = analysis_results
569-
project_root = config["project_root"]
570-
config_dir = config["config_dir"]
571569

572570
templates_config = config.get("templates", [])
573571
for item in templates_config:
574-
template_path = str(Path(config_dir) / item["template"])
575-
output_path = str(Path(project_root) / item["output"])
572+
template_name = item["template"]
573+
output_name = item["output"]
576574

575+
template_path = str(Path(config["config_dir"]) / template_name)
576+
output_path = str(Path(config["project_root"]) / output_name)
577577
template = utils.load_template(template_path)
578578
methods_context = []
579579
for class_name, methods in data.items():
@@ -669,18 +669,20 @@ def find_project_root(start_path: str, markers: list[str]) -> str | None:
669669
return None
670670
current_path = parent_path
671671

672-
# Load configuration from the YAML file.
673-
config = utils.load_config(config_path)
672+
# Get the absolute path of the config file
673+
abs_config_path = os.path.abspath(config_path)
674+
config = utils.load_config(abs_config_path)
674675

675-
# Determine the project root.
676-
script_dir = os.path.dirname(os.path.abspath(__file__))
677-
project_root = find_project_root(script_dir, ["setup.py", ".git"])
676+
# Determine the project root
677+
# Start searching from the directory of this script file
678+
script_file_dir = os.path.dirname(os.path.abspath(__file__))
679+
project_root = find_project_root(script_file_dir, [".git"])
678680
if not project_root:
679-
project_root = os.getcwd() # Fallback to current directory
681+
# Fallback to the directory from which the script was invoked
682+
project_root = os.getcwd()
680683

681-
# Set paths in the config dictionary.
682684
config["project_root"] = project_root
683-
config["config_dir"] = os.path.dirname(os.path.abspath(config_path))
685+
config["config_dir"] = os.path.dirname(abs_config_path)
684686

685687
return config
686688

@@ -721,9 +723,12 @@ def _execute_post_processing(config: Dict[str, Any]):
721723
all_end_index = i
722724

723725
if all_start_index != -1 and all_end_index != -1:
724-
for i in range(all_start_index + 1, all_end_index):
725-
member = lines[i].strip().replace('"', "").replace(",", "")
726-
if member:
726+
all_content = "".join(lines[all_start_index + 1 : all_end_index])
727+
728+
# Extract quoted strings
729+
found_members = re.findall(r'"([^"]+)"', all_content)
730+
for member in found_members:
731+
if member not in all_list:
727732
all_list.append(member)
728733

729734
# --- Add new items and sort ---
@@ -736,7 +741,9 @@ def _execute_post_processing(config: Dict[str, Any]):
736741
for new_member in job.get("add_to_all", []):
737742
if new_member not in all_list:
738743
all_list.append(new_member)
739-
all_list.sort()
744+
all_list = sorted(list(set(all_list))) # Ensure unique and sorted
745+
# Format for the template
746+
all_list = [f' "{item}",\n' for item in all_list]
740747

741748
# --- Render the new file content ---
742749
template = utils.load_template(template_path)

scripts/microgenerator/templates/post-processing/init.py.j2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ __version__ = package_version.__version__
2323
{%- endfor %}
2424

2525
__all__ = (
26-
{%- for item in all_list %}
27-
"{{ item }}",
26+
{% for item in all_list %}
27+
{{ item }}
2828
{%- endfor %}
2929
)

scripts/microgenerator/utils.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,10 @@ def write_code_to_file(output_path: str, content: str):
108108

109109
# An empty output_dir means the file is in the current directory.
110110
if output_dir:
111-
print(f" Ensuring output directory exists: {os.path.abspath(output_dir)}")
112111
os.makedirs(output_dir, exist_ok=True)
113112
if not os.path.isdir(output_dir):
114113
print(" Error: Output directory was not created.", file=sys.stderr)
115114
sys.exit(1)
116115

117-
print(f" Writing generated code to: {os.path.abspath(output_path)}")
118116
with open(output_path, "w", encoding="utf-8") as f:
119117
f.write(content)
120-
print(f"Successfully generated {output_path}")

0 commit comments

Comments
 (0)