Skip to content

Commit 8fdc651

Browse files
authored
refactor: microgen - cleans up minor issues that prevent bqclient build (#2318)
* refactor: Updates to enable a successful build of the centralized client * relocates import statement
1 parent 66504db commit 8fdc651

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
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
@@ -498,7 +499,6 @@ def analyze_source_files(
498499
# Make the pattern absolute
499500
absolute_pattern = os.path.join(project_root, pattern)
500501
source_files.extend(glob.glob(absolute_pattern, recursive=True))
501-
502502
# PASS 1: Build the request argument schema from the types files.
503503
request_arg_schema = _build_request_arg_schema(source_files, project_root)
504504

@@ -559,14 +559,14 @@ def generate_code(config: Dict[str, Any], analysis_results: tuple) -> None:
559559
Generates source code files using Jinja2 templates.
560560
"""
561561
data, all_imports, all_types, request_arg_schema = analysis_results
562-
project_root = config["project_root"]
563-
config_dir = config["config_dir"]
564562

565563
templates_config = config.get("templates", [])
566564
for item in templates_config:
567-
template_path = str(Path(config_dir) / item["template"])
568-
output_path = str(Path(project_root) / item["output"])
565+
template_name = item["template"]
566+
output_name = item["output"]
569567

568+
template_path = str(Path(config["config_dir"]) / template_name)
569+
output_path = str(Path(config["project_root"]) / output_name)
570570
template = utils.load_template(template_path)
571571
methods_context = []
572572
for class_name, methods in data.items():
@@ -662,18 +662,20 @@ def find_project_root(start_path: str, markers: list[str]) -> str | None:
662662
return None
663663
current_path = parent_path
664664

665-
# Load configuration from the YAML file.
666-
config = utils.load_config(config_path)
665+
# Get the absolute path of the config file
666+
abs_config_path = os.path.abspath(config_path)
667+
config = utils.load_config(abs_config_path)
667668

668-
# Determine the project root.
669-
script_dir = os.path.dirname(os.path.abspath(__file__))
670-
project_root = find_project_root(script_dir, ["setup.py", ".git"])
669+
# Determine the project root
670+
# Start searching from the directory of this script file
671+
script_file_dir = os.path.dirname(os.path.abspath(__file__))
672+
project_root = find_project_root(script_file_dir, [".git"])
671673
if not project_root:
672-
project_root = os.getcwd() # Fallback to current directory
674+
# Fallback to the directory from which the script was invoked
675+
project_root = os.getcwd()
673676

674-
# Set paths in the config dictionary.
675677
config["project_root"] = project_root
676-
config["config_dir"] = os.path.dirname(os.path.abspath(config_path))
678+
config["config_dir"] = os.path.dirname(abs_config_path)
677679

678680
return config
679681

@@ -714,9 +716,12 @@ def _execute_post_processing(config: Dict[str, Any]):
714716
all_end_index = i
715717

716718
if all_start_index != -1 and all_end_index != -1:
717-
for i in range(all_start_index + 1, all_end_index):
718-
member = lines[i].strip().replace('"', "").replace(",", "")
719-
if member:
719+
all_content = "".join(lines[all_start_index + 1 : all_end_index])
720+
721+
# Extract quoted strings
722+
found_members = re.findall(r'"([^"]+)"', all_content)
723+
for member in found_members:
724+
if member not in all_list:
720725
all_list.append(member)
721726

722727
# --- Add new items and sort ---
@@ -729,7 +734,9 @@ def _execute_post_processing(config: Dict[str, Any]):
729734
for new_member in job.get("add_to_all", []):
730735
if new_member not in all_list:
731736
all_list.append(new_member)
732-
all_list.sort()
737+
all_list = sorted(list(set(all_list))) # Ensure unique and sorted
738+
# Format for the template
739+
all_list = [f' "{item}",\n' for item in all_list]
733740

734741
# --- Render the new file content ---
735742
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: 1 addition & 4 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):
114-
print(f" Error: Output directory was not created.", file=sys.stderr)
113+
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)