1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| import re
ELEMENT_REG = re.compile(r'(?P<element_type>[^\s]+?) \((?P<element_name>.+?)\) \{(?P<content>.*?)\}',re.MULTILINE|re.DOTALL)
class Element(object) :
def __init__(self,name,content):
self.name = name
self.content = content
def __str__(self):
return "< %s(name = '%s') >"%(self.__class__.__name__,self.name)
def __repr__(self): return str(self)
class Cellule(Element) :
def __init__(self,*args,**kwargs):
Element.__init__(self,*args,**kwargs)
class Carre(Element) :
def __init__(self,*args,**kwargs):
Element.__init__(self,*args,**kwargs)
def cellule_content_parse(content_text):
# do something with the string representing the content of the element
return content_text
def carre_content_parse(content_text):
# do something with the string representing the content of the element
return content_text
OBJECTS = {
'cellule' : (Cellule ,cellule_content_parse),
'carre' : (Carre, carre_content_parse)
}
def parse(text):
matches = ELEMENT_REG.findall(text)
elements = []
for etype,name,content in matches :
cl,content_parse = OBJECTS[etype]
elements.append(cl(name,content_parse(content)))
return elements
if __name__ == '__main__' :
import sys
filename = sys.argv[1]
for e in parse(open(filename).read()) :
print e |