Esempio n. 1
0
    def test_compiler_str(self):
        """ Test Compiler.__str__ """
        source = """#encoding utf-8
#set $someUnicodeString = u"Bébé"
$someUnicodeString"""
        compiler = Compiler(source)
        self.assertIsInstance(str(compiler), str)
        self.assertEqual(compiler.getModuleEncoding(), 'utf-8')
Esempio n. 2
0
    def build_template(self, template, template_file, package):
        """
        Compile the cheetah template in src into a python file in build
        """

        try:
            from Cheetah.Compiler import Compiler
        except ImportError:
            self.announce("unable to import Cheetah.Compiler, build failed")
            raise
        else:
            comp = Compiler(file=template_file, moduleName=template)

        # load configuration if it exists
        conf_fn = DEFAULT_CONFIG
        if exists(conf_fn):
            with open(conf_fn, "rt") as config:
                comp.updateSettingsFromConfigFileObj(config)

        # and just why can't I configure these?
        comp.setShBang("")
        comp.addModuleHeader("pylint: disable=C,W,R,F")

        outfd = join(self.build_lib, *package.split("."))
        outfn = join(outfd, template + ".py")

        if not exists(outfd):
            makedirs(outfd)

        if newer(template_file, outfn):
            self.announce("compiling %s -> %s" % (template_file, outfd), 2)
            with open(outfn, "w") as output:
                output.write(str(comp))
Esempio n. 3
0
def __compiletemplate(template, base=None, isString=False):
    if isString: text = template
    else: text = open('templates/'+template).read()
    # implement #include at compile-time
    def do_include(match):
        text = open('templates/'+match.groups()[0]).read()
        return text
    while r_include.findall(text): text = r_include.sub(do_include, text)

    execspace = _compiletemplate.bases.copy()
    c = Compiler(source=text, mainClassName='GenTemplate')
    c.addImportedVarNames(execspace.keys())
    exec str(c) in execspace
    if base: _compiletemplate.bases[base] = execspace['GenTemplate']

    return execspace['GenTemplate']
Esempio n. 4
0
def __compiletemplate(template, base=None, isString=False):
    if isString: text = template
    else: text = open('templates/'+template).read()
    # implement #include at compile-time
    def do_include(match):
        text = open('templates/'+match.groups()[0]).read()
        return text
    while r_include.findall(text): text = r_include.sub(do_include, text)

    execspace = _compiletemplate.bases.copy()
    c = Compiler(source=text)
    c.addImportedVarNames(execspace.keys())
    exec str(c) in execspace
    if base: _compiletemplate.bases[base] = execspace['GenTemplate']

    return execspace['GenTemplate']
Esempio n. 5
0
    def _compile(self, name, tmplPath):
        if invalidate_caches:
            invalidate_caches()

        # @@ consider adding an ImportError raiser here
        code = str(Compiler(file=tmplPath, moduleName=name,
                            mainClassName=name))
        if _cacheDir:
            __file__ = os.path.join(
                _cacheDir[0], convertTmplPathToModuleName(tmplPath)) + '.py'
        else:
            __file__ = os.path.splitext(tmplPath)[0] + '.py'
        try:
            with open(__file__, 'w') as _py_file:
                _py_file.write(code)
        except (IOError, OSError):
            # @@ TR: need to add some error code here
            if self.debuglevel > 0:
                traceback.print_exc(file=sys.stderr)
            __file__ = tmplPath
        else:
            try:
                py_compile.compile(__file__)
            except IOError:
                pass
        co = compile(code + '\n', __file__, 'exec')

        mod = types.ModuleType(name)
        mod.__file__ = co.co_filename
        if _cacheDir:
            # @@TR: this is used in the WebKit filemonitoring code
            mod.__orig_file__ = tmplPath
        mod.__co__ = co
        return mod
Esempio n. 6
0
 def compileOrFillStdin(self):
         if self.isCompile:
             output = Compiler(file=sys.stdin)
         else:
             output = Template(file=sys.stdin)
         output = str(output)
         sys.stdout.write(output)
    def build_template(self, template, template_file, package):
        """
        Compile the cheetah template in src into a python file in build
        """

        comp = Compiler(file=template_file, moduleName=template)

        # load configuration if it exists
        conf_fn = DEFAULT_CONFIG
        if exists(conf_fn):
            with open(conf_fn, "rt") as config:
                comp.updateSettingsFromConfigFileObj(config)

        # and just why can't I configure these?
        comp.setShBang("")
        comp.addModuleHeader("pylint: disable=C,W,R,F")

        outfd = join(self.build_lib, *package.split("."))
        outfn = join(outfd, template+".py")

        if not exists(outfd):
            makedirs(outfd)

        if newer(template_file, outfn):
            self.announce("compiling %s -> %s" % (template_file, outfd), 2)
            with open(outfn, "w") as output:
                output.write(str(comp))
