You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
4.6 KiB

11 years ago
import os
import re
11 years ago
from couchpotato.core.event import addEvent
12 years ago
from couchpotato.core.helpers.encoding import ss
12 years ago
from couchpotato.core.helpers.variable import tryInt
from couchpotato.core.logger import CPLog
from couchpotato.core.plugins.base import Plugin
12 years ago
from couchpotato.environment import Env
from tornado.web import StaticFileHandler
11 years ago
log = CPLog(__name__)
autoload = 'ClientScript'
class ClientScript(Plugin):
12 years ago
core_static = {
'style': [
11 years ago
'style/combined.min.css',
12 years ago
],
'script': [
'scripts/combined.vendor.min.js',
'scripts/combined.base.min.js',
'scripts/combined.plugins.min.js',
12 years ago
],
}
watches = {}
11 years ago
original_paths = {'style': {}, 'script': {}}
11 years ago
paths = {'style': {}, 'script': {}}
12 years ago
comment = {
11 years ago
'style': '/*** %s:%d ***/\n',
'script': '// %s:%d\n'
}
html = {
'style': '<link rel="stylesheet" href="%s" type="text/css">',
'script': '<script type="text/javascript" src="%s"></script>',
}
def __init__(self):
addEvent('register_style', self.registerStyle)
addEvent('register_script', self.registerScript)
addEvent('clientscript.get_styles', self.getStyles)
addEvent('clientscript.get_scripts', self.getScripts)
11 years ago
addEvent('app.load', self.compile)
12 years ago
self.addCore()
def addCore(self):
for static_type in self.core_static:
for rel_path in self.core_static.get(static_type):
file_path = os.path.join(Env.get('app_dir'), 'couchpotato', 'static', rel_path)
core_url = 'static/%s' % rel_path
12 years ago
if static_type == 'script':
self.registerScript(core_url, file_path, position = 'front')
else:
self.registerStyle(core_url, file_path, position = 'front')
11 years ago
def compile(self):
12 years ago
# Create cache dir
cache = Env.get('cache_dir')
parent_dir = os.path.join(cache, 'minified')
self.makeDir(parent_dir)
Env.get('app').add_handlers(".*$", [(Env.get('web_base') + 'minified/(.*)', StaticFileHandler, {'path': parent_dir})])
12 years ago
for file_type in ['style', 'script']:
ext = 'js' if file_type is 'script' else 'css'
positions = self.original_paths.get(file_type, {})
12 years ago
for position in positions:
files = positions.get(position)
11 years ago
self._compile(file_type, files, position, position + '.' + ext)
12 years ago
11 years ago
def _compile(self, file_type, paths, position, out):
12 years ago
cache = Env.get('cache_dir')
out_name = out
11 years ago
minified_dir = os.path.join(cache, 'minified')
data_combined = ''
12 years ago
new_paths = []
for x in paths:
file_path, url_path = x
11 years ago
12 years ago
f = open(file_path, 'r').read()
11 years ago
if not Env.get('dev'):
data = f
11 years ago
data_combined += self.comment.get(file_type) % (ss(file_path), int(os.path.getmtime(file_path)))
data_combined += data + '\n\n'
else:
new_paths.append(x)
11 years ago
# Combine all files together with some comments
if not Env.get('dev'):
out_path = os.path.join(minified_dir, out_name)
self.createFile(out_path, data_combined.strip())
11 years ago
minified_url = 'minified/%s?%s' % (out_name, tryInt(os.path.getmtime(out)))
new_paths.append((out_path, {'url': minified_url}))
self.paths[file_type][position] = new_paths
11 years ago
def getStyles(self, *args, **kwargs):
return self.get('style', *args, **kwargs)
12 years ago
11 years ago
def getScripts(self, *args, **kwargs):
return self.get('script', *args, **kwargs)
11 years ago
def get(self, type, location = 'head'):
11 years ago
if type in self.paths and location in self.paths[type]:
paths = self.paths[type][location]
return [x[1] for x in paths]
return []
12 years ago
def registerStyle(self, api_path, file_path, position = 'head'):
self.register(api_path, file_path, 'style', position)
12 years ago
def registerScript(self, api_path, file_path, position = 'head'):
self.register(api_path, file_path, 'script', position)
12 years ago
def register(self, api_path, file_path, type, location):
api_path = '%s?%s' % (api_path, tryInt(os.path.getmtime(file_path)))
if not self.original_paths[type].get(location):
self.original_paths[type][location] = []
self.original_paths[type][location].append((file_path, api_path))
12 years ago
if not self.paths[type].get(location):
self.paths[type][location] = []
self.paths[type][location].append((file_path, api_path))