Skip to content

Commit a0b16ff

Browse files
authored
feat: microgen - adds two init file templates (#2286)
* 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: microgen - adds _helpers.py.j2 template (#2287) * feat: adds _helpers.py.js template * Updates with two usage examples * updates the license header
1 parent 67f3d07 commit a0b16ff

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2025 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
from .client import BigQueryClient
18+
19+
__all__ = ("BigQueryClient",)
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2025 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
from typing import Any, Dict, List, Optional, Type
18+
19+
20+
def _create_request(
21+
request_class: Type,
22+
path_identifier: str,
23+
expected_args: List[str],
24+
default_project_id: Optional[str] = None,
25+
) -> Any:
26+
"""
27+
Constructs a *Request object from a class, path_identifier, and expected args.
28+
29+
Args:
30+
request_class: The class of the request object to create (e.g., GetDatasetRequest).
31+
path_identifier: The dot-separated string of resource IDs.
32+
expected_args: An ordered list of the argument names the request object
33+
expects (e.g., ['project_id', 'dataset_id', 'table_id']).
34+
default_project_id: The default project ID to use if needed.
35+
36+
Returns:
37+
An instantiated request object.
38+
39+
Examples:
40+
>>> # Example with project_id provided in path_identifier
41+
>>> request = _create_request(
42+
... request_class=GetDatasetRequest,
43+
... path_identifier="my-project.my-dataset",
44+
... expected_args=["project_id", "dataset_id"]
45+
... )
46+
>>> request.project_id
47+
'my-project'
48+
>>> request.dataset_id
49+
'my-dataset'
50+
51+
>>> # Example with project_id omitted from path_identifier, using default_project_id
52+
>>> request = _create_request(
53+
... request_class=GetDatasetRequest,
54+
... path_identifier="my-dataset",
55+
... expected_args=["project_id", "dataset_id"],
56+
... default_project_id="my-default-project"
57+
... )
58+
>>> request.project_id
59+
'my-default-project'
60+
>>> request.dataset_id
61+
'my-dataset'
62+
63+
"""
64+
# Start of inlined parse_path_to_request_inputs
65+
segments = path_identifier.split(".")
66+
num_segments = len(segments)
67+
num_expected = len(expected_args)
68+
project_id_is_expected = "project_id" in expected_args
69+
70+
# Validate the number of segments.
71+
if not (
72+
num_segments == num_expected
73+
or (project_id_is_expected and num_segments == num_expected - 1)
74+
):
75+
raise ValueError(
76+
f"Invalid path identifier '{path_identifier}'. Expected "
77+
f"{num_expected} parts (or {num_expected - 1} if project_id is "
78+
f"omitted), but got {num_segments}."
79+
)
80+
81+
# If project_id is implicitly expected, use the default.
82+
if project_id_is_expected and num_segments == num_expected - 1:
83+
if not default_project_id:
84+
raise ValueError(
85+
f"Missing project_id in path '{path_identifier}' and no "
86+
"default_project_id was provided."
87+
)
88+
# Prepend the default project_id to the segments.
89+
segments.insert(0, default_project_id)
90+
91+
request_inputs = dict(zip(expected_args, segments))
92+
# End of inlined parse_path_to_request_inputs
93+
94+
# Instantiate the request object.
95+
return request_class(**request_inputs)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2025 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
from google.cloud.bigquery_v2 import gapic_version as package_version
18+
19+
__version__ = package_version.__version__
20+
21+
{% for import in imports %}
22+
{{ import }}
23+
{%- endfor %}
24+
25+
__all__ = (
26+
{%- for item in all_list %}
27+
"{{ item }}",
28+
{%- endfor %}
29+
)

0 commit comments

Comments
 (0)