Exemple #1
0
 def _raise_error(self, message='', text=None):
     """
     Raises an error using itself as the filename and textual content.
     """
     if text:
         raise restricted.RestrictedError(self.name, text, message)
     else:
         raise restricted.RestrictedError(self.name, self.text, message)
Exemple #2
0
def parse_template(
        filename,
        path='views/',
        cache='cache/',
        context=dict(),
):
    import restricted

    # ## read the template

    try:
        text = open(os.path.join(path, filename), 'rb').read()
    except IOError:
        raise restricted.RestrictedError('Processing View ' + filename, '',
                                         'Unable to find the file')

    # check whether it extends a layout

    while 1:
        match = re_extend.search(text)
        if not match:
            break
        t = os.path.join(path, eval(match.group('name'), context))
        try:
            parent = open(t, 'rb').read()
        except IOError:
            raise restricted.RestrictedError(
                'Processing View ' + filename, text, '',
                'Unable to open parent view file: ' + t)
        (a, b) = (match.start(), match.end())
        text = text[0:a] + replace(re_include_nameless, parent,
                                   lambda x: text[b:])

    # ## check whether it includes subtemplates

    while 1:
        match = re_include.search(text)
        if not match:
            break
        t = os.path.join(path, eval(match.group('name'), context))
        try:
            child = open(t, 'rb').read()
        except IOError:
            raise restricted.RestrictedError(
                'Processing View ' + filename, text, '',
                'Unable to open included view file: ' + t)
        text = replace(re_include, text, lambda x: child, 1)

    # ## now convert to a python expression

    return parse(text)
Exemple #3
0
def parse_template(filename,
                   path    = 'views/',
                   context = dict(),
                   lexers  = {},
                   delimiters = ('{{','}}')
                   ):
    """
    filename can be a view filename in the views folder or an input stream
    path is the path of a views folder
    context is a dictionary of symbols used to render the template
    """

    # First, if we have a str try to open the file
    if isinstance(filename, str):
        try:
            fp = open(os.path.join(path, filename), 'rb')
            text = fp.read()
            fp.close()
        except IOError:
            raise restricted.RestrictedError(filename, '', 'Unable to find the file')
    else:
        text = filename.read()

    # Use the file contents to get a parsed template and return it.
    return str(TemplateParser(text, context=context, path=path, lexers=lexers, delimiters=delimiters))