Back to index

plone3  3.1.7
Public Member Functions | Public Attributes
kss.core.pluginregistry._concatresource.compression.thirdparty.packer.KeywordMapper Class Reference
Inheritance diagram for kss.core.pluginregistry._concatresource.compression.thirdparty.packer.KeywordMapper:
Inheritance graph
[legend]

List of all members.

Public Member Functions

def __init__
def analyseKeywords
def analyse
def getKeywords
def sub

Public Attributes

 regexp
 encoder
 mapping

Detailed Description

Definition at line 27 of file packer.py.


Constructor & Destructor Documentation

Reimplemented in kss.core.pluginregistry._concatresource.compression.thirdparty.packer.JavascriptKeywordMapper.

Definition at line 28 of file packer.py.

00028 
00029     def __init__(self, regexp, encoder):
00030         if isinstance(regexp, (str, unicode)):
00031             self.regexp = re.compile(regexp)
00032         else:
00033             self.regexp = regexp
00034         self.encoder = encoder
00035         self.mapping = {}

Here is the caller graph for this function:


Member Function Documentation

Definition at line 95 of file packer.py.

00095 
00096     def analyse(self, input):
00097         self.mapping = self.analyseKeywords(input)

Here is the call graph for this function:

Definition at line 36 of file packer.py.

00036 
00037     def analyseKeywords(self, input):
00038         matches = self.regexp.findall(input)
00039 
00040         protected = {}
00041         keyword_count = {}
00042         index = 0
00043         for match in matches:
00044             if match not in keyword_count:
00045                 keyword_count[match] = 0
00046                 protected[self.encoder(index)] = index
00047                 index = index + 1
00048             keyword_count[match] = keyword_count[match] + 1
00049 
00050         for match in matches:
00051             if match in protected and keyword_count[match]:
00052                 keyword_count[match] = 0
00053 
00054         protected = {}
00055         for match in keyword_count:
00056             if not keyword_count[match]:
00057                 protected[match] = None
00058 
00059         ## sorted_matches = [(c,len(v),v) for v,c in keyword_count.iteritems()]
00060         # the above line implements the original behaviour, the code below
00061         # removes keywords which have not enough weight to be encoded, in total
00062         # this saves some bytes, because the total length of the generated
00063         # codes is a bit smaller. This needs corresponding code in the
00064         # fast_decode javascript function of the decoder, see comment there
00065         sorted_matches = []
00066         for value, count in keyword_count.iteritems():
00067             weight = count * len(value)
00068             if len(value) >= weight:
00069                 keyword_count[value] = 0
00070                 sorted_matches.append((0, value))
00071             else:
00072                 sorted_matches.append((weight, value))
00073         sorted_matches.sort()
00074         sorted_matches.reverse()
00075         sorted_matches = [x[-1] for x in sorted_matches]
00076 
00077         index = 0
00078         mapping = {}
00079         for match in sorted_matches:
00080             if not keyword_count[match]:
00081                 if match not in protected:
00082                     mapping[match] = (-1, match)
00083                 continue
00084             while 1:
00085                 encoded = self.encoder(index)
00086                 index = index + 1
00087                 if encoded in protected:
00088                     mapping[encoded] = (index-1, encoded)
00089                     continue
00090                 else:
00091                     break
00092             mapping[match] = (index-1, encoded)
00093 
00094         return mapping

Here is the caller graph for this function:

Definition at line 98 of file packer.py.

00098 
00099     def getKeywords(self):
00100         sorted = zip(self.mapping.itervalues(), self.mapping.iterkeys())
00101         sorted.sort()
00102         keywords = []
00103         for (index, encoded), value in sorted:
00104             if index >= 0:
00105                 if encoded != value:
00106                     keywords.append(value)
00107                 else:
00108                     keywords.append('')
00109         return keywords

Here is the caller graph for this function:

Definition at line 110 of file packer.py.

00110 
00111     def sub(self, input):
00112         def repl(m):
00113             return self.mapping.get(m.group(0), ('', m.group(0)))[1]
00114         return self.regexp.sub(repl, input)
00115 

Here is the caller graph for this function:


Member Data Documentation


The documentation for this class was generated from the following file: