source: products/quintagroup.seoptimizer/trunk/quintagroup/seoptimizer/util.py @ 3223

Last change on this file since 3223 was 3223, checked in by vmaksymiv, 13 years ago

has added upgrade step for fixing escaped seo properties

  • Property svn:eol-style set to native
File size: 1.7 KB
RevLine 
[387]1from AccessControl import ClassSecurityInfo
[3189]2from htmlentitydefs import entitydefs
3import re
[387]4
[1673]5try:
6    from App.class_init import InitializeClass
[3141]7    InitializeClass
[1673]8except ImportError:
9    from Globals import InitializeClass
10
[387]11
12class SortedDict(dict):
[1509]13    """ A sorted dictionary.
14    """
[387]15    security = ClassSecurityInfo()
[1313]16
17    security.declarePublic('items')
[3134]18
[387]19    def items(self):
[1313]20        primary_metatags = self.pmt
[3134]21        lst = [(name, self[name]) for name in primary_metatags \
22                                  if name in self.keys()] + \
23              [(name, self[name]) for name in self.keys() \
24                                  if name not in primary_metatags]
[387]25        return lst
26
[3134]27    security.declarePublic('__init__')
[1313]28
29    def __init__(self, *args, **kwargs):
[3134]30        super(SortedDict, self).__init__(*args, **kwargs)
[1313]31        self.pmt = []
[3134]32    security.declarePublic('__setitem__')
[1313]33
34    def __setitem__(self, i, y):
[3134]35        super(SortedDict, self).__setitem__(i, y)
[1313]36        if i not in self.pmt:
37            self.pmt.append(i)
[3134]38    security.declarePublic('pop')
[1313]39
[1317]40    def pop(self, k, *args, **kwargs):
[3134]41        super(SortedDict, self).pop(k, *args, **kwargs)
[1317]42        if k in self.pmt:
43            self.pmt.remove(k)
44
[387]45try:
46    InitializeClass(SortedDict)
47except:
48    pass
[3189]49
50
51def _group_unescape(m):
52    if m.group("ent"):
53        try:
[3223]54            return unescape(entitydefs[m.group("ent")])
[3189]55        except KeyError:
56            return m.group(0)
57    if m.group("dec"):
58        return unichr(int(m.group("dec")))
59    if m.group("hex"):
60        return unichr(int(m.group("hex"), 16))
61
62expr = re.compile(r'&(?:(?P<ent>\w+?)|'\
63                   '#(?P<dec>\d{1,10})|'\
64                   '#x(?P<hex>[0-9a-fA-F]{1,8}));')
65
66
67def unescape(s):
[3223]68    return expr.sub(_group_unescape, s)
Note: See TracBrowser for help on using the repository browser.