loaders.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. """Loaders are helper classes which will read environments and/or
  2. bundles from a source, like a configuration file.
  3. This can be used as an alternative to an imperative setup.
  4. """
  5. import os, sys
  6. from os import path
  7. import glob, fnmatch
  8. import inspect
  9. import types
  10. from webassets import six
  11. try:
  12. import yaml
  13. except ImportError:
  14. pass
  15. from webassets import six
  16. from webassets import Environment
  17. from webassets.bundle import Bundle
  18. from webassets.exceptions import EnvironmentError
  19. from webassets.filter import register_filter
  20. from webassets.importlib import import_module
  21. __all__ = ('Loader', 'LoaderError', 'PythonLoader', 'YAMLLoader',
  22. 'GlobLoader',)
  23. class LoaderError(Exception):
  24. """Loaders should raise this when they can't deal with a given file.
  25. """
  26. class YAMLLoader(object):
  27. """Will load an environment or a set of bundles from
  28. `YAML <http://en.wikipedia.org/wiki/YAML>`_ files.
  29. """
  30. def __init__(self, file_or_filename):
  31. try:
  32. yaml
  33. except NameError:
  34. raise EnvironmentError('PyYAML is not installed')
  35. else:
  36. self.yaml = yaml
  37. self.file_or_filename = file_or_filename
  38. def _yield_bundle_contents(self, data):
  39. """Yield bundle contents from the given dict.
  40. Each item yielded will be either a string representing a file path
  41. or a bundle."""
  42. contents = data.get('contents', [])
  43. if isinstance(contents, six.string_types):
  44. contents = contents,
  45. for content in contents:
  46. if isinstance(content, dict):
  47. content = self._get_bundle(content)
  48. yield content
  49. def _get_bundle(self, data):
  50. """Return a bundle initialised by the given dict."""
  51. kwargs = dict(
  52. filters=data.get('filters', None),
  53. output=data.get('output', None),
  54. debug=data.get('debug', None),
  55. extra=data.get('extra', {}),
  56. config=data.get('config', {}),
  57. depends=data.get('depends', None))
  58. return Bundle(*list(self._yield_bundle_contents(data)), **kwargs)
  59. def _get_bundles(self, obj, known_bundles=None):
  60. """Return a dict that keys bundle names to bundles."""
  61. bundles = {}
  62. for key, data in six.iteritems(obj):
  63. if data is None:
  64. data = {}
  65. bundles[key] = self._get_bundle(data)
  66. # now we need to recurse through the bundles and get any that
  67. # are included in each other.
  68. for bundle_name, bundle in bundles.items():
  69. # copy contents
  70. contents = list(bundle.contents)
  71. for i, item in enumerate(bundle.contents):
  72. if item in bundles:
  73. contents[i] = bundles[item]
  74. elif known_bundles and item in known_bundles:
  75. contents[i] = known_bundles[item]
  76. # cast back to a tuple
  77. contents = tuple(contents)
  78. if contents != bundle.contents:
  79. bundle.contents = contents
  80. return bundles
  81. def _open(self):
  82. """Returns a (fileobj, filename) tuple.
  83. The filename can be False if it is unknown.
  84. """
  85. if isinstance(self.file_or_filename, six.string_types):
  86. return open(self.file_or_filename), self.file_or_filename
  87. file = self.file_or_filename
  88. return file, getattr(file, 'name', False)
  89. @classmethod
  90. def _get_import_resolver(cls):
  91. """ method that can be overridden in tests """
  92. from zope.dottedname.resolve import resolve as resolve_dotted
  93. return resolve_dotted
  94. def load_bundles(self, environment=None):
  95. """Load a list of :class:`Bundle` instances defined in the YAML file.
  96. Expects the following format:
  97. .. code-block:: yaml
  98. bundle-name:
  99. filters: sass,cssutils
  100. output: cache/default.css
  101. contents:
  102. - css/jquery.ui.calendar.css
  103. - css/jquery.ui.slider.css
  104. another-bundle:
  105. # ...
  106. Bundles may reference each other:
  107. .. code-block:: yaml
  108. js-all:
  109. contents:
  110. - jquery.js
  111. - jquery-ui # This is a bundle reference
  112. jquery-ui:
  113. contents: jqueryui/*.js
  114. If an ``environment`` argument is given, it's bundles
  115. may be referenced as well. Note that you may pass any
  116. compatibly dict-like object.
  117. Finally, you may also use nesting:
  118. .. code-block:: yaml
  119. js-all:
  120. contents:
  121. - jquery.js
  122. # This is a nested bundle
  123. - contents: "*.coffee"
  124. filters: coffeescript
  125. """
  126. # TODO: Support a "consider paths relative to YAML location, return
  127. # as absolute paths" option?
  128. f, _ = self._open()
  129. try:
  130. obj = self.yaml.load(f) or {}
  131. return self._get_bundles(obj, environment)
  132. finally:
  133. f.close()
  134. def load_environment(self):
  135. """Load an :class:`Environment` instance defined in the YAML file.
  136. Expects the following format:
  137. .. code-block:: yaml
  138. directory: ../static
  139. url: /media
  140. debug: True
  141. updater: timestamp
  142. filters:
  143. - my_custom_package.my_filter
  144. config:
  145. compass_bin: /opt/compass
  146. another_custom_config_value: foo
  147. bundles:
  148. # ...
  149. All values, including ``directory`` and ``url`` are optional. The
  150. syntax for defining bundles is the same as for
  151. :meth:`~.YAMLLoader.load_bundles`.
  152. Sample usage::
  153. from webassets.loaders import YAMLLoader
  154. loader = YAMLLoader('asset.yml')
  155. env = loader.load_environment()
  156. env['some-bundle'].urls()
  157. """
  158. f, filename = self._open()
  159. try:
  160. obj = self.yaml.load(f) or {}
  161. env = Environment()
  162. # Load environment settings
  163. for setting in ('debug', 'cache', 'versions', 'url_expire',
  164. 'auto_build', 'url', 'directory', 'manifest', 'load_path',
  165. 'cache_file_mode',
  166. # TODO: The deprecated values; remove at some point
  167. 'expire', 'updater'):
  168. if setting in obj:
  169. setattr(env, setting, obj[setting])
  170. # Treat the 'directory' option special, make it relative to the
  171. # path of the YAML file, if we know it.
  172. if filename and 'directory' in env.config:
  173. env.directory = path.normpath(
  174. path.join(path.dirname(filename),
  175. env.config['directory']))
  176. # Treat the 'filters' option special, it should resolve the
  177. # entries as classes and register them to the environment
  178. if 'filters' in obj:
  179. try:
  180. resolve_dotted = self._get_import_resolver()
  181. except ImportError:
  182. raise EnvironmentError(
  183. "In order to use custom filters in the YAMLLoader "
  184. "you must install the zope.dottedname package")
  185. for filter_class in obj['filters']:
  186. try:
  187. cls = resolve_dotted(filter_class)
  188. except ImportError:
  189. raise LoaderError("Unable to resolve class %s" % filter_class)
  190. if inspect.isclass(cls):
  191. register_filter(cls)
  192. else:
  193. raise LoaderError("Custom filters must be classes "
  194. "not modules or functions")
  195. # Load custom config options
  196. if 'config' in obj:
  197. env.config.update(obj['config'])
  198. # Load bundles
  199. bundles = self._get_bundles(obj.get('bundles', {}))
  200. for name, bundle in six.iteritems(bundles):
  201. env.register(name, bundle)
  202. return env
  203. finally:
  204. f.close()
  205. class PythonLoader(object):
  206. """Basically just a simple helper to import a Python file and
  207. retrieve the bundles defined there.
  208. """
  209. environment = "environment"
  210. def __init__(self, module_name):
  211. if isinstance(module_name, types.ModuleType):
  212. self.module = module_name
  213. else:
  214. sys.path.insert(0, '') # Ensure the current directory is on the path
  215. try:
  216. try:
  217. if ":" in module_name:
  218. module_name, env = module_name.split(":")
  219. self.environment = env
  220. self.module = import_module(module_name)
  221. except ImportError as e:
  222. raise LoaderError(e)
  223. finally:
  224. sys.path.pop(0)
  225. def load_bundles(self):
  226. """Load ``Bundle`` objects defined in the Python module.
  227. Collects all bundles in the global namespace.
  228. """
  229. bundles = {}
  230. for name in dir(self.module):
  231. value = getattr(self.module, name)
  232. if isinstance(value, Bundle):
  233. bundles[name] = value
  234. return bundles
  235. def load_environment(self):
  236. """Load an ``Environment`` defined in the Python module.
  237. Expects as default a global name ``environment`` to be defined,
  238. or overriden by passing a string ``module:environent`` to the
  239. constructor.
  240. """
  241. try:
  242. return getattr(self.module, self.environment)
  243. except AttributeError as e:
  244. raise LoaderError(e)
  245. def recursive_glob(treeroot, pattern):
  246. """
  247. From:
  248. http://stackoverflow.com/questions/2186525/2186639#2186639
  249. """
  250. results = []
  251. for base, dirs, files in os.walk(treeroot):
  252. goodfiles = fnmatch.filter(files, pattern)
  253. results.extend(os.path.join(base, f) for f in goodfiles)
  254. return results
  255. class GlobLoader(object):
  256. """Base class with some helpers for loaders which need to search
  257. for files.
  258. """
  259. def glob_files(self, f, recursive=False):
  260. if isinstance(f, tuple):
  261. return iter(recursive_glob(f[0], f[1]))
  262. else:
  263. return iter(glob.glob(f))
  264. def with_file(self, filename, then_run):
  265. """Call ``then_run`` with the file contents.
  266. """
  267. file = open(filename, 'rb')
  268. try:
  269. contents = file.read()
  270. try:
  271. return then_run(filename, contents)
  272. except LoaderError:
  273. # We can't handle this file.
  274. pass
  275. finally:
  276. file.close()