def _create_module(code, name, filename, store=True, ns={}, exec_module=None): for recompiled in range(2): name = get_template_name(name, filename) mod = new.module(name) mod.__file__ = filename mod.__ctime__ = time.time() mod.__dict__.update(ns) try: if exec_module: exec_module(mod, code) else: exec code in mod.__dict__ except Exception: if store: sys.modules[name] = mod raise_template_error(module=name, filename=filename) if getattr(mod, 'kid_version', None) == __version__: break # the module has been compiled against an old Kid version, # recompile to ensure compatibility and best performance if recompiled: # already tried recompiling, to no avail raise TemplateImportError('Cannot recompile template file' ' %r for Kid version %s' % (filename, __version__)) template = KidFile(filename) template.stale = True template._python = template._code = None code = template.compile(dump_source=environ.get('KID_OUTPUT_PY')) if store: sys.modules[name] = mod return mod
def import_template(name, filename, force=False): if not force and name and sys.modules.has_key(name): return sys.modules[name] template = KidFile(filename) code = template.compile(dump_source=environ.get('KID_OUTPUT_PY')) module = _create_module(code, name, filename) return module
def loadClass(self, transaction, path): """Load servlet class for the given Kid template.""" classname = self.computeClassName(path) if self._cacheTemplates and self._useCache: # Cache the compiled templates separately: mtime = os.path.getmtime(path) classfile = os.path.join(self._cacheDir, classname + ".py") if not self._cacheSource: classfile += __debug__ and 'c' or 'o' if not os.path.exists(classfile) \ or os.path.getmtime(classfile) != mtime: kidFile = KidFile(path) if self._cacheSource: kidFile.dump_source(classfile) else: kidFile.dump_code(classfile) # Set the modification time of the compiled file # to be the same as the source file; # that's how we'll know if it needs to be recompiled: os.utime(classfile, (os.path.getatime(classfile), mtime)) module = self.importAsPackage(transaction, classfile) else: # Let Kid care about the caching: module = load_template(path, cache=self._cacheTemplates) # Setting __orig_file__ here is already too late, module.__orig_file__ = path # so we need to tell ImportSpy explicitely about the file: self._imp.watchFile(path) theClass = kidClass(module) theClass._orig_file = path theClass.__name__ = self.computeClassName(path) return theClass
def _create_module(code, name, filename, store=True, ns={}, exec_module=None): for recompiled in range(2): name = get_template_name(name, filename) mod = new.module(name) mod.__file__ = filename mod.__ctime__ = time.time() mod.__dict__.update(ns) try: if exec_module: exec_module(mod, code) else: exec code in mod.__dict__ except Exception: if store: sys.modules[name] = mod raise_template_error(module=name, filename=filename) if getattr(mod, "kid_version", None) == __version__: break # the module has been compiled against an old Kid version, # recompile to ensure compatibility and best performance if recompiled: # already tried recompiling, to no avail raise TemplateImportError( "Cannot recompile template file" " %r for Kid version %s" % (filename, __version__) ) template = KidFile(filename) template.stale = True template._python = template._code = None code = template.compile(dump_source=environ.get("KID_OUTPUT_PY")) if store: sys.modules[name] = mod return mod
def import_template(name, filename, force=False): if not force and name and sys.modules.has_key(name): return sys.modules[name] template = KidFile(filename) code = template.compile(dump_source=environ.get("KID_OUTPUT_PY")) module = _create_module(code, name, filename) return module
def loadClass(self, transaction, path): """Load servlet class for the given Kid template.""" classname = self.computeClassName(path) if self._cacheTemplates and self._useCache: # Cache the compiled templates separately: mtime = os.path.getmtime(path) classfile = os.path.join(self._cacheDir, classname + ".py") if not self._cacheSource: classfile += 'c' if __debug__ else 'o' if (not os.path.exists(classfile) or os.path.getmtime(classfile) != mtime): kidFile = KidFile(path) if self._cacheSource: kidFile.dump_source(classfile) else: kidFile.dump_code(classfile) # Set the modification time of the compiled file # to be the same as the source file; # that's how we'll know if it needs to be recompiled: os.utime(classfile, (os.path.getatime(classfile), mtime)) module = self.importAsPackage(transaction, classfile) else: # Let Kid care about the caching: module = load_template(path, cache=self._cacheTemplates) # Setting __orig_file__ here is already too late, module.__orig_file__ = path # so we need to tell ImportSpy explicitely about the file: self._imp.watchFile(path) theClass = kidClass(module) theClass._orig_file = path theClass.__name__ = self.computeClassName(path) return theClass