Mercurial > traipse_dev
comparison upmana/manifest.py @ 33:3687514e0218 traipse_dev
I actally forgot the Update Manager's new package. Sorry.
Here it is!
author | sirebral |
---|---|
date | Sun, 02 Aug 2009 17:13:45 -0500 |
parents | |
children | 8b630fc8a343 |
comparison
equal
deleted
inserted
replaced
32:c223c1051f4a | 33:3687514e0218 |
---|---|
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="updatemana.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: print "successfully found the value" | |
20 if len(child): return str( self.normal(child[0]) ) | |
21 else: return "" | |
22 else: | |
23 if verbose: | |
24 print "plugindb: no value has been stored for " + strname + " in " + plugname + " so the default has been returned" | |
25 return defaultval | |
26 | |
27 def SetString(self, plugname, strname, val): | |
28 #Set Node, <repo, name, description, value> | |
29 #Set Setting, <setting, value> | |
30 val = self.safe(val) | |
31 strname = self.safe(strname) | |
32 for plugin in self.xml_dom: | |
33 ##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): return [] | |
47 for litem in parent[0]._dir: | |
48 if len(litem): | |
49 if litem._attrs["type"] == "int": retlist += [int(litem[0])] | |
50 elif litem._attrs["type"] == "long": retlist += [long(litem[0])] | |
51 elif litem._attrs["type"] == "float": retlist += [float(litem[0])] | |
52 elif litem._attrs["type"] == "list": retlist += [self.FetchList(litem)] | |
53 elif litem._attrs["type"] == "dict": retlist += [self.FetchDict(litem)] | |
54 else: retlist += [str( self.normal(litem[0]) )] | |
55 else: retlist += [""] | |
56 return retlist | |
57 | |
58 def GetList(self, plugname, listname, defaultval, verbose=0): | |
59 listname = self.safe(listname) | |
60 for plugin in self.xml_dom: | |
61 if plugname == plugin._name: | |
62 for child in plugin._dir: | |
63 if child._name == listname and child._attrs["type"] == "list": | |
64 retlist = self.FetchList(child) | |
65 if verbose: print "successfully found the value" | |
66 return retlist | |
67 else: | |
68 if verbose: | |
69 print "plugindb: no value has been stored for " + listname + " in " + plugname + " so the default has been returned" | |
70 return defaultval | |
71 | |
72 def BuildList(self, val): | |
73 listerine = "<list>" | |
74 for item in val: | |
75 if isinstance(item, basestring):#it's a string | |
76 listerine += "<lobject type=\"str\">" + self.safe(item) + "</lobject>" | |
77 elif isinstance(item, IntType):#it's an int | |
78 listerine += "<lobject type=\"int\">" + str(item) + "</lobject>" | |
79 elif isinstance(item, FloatType):#it's a float | |
80 listerine += "<lobject type=\"float\">" + str(item) + "</lobject>" | |
81 elif isinstance(item, LongType):#it's a long | |
82 listerine += "<lobject type=\"long\">" + str(item) + "</lobject>" | |
83 elif isinstance(item, ListType):#it's a list | |
84 listerine += "<lobject type=\"list\">" + self.BuildList(item) + "</lobject>" | |
85 elif isinstance(item, DictType):#it's a dictionary | |
86 listerine += "<lobject type=\"dict\">" + self.BuildDict(item) + "</lobject>" | |
87 else: return "type unknown" | |
88 listerine += "</list>" | |
89 return listerine | |
90 | |
91 def SetList(self, plugname, listname, val): | |
92 listname = self.safe(listname) | |
93 list = xmltramp.parse(self.BuildList(val)) | |
94 for plugin in self.xml_dom: | |
95 if plugname == plugin._name: | |
96 plugin[listname] = list | |
97 plugin[listname]._attrs["type"] = "list" | |
98 self.SaveDoc() | |
99 return "found plugin" | |
100 else: | |
101 self.xml_dom[plugname] = xmltramp.parse("<" + listname + "></" + listname + ">") | |
102 self.xml_dom[plugname][listname] = list | |
103 self.xml_dom[plugname][listname]._attrs["type"] = "list" | |
104 self.SaveDoc() | |
105 return "added plugin" | |
106 | |
107 def BuildDict(self, val): | |
108 dictator = "<dict>" | |
109 for item in val.keys(): | |
110 if isinstance(val[item], basestring): | |
111 dictator += "<dobject name=\"" + self.safe(item) + "\" type=\"str\">" + self.safe(val[item]) + "</dobject>" | |
112 elif isinstance(val[item], IntType):#it's an int | |
113 dictator += "<dobject name=\"" + self.safe(item) + "\" type=\"int\">" + str(val[item]) + "</dobject>" | |
114 elif isinstance(val[item], FloatType):#it's a float | |
115 dictator += "<dobject name=\"" + self.safe(item) + "\" type=\"float\">" + str(val[item]) + "</dobject>" | |
116 elif isinstance(val[item], LongType):#it's a long | |
117 dictator += "<dobject name=\"" + self.safe(item) + "\" type=\"long\">" + str(val[item]) + "</dobject>" | |
118 elif isinstance(val[item], DictType):#it's a dictionary | |
119 dictator += "<dobject name=\"" + self.safe(item) + "\" type=\"dict\">" + self.BuildDict(val[item]) + "</dobject>" | |
120 elif isinstance(val[item], ListType):#it's a list | |
121 dictator += "<dobject name=\"" + self.safe(item) + "\" type=\"list\">" + self.BuildList(val[item]) + "</dobject>" | |
122 else: return str(val[item]) + ": type unknown" | |
123 dictator += "</dict>" | |
124 return dictator | |
125 | |
126 def SetDict(self, plugname, dictname, val, file="plugindb.xml"): | |
127 dictname = self.safe(dictname) | |
128 dict = xmltramp.parse(self.BuildDict(val)) | |
129 for plugin in self.xml_dom: | |
130 if plugname == plugin._name: | |
131 plugin[dictname] = dict | |
132 plugin[dictname]._attrs["type"] = "dict" | |
133 self.SaveDoc() | |
134 return "found plugin" | |
135 else: | |
136 self.xml_dom[plugname] = xmltramp.parse("<" + dictname + "></" + dictname + ">") | |
137 self.xml_dom[plugname][dictname] = dict | |
138 self.xml_dom[plugname][dictname]._attrs["type"] = "dict" | |
139 self.SaveDoc() | |
140 return "added plugin" | |
141 | |
142 def FetchDict(self, parent): | |
143 retdict = {} | |
144 if not len(parent): return {} | |
145 for ditem in parent[0]._dir: | |
146 if len(ditem): | |
147 ditem._attrs["name"] = self.normal(ditem._attrs["name"]) | |
148 if ditem._attrs["type"] == "int": retdict[ditem._attrs["name"]] = int(ditem[0]) | |
149 elif ditem._attrs["type"] == "long": retdict[ditem._attrs["name"]] = long(ditem[0]) | |
150 elif ditem._attrs["type"] == "float": retdict[ditem._attrs["name"]] = float(ditem[0]) | |
151 elif ditem._attrs["type"] == "list": retdict[ditem._attrs["name"]] = self.FetchList(ditem) | |
152 elif ditem._attrs["type"] == "dict": retdict[ditem._attrs["name"]] = self.FetchDict(ditem) | |
153 else: retdict[ditem._attrs["name"]] = str( self.normal(ditem[0]) ) | |
154 else: retdict[ditem._attrs["name"]] = "" | |
155 return retdict | |
156 | |
157 def GetDict(self, plugname, dictname, defaultval, verbose=0): | |
158 dictname = self.safe(dictname) | |
159 for plugin in self.xml_dom: | |
160 if plugname == plugin._name: | |
161 for child in plugin._dir: | |
162 if child._name == dictname and child._attrs["type"] == "dict": return self.FetchDict(child) | |
163 else: | |
164 if verbose: | |
165 print "plugindb: no value has been stored for " + dictname + " in " + plugname + " so the default has been returned" | |
166 return defaultval | |
167 | |
168 def safe(self, string): | |
169 return string.replace("<", "$$lt$$").replace(">", "$$gt$$").replace("&","$$amp$$").replace('"',"$$quote$$") | |
170 | |
171 def normal(self, string): | |
172 return string.replace("$$lt$$", "<").replace("$$gt$$", ">").replace("$$amp$$","&").replace("$$quote$$",'"') | |
173 | |
174 def SaveDoc(self): | |
175 f = open(self.filename, "w") | |
176 f.write(self.xml_dom.__repr__(1, 1)) | |
177 f.close() | |
178 | |
179 def LoadDoc(self): | |
180 xml_file = open(self.filename) | |
181 manifest = xml_file.read() | |
182 xml_file.close() | |
183 return xmltramp.parse(manifest) |