Package rubacon :: Package backend :: Module XMLStorage
[hide private]
[frames] | no frames]

Source Code for Module rubacon.backend.XMLStorage

  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  # Parsing from a string will otherwise print a 
  9  # warning which I want to suppress. 
10 -def disable_warnings(*args): pass
11 12 warnings.filterwarnings("ignore", category=Warning) 13 warnings.showwarning = disable_warnings 14 15 # Used to store and retrieve information in a common format.
16 -class XMLStorage(object):
17 18 # The directory where information id stored 19 ___DATADIR="data/" 20
21 - def __init__(self, datadir="data/"):
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 # @ReturnType void 33 # @ParamType aColName string 34 # Clear the given collection.
35 - def clearCollection(self, aColName):
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 # @ReturnType void 45 # @ParamType aColName string 46 # @ParamType aXpath string 47 # Remove a resource from the given collection.
48 - def removeResource(self, aColName, aXpath):
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 # @ParamType aColName string 67 # @ParamType aXpath string 68 # Retrieve the matching xpath in the given collection.
69 - def retrieve(self, aColName, aXpath):
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 # @ReturnType void 78 # @ParamType aColName string 79 # @ParamType aSnippet string 80 # Store an xml snippet in the given collection.
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 # Create a doc from the file: 90 file_uri = Uri.OsPathToUri(self.___DATADIR + aColName) 91 doc = NonvalidatingReader.parseUri(file_uri) 92 93 # Doc should be alright now 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