Skip to content

Commit a309a79

Browse files
authored
Feat: microgen - adds _client_helpers.py.j2 template (#2299)
* chore: removes old proof of concept * removes old __init__.py * Adds two utility files to handle basic tasks * Adds a configuration file for the microgenerator * Removes unused comment * chore: adds noxfile.py for the microgenerator * feat: microgen - adds two init file templates * feat: adds _helpers.py.js template * Updates with two usage examples * feat: adds two partial templates for creating method signatures * feat: Add microgenerator __init__.py Migrates the empty __init__.py file to the microgenerator package. * feat: Add AST analysis utilities Introduces the CodeAnalyzer class and helper functions for parsing Python code using the ast module. This provides the foundation for understanding service client structures. * feat: Add source file analysis capabilities Implements functions to analyze Python source files, including: - Filtering classes and methods based on configuration. - Building a schema of request classes and their arguments. - Processing service client files to extract relevant information. * feat: adds code generation logic * removes extraneous content * feat: microgen - adds code generation logic * feat: microgen - adds main execution and post-processing logic * minor tweak to markers * feat: Add testing directory\n\nAdds the scripts/microgenerator/testing directory. * feat: Enhance to_snake_case to handle acronyms\n\nImproves the to_snake_case function in name_utils.py to correctly convert PascalCase names containing acronyms to snake_case. * feat: Add client.py.j2 template\n\nAdds the main Jinja2 template for generating the BigQueryClient class. * feat: Add _client_helpers.j2 partial template\n\nAdds a Jinja2 partial template containing helper macros for the client generation. * Update scripts/microgenerator/testing/constraints-3.13.txt * Update scripts/microgenerator/generate.py * Update scripts/microgenerator/generate.py
1 parent 5be86dd commit a309a79

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{#
2+
This is a partial template file intended to be included in other templates.
3+
It contains helper methods for the BigQueryClient class.
4+
#}
5+
6+
# --- HELPER METHODS ---
7+
def _parse_dataset_path(self, dataset_path: str) -> Tuple[Optional[str], str]:
8+
"""
9+
Helper to parse project_id and/or dataset_id from a string identifier.
10+
11+
Args:
12+
dataset_path: A string in the format 'project_id.dataset_id' or
13+
'dataset_id'.
14+
15+
Returns:
16+
A tuple of (project_id, dataset_id).
17+
"""
18+
if "." in dataset_path:
19+
# Use rsplit to handle legacy paths like `google.com:my-project.my_dataset`.
20+
project_id, dataset_id = dataset_path.rsplit(".", 1)
21+
return project_id, dataset_id
22+
return self.project, dataset_path
23+
24+
def _parse_dataset_id_to_dict(self, dataset_id: "DatasetIdentifier") -> dict:
25+
"""
26+
Helper to create a dictionary from a project_id and dataset_id to pass
27+
internally between helper functions.
28+
29+
Args:
30+
dataset_id: A string or DatasetReference.
31+
32+
Returns:
33+
A dict of {"project_id": project_id, "dataset_id": dataset_id_str }.
34+
"""
35+
if isinstance(dataset_id, str):
36+
project_id, dataset_id_str = self._parse_dataset_path(dataset_id)
37+
return {"project_id": project_id, "dataset_id": dataset_id_str}
38+
elif isinstance(dataset_id, dataset_reference.DatasetReference):
39+
return {
40+
"project_id": dataset_id.project_id,
41+
"dataset_id": dataset_id.dataset_id,
42+
}
43+
else:
44+
raise TypeError(f"Invalid type for dataset_id: {type(dataset_id)}")
45+
46+
def _parse_project_id_to_dict(self, project_id: Optional[str] = None) -> dict:
47+
"""Helper to create a request dictionary from a project_id."""
48+
final_project_id = project_id or self.project
49+
return {"project_id": final_project_id}

0 commit comments

Comments
 (0)