Esempio n. 8
0
def parse(cheetah_content, encoding=None):

	from Cheetah.Compiler import Compiler
	# This is very screwy, but so is cheetah. Apologies.
	compiler = Compiler()
	compiler._parser = InstrumentedParser(cheetah_content, compiler=compiler)
	compiler.compile()
	data = compiler._parser.data

	if DEBUG:
		data = show_data(data, cheetah_content)
	data = nice_names(data)
	data = remove_empty(data)
	data = dedup(data)

	dictnode = parser_data_to_dictnode(data, cheetah_content)

	from refactorlib.parse import dictnode_to_lxml
	from refactorlib.cheetah.node import node_lookup
	root = dictnode_to_lxml(dictnode, node_lookup, encoding)
	return root
Esempio n. 9
0
def parse(cheetah_content, encoding=None):

    from Cheetah.Compiler import Compiler
    # This is very screwy, but so is cheetah. Apologies.
    compiler = Compiler()
    compiler._parser = InstrumentedParser(cheetah_content, compiler=compiler)
    compiler.compile()
    data = compiler._parser.data

    if DEBUG:
        data = show_data(data, cheetah_content)
    data = nice_names(data)
    data = remove_empty(data)
    data = dedup(data)

    dictnode = parser_data_to_dictnode(data, cheetah_content)

    from refactorlib.parse import dictnode_to_lxml
    from refactorlib.cheetah.node import node_lookup
    root = dictnode_to_lxml(dictnode, node_lookup, encoding)
    return root
Esempio n. 10
0
def compileTemplate(tpl, cplFileName, moduleName):
    """Compile one user template to python bytecode
		Input: (string) template file name;
			(string) compiled (target) file name
		Output: none
	"""
    if not hasTemplating:
        return

    debug("Compiling template %s to file: %s" % (moduleName, cplFileName))
    try:
        debug('  Generating code')
        cpl = Compiler(source=tpl, moduleName=moduleName)
        cpl.compile()
    except:
        printException()
        error('Error compiling template: %s' % moduleName)
        return
    # write compiled template to file
    try:
        debug('  Writing to file')
        fh = open(cplFileName, 'w')
        fh.write(str(cpl))
        fh.close()
    except:
        printException()
        error('Can not write compiled template to file: %s' % cplFileName)
        return
    # compile generated template to python bytecode
    try:
        debug('  Compiling to Python bytecode')
        compiler.compileFile(cplFileName)
    except:
        printException()
        error(
            'Can not compile generated template to python bytecode. File: %s' %
            cplFileName)
        return
Esempio n. 11
0
def compileTemplate(tpl, cplFileName, moduleName):
	"""Compile one user template to python bytecode
		Input: (string) template file name;
			(string) compiled (target) file name
		Output: none
	"""
	if not hasTemplating:
		return
	
	debug ("Compiling template %s to file: %s" % (moduleName, cplFileName))
	try:
		debug ('  Generating code')
		cpl = Compiler(source = tpl, moduleName = moduleName)
		cpl.compile()
	except:
		printException()
		error('Error compiling template: %s' % moduleName)
		return
	# write compiled template to file
	try:
		debug ('  Writing to file')
		fh = open(cplFileName, 'w')
		fh.write(str(cpl))
		fh.close()
	except:
		printException()
		error('Can not write compiled template to file: %s' % cplFileName)
		return
	# compile generated template to python bytecode
	try:
		debug ('  Compiling to Python bytecode')
		compiler.compileFile(cplFileName)
	except:
		printException()
		error('Can not compile generated template to python bytecode. File: %s' % cplFileName)
		return
Esempio n. 12
0
def cheetahGenerate(filename):
    # check permissions
    if not os.path.exists(filename):
        raise spyceException.spyceNotFound()
    if not os.access(filename, os.R_OK):
        raise spyceException.spyceForbidden()
    # read the template
    f = None
    try:
        f = open(filename, 'r')
        buf = f.read()
    finally:
        if f: f.close()
    # compile template, get timestamp
    mtime = os.path.getmtime(filename)
    from Cheetah.Compiler import Compiler
    code = Compiler(source=buf).__str__()
    dict = {}
    exec code in dict
    return mtime, dict['GenTemplate']
