Beispiel #1
0
 def tokens(self):
     '''
     returns a list with all the tokens in this template.
     '''
     source_content = self.source_file.read()
     lexer = DebugLexer(source_content, None)
     return lexer.tokenize()
Beispiel #2
0
    def clean_content(self):
        content = self.cleaned_data.get('content', '')
        if content:

            # Pre-Django1.9
            try:
                from django.template.debug import DebugLexer, DebugParser
            # Django1.9
            except ImportError:
                from django.template import Template
                template = Template(template_string=content)
                template.compile_nodelist()
            # Pre-Django1.9
            else:
                origin = StringOrigin(content)
                lexer = DebugLexer(content, origin)
                try:
                    parser = DebugParser(lexer.tokenize())
                    parser.parse()
                except Exception as e:
                    self.exc_info = sys.exc_info()
                    if not hasattr(self.exc_info[1], 'django_template_source'):
                        self.exc_info[1].django_template_source = origin, (0,
                                                                           0)
                    raise forms.ValidationError('Invalid Django Template')
        return content
Beispiel #3
0
    def clean_content(self):
        content = self.cleaned_data.get('content', '')
        if content:

            # Pre-Django1.9
            try:
                from django.template.debug import DebugLexer, DebugParser
            # Django1.9
            except ImportError:
                from django.template import Template
                template = Template(template_string=content)
                template.compile_nodelist()
            # Pre-Django1.9
            else:
                origin = StringOrigin(content)
                lexer = DebugLexer(content, origin)
                try:
                    parser = DebugParser(lexer.tokenize())
                    parser.parse()
                except Exception as e:
                    self.exc_info = sys.exc_info()
                    if not hasattr(self.exc_info[1], 'django_template_source'):
                        self.exc_info[1].django_template_source = origin, (0, 0)
                    raise forms.ValidationError('Invalid Django Template')
        return content
Beispiel #4
0
 def clean_content(self):
     content = self.cleaned_data.get('content', '')
     if content:
         origin = StringOrigin(content)
         lexer = DebugLexer(content, origin)
         try:
             parser = DebugParser(lexer.tokenize())
             parser.parse()
         except Exception as e:
             self.exc_info = sys.exc_info()
             if not hasattr(self.exc_info[1], 'django_template_source'):
                 self.exc_info[1].django_template_source = origin, (0, 0)
             raise forms.ValidationError('Invalid Django Template')
     return content
Beispiel #5
0
 def clean_content(self):
     content = self.cleaned_data.get('content', '')
     if content:
         origin = StringOrigin(content)
         lexer = DebugLexer(content, origin)
         try:
             parser = DebugParser(lexer.tokenize())
             parser.parse()
         except Exception as e:
             self.exc_info = sys.exc_info()
             if not hasattr(self.exc_info[1], 'django_template_source'):
                 self.exc_info[1].django_template_source = origin, (0, 0)
             raise forms.ValidationError('Invalid Django Template')
     return content
Beispiel #6
0
    def _get_tokens(self):
        """
        Get the list of tokens from the template source.

        A modified version of Django's compile_nodelist method (Template class)
        https://github.com/django/django/blob/master/django/template/base.py#L221

        :returns: a list of Tokens
        """
        # From Django's source code
        if self.engine.debug:
            from django.template.debug import DebugLexer
            lexer = DebugLexer(self.source, self.origin)
        else:
            lexer = Lexer(self.source, self.origin)

        return lexer.tokenize()
Beispiel #7
0
    def clean_content(self):
        content = self.cleaned_data.get('content', '')
        if content:
            origin = StringOrigin(content)

            try:
                from django.template.debug import DebugLexer, DebugParser
            except ImportError:
                # django.template.debug doesn't exist in Django >= 1.9, so use
                # Template from django.template instead
                from django.template import Template
                # Try to create a Template
                try:
                    template = Template(template_string=origin)
                # This is an error with creating the template
                except Exception as e:
                    self.exc_info = {
                        'message': e.args,
                        'line': e.token.lineno,
                        'name': origin.name,
                    }
                    raise forms.ValidationError('Invalid Django Template')
                # Template has been created; try to parse
                try:
                    template.compile_nodelist()
                # This is an error with parsing
                except Exception as e:
                    # The data we pass to the views is in e.template_debug
                    e.template_debug = template.get_exception_info(e, e.token)
                    self.exc_info = e.template_debug
                    raise forms.ValidationError('Parsing Error')
            else:
                lexer = DebugLexer(content, origin)
                try:
                    parser = DebugParser(lexer.tokenize())
                    parser.parse()
                except Exception as e:
                    self.exc_info = sys.exc_info()
                    if not hasattr(self.exc_info[1], 'django_template_source'):
                        self.exc_info[1].django_template_source = origin, (0, 0)
                    raise forms.ValidationError('Invalid Django Template')
        return content
