[387] | 1 | from AccessControl import ClassSecurityInfo |
---|
[3189] | 2 | from htmlentitydefs import entitydefs |
---|
| 3 | import re |
---|
[387] | 4 | |
---|
[1673] | 5 | try: |
---|
| 6 | from App.class_init import InitializeClass |
---|
[3141] | 7 | InitializeClass |
---|
[1673] | 8 | except ImportError: |
---|
| 9 | from Globals import InitializeClass |
---|
| 10 | |
---|
[387] | 11 | |
---|
| 12 | class 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] | 45 | try: |
---|
| 46 | InitializeClass(SortedDict) |
---|
| 47 | except: |
---|
| 48 | pass |
---|
[3189] | 49 | |
---|
| 50 | |
---|
| 51 | def _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 | |
---|
| 62 | expr = re.compile(r'&(?:(?P<ent>\w+?)|'\ |
---|
| 63 | '#(?P<dec>\d{1,10})|'\ |
---|
| 64 | '#x(?P<hex>[0-9a-fA-F]{1,8}));') |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | def unescape(s): |
---|
[3223] | 68 | return expr.sub(_group_unescape, s) |
---|