Created
September 25, 2010 22:40
-
-
Save chrisgeo/597370 to your computer and use it in GitHub Desktop.
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
class ComboLoaderApp(object): | |
"""ComboLoader WSGI App | |
Retrieves files from a filesystem or HTTP Request and concatenates them | |
into one file request | |
""" | |
def __init__(self, config): | |
self.config = parse_config_file(config) | |
self.js_regex = re.compile('^.+?\.js$', re.IGNORECASE) | |
self.css_regex = re.compile('^.+?\.css$', re.IGNORECASE) | |
def __call__(self, environ, start_response): | |
req = webob.Request(environ) | |
req.session = environ['beaker.session'] | |
self.js_regex = re.compile('^.+?\.js$', re.IGNORECASE) | |
self.css_regex = re.compile('^.+?\.css$', re.IGNORECASE) | |
#get files and munge together | |
#command line arguments | |
if not req.query_string: | |
return exc.HTTPBadRequest("Cannot have empty parameter list")(environ, start_response) | |
else: | |
files = dict(js=list(), css=list()) | |
#probably a more elegant way to do this. | |
for param in req.query_string.split('&'): | |
if(self.js_regex.match(param)): | |
files['js'].append(param) | |
elif(self.css_regex.match(param)): | |
files['css'].append(param) | |
import httplib2 | |
import json | |
from webob import Response | |
import logging | |
import pprint | |
log = logging.getLogger(__name__) | |
pp = pprint.PrettyPrinter() | |
class RequestLoader(object): | |
"""ComboLoader Object | |
Creates a combo loader object | |
""" | |
def __call__(self, request, config, files): | |
self.request = request | |
self.config = config | |
self.files = files | |
log.debug(files) | |
return config['request_type'](request, **kwargs) | |
class HttpRequest(RequestLoader): | |
pass | |
class FileRequest(RequestLoader): | |
"""FileRequest loads files with a base and path | |
Concatenates the files given in a list | |
""" | |
def __init__(self, request, config, files): | |
self.files = files | |
self.config = config | |
self.request = request | |
self._combine() | |
def _combine(self): | |
content = "" | |
log.debug(pprint.pformat(self.files)) | |
import pdb; pdb.set_trace() | |
for ft, items in self.files.items(): | |
for item in items: | |
try: | |
log.debug("File Type: %s :::: File Path: %s" % (ft, item)) | |
f = open("%s/%s" % (self._get_file_path(ft), item), 'r') | |
content += f.read() | |
f.close() | |
except IOError as e: | |
log.error("File not found: %s ::: Continuing..." % e) | |
pp.pprint(content) | |
return Response(body=content) | |
def _get_file_path(self, file_type): | |
if file_type.lower() == 'js': | |
return self.config['js_path'] | |
elif file_type.lower() == 'css': | |
return self.config['css_path'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment