1 import os
2
3 from Ft.Xml.Domlette import NonvalidatingReader
4 from Ft.Lib import Uri
5 import Ft.Xml.Domlette as dom
6 import warnings
7
8
9
11
12 warnings.filterwarnings("ignore", category=Warning)
13 warnings.showwarning = disable_warnings
14
15
17
18
19 ___DATADIR="data/"
20
22 """Create the XMLStorage.
23
24 Check if the dir is present, otherwise
25 create it
26 """
27 if not os.path.exists(datadir):
28 os.mkdir(datadir)
29 self.___DATADIR = datadir
30
31
32
33
34
36 """Clear the given collection.
37
38 Implementation is simple: just remove the file
39 """
40 if os.path.exists(self.___DATADIR + aColName):
41 os.remove(self.___DATADIR + aColName)
42
43
44
45
46
47
49 """The XPath matches some elements, these are removed
50 from the resource."""
51 if not os.path.exists(self.___DATADIR + aColName):
52 return
53
54 file_uri = Uri.OsPathToUri(self.___DATADIR + aColName)
55 doc = NonvalidatingReader.parseUri(file_uri)
56 res = doc.xpath(aXpath)
57
58 for e in res:
59 p = e.parentNode
60 p.removeChild(e)
61
62 FILE = open(self.___DATADIR + aColName, "w")
63 dom.Print(doc, FILE)
64 FILE.close()
65
66
67
68
70 """Retrieve the given XPath expression from the document"""
71 if not os.path.exists(self.___DATADIR + aColName):
72 return None
73 file_uri = Uri.OsPathToUri(self.___DATADIR + aColName)
74 doc = NonvalidatingReader.parseUri(file_uri)
75 return doc.xpath(aXpath)
76
77
78
79
80
81 - def store(self, aColName, aSnippet):
82 """Store a given snippet in the collection."""
83 if not os.path.exists(self.___DATADIR + aColName):
84 FILE = open(self.___DATADIR + aColName, "w")
85 doc = dom.implementation.createDocument(None, "collection", None)
86 dom.Print(doc, FILE)
87 FILE.close()
88 else:
89
90 file_uri = Uri.OsPathToUri(self.___DATADIR + aColName)
91 doc = NonvalidatingReader.parseUri(file_uri)
92
93
94 root = doc.documentElement
95 snippet = NonvalidatingReader.parseString(aSnippet)
96 root.appendChild(snippet.documentElement)
97 FILE = open(self.___DATADIR + aColName, "w")
98 dom.Print(root, FILE)
99 FILE.close()
100