]>
code.delx.au - pymsnt/blob - src/xdb.py
98cdfadcc1688596cdac593023a13c2d9c1fcdc1
1 # Copyright 2004 James Bunton <james@delx.cjb.net>
2 # Licensed for distribution under the GPL version 2, check COPYING for details
5 if(utils
.checkTwisted()):
6 from twisted
.xish
.domish
import Element
8 from tlib
.domish
import Element
20 Class for storage of data. Compatible with xdb_file from Jabberd1.4.x
21 Allows PyMSN-t to be compatible with MSN-t
23 Create one instance of the class for each XDB 'folder' you want.
24 Call request()/set() with the xdbns argument you wish to retrieve
26 def __init__(self
, name
, mangle
=False):
27 """ Creates an XDB object. If mangle is True then any '@' signs in filenames will be changed to '%' """
28 self
.name
= utils
.doPath(config
.spooldir
) + '/' + name
29 if not os
.path
.exists(self
.name
) :
30 os
.makedirs(self
.name
)
33 def __getFile(self
, file):
35 file = file.replace('@', '%')
37 document
= utils
.parseFile(self
.name
+ "/" + file + ".xml")
41 def __writeFile(self
, file, text
):
43 file = file.replace('@', '%')
45 prev_umask
= os
.umask(SPOOL_UMASK
)
46 f
= open(self
.name
+ "/" + file + ".xml", "w")
52 def request(self
, file, xdbns
):
53 """ Requests a specific xdb namespace from the XDB 'file' """
55 document
= self
.__getFile
(file)
56 for child
in document
.elements():
57 if(child
.getAttribute("xdbns") == xdbns
):
62 def set(self
, file, xdbns
, element
):
63 """ Sets a specific xdb namespace in the XDB 'file' to element """
65 element
.attributes
["xdbns"] = xdbns
68 document
= self
.__getFile
(file)
72 document
= Element((None, "xdb"))
74 # Remove the existing node (if any)
75 for child
in document
.elements():
76 if(child
.getAttribute("xdbns") == xdbns
):
77 document
.children
.remove(child
)
79 document
.addChild(element
)
81 self
.__writeFile
(file, document
.toXml())
83 debug
.log("XDB error writing entry %s to file %s" % (xdbns
, file))
86 def remove(self
, file):
87 """ Removes an XDB file """
88 file = self
.name
+ "/" + file + ".xml"
90 file = file.replace('@', '%')
94 debug
.log("XDB error removing file " + file)