/usr/lib/python2.7/dist-packages/webassets/loaders.py is in python-webassets 3:0.10.1-2.
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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | """Loaders are helper classes which will read environments and/or
bundles from a source, like a configuration file.
This can be used as an alternative to an imperative setup.
"""
import os, sys
from os import path
import glob, fnmatch
import types
from webassets import six
try:
import yaml
except ImportError:
pass
from webassets import six
from webassets import Environment
from webassets.bundle import Bundle
from webassets.importlib import import_module
__all__ = ('Loader', 'LoaderError', 'PythonLoader', 'YAMLLoader',
'GlobLoader',)
class LoaderError(Exception):
"""Loaders should raise this when they can't deal with a given file.
"""
class YAMLLoader(object):
"""Will load an environment or a set of bundles from
`YAML <http://en.wikipedia.org/wiki/YAML>`_ files.
"""
def __init__(self, file_or_filename):
try:
yaml
except NameError:
raise EnvironmentError('PyYAML is not installed')
else:
self.yaml = yaml
self.file_or_filename = file_or_filename
def _yield_bundle_contents(self, data):
"""Yield bundle contents from the given dict.
Each item yielded will be either a string representing a file path
or a bundle."""
contents = data.get('contents', [])
if isinstance(contents, six.string_types):
contents = contents,
for content in contents:
if isinstance(content, dict):
content = self._get_bundle(content)
yield content
def _get_bundle(self, data):
"""Return a bundle initialised by the given dict."""
kwargs = dict(
filters=data.get('filters', None),
output=data.get('output', None),
debug=data.get('debug', None),
extra=data.get('extra', {}),
config=data.get('config', {}),
depends=data.get('depends', None))
return Bundle(*list(self._yield_bundle_contents(data)), **kwargs)
def _get_bundles(self, obj, known_bundles=None):
"""Return a dict that keys bundle names to bundles."""
bundles = {}
for key, data in six.iteritems(obj):
if data is None:
data = {}
bundles[key] = self._get_bundle(data)
# now we need to recurse through the bundles and get any that
# are included in each other.
for bundle_name, bundle in bundles.items():
# copy contents
contents = list(bundle.contents)
for i, item in enumerate(bundle.contents):
if item in bundles:
contents[i] = bundles[item]
elif known_bundles and item in known_bundles:
contents[i] = known_bundles[item]
# cast back to a tuple
contents = tuple(contents)
if contents != bundle.contents:
bundle.contents = contents
return bundles
def _open(self):
"""Returns a (fileobj, filename) tuple.
The filename can be False if it is unknown.
"""
if isinstance(self.file_or_filename, six.string_types):
return open(self.file_or_filename), self.file_or_filename
file = self.file_or_filename
return file, getattr(file, 'name', False)
def load_bundles(self, environment=None):
"""Load a list of :class:`Bundle` instances defined in the YAML file.
Expects the following format:
.. code-block:: yaml
bundle-name:
filters: sass,cssutils
output: cache/default.css
contents:
- css/jquery.ui.calendar.css
- css/jquery.ui.slider.css
another-bundle:
# ...
Bundles may reference each other:
.. code-block:: yaml
js-all:
contents:
- jquery.js
- jquery-ui # This is a bundle reference
jquery-ui:
contents: jqueryui/*.js
If an ``environment`` argument is given, it's bundles
may be referenced as well. Note that you may pass any
compatibly dict-like object.
Finally, you may also use nesting:
.. code-block:: yaml
js-all:
contents:
- jquery.js
# This is a nested bundle
- contents: "*.coffee"
filters: coffeescript
"""
# TODO: Support a "consider paths relative to YAML location, return
# as absolute paths" option?
f, _ = self._open()
try:
obj = self.yaml.load(f) or {}
return self._get_bundles(obj, environment)
finally:
f.close()
def load_environment(self):
"""Load an :class:`Environment` instance defined in the YAML file.
Expects the following format:
.. code-block:: yaml
directory: ../static
url: /media
debug: True
updater: timestamp
config:
compass_bin: /opt/compass
another_custom_config_value: foo
bundles:
# ...
All values, including ``directory`` and ``url`` are optional. The
syntax for defining bundles is the same as for
:meth:`~.YAMLLoader.load_bundles`.
Sample usage::
from webassets.loaders import YAMLLoader
loader = YAMLLoader('asset.yml')
env = loader.load_environment()
env['some-bundle'].urls()
"""
f, filename = self._open()
try:
obj = self.yaml.load(f) or {}
env = Environment()
# Load environment settings
for setting in ('debug', 'cache', 'versions', 'url_expire',
'auto_build', 'url', 'directory', 'manifest', 'load_path',
# TODO: The deprecated values; remove at some point
'expire', 'updater'):
if setting in obj:
setattr(env, setting, obj[setting])
# Treat the 'directory' option special, make it relative to the
# path of the YAML file, if we know it.
if filename and 'directory' in env.config:
env.directory = path.normpath(
path.join(path.dirname(filename),
env.config['directory']))
# Load custom config options
if 'config' in obj:
env.config.update(obj['config'])
# Load bundles
bundles = self._get_bundles(obj.get('bundles', {}))
for name, bundle in six.iteritems(bundles):
env.register(name, bundle)
return env
finally:
f.close()
class PythonLoader(object):
"""Basically just a simple helper to import a Python file and
retrieve the bundles defined there.
"""
environment = "environment"
def __init__(self, module_name):
if isinstance(module_name, types.ModuleType):
self.module = module_name
else:
sys.path.insert(0, '') # Ensure the current directory is on the path
try:
try:
if ":" in module_name:
module_name, env = module_name.split(":")
self.environment = env
self.module = import_module(module_name)
except ImportError as e:
raise LoaderError(e)
finally:
sys.path.pop(0)
def load_bundles(self):
"""Load ``Bundle`` objects defined in the Python module.
Collects all bundles in the global namespace.
"""
bundles = {}
for name in dir(self.module):
value = getattr(self.module, name)
if isinstance(value, Bundle):
bundles[name] = value
return bundles
def load_environment(self):
"""Load an ``Environment`` defined in the Python module.
Expects as default a global name ``environment`` to be defined,
or overriden by passing a string ``module:environent`` to the
constructor.
"""
try:
return getattr(self.module, self.environment)
except AttributeError as e:
raise LoaderError(e)
def recursive_glob(treeroot, pattern):
"""
From:
http://stackoverflow.com/questions/2186525/2186639#2186639
"""
results = []
for base, dirs, files in os.walk(treeroot):
goodfiles = fnmatch.filter(files, pattern)
results.extend(os.path.join(base, f) for f in goodfiles)
return results
class GlobLoader(object):
"""Base class with some helpers for loaders which need to search
for files.
"""
def glob_files(self, f, recursive=False):
if isinstance(f, tuple):
return iter(recursive_glob(f[0], f[1]))
else:
return iter(glob.glob(f))
def with_file(self, filename, then_run):
"""Call ``then_run`` with the file contents.
"""
file = open(filename, 'rb')
try:
contents = file.read()
try:
return then_run(filename, contents)
except LoaderError:
# We can't handle this file.
pass
finally:
file.close()
|