#Containers can hold other containers or associations
class container:
def addChild(self, child):
self.children.append(child)
child.parent=self
return self
def __init__(self, name):
self.name=name
self.children=[]
self.parent=None
def display(self,padding=""):
paddingAdd=""
if self.parent!=None:
print padding + self.name + "{"
paddingAdd=" "
for child in self.children:
child.display(padding+paddingAdd)
if self.parent!=None:
print padding+"}"
def find(self,searchTerm):
returnList=[]
if searchTerm[0]=="*":
for child in self.children:
returnList.extend(child.find(searchTerm))
searchTerm=searchTerm[1:]
name,slash,childSearchTerm=searchTerm.partition("/")
for child in self.children:
if child.name==name:
if slash!="/":
returnList.append(child)
else:
returnList.extend(child.find(childSearchTerm))
return returnList
#Associations are just a relationship between two strings, name and data
class association:
def __init__(self,name,data):
self.name=name
self.data=data
self.parent=None
def display(self,padding=""):
print padding + self.name+" : "+self.data+";"
def find(self, searchTerm):
return []
#__findNext__ is used internally to find the next
def __findNextOperator__(text,chars=['{','}',':']):
if (len(chars)==1):
return chars[0]
nextOperator=__findNextOperator__(text,chars[1:])
if (text.find(nextOperator)0):
data,operator,text=text.partition(__findNextOperator__(text))
if operator=="":
break
elif operator=="{":
current=current.addChild(container(data.strip())).children[len(current.children)-1]
elif operator=="}":
current=current.parent
elif operator==":":
value,SemiColon,text=text.partition(";")
if (SemiColon!=";"):
return container("root").addChild(container("Error").addChild(association("Log","No semi-colon found")))
if (value[0]==" " or value[0]=="\n"):
value=value[1:]
current.addChild(association(data.strip(),value))
return root