Skip to content

Commit

Permalink
Another try to implement public directive
Browse files Browse the repository at this point in the history
  • Loading branch information
yumike committed Apr 24, 2014
1 parent bcb8230 commit 0f3e8d5
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 5 deletions.
12 changes: 12 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
Changelog
=========

Next Release
------------

- Add ``public`` directive to mark the asset as public:

.. code-block:: javascript
//= public
It can be used as an alternative to :class:`~gears.environment.Environment`'s
``public_assets`` param.

0.7 (2014-04-23)
----------------

Expand Down
7 changes: 5 additions & 2 deletions gears/asset_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class BaseAssetHandler(object):
with asset as argument.
"""

supports_check_mode = False

def __call__(self, asset):
"""Subclasses have to override this method to implement the actual
handler function code. This method is called with asset as argument.
Expand All @@ -33,9 +35,10 @@ def as_handler(cls, **initkwargs):
constructor of the class.
"""
@wraps(cls, updated=())
def handler(asset):
return handler.handler_class(**initkwargs)(asset)
def handler(asset, *args, **kwargs):
return handler.handler_class(**initkwargs)(asset, *args, **kwargs)
handler.handler_class = cls
handler.supports_check_mode = cls.supports_check_mode
return handler


Expand Down
32 changes: 31 additions & 1 deletion gears/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,16 @@ def __init__(self, attributes, absolute_path, calls=None):
def __repr__(self):
return '<%s absolute_path=%s>' % (self.__class__.__name__, self.absolute_path)

@cached_property
def is_public(self):
environment = self.attributes.environment
logical_path = os.path.normpath(self.attributes.logical_path)
return environment.is_public(logical_path) or self.params.get('public')

@cached_property
def params(self):
return {}

@cached_property
def hexdigest_path(self):
return EXTENSION_RE.sub(
Expand Down Expand Up @@ -333,12 +343,32 @@ def __iter__(self):
return iter(self.source)


def build_asset(environment, path):
class CheckAsset(BaseAsset):

def __init__(self, *args, **kwargs):
super(CheckAsset, self).__init__(*args, **kwargs)
self.processed_source = self.source
for process in self.attributes.processors:
if getattr(process, 'supports_check_mode', False):
process(self, check=True)

@cached_property
def source(self):
try:
with codecs.open(self.absolute_path, encoding='utf-8') as f:
return f.read()
except UnicodeDecodeError as e:
raise GearsUnicodeError(self.absolute_path, str(e))


def build_asset(environment, path, check=False):
path = strip_fingerprint(path)
asset_attributes = AssetAttributes(environment, path)
asset_attributes, absolute_path = environment.find(asset_attributes, True)
if not asset_attributes.processors:
return StaticAsset(asset_attributes, absolute_path)
if check:
return CheckAsset(asset_attributes, absolute_path)
return Asset(asset_attributes, absolute_path)


Expand Down
3 changes: 2 additions & 1 deletion gears/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,8 @@ def save(self):
"""Save handled public assets to :attr:`root` directory."""
for asset_attributes, absolute_path in self.list('**'):
logical_path = os.path.normpath(asset_attributes.logical_path)
if self.is_public(logical_path):
check_asset = build_asset(self, logical_path, check=True)
if check_asset.is_public:
asset = build_asset(self, logical_path)
source = bytes(asset)
self.save_file(logical_path, source, asset.gzippable)
Expand Down
21 changes: 20 additions & 1 deletion gears/processors/directives.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@

class DirectivesProcessor(BaseProcessor):

supports_check_mode = True

def __init__(self):
self.types = {
'public': self.process_public_directive,
'params': self.process_params_directive,
'require': self.process_require_directive,
'require_directory': self.process_require_directory_directive,
Expand All @@ -21,8 +24,9 @@ def __init__(self):
'depend_on': self.process_depend_on_directive,
}

def __call__(self, asset):
def __call__(self, asset, check=False):
self.asset = asset
self.check = check
self.parse()
self.process_directives()

Expand All @@ -39,13 +43,19 @@ def process_directives(self):
args = shlex.split(directive)
self.types[args[0]](*args[1:])

def process_public_directive(self):
self.asset.params['public'] = True

def process_params_directive(self, *params):
for param in params:
if '=' in param:
key, value = param.split('=', 1)
self.asset.params[key] = value

def process_require_directive(self, path):
if self.check:
return

found = False
path = self.get_relative_path(path)
list = self.asset.attributes.environment.list(path, self.asset.attributes.mimetype)
Expand All @@ -57,15 +67,24 @@ def process_require_directive(self, path):
raise FileNotFound(path)

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

def process_require_tree_directive(self, path):
if self.check:
return
self.process_require_directive(os.path.join(path, '**'))

def process_require_self_directive(self):
if self.check:
return
self.asset.requirements.add(self.asset)

def process_depend_on_directive(self, path):
if self.check:
return

found = False
path = self.get_relative_path(path)
list = self.asset.attributes.environment.list(path, self.asset.attributes.mimetype)
Expand Down

0 comments on commit 0f3e8d5

Please sign in to comment.