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