Esempio n. 13
0
    def getmod(
        self,
        name,
        pathIsDir=os.path.isdir,
        join=os.path.join,
        newmod=imp.new_module,
        convertTmplPath=convertTmplPath,
    ):

        tmplPath = os.path.join(self.path, name + '.tmpl')
        mod = DirOwner.getmod(self, name)
        if mod:
            return mod
        elif not os.path.exists(tmplPath):
            return None
        else:
            self._acquireLock()
            ## @@ consider adding an ImportError raiser here
            code = str(
                Compiler(file=tmplPath, moduleName=name, mainClassName=name))
            if _cacheDir:
                __file__ = join(_cacheDir[0],
                                convertTmplPath(tmplPath)) + '.py'
                try:
                    open(__file__, 'w').write(code)
                except OSError:
                    ## @@ TR: need to add some error code here
                    traceback.print_exc(file=sys.stderr)
                    __file__ = tmplPath
            else:
                __file__ = tmplPath
            co = compile(code + '\n', __file__, 'exec')

            mod = newmod(name)
            mod.__file__ = co.co_filename
            if _cacheDir:
                mod.__orig_file__ = tmplPath  # @@TR: this is used in the WebKit
                # filemonitoring code
            mod.__co__ = co
            self._releaseLock()
            return mod
Esempio n. 14
0
    def compileOrFillBundle(self, b):
        C, D, W = self.chatter, self.debug, self.warn
        src = b.src
        dst = b.dst
        base = b.base
        basename = b.basename
        dstDir = os.path.dirname(dst)
        what = self.isCompile and "Compiling" or "Filling"
        C("%s %s -> %s^", what, src, dst) # No trailing newline.
        if os.path.exists(dst) and not self.opts.nobackup:
            bak = b.bak
            C(" (backup %s)", bak) # On same line as previous message.
        else:
            bak = None
            C("")
        if self.isCompile:
            if not moduleNameRE.match(basename):
                tup = basename, src
                raise Error("""\
%s: base name %s contains invalid characters.  It must
be named according to the same rules as Python modules.""" % tup)
            obj = Compiler(file=src, \
                moduleName=basename, mainClassName=basename)
        else:
            obj = Template(file=src, searchList=self.searchList)
        output = str(obj)
        if bak:
            shutil.copyfile(dst, bak)
        if dstDir and not os.path.exists(dstDir):
            if self.isCompile:
                mkdirsWithPyInitFiles(dstDir)
            else:
                os.makedirs(dstDir)
        if self.opts.stdout:
            sys.stdout.write(output)
        else:
            f = open(dst, 'w')
            f.write(output)
            f.close()
Esempio n. 15
0
    def _compile(self, name, tmplPath):
        ## @@ consider adding an ImportError raiser here
        code = str(Compiler(file=tmplPath, moduleName=name,
                            mainClassName=name))
        if _cacheDir:
            __file__ = os.path.join(
                _cacheDir[0], convertTmplPathToModuleName(tmplPath)) + '.py'
            try:
                open(__file__, 'w').write(code)
            except OSError:
                ## @@ TR: need to add some error code here
                traceback.print_exc(file=sys.stderr)
                __file__ = tmplPath
        else:
            __file__ = tmplPath
        co = compile(code + '\n', __file__, 'exec')

        mod = imp.new_module(name)
        mod.__file__ = co.co_filename
        if _cacheDir:
            mod.__orig_file__ = tmplPath  # @@TR: this is used in the WebKit
            # filemonitoring code
        mod.__co__ = co
        return mod
Esempio n. 16
0
    def precompileTemplates(self):
        """Looks for Cheetah templates and precompile them (into Python code)
		if necessary"""
        # Iterates on the site templates
        for template in self.site.templates():
            filename = os.path.basename(os.path.splitext(template)[0])
            # Templates are only compiled if they were not previouly compiled or
            # if the changed.
            if self.hasChanged(template) or \
            not os.path.exists(os.path.splitext(template)[0]+".py"):
                log("Precompiling template '%s'" %
                    (shorten_path(os.path.splitext(template)[0])))
                temp = Compiler(file=template,
                                moduleName=filename,
                                mainClassName=filename)
                try:
                    temp = str(temp)
                except Cheetah.Parser.ParseError, e:
                    fatal(e)
                    temp = None
                if temp != None:
                    output = open(os.path.splitext(template)[0] + ".py", "wb")
                    output.write("# Encoding: ISO-8859-1\n" + str(temp))
                    output.close()