Skip to content

Commit f256a7f

Browse files
committed
Removed all references to jinja, and removed it as a dependency.
Test Plan: Tests pass. Reviewed By: guido
1 parent f584362 commit f256a7f

26 files changed

Lines changed: 40 additions & 3626 deletions

babelapi/compiler.py

Lines changed: 7 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
import re
77
import shutil
88

9-
from babelapi.generator.generator import CodeGenerator
10-
from babelapi.generator.jinja import Jinja2Generator
9+
from babelapi.generator.generator import Generator
1110

1211
class UnknownGenerator(Exception): pass
1312
class UnknownSourceType(Exception): pass
@@ -50,13 +49,8 @@ def __init__(self,
5049
self.build_path)
5150
shutil.rmtree(self.build_path)
5251

53-
self.generators = dict(
54-
jinja2=Jinja2Generator(api),
55-
)
56-
5752
def build(self):
58-
"""Creates outputs. Outputs may be renderings of templates or files
59-
made by a code generator."""
53+
"""Creates outputs. Outputs are files made by a generator."""
6054
if os.path.exists(self.build_path) and not os.path.isdir(self.build_path):
6155
self._logger.error('Output path must be a folder if it already exists')
6256
return
@@ -68,7 +62,7 @@ def build(self):
6862
else:
6963
self._logger.info('Found generator at %s', self.generator_path)
7064
Compiler._mkdir(self.build_path)
71-
self._process_file(self.generator_path, self.build_path)
65+
self._process_file(self.generator_path)
7266
else:
7367
self._logger.error('Could not find generator at %s', self.generator_path)
7468

@@ -85,16 +79,6 @@ def _mkdir(path):
8579
if e.errno != 17:
8680
raise
8781

88-
@classmethod
89-
def _is_babel_template(cls, path):
90-
"""
91-
Returns True if the file name matches the format of a babel template
92-
file, ie. its inner extension is "babelt". For example: xyz.babelt.py
93-
"""
94-
path_without_ext, first_ext = os.path.splitext(path)
95-
_, second_ext = os.path.splitext(path_without_ext)
96-
return second_ext == cls.template_extension
97-
9882
@classmethod
9983
def _is_babel_generator(cls, path):
10084
"""
@@ -114,42 +98,13 @@ def _get_babel_template_target_file(path):
11498
path_without_babel_ext, second_ext = os.path.splitext(path_without_ext)
11599
return path_without_babel_ext + first_ext
116100

117-
def _process_file(self, source_path, target_path):
101+
def _process_file(self, source_path):
118102
"""Renders a source file into its final form."""
119103

120104
with open(source_path) as f:
121105
file_contents = f.read()
122106

123-
if self._is_babel_template(source_path):
124-
# Read first line for preamble
125-
first_line = file_contents.split('\n', 1)[0]
126-
matches = self.first_line_re.search(first_line)
127-
if matches:
128-
target_file_path = os.path.join(target_path, os.path.basename(source_path))
129-
output_path = Compiler._get_babel_template_target_file(target_file_path)
130-
self._logger.info('Converting template file %s to %s',
131-
source_path, output_path)
132-
# Use preamble to determine which generator to use
133-
generator_type = matches.group(1)
134-
if generator_type not in self.generators:
135-
raise UnknownGenerator(generator_type)
136-
137-
generator = self.generators[generator_type]
138-
139-
_, ext = os.path.splitext(source_path)
140-
rendered_contents = generator.render(ext, file_contents)
141-
142-
# Inject "Generated by" message in place of the preamble
143-
new_first_line = first_line.replace(matches.group(0),
144-
'Generated by BabelAPI')
145-
rendered_contents = (new_first_line +
146-
rendered_contents[len(first_line):])
147-
148-
with open(output_path, 'w') as f:
149-
f.write(rendered_contents)
150-
else:
151-
raise MissingBabelPreamble()
152-
elif self._is_babel_generator(source_path):
107+
if self._is_babel_generator(source_path):
153108
self._logger.info('Running generator at %s', source_path)
154109
# If there's no preamble, then we assume this is a Python file
155110
# that defines a generator.
@@ -163,9 +118,9 @@ def _process_file(self, source_path, target_path):
163118
for attr_key in dir(generator_module):
164119
attr_value = getattr(generator_module, attr_key)
165120
if (inspect.isclass(attr_value)
166-
and issubclass(attr_value, CodeGenerator)
121+
and issubclass(attr_value, Generator)
167122
and not inspect.isabstract(attr_value)):
168123
generator = attr_value(self.api, self.build_path)
169124
generator.generate()
170125
else:
171-
raise UnknownSourceType()
126+
raise UnknownSourceType(source_path)

babelapi/generator/generator.py

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
import os
55
import textwrap
66

7-
class CodeGenerator(object):
7+
class Generator(object):
88
"""
9-
The parent class for code generation. All code generators should extend
10-
this class to be recognized as such.
9+
The parent class for all generators. All generators should extend this
10+
class to be recognized as such.
1111
12-
You will want to implement the generate() function to do the code generation
12+
You will want to implement the generate() function to do the generation
1313
that you need.
1414
1515
Here's roughly what you need to do in generate().
@@ -37,6 +37,28 @@ def __init__(self, api, target_folder_path):
3737
self.lineno = 1
3838
self.cur_indent = 0
3939

