]>
code.delx.au - pymsnt/blob - src/xdb.py
1 # Copyright 2004-2005 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
9 from debug
import LogEvent
, INFO
, WARN
19 chunks
= file.split("%")
21 file = "%s@%s" % ("%".join(chunks
), end
)
25 return jid
.replace("@", "%")
30 Class for storage of data.
32 Create one instance of the class for each XDB 'folder' you want.
33 Call request()/set() with the xdbns argument you wish to retrieve
35 def __init__(self
, name
, mangle
=False):
36 """ Creates an XDB object. If mangle is True then any '@' signs in filenames will be changed to '%' """
37 self
.name
= os
.path
.abspath(config
.spooldir
) + '/' + name
38 if not os
.path
.exists(self
.name
):
39 os
.makedirs(self
.name
)
42 def __getFile(self
, file):
47 document
= utils
.parseFile(self
.name
+ "/" + hash + "/" + file + ".xml")
51 def __writeFile(self
, file, text
):
55 prev_umask
= os
.umask(SPOOL_UMASK
)
57 pre
= self
.name
+ "/" + hash + "/"
58 if not os
.path
.exists(pre
):
60 f
= open(pre
+ file + ".xml", "w")
66 """ Returns a list containing the files in the current XDB database """
68 for dir in os
.listdir(self
.name
):
69 if(os
.path
.isdir(self
.name
+ "/" + dir)):
70 files
.extend(os
.listdir(self
.name
+ "/" + dir))
72 files
= [unmangle(x
)[:-4] for x
in files
]
74 files
= [x
[:-4] for x
in files
]
76 while files
.count(''):
81 def request(self
, file, xdbns
):
82 """ Requests a specific xdb namespace from the XDB 'file' """
84 document
= self
.__getFile
(file)
85 for child
in document
.elements():
86 if(child
.getAttribute("xdbns") == xdbns
):
91 def set(self
, file, xdbns
, element
):
92 """ Sets a specific xdb namespace in the XDB 'file' to element """
94 element
.attributes
["xdbns"] = xdbns
97 document
= self
.__getFile
(file)
101 document
= Element((None, "xdb"))
103 # Remove the existing node (if any)
104 for child
in document
.elements():
105 if(child
.getAttribute("xdbns") == xdbns
):
106 document
.children
.remove(child
)
108 document
.addChild(element
)
110 self
.__writeFile
(file, document
.toXml())
112 LogEvent(WARN
, "", "IOError " + str(e
))
115 def remove(self
, file):
116 """ Removes an XDB file """
117 file = self
.name
+ "/" + file[0:2] + "/" + file + ".xml"
123 LogEvent(WARN
, "", "IOError " + str(e
))