Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
821 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import os.path | ||
import sys | ||
import ConfigParser | ||
|
||
from fanstatic import Library, Resource | ||
|
||
from ckan.include.rjsmin import jsmin | ||
from ckan.include.rcssmin import cssmin | ||
|
||
# TODO | ||
# loop through dirs to setup | ||
# warn on no entry point provided for fanstatic | ||
|
||
def setup(name, path): | ||
|
||
def min_path(path): | ||
''' return the .min filename eg moo.js -> moo.min.js ''' | ||
if f.endswith('.js'): | ||
return path[:-3] + '.min.js' | ||
if f.endswith('.css'): | ||
return path[:-4] + '.min.css' | ||
|
||
def minify(filename, min_function): | ||
''' Minify file path using min_function. ''' | ||
# if the minified file was modified after the source file we can | ||
# assume that it is up-to-date | ||
path = os.path.join(resource_path, filename) | ||
path_min = min_path(path) | ||
op = os.path | ||
if op.exists(path_min) and op.getmtime(path) < op.getmtime(path_min): | ||
return | ||
source = open(path, 'r').read() | ||
f = open(path_min, 'w') | ||
f.write(min_function(source)) | ||
f.close() | ||
print 'minified %s' % path | ||
|
||
def create_resource(filename): | ||
''' create the fanstatic Resource ''' | ||
# resource_name is name of the file without the .js/.css | ||
resource_name = '.'.join(filename.split('.')[:-1]) | ||
kw = {} | ||
path_min = min_path(os.path.join(resource_path, filename)) | ||
if os.path.exists(path_min): | ||
kw['minified'] = min_path(filename) | ||
if filename.endswith('.js'): | ||
kw['bottom'] = True | ||
if resource_name in depends: | ||
dependencies = [] | ||
for dependency in depends[resource_name]: | ||
dependencies.append(getattr(module, dependency)) | ||
kw['depends'] = dependencies | ||
if resource_name in dont_bundle: | ||
kw['dont_bundle'] = True | ||
|
||
resource = Resource(library, filename, **kw) | ||
# add the resource to this module | ||
setattr(module, resource_name, resource) | ||
|
||
order = [] | ||
dont_bundle = [] | ||
depends = {} | ||
|
||
# parse the config.ini file if it exists | ||
resource_path = os.path.dirname(__file__) | ||
resource_path = os.path.join(resource_path, path) | ||
config_path = os.path.join(resource_path, 'config.ini') | ||
if os.path.exists(config_path): | ||
config = ConfigParser.RawConfigParser() | ||
config.read(config_path) | ||
if config.has_option('main', 'order'): | ||
order = config.get('main', 'order').split() | ||
if config.has_option('main', 'dont_bundle'): | ||
dont_bundle = config.get('main', 'dont_bundle').split() | ||
if config.has_section('depends'): | ||
items = config.items('depends') | ||
depends = dict((n, v.split()) for (n, v) in items) | ||
|
||
library = Library(name, path) | ||
module = sys.modules[__name__] | ||
|
||
# process each .js/.css file found | ||
for dirname, dirnames, filenames in os.walk(resource_path): | ||
for x in reversed(order): | ||
if x in filenames: | ||
filenames.remove(x) | ||
filenames.insert(0, x) | ||
for f in filenames: | ||
if f.endswith('.js') and not f.endswith('.min.js'): | ||
minify(f, jsmin) | ||
create_resource(f) | ||
if f.endswith('.css') and not f.endswith('.min.css'): | ||
minify(f, cssmin) | ||
create_resource(f) | ||
# finally add the library to this module | ||
setattr(module, name, library) | ||
|
||
|
||
setup('resources', 'resources') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
rjsmin.py | ||
is taken from the rjsmin project and licensed under Apache License, Version 2 | ||
http://opensource.perlig.de/rjsmin/ | ||
|
||
# Copyright 2011, 2012 | ||
# Andr\xe9 Malo or his licensors, as applicable | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
rcssmin.py | ||
is taken from the rcssmin project and licensed under Apache License, Version 2 | ||
http://opensource.perlig.de/rcssmin/ | ||
|
||
# Copyright 2011, 2012 | ||
# Andr\xe9 Malo or his licensors, as applicable | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. |
Empty file.
Oops, something went wrong.