comparison orpg/plugindb.py @ 122:36919b8a3ef9 alpha

Traipse Alpha 'OpenRPG' {091031-00} Traipse is a distribution of OpenRPG that is designed to be easy to setup and go. Traipse also makes it easy for developers to work on code without fear of sacrifice. 'Ornery-Orc' continues the trend of 'Grumpy' and adds fixes to the code. 'Ornery-Orc's main goal is to offer more advanced features and enhance the productivity of the user. Update Summary (Cleaning up for Beta) Added Bookmarks Fix to Remote Admin Commands Minor fix to text based Server Fix to Pretty Print, from Core Fix to Splitter Nodes not being created Fix to massive amounts of images loading, from Core Added 'boot' command to remote admin Added confirmation window for sent nodes Minor changes to allow for portability to an OpenSUSE linux OS Miniatures Layer pop up box allows users to turn off Mini labels, from FlexiRPG Zoom Mouse plugin added Images added to Plugin UI Switching to Element Tree Map efficiency, from FlexiRPG Added Status Bar to Update Manager default_manifest.xml renamed to default_upmana.xml Cleaner clode for saved repositories New TrueDebug Class in orpg_log Mercurial's hgweb folder is ported to upmana Happy Halloween!
author sirebral
date Sat, 31 Oct 2009 22:07:55 -0500
parents c54768cffbd4
children 8e07c1a2c69b
comparison
equal deleted inserted replaced
121:496dbf12a6cb 122:36919b8a3ef9
1 import xmltramp 1 from __future__ import with_statement
2
2 from orpg.dirpath import dir_struct 3 from orpg.dirpath import dir_struct
3 #import orpg.tools.validate 4 from orpg.tools.validate import validate
4 from types import * 5 from orpg.tools.orpg_log import logger
5 from orpg.orpgCore import component 6
6 7 from xml.etree.ElementTree import ElementTree, Element, parse
7 class PluginDB: 8 from xml.etree.ElementPath import find
8 def __init__(self, filename="plugindb.xml"): 9
9 self.filename = dir_struct["user"] + filename 10 class PluginDB(object):
10 component.get('validate').config_file(filename,"default_plugindb.xml") 11 etree = ElementTree()
11 self.xml_dom = self.LoadDoc() 12 filename = dir_struct["user"] + "plugindb.xml"
12 13
13 def GetString(self, plugname, strname, defaultval, verbose=0): 14 def __new__(cls, *args, **kwargs):
15 it = cls.__dict__.get("__it__")
16 if it is not None:
17 return it
18 cls.__it__ = it = object.__new__(cls)
19 it._init()
20 return it
21
22 def _init(self):
23 validate.config_file("plugindb.xml", "default_plugindb.xml")
24 self.LoadDoc()
25
26 def GetString(self, plugname, strname, defaultval="", verbose=False):
14 strname = self.safe(strname) 27 strname = self.safe(strname)
15 for plugin in self.xml_dom: 28
16 if plugname == plugin._name: 29 plugin = self.etree.find(plugname)
17 for child in plugin._dir: 30 if plugin is None or plugin.find(strname) is None:
18 if child._name == strname: 31 msg = ["plugindb: no value has been stored for", strname, "in",
19 #str() on this to make sure it's ASCII, not unicode, since orpg can't handle unicode. 32 plugname, "so the default has been returned"]
20 if verbose: print "successfully found the value" 33 logger.info(' '.join(msg), verbose)
21 if len(child): return str( self.normal(child[0]) )
22 else: return ""
23 else:
24 if verbose:
25 print "plugindb: no value has been stored for " + strname + " in " + plugname + " so the default has been returned"
26 return defaultval 34 return defaultval
35
36 logger.debug("successfully found the str value", verbose)
37 return self.normal(plugin.find(strname).text)
27 38
28 def SetString(self, plugname, strname, val): 39 def SetString(self, plugname, strname, val):
29 val = self.safe(val) 40 val = self.safe(val)
30 strname = self.safe(strname) 41 strname = self.safe(strname)
31 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. 42
32 if plugname == plugin._name: 43 plugin = self.etree.find(plugname)
33 plugin[strname] = val 44 if plugin is None:
34 plugin[strname]._attrs["type"] = "string" 45 plugin = Element(plugname)
35 self.SaveDoc() 46 self.etree.getroot().append(plugin)
36 return "found plugin" 47
37 else: 48 str_el = plugin.find(strname)
38 self.xml_dom[plugname] = xmltramp.parse("<" + strname + " type=\"string\">" + val + "</" + strname + ">") 49 if str_el is None:
39 self.SaveDoc() 50 str_el = Element(strname)
40 return "added plugin" 51 str_el.set('type', 'str')
52 plugin.append(str_el)
53 str_el.text = val
54 self.SaveDoc()
41 55
42 def FetchList(self, parent): 56 def FetchList(self, parent):
43 retlist = [] 57 retlist = []
44 if not len(parent): return [] 58 for litem in parent.findall('lobject'):
45 for litem in parent[0]._dir: 59 if litem.get('type') == 'int': retlist.append(int(litem.text))
46 if len(litem): 60 if litem.get('type') == 'bool': retlist.append(litem.text == 'True')
47 if litem._attrs["type"] == "int": retlist += [int(litem[0])] 61 elif litem.get('type') == 'float': retlist.append(float(litem.text))
48 elif litem._attrs["type"] == "long": retlist += [long(litem[0])] 62 elif litem.get('type') == 'list': retlist.append(self.FetchList(litem))
49 elif litem._attrs["type"] == "float": retlist += [float(litem[0])] 63 elif litem.get('type') == 'dict': retlist.append(self.FetchDict(litem))
50 elif litem._attrs["type"] == "list": retlist += [self.FetchList(litem)] 64 else: retlist.append(str(self.normal(litem.text)))
51 elif litem._attrs["type"] == "dict": retlist += [self.FetchDict(litem)]
52 else: retlist += [str( self.normal(litem[0]) )]
53 else: retlist += [""]
54 return retlist 65 return retlist
55 66
56 def GetList(self, plugname, listname, defaultval, verbose=0): 67 def GetList(self, plugname, listname, defaultval=list(), verbose=False):
57 listname = self.safe(listname) 68 listname = self.safe(listname)
58 for plugin in self.xml_dom: 69 plugin = self.etree.find(plugname)
59 if plugname == plugin._name: 70
60 for child in plugin._dir: 71 if plugin is None or plugin.find(listname) is None:
61 if child._name == listname and child._attrs["type"] == "list": 72 msg = ["plugindb: no value has been stored for", listname, "in",
62 retlist = self.FetchList(child) 73 plugname, "so the default has been returned"]
63 if verbose: print "successfully found the value" 74 logger.info(' '.join(msg), verbose)
64 return retlist
65 else:
66 if verbose:
67 print "plugindb: no value has been stored for " + listname + " in " + plugname + " so the default has been returned"
68 return defaultval 75 return defaultval
69 76
77 retlist = self.FetchList(plugin.find(listname))
78 logger.debug("successfully found the list value", verbose)
79 return retlist
80
70 def BuildList(self, val): 81 def BuildList(self, val):
71 listerine = "<list>" 82 list_el = Element('list')
72 for item in val: 83 for item in val:
73 if isinstance(item, basestring):#it's a string 84 i = Element('lobject')
74 listerine += "<lobject type=\"str\">" + self.safe(item) + "</lobject>" 85 if isinstance(item, bool):
75 elif isinstance(item, IntType):#it's an int 86 i.set('type', 'bool')
76 listerine += "<lobject type=\"int\">" + str(item) + "</lobject>" 87 i.text = str(item)
77 elif isinstance(item, FloatType):#it's a float 88 elif isinstance(item, int):#it's an int
78 listerine += "<lobject type=\"float\">" + str(item) + "</lobject>" 89 i.set('type', 'int')
79 elif isinstance(item, LongType):#it's a long 90 i.text = str(item)
80 listerine += "<lobject type=\"long\">" + str(item) + "</lobject>" 91 elif isinstance(item, float):#it's a float
81 elif isinstance(item, ListType):#it's a list 92 i.set('type', 'float')
82 listerine += "<lobject type=\"list\">" + self.BuildList(item) + "</lobject>" 93 i.text = str(item)
83 elif isinstance(item, DictType):#it's a dictionary 94 elif isinstance(item, (list, tuple)):#it's a list
84 listerine += "<lobject type=\"dict\">" + self.BuildDict(item) + "</lobject>" 95 i.set('type', 'list')
96 i.append(self.BuildList(item))
97 elif isinstance(item, dict):#it's a dictionary
98 i.set('type', 'dict')
99 i.append(self.BuildDict(item))
85 else: 100 else:
86 return "type unknown" 101 i.set('type', 'str')
87 listerine += "</list>" 102 i.text = self.safe(item)
88 return listerine 103 list_el.append(i)
104 return list_el
89 105
90 def SetList(self, plugname, listname, val): 106 def SetList(self, plugname, listname, val):
91 listname = self.safe(listname) 107 listname = self.safe(listname)
92 list = xmltramp.parse(self.BuildList(val)) 108 plugin = self.etree.find(plugname)
93 for plugin in self.xml_dom: 109 if plugin is None:
94 if plugname == plugin._name: 110 plugin = Element(plugname)
95 plugin[listname] = list 111 self.etree.getroot().append(plugin)
96 plugin[listname]._attrs["type"] = "list" 112 list_el = plugin.find(listname)
97 self.SaveDoc() 113 if list_el is None:
98 return "found plugin" 114 list_el = Element(listname)
115 list_el.set('type', 'list')
116 plugin.append(list_el)
99 else: 117 else:
100 self.xml_dom[plugname] = xmltramp.parse("<" + listname + "></" + listname + ">") 118 list_el.remove(list_el.find('list'))
101 self.xml_dom[plugname][listname] = list 119 list_el.append(self.BuildList(val))
102 self.xml_dom[plugname][listname]._attrs["type"] = "list" 120 self.SaveDoc()
103 self.SaveDoc()
104 return "added plugin"
105 121
106 def BuildDict(self, val): 122 def BuildDict(self, val):
107 dictator = "<dict>" 123 dict_el = Element('dict')
108 for item in val.keys(): 124 for key, item in val.iteritems():
109 if isinstance(val[item], basestring): 125 i = Element('dobject')
110 dictator += "<dobject name=\"" + self.safe(item) + "\" type=\"str\">" + self.safe(val[item]) + "</dobject>" 126
111 elif isinstance(val[item], IntType):#it's an int 127 if isinstance(item, bool):
112 dictator += "<dobject name=\"" + self.safe(item) + "\" type=\"int\">" + str(val[item]) + "</dobject>" 128 i.set('type', 'bool')
113 elif isinstance(val[item], FloatType):#it's a float 129 i.set('name', self.safe(key))
114 dictator += "<dobject name=\"" + self.safe(item) + "\" type=\"float\">" + str(val[item]) + "</dobject>" 130 i.text = str(item)
115 elif isinstance(val[item], LongType):#it's a long 131 elif isinstance(item, int):#it's an int
116 dictator += "<dobject name=\"" + self.safe(item) + "\" type=\"long\">" + str(val[item]) + "</dobject>" 132 i.set('type', 'int')
117 elif isinstance(val[item], DictType):#it's a dictionary 133 i.set('name', self.safe(key))
118 dictator += "<dobject name=\"" + self.safe(item) + "\" type=\"dict\">" + self.BuildDict(val[item]) + "</dobject>" 134 i.text = str(item)
119 elif isinstance(val[item], ListType):#it's a list 135 elif isinstance(item, float):#it's a float
120 dictator += "<dobject name=\"" + self.safe(item) + "\" type=\"list\">" + self.BuildList(val[item]) + "</dobject>" 136 i.set('type', 'float')
137 i.set('name', self.safe(key))
138 i.text = str(item)
139 elif isinstance(item, (list, tuple)):#it's a list
140 i.set('type', 'list')
141 i.set('name', self.safe(key))
142 i.append(self.BuildList(item))
143 elif isinstance(item, dict):#it's a dictionary
144 i.set('type', 'dict')
145 i.set('name', self.safe(key))
146 i.append(self.BuildDict(item))
121 else: 147 else:
122 return str(val[item]) + ": type unknown" 148 i.set('type', 'str')
123 dictator += "</dict>" 149 i.set('name', self.safe(key))
124 return dictator 150 i.text = self.safe(item)
125 151 dict_el.append(i)
126 def SetDict(self, plugname, dictname, val, file="plugindb.xml"): 152 return dict_el
153
154 def SetDict(self, plugname, dictname, val):
127 dictname = self.safe(dictname) 155 dictname = self.safe(dictname)
128 dict = xmltramp.parse(self.BuildDict(val)) 156 plugin = self.etree.find(plugname)
129 for plugin in self.xml_dom: 157 if plugin is None:
130 if plugname == plugin._name: 158 plugin = Element(plugname)
131 plugin[dictname] = dict 159 self.etree.getroot().append(plugin)
132 plugin[dictname]._attrs["type"] = "dict" 160 dict_el = plugin.find(dictname)
133 self.SaveDoc() 161 if dict_el is None:
134 return "found plugin" 162 dict_el = Element(dictname)
163 dict_el.set('type', 'dict')
164 plugin.append(dict_el)
135 else: 165 else:
136 self.xml_dom[plugname] = xmltramp.parse("<" + dictname + "></" + dictname + ">") 166 dict_el.remove(list_el.find('dict'))
137 self.xml_dom[plugname][dictname] = dict 167 dict_el.append(self.BuildDict(val))
138 self.xml_dom[plugname][dictname]._attrs["type"] = "dict" 168 self.SaveDoc()
139 self.SaveDoc()
140 return "added plugin"
141 169
142 def FetchDict(self, parent): 170 def FetchDict(self, parent):
143 retdict = {} 171 retdict = {}
144 if not len(parent): 172 for ditem in parent.findall('dobject'):
145 return {} 173 key = self.normal(ditem.get('name'))
146 for ditem in parent[0]._dir: 174 if ditem.get('type') == 'int': value = int(ditem.text)
147 if len(ditem): 175 elif ditem.get('type') == 'bool': value = ditem.text == 'True'
148 ditem._attrs["name"] = self.normal(ditem._attrs["name"]) 176 elif ditem.get('type') == 'float': value = float(ditem.text)
149 if ditem._attrs["type"] == "int": retdict[ditem._attrs["name"]] = int(ditem[0]) 177 elif ditem.get('type') == 'list': value = self.FetchList(ditem)
150 elif ditem._attrs["type"] == "long": retdict[ditem._attrs["name"]] = long(ditem[0]) 178 elif ditem.get('type') == 'dict': value = self.FetchDict(ditem)
151 elif ditem._attrs["type"] == "float": retdict[ditem._attrs["name"]] = float(ditem[0]) 179 else: value = str(self.normal(ditem[0]))
152 elif ditem._attrs["type"] == "list": retdict[ditem._attrs["name"]] = self.FetchList(ditem) 180 retdict[key] = value
153 elif ditem._attrs["type"] == "dict": retdict[ditem._attrs["name"]] = self.FetchDict(ditem)
154 else: retdict[ditem._attrs["name"]] = str( self.normal(ditem[0]) )
155 else: retdict[ditem._attrs["name"]] = ""
156 return retdict 181 return retdict
157 182
158 def GetDict(self, plugname, dictname, defaultval, verbose=0): 183 def GetDict(self, plugname, dictname, defaultval=dict(), verbose=False):
159 dictname = self.safe(dictname) 184 dictname = self.safe(dictname)
160 for plugin in self.xml_dom: 185 plugin = self.etree.find(plugname)
161 if plugname == plugin._name: 186 if plugin is None or plugin.find(dictname) is None:
162 for child in plugin._dir: 187 msg = ["plugindb: no value has been stored for", dictname, "in",
163 if child._name == dictname and child._attrs["type"] == "dict": return self.FetchDict(child) 188 plugname, "so the default has been returned"]
164 else: 189 logger.info(' '.join(msg), verbose)
165 if verbose:
166 print "plugindb: no value has been stored for " + dictname + " in " + plugname + " so the default has been returned"
167 return defaultval 190 return defaultval
191 retdict = self.FetchDict(plugin.find(dictname))
192 logger.debug("successfully found dict value", verbose)
193 return retdict
168 194
169 def safe(self, string): 195 def safe(self, string):
170 return string.replace("<", "$$lt$$").replace(">", "$$gt$$").replace("&","$$amp$$").replace('"',"$$quote$$") 196 return string.replace("<", "$$lt$$").replace(">", "$$gt$$")\
197 .replace("&","$$amp$$").replace('"',"$$quote$$")
171 198
172 def normal(self, string): 199 def normal(self, string):
173 return string.replace("$$lt$$", "<").replace("$$gt$$", ">").replace("$$amp$$","&").replace("$$quote$$",'"') 200 return string.replace("$$lt$$", "<").replace("$$gt$$", ">")\
201 .replace("$$amp$$","&").replace("$$quote$$",'"')
174 202
175 def SaveDoc(self): 203 def SaveDoc(self):
176 f = open(self.filename, "w") 204 with open(self.filename, "w") as f:
177 f.write(self.xml_dom.__repr__(1, 1)) 205 self.etree.write(f)
178 f.close()
179 206
180 def LoadDoc(self): 207 def LoadDoc(self):
181 xml_file = open(self.filename) 208 with open(self.filename) as f:
182 plugindb = xml_file.read() 209 self.etree.parse(f)
183 xml_file.close() 210
184 return xmltramp.parse(plugindb) 211 plugindb = PluginDB()