40+
@abstractmethod
41+
def generate(self):
42+
"""Subclasses should override this method. It's the entry point for
43+
all code generation given the api description."""
44+
raise NotImplemented
45+
46+
@contextmanager
47+
def output_to_relative_path(self, relative_path):
48+
"""
49+
Sets up generator so that all emits are directed towards the new file
50+
created at :param:`relative_path`.
51+
52+
Clears output buffer on enter, and on exit.
53+
"""
54+
full_path = os.path.join(self.target_folder_path, relative_path)
55+
self._logger.info('Generating %s', full_path)
56+
self.output = []
57+
yield
58+
with open(full_path, 'w') as f:
59+
f.write(''.join(self.output))
60+
self.output = []
61+
4062
@contextmanager
4163
def indent(self, dent=None):
4264
"""
@@ -129,6 +151,12 @@ def emit_wrapped_lines(self, s, prefix='', width=80, trailing_newline=True, firs
129151
if trailing_newline:
130152
self.emit('\n')
131153

154+
class CodeGenerator(Generator):
155+
"""
156+
Extend this instead of :class:`Generator` when generating source code.
157+
Contains helper functions specific to code generation.
158+
"""
159+
132160
def _filter_out_none_valued_keys(self, d):
133161
"""Given a dict, returns a new dict with all the same key/values except
134162
for keys that had values of None."""
@@ -138,28 +166,6 @@ def _filter_out_none_valued_keys(self, d):
138166
new_d[k] = v
139167
return new_d
140168

141-
@contextmanager
142-
def output_to_relative_path(self, relative_path):
143-
"""
144-
Sets up generator so that all emits are directed towards the new file
145-
created at :param:`relative_path`.
146-
147-
Clears output buffer on enter, and on exit.
148-
"""
149-
full_path = os.path.join(self.target_folder_path, relative_path)
150-
self._logger.info('Generating %s', full_path)
151-
self.output = []
152-
yield
153-
with open(full_path, 'w') as f:
154-
f.write(''.join(self.output))
155-
self.output = []
156-
157-
@abstractmethod
158-
def generate(self):
159-
"""Subclasses should override this method. It's the entry point for
160-
all code generation given the api description."""
161-
raise NotImplemented
162-
163169
def _generate_func_arg_list(self, args, compact=True):
164170
"""
165171
Given a list of arguments to a function, emits the args, one per line
@@ -211,16 +217,3 @@ class CodeGeneratorMonolingual(CodeGenerator):
211217
def __init__(self, api, target_folder_path):
212218
assert self.lang, 'Language must be specified'
213219
super(CodeGeneratorMonolingual, self).__init__(api, target_folder_path)
214-
215-
class Generator(object):
216-
"""
217-
Deprecated way of generating code using jinja2.
218-
TODO(kelkabany): Remove this when jinja2 has been excised.
219-
"""
220-
def __init__(self, api):
221-
self.api = api
222-
self._logger = logging.getLogger('bablesdk.generator.%s'
223-
% self.__class__.__name__)
224-
225-
def render(self, extension, text):
226-
raise NotImplemented

babelapi/generator/jinja.py

Lines changed: 0 additions & 167 deletions
This file was deleted.
-2.32 KB
Binary file not shown.

example/template/docs/developers-vfltrPnIR.css

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)