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, 'cache', 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 output = compile_to_parse_tree(template, loader = lambda path: self.find_template(path)[0], path=template_name) # Compile to python output2 = compile_tree(output) template = Template(output2, template_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 _spellcheck(self, input_path): # Now compile all templates to the cache directory try: print termcolor.colored(input_path, 'green') # Open input file code = codecs.open(input_path, 'r', 'utf-8').read() # Compile tree = compile_to_parse_tree(code, loader=load_template_source, path=input_path) # For every text node for tag in tree.child_nodes_of_class([ DjangoTransTag, DjangoBlocktransTag, ]): if isinstance(tag, DjangoTransTag): self._check_sentence(f, tag, tag.string) if isinstance(tag, DjangoBlocktransTag): for t in tag.children: if isinstance(t, basestring): self._check_sentence(f, tag, t) except CompileException, e: self.print_warning(u'Warning: %s' % unicode(e))
def _compile_template(self, lang, input_path, output_path): try: # Open input file code = codecs.open(input_path, 'r', 'utf-8').read() # Compile output = compile_to_parse_tree(code, loader=load_template_source, path=input_path) output2 = compile_tree(output) # Open output file self._create_dir(os.path.split(output_path)[0]) codecs.open(output_path, 'w', 'utf-8').write(output2) except CompileException, e: self.print_error(u'ERROR: %s' % unicode(e))
def _spellcheck(self, input_path): # Now compile all templates to the cache directory try: print termcolor.colored(input_path, 'green') # Open input file code = codecs.open(input_path, 'r', 'utf-8').read() # Compile tree = compile_to_parse_tree(code, loader=load_template_source, path=input_path) # For every text node for tag in tree.child_nodes_of_class([DjangoTransTag, DjangoBlocktransTag, ]): if isinstance(tag, DjangoTransTag): self._check_sentence(f, tag, tag.string) if isinstance(tag, DjangoBlocktransTag): for t in tag.children: if isinstance(t, basestring): self._check_sentence(f, tag, t) except CompileException, e: self.print_warning(u'Warning: %s' % unicode(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, 'cache', 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 output = compile_to_parse_tree( template, loader=lambda path: self.find_template(path)[0], path=template_name) # Compile to python output2 = compile_tree(output) template = Template(output2, template_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
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')