/usr/lib/python2.7/dist-packages/chameleon/loader.py is in python-chameleon 2.24-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | import functools
import imp
import logging
import os
import py_compile
import shutil
import sys
import tempfile
import warnings
import pkg_resources
log = logging.getLogger('chameleon.loader')
from .utils import string_type
from .utils import encode_string
def cache(func):
def load(self, *args, **kwargs):
template = self.registry.get(args)
if template is None:
self.registry[args] = template = func(self, *args, **kwargs)
return template
return load
def abspath_from_asset_spec(spec):
pname, filename = spec.split(':', 1)
return pkg_resources.resource_filename(pname, filename)
if os.name == "nt":
def abspath_from_asset_spec(spec, f=abspath_from_asset_spec):
if spec[1] == ":":
return spec
return f(spec)
class TemplateLoader(object):
"""Template loader class.
To load templates using relative filenames, pass a sequence of
paths (or a single path) as ``search_path``.
To apply a default filename extension to inputs which do not have
an extension already (i.e. no dot), provide this as
``default_extension`` (e.g. ``'.pt'``).
Additional keyword-arguments will be passed on to the template
constructor.
"""
default_extension = None
def __init__(self, search_path=None, default_extension=None, **kwargs):
if search_path is None:
search_path = []
if isinstance(search_path, string_type):
search_path = [search_path]
if default_extension is not None:
self.default_extension = ".%s" % default_extension.lstrip('.')
self.search_path = search_path
self.registry = {}
self.kwargs = kwargs
@cache
def load(self, spec, cls=None):
if cls is None:
raise ValueError("Unbound template loader.")
spec = spec.strip()
if self.default_extension is not None and '.' not in spec:
spec += self.default_extension
if ':' in spec:
spec = abspath_from_asset_spec(spec)
if not os.path.isabs(spec):
for path in self.search_path:
path = os.path.join(path, spec)
if os.path.exists(path):
spec = path
break
else:
raise ValueError("Template not found: %s." % spec)
return cls(spec, search_path=self.search_path, **self.kwargs)
def bind(self, cls):
return functools.partial(self.load, cls=cls)
class MemoryLoader(object):
def build(self, source, filename):
code = compile(source, filename, 'exec')
env = {}
exec(code, env)
return env
def get(self, name):
return None
class ModuleLoader(object):
def __init__(self, path, remove=False):
self.path = path
self.remove = remove
def __del__(self, shutil=shutil):
if not self.remove:
return
try:
shutil.rmtree(self.path)
except:
warnings.warn("Could not clean up temporary file path: %s" % (self.path,))
def get(self, filename):
path = os.path.join(self.path, filename)
if os.path.exists(path):
log.debug("loading module from cache: %s." % filename)
base, ext = os.path.splitext(filename)
return self._load(base, path)
else:
log.debug('cache miss: %s' % filename)
def build(self, source, filename):
imp.acquire_lock()
try:
d = self.get(filename)
if d is not None:
return d
base, ext = os.path.splitext(filename)
name = os.path.join(self.path, base + ".py")
log.debug("writing source to disk (%d bytes)." % len(source))
fd, fn = tempfile.mkstemp(prefix=base, suffix='.tmp', dir=self.path)
temp = os.fdopen(fd, 'wb')
encoded = source.encode('utf-8')
header = encode_string("# -*- coding: utf-8 -*-" + "\n")
try:
try:
temp.write(header)
temp.write(encoded)
finally:
temp.close()
except:
os.remove(fn)
raise
os.rename(fn, name)
log.debug("compiling %s into byte-code..." % filename)
py_compile.compile(name)
return self._load(base, name)
finally:
imp.release_lock()
def _load(self, base, filename):
imp.acquire_lock()
try:
module = sys.modules.get(base)
if module is None:
f = open(filename, 'rb')
try:
assert base not in sys.modules
module = imp.load_source(base, filename, f)
finally:
f.close()
finally:
imp.release_lock()
return module.__dict__
|