]>
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
17 chunks
= file.split("%")
19 file = "%s@%s" % ("%".join(chunks
), end
)
23 return file.replace("@", "%")
26 return md5
.md5(file).hexdigest()[0:3]
31 Class for storage of data.
33 Create one instance of the class for each XDB 'folder' you want.
34 Call request()/set() with the xdbns argument you wish to retrieve
36 def __init__(self
, name
, mangle
=False):
37 """ Creates an XDB object. If mangle is True then any '@' signs in filenames will be changed to '%' """
38 self
.name
= os
.path
.join(os
.path
.abspath(config
.spooldir
), name
)
39 if not os
.path
.exists(self
.name
):
40 os
.makedirs(self
.name
)
43 def __getFile(self
, file):
48 document
= xmlw
.parseFile(self
.name
+ X
+ hash + X
+ file + ".xml")
52 def __writeFile(self
, file, text
):
56 prev_umask
= os
.umask(SPOOL_UMASK
)
58 pre
= self
.name
+ X
+ hash + X
59 if not os
.path
.exists(pre
):
62 f
= open(pre
+ file + ".xml.new", "w")
65 shutil
.move(pre
+ file + ".xml.new", pre
+ file + ".xml")
67 LogEvent(WARN
, "", "IOError " + str(e
))
72 """ Returns a list containing the files in the current XDB database """
74 for dir in os
.listdir(self
.name
):
75 if(os
.path
.isdir(self
.name
+ X
+ dir)):
76 files
.extend(os
.listdir(self
.name
+ X
+ dir))
78 files
= [unmangle(x
)[:-4] for x
in files
]
80 files
= [x
[:-4] for x
in files
]
82 while files
.count(''):
87 def request(self
, file, xdbns
):
88 """ Requests a specific xdb namespace from the XDB 'file' """
90 document
= self
.__getFile
(file)
91 for child
in document
.elements():
92 if(child
.getAttribute("xdbns") == xdbns
):
97 def set(self
, file, xdbns
, element
):
98 """ Sets a specific xdb namespace in the XDB 'file' to element """
100 element
.attributes
["xdbns"] = xdbns
103 document
= self
.__getFile
(file)
107 document
= xmlw
.Element((None, "xdb"))
109 # Remove the existing node (if any)
110 for child
in document
.elements():
111 if(child
.getAttribute("xdbns") == xdbns
):
112 document
.children
.remove(child
)
114 document
.addChild(element
)
116 self
.__writeFile
(file, document
.toXml())
118 LogEvent(WARN
, "", "IOError " + str(e
))
121 def remove(self
, file):
122 """ Removes an XDB file """
123 file = self
.name
+ X
+ file[0:2] + X
+ file + ".xml"
129 LogEvent(WARN
, "", "IOError " + str(e
))