def templatize(src, origin=None): # Get all the paths that we know about paths = [os.path.abspath(path) for name, path in find('(.*)')] # Hijack the process if the file we're talking about is in one of the # finder paths. if origin and os.path.abspath(origin) in paths: with open(origin) as template_file: # Load the template content. content = template_file.read() # Find all the translatable strings. processor = I18nPreprocessor() pattern = re.compile(processor.trans_re, flags=re.DOTALL) strings = set(re.findall(pattern, content)) def escape(s): s = s.replace('\\', '\\\\') s = s.replace('"', r'\"') return s # Build a string that looks like a Python file that's ready to be # translated. translatable = '\n'.join(['_("""{0}""")'.format(escape(string)) for string in strings]) return translatable # If the file isn't in one of our paths, then delegate to the original # method. else: return base_templatize(src, origin)
def templatize(src, origin=None): # Get all the paths that we know about paths = [os.path.abspath(path) for name, path in find('(.*)')] # Hijack the process if the file we're talking about is in one of the # finder paths. if origin and os.path.abspath(origin) in paths: with open(origin) as template_file: # Load the template content. content = template_file.read() # Find all the translatable strings. processor = I18nPreprocessor() pattern = re.compile(processor.trans_re, flags=re.DOTALL) strings = set(re.findall(pattern, content)) def escape(s): s = s.replace('\\', '\\\\') s = s.replace('"', r'\"') return s # Build a string that looks like a Python file that's ready to be # translated. translatable = '\n'.join( ['_("""{0}""")'.format(escape(string)) for string in strings]) return translatable # If the file isn't in one of our paths, then delegate to the original # method. else: return base_templatize(src, origin)
def _load_template(template_name): from mustachejs.loading import find with open(find(template_name), 'r') as f: return f.read()