3838import collections
3939import traceback
4040import copy
41- import codecs
4241import jinja2
4342from .addons import template_helpers
4443from . import syslog_error , syslog_notice
@@ -81,7 +80,7 @@ def _encode_idna(x):
8180 # return source when unable to decode
8281 return x
8382
84- def list_module (self , module_name ):
83+ def list_module (self , module_name , strict = False ):
8584 """ list single module content
8685 :param module_name: module name in dot notation ( company.module )
8786 :return: dictionary with module data
@@ -99,7 +98,10 @@ def list_module(self, module_name):
9998 for line in fhandle .read ().split ('\n ' ):
10099 parts = line .split (':' )
101100 if len (parts ) > 1 and parts [0 ].strip ()[0 ] != '#' :
102- source_file = parts [0 ].strip ()
101+ if parts [0 ].startswith ('!' ) and not strict :
102+ # template reference is strict, only generate when explicity requested
103+ continue
104+ source_file = parts [0 ].lstrip ('!' ).strip ()
103105 target_name = parts [1 ].strip ()
104106 if target_name in list (result ['+TARGETS' ].values ()):
105107 syslog_notice ("template overlay %s with %s" % (
@@ -214,15 +216,16 @@ def _create_directory(filename):
214216 if not os .path .exists (tmppart ):
215217 os .mkdir (tmppart )
216218
217- def _generate (self , module_name , create_directory = True ):
219+ def _generate (self , module_name , create_directory = True , strict = False ):
218220 """ generate configuration files for one section using bound config and template data
219221
220222 :param module_name: module name in dot notation ( company.module )
221223 :param create_directory: automatically create directories to place template output in ( if not existing )
224+ :param strict: exact module was matched instead of a pattern used (e.g. template reload *)
222225 :return: list of generated output files
223226 """
224227 result = []
225- module_data = self .list_module (module_name )
228+ module_data = self .list_module (module_name , strict )
226229 for src_template in list (module_data ['+TARGETS' ]):
227230 target = module_data ['+TARGETS' ][src_template ]
228231
@@ -270,7 +273,7 @@ def _generate(self, module_name, create_directory=True):
270273 # make sure the target directory exists
271274 self ._create_directory (filename )
272275
273- f_out = codecs . open (filename , 'wb ' , encoding = "utf-8" )
276+ f_out = open (filename , 'w ' , encoding = "utf-8" )
274277 f_out .write (content )
275278 # Check if the last character of our output contains an end-of-line, if not copy it in if
276279 # it was in the original template.
@@ -325,7 +328,7 @@ def generate(self, module_name, create_directory=True):
325328 for template_name in self .iter_modules (module_name ):
326329 syslog_notice ("generate template container %s" % template_name )
327330 try :
328- for filename in self ._generate (template_name , create_directory ):
331+ for filename in self ._generate (template_name , create_directory , module_name . find ( '*' ) > 0 ):
329332 result .append (filename )
330333 except Exception as render_exception :
331334 # log failure, but proceed processing for possible wildcard search
0 commit comments