]>
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 from tlib
.xmlw
import Element
6 from debug
import LogEvent
, INFO
, WARN
16 chunks
= file.split("%")
18 file = "%s@%s" % ("%".join(chunks
), end
)
22 return jid
.replace("@", "%")
27 Class for storage of data.
29 Create one instance of the class for each XDB 'folder' you want.
30 Call request()/set() with the xdbns argument you wish to retrieve
32 def __init__(self
, name
, mangle
=False):
33 """ Creates an XDB object. If mangle is True then any '@' signs in filenames will be changed to '%' """
34 self
.name
= os
.path
.abspath(config
.spooldir
) + '/' + name
35 if not os
.path
.exists(self
.name
):
36 os
.makedirs(self
.name
)
39 def __getFile(self
, file):
44 document
= utils
.parseFile(self
.name
+ "/" + hash + "/" + file + ".xml")
48 def __writeFile(self
, file, text
):
52 prev_umask
= os
.umask(SPOOL_UMASK
)
54 pre
= self
.name
+ "/" + hash + "/"
55 if not os
.path
.exists(pre
):
57 f
= open(pre
+ file + ".xml", "w")
63 """ Returns a list containing the files in the current XDB database """
65 for dir in os
.listdir(self
.name
):
66 if(os
.path
.isdir(self
.name
+ "/" + dir)):
67 files
.extend(os
.listdir(self
.name
+ "/" + dir))
69 files
= [unmangle(x
)[:-4] for x
in files
]
71 files
= [x
[:-4] for x
in files
]
73 while files
.count(''):
78 def request(self
, file, xdbns
):
79 """ Requests a specific xdb namespace from the XDB 'file' """
81 document
= self
.__getFile
(file)
82 for child
in document
.elements():
83 if(child
.getAttribute("xdbns") == xdbns
):
88 def set(self
, file, xdbns
, element
):
89 """ Sets a specific xdb namespace in the XDB 'file' to element """
91 element
.attributes
["xdbns"] = xdbns
94 document
= self
.__getFile
(file)
98 document
= Element((None, "xdb"))
100 # Remove the existing node (if any)
101 for child
in document
.elements():
102 if(child
.getAttribute("xdbns") == xdbns
):
103 document
.children
.remove(child
)
105 document
.addChild(element
)
107 self
.__writeFile
(file, document
.toXml())
109 LogEvent(WARN
, "", "IOError " + str(e
))
112 def remove(self
, file):
113 """ Removes an XDB file """
114 file = self
.name
+ "/" + file[0:2] + "/" + file + ".xml"
120 LogEvent(WARN
, "", "IOError " + str(e
))