]>
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 debug
import LogEvent
, INFO
, WARN
15 chunks
= file.split("%")
17 file = "%s@%s" % ("%".join(chunks
), end
)
21 return jid
.replace("@", "%")
26 Class for storage of data.
28 Create one instance of the class for each XDB 'folder' you want.
29 Call request()/set() with the xdbns argument you wish to retrieve
31 def __init__(self
, name
, mangle
=False):
32 """ Creates an XDB object. If mangle is True then any '@' signs in filenames will be changed to '%' """
33 self
.name
= os
.path
.abspath(config
.spooldir
) + '/' + name
34 if not os
.path
.exists(self
.name
):
35 os
.makedirs(self
.name
)
38 def __getFile(self
, file):
43 document
= xmlw
.parseFile(self
.name
+ "/" + hash + "/" + file + ".xml")
47 def __writeFile(self
, file, text
):
51 prev_umask
= os
.umask(SPOOL_UMASK
)
53 pre
= self
.name
+ "/" + hash + "/"
54 if not os
.path
.exists(pre
):
56 f
= open(pre
+ file + ".xml", "w")
62 """ Returns a list containing the files in the current XDB database """
64 for dir in os
.listdir(self
.name
):
65 if(os
.path
.isdir(self
.name
+ "/" + dir)):
66 files
.extend(os
.listdir(self
.name
+ "/" + dir))
68 files
= [unmangle(x
)[:-4] for x
in files
]
70 files
= [x
[:-4] for x
in files
]
72 while files
.count(''):
77 def request(self
, file, xdbns
):
78 """ Requests a specific xdb namespace from the XDB 'file' """
80 document
= self
.__getFile
(file)
81 for child
in document
.elements():
82 if(child
.getAttribute("xdbns") == xdbns
):
87 def set(self
, file, xdbns
, element
):
88 """ Sets a specific xdb namespace in the XDB 'file' to element """
90 element
.attributes
["xdbns"] = xdbns
93 document
= self
.__getFile
(file)
97 document
= xmlw
.Element((None, "xdb"))
99 # Remove the existing node (if any)
100 for child
in document
.elements():
101 if(child
.getAttribute("xdbns") == xdbns
):
102 document
.children
.remove(child
)
104 document
.addChild(element
)
106 self
.__writeFile
(file, document
.toXml())
108 LogEvent(WARN
, "", "IOError " + str(e
))
111 def remove(self
, file):
112 """ Removes an XDB file """
113 file = self
.name
+ "/" + file[0:2] + "/" + file + ".xml"
119 LogEvent(WARN
, "", "IOError " + str(e
))