comparison orpg/plugindb.py @ 0:4385a7d0efd1 grumpy-goblin

Deleted and repushed it with the 'grumpy-goblin' branch. I forgot a y
author sirebral
date Tue, 14 Jul 2009 16:41:58 -0500
parents
children 551cd440acce
comparison
equal deleted inserted replaced
-1:000000000000 0:4385a7d0efd1
1 import xmltramp
2 import orpg.dirpath
3 import orpg.tools.validate
4 from types import *
5
6 class PluginDB:
7 def __init__(self, filename="plugindb.xml"):
8 self.filename = orpg.dirpath.dir_struct["user"] + filename
9 orpg.tools.validate.Validate().config_file(filename,"default_plugindb.xml")
10 self.xml_dom = self.LoadDoc()
11
12 def GetString(self, plugname, strname, defaultval, verbose=0):
13 strname = self.safe(strname)
14 for plugin in self.xml_dom:
15 if plugname == plugin._name:
16 for child in plugin._dir:
17 if child._name == strname:
18 #str() on this to make sure it's ASCII, not unicode, since orpg can't handle unicode.
19 if verbose:
20 print "successfully found the value"
21 if len(child):
22 return str( self.normal(child[0]) )
23 else:
24 return ""
25 else:
26 if verbose:
27 print "plugindb: no value has been stored for " + strname + " in " + plugname + " so the default has been returned"
28 return defaultval
29
30 def SetString(self, plugname, strname, val):
31 val = self.safe(val)
32 strname = self.safe(strname)
33 for plugin in self.xml_dom:##this isn't absolutely necessary, but it saves the trouble of sending a parsed object instead of a simple string.
34 if plugname == plugin._name:
35 plugin[strname] = val
36 plugin[strname]._attrs["type"] = "string"
37 self.SaveDoc()
38 return "found plugin"
39 else:
40 self.xml_dom[plugname] = xmltramp.parse("<" + strname + " type=\"string\">" + val + "</" + strname + ">")
41 self.SaveDoc()
42 return "added plugin"
43
44 def FetchList(self, parent):
45 retlist = []
46 if not len(parent):
47 return []
48 for litem in parent[0]._dir:
49 if len(litem):
50 if litem._attrs["type"] == "int":
51 retlist += [int(litem[0])]
52 elif litem._attrs["type"] == "long":
53 retlist += [long(litem[0])]
54 elif litem._attrs["type"] == "float":
55 retlist += [float(litem[0])]
56 elif litem._attrs["type"] == "list":
57 retlist += [self.FetchList(litem)]
58 elif litem._attrs["type"] == "dict":
59 retlist += [self.FetchDict(litem)]
60 else:
61 retlist += [str( self.normal(litem[0]) )]
62 else:
63 retlist += [""]
64 return retlist
65
66 def GetList(self, plugname, listname, defaultval, verbose=0):
67 listname = self.safe(listname)
68 for plugin in self.xml_dom:
69 if plugname == plugin._name:
70 for child in plugin._dir:
71 if child._name == listname and child._attrs["type"] == "list":
72 retlist = self.FetchList(child)
73 if verbose:
74 print "successfully found the value"
75 return retlist
76 else:
77 if verbose:
78 print "plugindb: no value has been stored for " + listname + " in " + plugname + " so the default has been returned"
79 return defaultval
80
81 def BuildList(self, val):
82 listerine = "<list>"
83 for item in val:
84 if isinstance(item, basestring):#it's a string
85 listerine += "<lobject type=\"str\">" + self.safe(item) + "</lobject>"
86 elif isinstance(item, IntType):#it's an int
87 listerine += "<lobject type=\"int\">" + str(item) + "</lobject>"
88 elif isinstance(item, FloatType):#it's a float
89 listerine += "<lobject type=\"float\">" + str(item) + "</lobject>"
90 elif isinstance(item, LongType):#it's a long
91 listerine += "<lobject type=\"long\">" + str(item) + "</lobject>"
92 elif isinstance(item, ListType):#it's a list
93 listerine += "<lobject type=\"list\">" + self.BuildList(item) + "</lobject>"
94 elif isinstance(item, DictType):#it's a dictionary
95 listerine += "<lobject type=\"dict\">" + self.BuildDict(item) + "</lobject>"
96 else:
97 return "type unknown"
98 listerine += "</list>"
99 return listerine
100
101 def SetList(self, plugname, listname, val):
102 listname = self.safe(listname)
103 list = xmltramp.parse(self.BuildList(val))
104 for plugin in self.xml_dom:
105 if plugname == plugin._name:
106 plugin[listname] = list
107 plugin[listname]._attrs["type"] = "list"
108 self.SaveDoc()
109 return "found plugin"
110 else:
111 self.xml_dom[plugname] = xmltramp.parse("<" + listname + "></" + listname + ">")
112 self.xml_dom[plugname][listname] = list
113 self.xml_dom[plugname][listname]._attrs["type"] = "list"
114 self.SaveDoc()
115 return "added plugin"
116
117 def BuildDict(self, val):
118 dictator = "<dict>"
119 for item in val.keys():
120 if isinstance(val[item], basestring):
121 dictator += "<dobject name=\"" + self.safe(item) + "\" type=\"str\">" + self.safe(val[item]) + "</dobject>"
122 elif isinstance(val[item], IntType):#it's an int
123 dictator += "<dobject name=\"" + self.safe(item) + "\" type=\"int\">" + str(val[item]) + "</dobject>"
124 elif isinstance(val[item], FloatType):#it's a float
125 dictator += "<dobject name=\"" + self.safe(item) + "\" type=\"float\">" + str(val[item]) + "</dobject>"
126 elif isinstance(val[item], LongType):#it's a long
127 dictator += "<dobject name=\"" + self.safe(item) + "\" type=\"long\">" + str(val[item]) + "</dobject>"
128 elif isinstance(val[item], DictType):#it's a dictionary
129 dictator += "<dobject name=\"" + self.safe(item) + "\" type=\"dict\">" + self.BuildDict(val[item]) + "</dobject>"
130 elif isinstance(val[item], ListType):#it's a list
131 dictator += "<dobject name=\"" + self.safe(item) + "\" type=\"list\">" + self.BuildList(val[item]) + "</dobject>"
132 else:
133 return str(val[item]) + ": type unknown"
134 dictator += "</dict>"
135 return dictator
136
137 def SetDict(self, plugname, dictname, val, file="plugindb.xml"):
138 dictname = self.safe(dictname)
139 dict = xmltramp.parse(self.BuildDict(val))
140 for plugin in self.xml_dom:
141 if plugname == plugin._name:
142 plugin[dictname] = dict
143 plugin[dictname]._attrs["type"] = "dict"
144 self.SaveDoc()
145 return "found plugin"
146 else:
147 self.xml_dom[plugname] = xmltramp.parse("<" + dictname + "></" + dictname + ">")
148 self.xml_dom[plugname][dictname] = dict
149 self.xml_dom[plugname][dictname]._attrs["type"] = "dict"
150 self.SaveDoc()
151 return "added plugin"
152
153 def FetchDict(self, parent):
154 retdict = {}
155 if not len(parent):
156 return {}
157 for ditem in parent[0]._dir:
158 if len(ditem):
159 ditem._attrs["name"] = self.normal(ditem._attrs["name"])
160 if ditem._attrs["type"] == "int":
161 retdict[ditem._attrs["name"]] = int(ditem[0])
162 elif ditem._attrs["type"] == "long":
163 retdict[ditem._attrs["name"]] = long(ditem[0])
164 elif ditem._attrs["type"] == "float":
165 retdict[ditem._attrs["name"]] = float(ditem[0])
166 elif ditem._attrs["type"] == "list":
167 retdict[ditem._attrs["name"]] = self.FetchList(ditem)
168 elif ditem._attrs["type"] == "dict":
169 retdict[ditem._attrs["name"]] = self.FetchDict(ditem)
170 else:
171 retdict[ditem._attrs["name"]] = str( self.normal(ditem[0]) )
172 else:
173 retdict[ditem._attrs["name"]] = ""
174 return retdict
175
176 def GetDict(self, plugname, dictname, defaultval, verbose=0):
177 dictname = self.safe(dictname)
178 for plugin in self.xml_dom:
179 if plugname == plugin._name:
180 for child in plugin._dir:
181 if child._name == dictname and child._attrs["type"] == "dict":
182 return self.FetchDict(child)
183 else:
184 if verbose:
185 print "plugindb: no value has been stored for " + dictname + " in " + plugname + " so the default has been returned"
186 return defaultval
187
188 def safe(self, string):
189 return string.replace("<", "$$lt$$").replace(">", "$$gt$$").replace("&","$$amp$$").replace('"',"$$quote$$")
190
191 def normal(self, string):
192 return string.replace("$$lt$$", "<").replace("$$gt$$", ">").replace("$$amp$$","&").replace("$$quote$$",'"')
193
194 def SaveDoc(self):
195 f = open(self.filename, "w")
196 f.write(self.xml_dom.__repr__(1, 1))
197 f.close()
198
199 def LoadDoc(self):
200 xml_file = open(self.filename)
201 plugindb = xml_file.read()
202 xml_file.close()
203 return xmltramp.parse(plugindb)