Skip to content

Commit

Permalink
Add globing support to the require directive
Browse files Browse the repository at this point in the history
  • Loading branch information
yumike committed Apr 22, 2014
1 parent 63c0862 commit 16bbe6c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 21 deletions.
2 changes: 1 addition & 1 deletion gears/directives_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class DirectivesParser(object):
""", re.S | re.X)

directive_re = re.compile(r"""
^ \s* (?:\*|//|\#) \s* = \s* ( \w+ [=./'"\s\w-]* ) $
^ \s* (?:\*|//|\#) \s* = \s* ( \w+ [=*?\[\]./'"\s\w-]* ) $
""", re.X)

def split_source(self, source):
Expand Down
41 changes: 31 additions & 10 deletions gears/environment.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import gzip
import os
from pkg_resources import iter_entry_points
from glob2.fnmatch import fnmatch

from .asset_attributes import AssetAttributes
from .assets import build_asset
Expand All @@ -13,7 +14,7 @@
HexdigestPathsProcessor,
SemicolonsProcessor
)
from .utils import get_condition_func
from .utils import get_condition_func, unique


DEFAULT_PUBLIC_ASSETS = (
Expand Down Expand Up @@ -43,6 +44,11 @@ def unregister(self, finder):
if finder in self:
self.remove(finder)

def list(self, path):
for finder in self:
for item in finder.list(path):
yield item


class MIMETypes(dict):
"""The registry for MIME types. It acts like a dict with extensions as
Expand Down Expand Up @@ -390,15 +396,30 @@ def list(self, path, mimetype=None):
# 'application/javascript' MIME type of compiled source code.
environment.list('js/templates/*', mimetype='application/javascript')
"""
found = set()
for finder in self.finders:
for logical_path, absolute_path in finder.list(path):
asset_attributes = AssetAttributes(self, logical_path)
if mimetype is not None and asset_attributes.mimetype != mimetype:
continue
if logical_path not in found:
yield asset_attributes, absolute_path
found.add(logical_path)
basename_pattern = os.path.basename(path)

if path.endswith('**'):
paths = [path]
else:
paths = AssetAttributes(self, path).search_paths
paths = map(lambda p: p if p.endswith('*') else p + '*', paths)

results = unique(self._list_paths(paths), lambda x: x[0])
for logical_path, absolute_path in results:
asset_attributes = AssetAttributes(self, logical_path)
if mimetype is not None and asset_attributes.mimetype != mimetype:
continue

basename = os.path.basename(asset_attributes.path_without_suffix)
if not fnmatch(basename, basename_pattern) and basename != 'index':
continue

yield asset_attributes, absolute_path

def _list_paths(self, paths):
for path in paths:
for result in self.finders.list(path):
yield result

def save(self):
"""Save handled public assets to :attr:`root` directory."""
Expand Down
16 changes: 6 additions & 10 deletions gears/processors/directives.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,17 @@ def process_params_directive(self, *params):
self.asset.params[key] = value

def process_require_directive(self, path):
self.asset.requirements.add(self.get_asset(*self.find(path)))

def process_require_directory_directive(self, path, recursive=False):
path = self.get_relative_path(path, is_directory=True)
path = os.path.join(path, '**' if recursive else '*')
path = self.get_relative_path(path)
list = self.asset.attributes.environment.list(path, self.asset.attributes.mimetype)
for asset_attributes, absolute_path in sorted(list, key=lambda x: x[0].path.split('/')):
self.asset.requirements.add(self.get_asset(asset_attributes, absolute_path))
self.asset.dependencies.add(os.path.dirname(absolute_path))

def process_require_directory_directive(self, path):
self.process_require_directive(os.path.join(path, '*'))

def process_require_tree_directive(self, path):
self.process_require_directory_directive(path, recursive=True)
self.process_require_directive(os.path.join(path, '**'))

def process_require_self_directive(self):
self.asset.requirements.add(self.asset)
Expand All @@ -75,10 +74,7 @@ def find(self, require_path):

def get_relative_path(self, require_path, is_directory=False):
require_path = os.path.join(self.asset.attributes.dirname, require_path)
require_path = os.path.normpath(require_path)
if is_directory:
return require_path
return require_path + ''.join(self.asset.attributes.extensions)
return os.path.normpath(require_path)

def get_asset(self, asset_attributes, absolute_path):
return Asset(asset_attributes, absolute_path, self.asset.calls)

0 comments on commit 16bbe6c

Please sign in to comment.