comparison MoinMoin/wikidicts.py @ 3729:fef3c5039997

wikidicts: simplify regex compiling
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Fri, 20 Jun 2008 23:54:46 +0200
parents 874c4070bd7d
children 3f9147c23d9c 7a826f946da3
comparison
equal deleted inserted replaced
3727:fd63e84632df 3729:fef3c5039997
24 dict.__init__(self) 24 dict.__init__(self)
25 self.name = None 25 self.name = None
26 if request is not None and pagename is not None: 26 if request is not None and pagename is not None:
27 self.loadFromPage(request, pagename) 27 self.loadFromPage(request, pagename)
28 28
29 # Regular expression used to parse text - subclass should override this 29 # Regular expression used to parse text - subclass must override this
30 regex = u'' 30 regex = None # re.compile(u'...', re.MULTILINE | re.UNICODE)
31 def initRegex(cls):
32 """ Make it a class attribute to avoid it being pickled. """
33 if isinstance(cls.regex, unicode): # not compiled yet
34 cls.regex = re.compile(cls.regex, re.MULTILINE | re.UNICODE)
35 initRegex = classmethod(initRegex)
36 31
37 def loadFromPage(self, request, name): 32 def loadFromPage(self, request, name):
38 """ load the dict from wiki page <name>'s content """ 33 """ load the dict from wiki page <name>'s content """
39 self.name = name 34 self.name = name
40 self.initRegex()
41 text = Page.Page(request, name).get_raw_body() 35 text = Page.Page(request, name).get_raw_body()
42 self.initFromText(text) 36 self.initFromText(text)
43 37
44 def initFromText(self, text): 38 def initFromText(self, text):
45 """ parse the wiki page text and init the dict from it """ 39 """ parse the wiki page text and init the dict from it """
57 key2:: value2 containing spaces 51 key2:: value2 containing spaces
58 ... 52 ...
59 keyn:: .... 53 keyn:: ....
60 any text ignored 54 any text ignored
61 """ 55 """
62 # Key:: Value - ignore all but key:: value pairs, strip whitespace 56 # Key:: Value - ignore all but key:: value pairs, strip whitespace, exactly one space after the :: is required
63 regex = ur'^ (?P<key>.+?):: (?P<val>.*?) *$' # exactly one space after the :: is required 57 regex = re.compile(ur'^ (?P<key>.+?):: (?P<val>.*?) *$', re.MULTILINE | re.UNICODE)
64 58
65 def initFromText(self, text): 59 def initFromText(self, text):
66 for match in self.regex.finditer(text): 60 for match in self.regex.finditer(text):
67 key, val = match.groups() 61 key, val = match.groups()
68 self[key] = val 62 self[key] = val
85 any text ignored 79 any text ignored
86 80
87 If there are any free links using [[free link]] notation, the markup 81 If there are any free links using [[free link]] notation, the markup
88 is stripped from the member. 82 is stripped from the member.
89 """ 83 """
90 # * Member - ignore all but first level list items, strip whitespace, 84 # * Member - ignore all but first level list items, strip whitespace, strip free links markup
91 # strip free links markup if exists. 85 regex = re.compile(ur'^ \* +(?:\[\[)?(?P<member>.+?)(?:\]\])? *$', re.MULTILINE | re.UNICODE)
92 regex = ur'^ \* +(?:\[\[)?(?P<member>.+?)(?:\]\])? *$'
93 86
94 def __init__(self, request=None, pagename=None): 87 def __init__(self, request=None, pagename=None):
95 self._list = [] 88 self._list = []
96 DictBase.__init__(self, request, pagename) 89 DictBase.__init__(self, request, pagename)
97 90