Exemplo n.º 1
0
def main(argv=sys.argv, 
        run_juicer=run_juicer, run_replace_url=run_replace_url,
        copy_images=copy_images, remove_file=os.remove):
    if len(argv) !=3:
        raise RuntimeError, 'Usage: rejuice inifile section_name'
    inifile = argv[1]
    section_name = argv[2]

    resolver = ResolverList(())
    resolver.add_new_section(inifile, section_name, allow_filter_section_keys=True)
    
    for o in resolver:
        for resource in o.resources:
            prod_resource = resource
            prod_resource_path = os.path.join(o.filepath, prod_resource)
            devel_resources = o.resources[resource]
            devel_resource_path_list = []
            has_import_resource = False
            for devel_resource_spec, extend_inifile, extend_section in devel_resources:
                if extend_inifile is not None:
                    assert extend_section is not None
                    extend_resource = o.sections.get((extend_inifile, extend_section), None)
                    devel_resource_path = os.path.join(extend_resource.filepath, devel_resource_spec)
                    # Is this a resource imported from another static tree?
                    if extend_resource.filepath != o.filepath:
                        has_import_resource = True
                else:
                    assert extend_section is None
                    devel_resource_path = os.path.join(o.filepath, devel_resource_spec)
                devel_resource_path_list.append(devel_resource_path)
            # Do we have to collect the images to this resource?
            if has_import_resource and prod_resource_path.endswith('css'):
                # import mode
                output = os.tmpnam() + '.css'
                try:
                    run_juicer(tuple(devel_resource_path_list), output=output)
                    images = run_replace_url(output, prod_resource_path)
                finally:
                    try:
                        remove_file(output)
                    except (OSError, IOError):
                        pass
                copy_images(images, prod_resource_path)
            else:
                # run external program to do the concatenation
                run_juicer(tuple(devel_resource_path_list), output=prod_resource_path)
                
                

    print "\n\n##### All files compressed OK"
Exemplo n.º 2
0
 def __init__(self, app, global_conf,
         ResolverList=ResolverList,
         base_urls = '', 
         use_request_url = 'true',
         # config contains the followings: url_prefix=None, filepath=None, resources=None, extend_resources=None,
         **config
         ):
     self.app = app
     self.global_conf = global_conf
     self.base_urls = listify_param(base_urls)
     
     if use_request_url not in ('true', 'True', 'false', 'False'):
         raise ValueError('(`use_request_urls`) must be true or false.')
     if use_request_url in ('true', 'True'):
         self.base_urls.append(None)
     new = []
     for base_url in self.base_urls:
         if base_url and base_url[-1] == '/':
             base_url == base_url[:-1]
         new.append(base_url)
     self.base_urls = new
     self.resolver = ResolverList(self.base_urls)
     # Add the resources
     self.resolver.add_current_section(config, inifile=global_conf['__file__'], section_name=None)
     #check all the section
     for res in self.resolver:
         for resource in res.resources:
             for devel_resource in res.resources[resource]:
                 if devel_resource[1] is not None:
                     try:
                         self.resolver.sections[devel_resource[1], devel_resource[2]]
                     except KeyError:
                         raise ValueError, \
                             'Resource "%s" is imported from section "%s" in file "%s",' \
                             ' but this section is missing from extend_resources. Please include it!' \
                             % (devel_resource[0], devel_resource[2], devel_resource[1])                        
Exemplo n.º 3
0
class DevelJuiceMiddleware(object):
    
    def __init__(self, app, global_conf,
            ResolverList=ResolverList,
            base_urls = '', 
            use_request_url = 'true',
            # config contains the followings: url_prefix=None, filepath=None, resources=None, extend_resources=None,
            **config
            ):
        self.app = app
        self.global_conf = global_conf
        self.base_urls = listify_param(base_urls)
        
        if use_request_url not in ('true', 'True', 'false', 'False'):
            raise ValueError('(`use_request_urls`) must be true or false.')
        if use_request_url in ('true', 'True'):
            self.base_urls.append(None)
        new = []
        for base_url in self.base_urls:
            if base_url and base_url[-1] == '/':
                base_url == base_url[:-1]
            new.append(base_url)
        self.base_urls = new
        self.resolver = ResolverList(self.base_urls)
        # Add the resources
        self.resolver.add_current_section(config, inifile=global_conf['__file__'], section_name=None)
        #check all the section
        for res in self.resolver:
            for resource in res.resources:
                for devel_resource in res.resources[resource]:
                    if devel_resource[1] is not None:
                        try:
                            self.resolver.sections[devel_resource[1], devel_resource[2]]
                        except KeyError:
                            raise ValueError, \
                                'Resource "%s" is imported from section "%s" in file "%s",' \
                                ' but this section is missing from extend_resources. Please include it!' \
                                % (devel_resource[0], devel_resource[2], devel_resource[1])                        

    def __call__(self, environ, start_response):
        #base_urls = list(self.base_urls)
        request_base_url = environ['wsgi.url_scheme'] + '://' + environ['REMOTE_ADDR'] + ':' + environ['SERVER_PORT']
        request = webob.Request(environ)
        response = request.get_response(self.app, catch_exc_info=True)
        content_type = response.content_type
        if content_type and content_type.startswith('text/html'):
            # Attempt to parse this, although the content type may be lying.
            body = response.body.strip()
            if body:
                tree = lxml.html.fromstring(body)
                changed = False
                # process js and css
                for processor in (JsProcessor, CssProcessor):
                    elements = processor.select_elements(tree)
                    for element in elements:
                        url =  processor.fetch_url(element)
                        # make a full url, even if relative
                        url = urljoin(request.url, url, True)
                        if url is not None:
                            replace_url_list = processor.resolve_method(self.resolver)(url, request_base_url)
                            replace_url = replace_url_list[-1]
                            dependencies = replace_url_list[:-1]
                            if replace_url != url:
                                changed = True
                                processor.update_url(element, replace_url)
                            if dependencies:
                                changed = True
                                for new_url in dependencies:
                                    new_el = processor.create_new_element(element, new_url)
                                    element.addprevious(new_el)
                # Regenerate html
                if changed:
                    response.body = XMLSerializer(tree)
        return response(environ, start_response)