Attachment 'attrparse.py'

Download

   1 """
   2 attributed text parser
   3 """
   4 
   5 import re
   6 
   7 scanner = [
   8     r"(?P<strong>''')",
   9     r"(?P<em>'')",
  10     r"(?P<u>__)",
  11     ]
  12 scanner = re.compile('|'.join(scanner))
  13 
  14 textAttribtues = ['em', 'strong', 'u']
  15 
  16 
  17 class Parser:
  18     """ Attributed text parser """
  19     
  20     def __init__(self):
  21         self.attributes = {}
  22         self.output = []
  23             
  24     def scan(self, line):
  25         pos = 0
  26         for match in scanner.finditer(line):
  27             # Add text before match
  28             if pos < match.start():
  29                 self.addText(line[pos:match.start()])
  30             self.replace(match)
  31             pos = match.end()
  32                 
  33         # Add text after match
  34         if pos < len(line) -1:
  35             self.addText(line[pos:])
  36     
  37     def replace(self, match):
  38         """ Handle key or call handler for key """
  39         key, val = self.getKeyval(match)
  40         if key in textAttribtues:
  41             self.updateAttribtues(key)
  42         else:
  43             handler = getattr(self, '_repl_' + key)
  44             handler(match)
  45     
  46     def updateAttribtues(self, key):
  47         """ Update text attributes state """
  48         if key in self.attributes:
  49             del self.attributes[key]
  50         else:
  51             self.attributes[key] = 1
  52 
  53     def addText(self, text):
  54         attributes = self.attributes.keys()
  55         attributes.sort()
  56         self.output.append((text, attributes))
  57 
  58     def getKeyval(self, match):
  59         # Get match name
  60         for key, val in match.groupdict().items():
  61             if val:
  62                 return key, val
  63                 

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2004-11-11 08:34:02, 1.5 KB) [[attachment:attrparse.py]]
  • [get | view] (2004-11-11 05:37:19, 3.1 KB) [[attachment:test_attrparse.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.