Skip to content

Commit bef9081

Browse files
committed
adds templating utility and template file
1 parent be1f568 commit bef9081

File tree

3 files changed

+36
-25
lines changed

3 files changed

+36
-25
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class BigQueryClient:
2+
def __init__(self):
3+
self._clients = {}
4+
5+
{% for method in methods %}
6+
def {{ method.name }}({{ method.args_for_def }}):
7+
"""A generated method to call the BigQuery API."""
8+
9+
if "{{ method.class_name }}" not in self._clients:
10+
from google.cloud.bigquery_v2 import {{ method.class_name }}
11+
self._clients["{{ method.class_name }}"] = {{ method.class_name }}()
12+
13+
client = self._clients["{{ method.class_name }}"]
14+
from google.cloud.bigquery_v2 import types
15+
request = types.{{ method.request_class_name }}({{ method.args_for_call }})
16+
return client.{{ method.name }}(request=request)
17+
18+
{% endfor %}

scripts/microgenerator/bigqueryclient_generator.py

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
import os
33
from collections import defaultdict
44

5-
import jinja2
6-
75
from config_helper import (
86
CLASSES_TO_INCLUDE,
97
# CLASSES_TO_EXCLUDE, # Not currently being used.
108
METHODS_TO_INCLUDE,
119
METHODS_TO_EXCLUDE,
1210
)
11+
from template_utils import load_template
1312

1413
# Constants
1514
BASE_DIR = "google/cloud/bigquery_v2/services"
@@ -112,27 +111,7 @@ def generate_client_class_source(data):
112111
for the BigQueryClient class.
113112
"""
114113

115-
# TODO: move template strings to a separate file.
116-
class_template_string = """\
117-
class BigQueryClient:
118-
def __init__(self):
119-
self._clients = {}
120-
121-
{% for method in methods %}
122-
def {{ method.name }}({{ method.args_for_def }}):
123-
\"\"\"A generated method to call the BigQuery API.\"\"\"
124-
125-
if "{{ method.class_name }}" not in self._clients:
126-
from google.cloud.bigquery_v2 import {{ method.class_name }}
127-
self._clients["{{ method.class_name }}"] = {{ method.class_name }}()
128-
129-
client = self._clients["{{ method.class_name }}"]
130-
from google.cloud.bigquery_v2 import types
131-
request = types.{{ method.request_class_name }}({{ method.args_for_call }})
132-
return client.{{ method.name }}(request=request)
133-
134-
{% endfor %}
135-
"""
114+
template = load_template("bigqueryclient.py.j2")
136115

137116
# Prepare the context for the template.
138117
# We transform the input data into a flat list of methods
@@ -151,8 +130,7 @@ def {{ method.name }}({{ method.args_for_def }}):
151130
}
152131
)
153132

154-
# Create a Jinja2 Template object and render it with the context.
155-
template = jinja2.Template(class_template_string, trim_blocks=True)
133+
# Render the template with the context.
156134
generated_code = template.render(methods=methods_context)
157135

158136
return generated_code
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import os
2+
import jinja2
3+
4+
5+
def load_template(template_name):
6+
"""
7+
Loads a Jinja2 template from the same directory as the script.
8+
"""
9+
template_dir = os.path.dirname(os.path.abspath(__file__))
10+
env = jinja2.Environment(
11+
loader=jinja2.FileSystemLoader(template_dir),
12+
trim_blocks=True,
13+
lstrip_blocks=True,
14+
)
15+
return env.get_template(template_name)

0 commit comments

Comments
 (0)