def _compile_template(self, lang, input_path, output_path, no_html=False): try: # Create output directory self._create_dir(os.path.split(output_path)[0]) try: # Open input file code = codecs.open(input_path, 'r', 'utf-8').read() except UnicodeDecodeError, e: raise CompileException(0, 0, input_path, str(e)) # Compile if no_html: output = compile(code, loader=load_template_source, path=input_path, options=get_options_for_path(input_path) + ['no-html']) else: output = compile(code, loader=load_template_source, path=input_path, options=get_options_for_path(input_path)) # Open output file codecs.open(output_path, 'w', 'utf-8').write(output) # Delete -c-recompile file (mark for recompilation) if one such exist. if os.path.exists(output_path + '-c-recompile'): os.remove(output_path + '-c-recompile') return True
def load_template(self, template_name, template_dirs=None): # IMPORTANT NOTE: We load the template, using the original loaders. # call compile, but still return the original, # unmodified result. This causes the django to call # load_template again for every include node where of # course the validation may fail. (incomplete HTML # tree, maybe only javascript, etc...) # Load template template, origin = self.find_template(template_name, template_dirs) # Compile template as a test (could raise CompileException), throw away the compiled result. try: # Don't compile template when a parent frame was a 'render' method. Than it's probably a # runtime call from an IncludeNode or ExtendsNode. import inspect if not any(i[3] in ('render', 'do_include') for i in inspect.getouterframes(inspect.currentframe())): # Precompile command print 'compiling %s' % template_name execute_precompile_command() compile(template, loader = lambda path: self.find_template(path)[0], path=template_name, options=get_options_for_path(origin.name) + _OVERRIDE_OPTIONS_FOR_VALIDATION ) except Exception, e: # Print exception on console print '---' print 'Template: %s' % template_name print e print '-' import traceback traceback.print_exc() print '---' raise e
def load_template(self, template_name, template_dirs=None): lang = translation.get_language() or 'en' key = '%s-%s' % (lang, template_name) if key not in self.template_cache: # Path in the cache directory output_path = os.path.join(self.__cache_dir, lang, template_name) # Load template if os.path.exists(output_path): # Prefer precompiled version template = codecs.open(output_path, 'r', 'utf-8').read() origin = StringOrigin(template) else: template, origin = self.find_template(template_name, template_dirs) # Compile template template, context = compile( template, loader=lambda path: self.find_template(path)[0], path=template_name, options=get_options_for_path(origin.name) ) # Turn into Template object template = get_template_from_string(template, origin, template_name) # Save in cache self.template_cache[key] = template # Return result return self.template_cache[key], None
def process_template(self, template, input_path): # TODO: when HTML processing fails, the 'context' attribute is not # retreived and no translations are failed. so, translate again # without html. (But we need to process html, in order to find # gettext() in javascript.) try: try: # Open input file code = codecs.open(input_path, 'r', 'utf-8').read() except UnicodeDecodeError, e: raise CompileException(0, 0, input_path, str(e)) # Compile output, context = compile(code, path=input_path, loader=load_template_source, options=get_options_for_path(input_path)) for entry in context.gettext_entries: line = '#: %s:%s:%s' % (entry.path, entry.line, entry.column) if not entry.text in self.strings: self.strings[entry.text] = set() self.strings[entry.text].add(line) if self.verbosity >= 2: sys.stderr.write(line + '\n') sys.stderr.write('msgid "%s"\n\n' % entry.text.replace('"', r'\"'))
def load_template(self, template_name, template_dirs=None): template, origin = self.find_template(template_name, template_dirs) # Compile template (we shouldn't compile anything at runtime.) template = compile(template, loader = lambda path: self.find_template(path)[0], path=template_name, options=get_options_for_path(origin.name)) # Turn into Template object template = get_template_from_string(template, origin, template_name) # Return result return template, None
def load_template(self, template_name, template_dirs=None): template, origin = self.find_template(template_name, template_dirs) # Precompile command execute_precompile_command() # Compile template template, context = compile(template, path=template_name, loader = lambda path: self.find_template(path)[0], options=get_options_for_path(origin.name) + self.options, context_class=self.context_class) # Turn into Template object template = get_template_from_string(template, origin, template_name) # Return result return template, None
def load_template(self, template_name, template_dirs=None): template, origin = self.find_template(template_name, template_dirs) # Precompile command execute_precompile_command() print 'compiling %s' % template_name # Compile template template, context = compile( template, path=template_name, loader=lambda path: self.find_template(path)[0], options=get_options_for_path(origin.name) + self.options, context_class=self.context_class) # Turn into Template object template = get_template_from_string(template, origin, template_name) # Return result return template, None
def load_template(self, template_name, template_dirs=None): # IMPORTANT NOTE: We load the template, using the original loaders. # call compile, but still return the original, # unmodified result. This causes the django to call # load_template again for every include node where of # course the validation may fail. (incomplete HTML # tree, maybe only javascript, etc...) # Load template template, origin = self.find_template(template_name, template_dirs) # Compile template as a test (could raise CompileException), throw away the compiled result. try: # Don't compile template when a parent frame was a 'render' method. Than it's probably a # runtime call from an IncludeNode or ExtendsNode. import inspect if not any( i[3] in ('render', 'do_include') for i in inspect.getouterframes(inspect.currentframe())): # Precompile command print 'compiling %s' % template_name execute_precompile_command() compile(template, loader=lambda path: self.find_template(path)[0], path=template_name, options=get_options_for_path(origin.name) + _OVERRIDE_OPTIONS_FOR_VALIDATION) except Exception, e: # Print exception on console print '---' print 'Template: %s' % template_name print e print '-' import traceback traceback.print_exc() print '---' raise e
try: # Create output directory self._create_dir(os.path.split(output_path)[0]) try: # Open input file code = codecs.open(input_path, 'r', 'utf-8').read() except UnicodeDecodeError, e: raise CompileException(0, 0, input_path, str(e)) except IOError, e: raise CompileException(0, 0, input_path, str(e)) # Compile if no_html: output, context = compile(code, path=input_path, loader=load_template_source, options=get_options_for_path(input_path) + ['no-html'], context_class=self.NiceContext) else: output, context = compile(code, path=input_path, loader=load_template_source, options=get_options_for_path(input_path), context_class=self.NiceContext) # store dependencies self._save_template_dependencies(lang, template, context.template_dependencies) self._save_first_level_template_dependencies(lang, template, context.include_dependencies, context.extends_dependencies) # Open output file codecs.open(output_path, 'w', 'utf-8').write(output) # Delete -c-recompile file (mark for recompilation) if one such exist.
try: # Open input file code = codecs.open(input_path, 'r', 'utf-8').read() except UnicodeDecodeError, e: raise CompileException(0, 0, input_path, str(e)) except IOError, e: raise CompileException(0, 0, input_path, str(e)) # Compile if no_html: output, context = compile( code, path=input_path, loader=load_template_source, options=get_options_for_path(input_path) + ['no-html'], context_class=self.NiceContext) else: output, context = compile( code, path=input_path, loader=load_template_source, options=get_options_for_path(input_path), context_class=self.NiceContext) # store dependencies self._save_template_dependencies(lang, template, context.template_dependencies) self._save_first_level_template_dependencies( lang, template, context.include_dependencies, context.extends_dependencies)
class Command(BaseCommand): help = "Print an overview of all the strings in the templates which lack {% trans %} blocks around them." option_list = BaseCommand.option_list + ( make_option('--single-template', action='append', dest='single_template', help='Compile only this template'), make_option('--boring', action='store_true', dest='boring', help='No colors in output'), ) def print_error(self, text): self._errors.append(text) print self.colored(text, 'white', 'on_red').encode('utf-8') def colored(self, text, *args, **kwargs): if self.boring: return text else: return termcolor.colored(text, *args, **kwargs) def handle(self, *args, **options): single_template = options['single_template'] # Default verbosity self.verbosity = int(options.get('verbosity', 1)) # Colors? self.boring = bool(options.get('boring')) self._errors = [] # Build compile queue queue = self._build_compile_queue(single_template) # Compile queue for i in range(0, len(queue)): if self.verbosity >= 2: print self.colored('%i / %i |' % (i + 1, len(queue)), 'yellow'), print self.colored(queue[i][1], 'green') self._compile_template(*queue[i]) # Ring bell :) print '\x07' def _build_compile_queue(self, single_template=None): """ Build a list of all the templates to be compiled. """ # Create compile queue queue = set() # Use a set, avoid duplication of records. if self.verbosity >= 2: print 'Building queue' # Now compile all templates to the cache directory for dir, t in template_iterator(): input_path = os.path.normpath(os.path.join(dir, t)) # Compile this template if: if ( # We are compiling *everything* not single_template or # Or this is the only template that we want to compile (single_template and t in single_template)): queue.add((t, input_path)) # Return ordered queue queue = list(queue) queue.sort() return queue def _compile_template(self, template, input_path): try: try: # Open input file code = codecs.open(input_path, 'r', 'utf-8').read() except UnicodeDecodeError, e: raise CompileException(0, 0, input_path, str(e)) except IOError, e: raise CompileException(0, 0, input_path, str(e)) # Get options for this template options = get_options_for_path(input_path) options.append('no-i18n-preprocessing') # Compile tree, context = compile_to_parse_tree(code, path=input_path, loader=load_template_source, options=options) # Now find all nodes, which contain text, but not in trans blocks. from template_preprocessor.core.html_processor import HtmlContent, HtmlStyleNode, HtmlScriptNode def contains_alpha(s): # Return True when string contains at least a letter. for i in s: if i in string.ascii_letters: return True return False for node in tree.child_nodes_of_class(HtmlContent, dont_enter=(HtmlStyleNode, HtmlScriptNode)): content = node.output_as_string() s = content.strip() if s and contains_alpha(s): print self.colored(node.path, 'yellow'), ' ', print self.colored(' %s:%s' % (node.line, node.column), 'red') print s.encode('utf-8')