Beispiel #8
0
def install_templates(srcroot,
                      destroot,
                      prefix='',
                      excludes=None,
                      includes=None,
                      app_name=None):
    #pylint:disable=too-many-arguments
    """
    Expand link to compiled assets all templates in *srcroot*
    and its subdirectories.
    """
    #pylint: disable=too-many-locals
    if not os.path.exists(os.path.join(prefix, destroot)):
        os.makedirs(os.path.join(prefix, destroot))
    for pathname in os.listdir(os.path.join(srcroot, prefix)):
        pathname = os.path.join(prefix, pathname)
        excluded = False
        for pat in excludes:
            if re.match(pat, pathname):
                excluded = True
                break
        if excluded:
            for pat in includes:
                if re.match(pat, pathname):
                    excluded = False
                    break
        if excluded:
            LOGGER.debug("skip %s", pathname)
            continue
        source_name = os.path.join(srcroot, pathname)
        dest_name = os.path.join(destroot, pathname)
        if os.path.isfile(source_name):
            with open(source_name) as source:
                template_string = source.read()
            try:
                template_string = force_text(template_string)
                lexer = DebugLexer(template_string, origin=None)
                tokens = lexer.tokenize()
                if not os.path.isdir(os.path.dirname(dest_name)):
                    os.makedirs(os.path.dirname(dest_name))
                with open(dest_name, 'w') as dest:
                    parser = AssetsParser(tokens,
                                          URLRewriteWrapper(dest, app_name))
                    parser.parse_through()
                cmdline = ['diff', '-u', source_name, dest_name]
                cmd = subprocess.Popen(cmdline, stdout=subprocess.PIPE)
                lines = cmd.stdout.readlines()
                cmd.wait()
                # Non-zero error codes are ok here. That's how diff
                # indicates the files are different.
                if len(lines) > 0:
                    verb = 'compile'
                else:
                    verb = 'install'
                print "%s %s to %s" % (
                    verb,
                    source_name.replace(django_settings.BASE_DIR,
                                        '*APP_ROOT*'),
                    dest_name.replace(destroot, '*MULTITIER_TEMPLATES_ROOT*'))
                LOGGER.info(
                    "%s %s to %s", verb,
                    source_name.replace(django_settings.BASE_DIR,
                                        '*APP_ROOT*'),
                    dest_name.replace(destroot, '*MULTITIER_TEMPLATES_ROOT*'))
            except UnicodeDecodeError:
                LOGGER.warning(
                    "%s: Templates can only be constructed "
                    "from unicode or UTF-8 strings.", source_name)
        elif os.path.isdir(source_name):
            install_templates(srcroot,
                              destroot,
                              prefix=pathname,
                              excludes=excludes,
                              includes=includes,
                              app_name=app_name)
def install_templates(srcroot, destroot,
                      prefix='', excludes=None, includes=None, app_name=None):
    #pylint:disable=too-many-arguments
    """
    Expand link to compiled assets all templates in *srcroot*
    and its subdirectories.
    """
    #pylint: disable=too-many-locals
    if not os.path.exists(os.path.join(prefix, destroot)):
        os.makedirs(os.path.join(prefix, destroot))
    for pathname in os.listdir(os.path.join(srcroot, prefix)):
        pathname = os.path.join(prefix, pathname)
        excluded = False
        for pat in excludes:
            if re.match(pat, pathname):
                excluded = True
                break
        if excluded:
            for pat in includes:
                if re.match(pat, pathname):
                    excluded = False
                    break
        if excluded:
            LOGGER.debug("skip %s", pathname)
            continue
        source_name = os.path.join(srcroot, pathname)
        dest_name = os.path.join(destroot, pathname)
        if os.path.isfile(source_name):
            with open(source_name) as source:
                template_string = source.read()
            try:
                template_string = force_text(template_string)
                lexer = DebugLexer(template_string, origin=None)
                tokens = lexer.tokenize()
                if not os.path.isdir(os.path.dirname(dest_name)):
                    os.makedirs(os.path.dirname(dest_name))
                with open(dest_name, 'w') as dest:
                    parser = AssetsParser(
                        tokens, URLRewriteWrapper(dest, app_name))
                    parser.parse_through()
                cmdline = ['diff', '-u', source_name, dest_name]
                cmd = subprocess.Popen(cmdline, stdout=subprocess.PIPE)
                lines = cmd.stdout.readlines()
                cmd.wait()
                # Non-zero error codes are ok here. That's how diff
                # indicates the files are different.
                if len(lines) > 0:
                    verb = 'compile'
                else:
                    verb = 'install'
                print "%s %s to %s" % (verb,
                    source_name.replace(
                        django_settings.BASE_DIR, '*APP_ROOT*'),
                    dest_name.replace(destroot,
                        '*MULTITIER_TEMPLATES_ROOT*'))
                LOGGER.info("%s %s to %s", verb,
                    source_name.replace(
                        django_settings.BASE_DIR, '*APP_ROOT*'),
                    dest_name.replace(destroot,
                        '*MULTITIER_TEMPLATES_ROOT*'))
            except UnicodeDecodeError:
                LOGGER.warning("%s: Templates can only be constructed "
                    "from unicode or UTF-8 strings.", source_name)
        elif os.path.isdir(source_name):
            install_templates(srcroot, destroot, prefix=pathname,
                excludes=excludes, includes=includes, app_name=app_name)
def get_called_templates(tpl_string, caller):
    template_string = smart_unicode(tpl_string)
    origin = StringOrigin(template_string)
    lexer = DebugLexer(template_string, origin)
    parser = CalledTemplatesParser(lexer.tokenize())
    return parser.parse(caller)