66import re
77import shutil
88
9- from babelapi .generator .generator import CodeGenerator
10- from babelapi .generator .jinja import Jinja2Generator
9+ from babelapi .generator .generator import Generator
1110
1211class UnknownGenerator (Exception ): pass
1312class 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 )
0 commit comments