Пример #1
0
    def compress(self, phase):
        if compressor.get_mode(phase) != COMPRESSOR_MODE_INLINE:
            return
        if not compressor.is_used_type(phase):
            return
        
        cache_name, cache_url, data = self.make_cache(phase)
        
        if phase=="css":
            compressor.content_add('<style type="text/css">\n<!--\n')
        elif phase=="js":
            compressor.content_add('<script type="text/javascript">//<![CDATA[\n')

        if not data:
            #load data from file cache
            f = open(cache_name)
            data = f.read()
            f.close()
            
        compressor.content_add(data)
                    
        if phase=="css":
            compressor.content_add('\n-->\n</style>')
        elif phase=="js":
            compressor.content_add('//]]></script>')
Пример #2
0
 def compress(self, phase):
     if compressor.get_mode(phase) != COMPRESSOR_MODE_ONE_FILE:
         return
     if not compressor.is_used_type(phase):
         return
     cache_name, cache_url, data = self.make_cache(phase)
     compressor.content_add(self.html(phase, cache_url))
Пример #3
0
 def compress(self, phase):
     
     if compressor.get_mode(phase) != COMPRESSOR_MODE_LINKS:
         return
     if not compressor.is_used_type(phase):
         return
     
     
     for file in compressor.get(phase):
         compressor.content_add( self.html( phase, self.copy_to_media( file)))
Пример #4
0
    def make_cache(self, medi_type):
        #check cache
        compress_files = []
        for file in compressor.get(medi_type):
            if file in COMPRESSOR_ONE_FILE_IGNORE:
                compressor.content_add( self.html( medi_type, self.copy_to_media( file)))
            else:
                compress_files.append(search_media(file))        
        
        max_mtime = max(map(lambda file: os.stat(file).st_mtime, compress_files))
        
        key = zlib.adler32("_".join(compress_files)) & 0xffffffff
        name = "%s_%s.%s" % (key, int(max_mtime), medi_type)
        cache_name = os.path.join(COMPRESSOR_CACHE_ROOT, name)
        cache_url = COMPRESSOR_CACHE_URL + name
        
        
        if not os.path.isfile(cache_name):
            #make file
            pattern = re.compile(r"^%s_(\d+).%s$" % (key, medi_type))
            
            #check and delete old cached CSS style
            for f in os.listdir(COMPRESSOR_CACHE_ROOT):
                if pattern.match(f):
                    os.unlink(os.path.join(COMPRESSOR_CACHE_ROOT, f))
            
            
            content=[]
            for file in compress_files:
                try:
                    f = codecs.open(file, "r", "utf-8")
                    if medi_type=="js":
                        content.append(FILE_MARKER + file)
                    content.append(f.read())
                    f.close()
                except IOError, e:
                    raise Exception("Can't find '%s'\n%s" % (file, e))
                
#            
            data = self.make_obfuscation(medi_type, "\n".join(content))
                        
            error_happend = False
            f = open(cache_name, "w")
            try:                
                f.write(data)
            except Exception, e:
                error_happend = True