1 # Module used to parse osso-backup xml conf files, retrieving categories and
2 # backup paths information
8 from pcsbackuplocation import *
11 class PcsBackupParser:
12 """Holds a list of Backup_location objects
14 Can parse .conf xml files with osso-backup format at the given path with
15 fill_locations_list, creating Backup_location objects based on type,
16 category and path of each location inside each xml file and then holding
17 all objects in the locations_list attribute.
21 self.locationsDict = {}
23 def getLocationsDict(self):
24 return self.locationsDict
26 def addLocation(self, location):
27 """Add a location to locations_list attribute of theis class.
30 location -- the location object to be added
33 category = location.category
34 if category in self.locationsDict.keys():
35 self.locationsDict[category].append(location)
37 self.locationsDict[category] = [location]
39 def fillLocationsDict(self, path):
40 """Add all locations that can be found inside xml files of the given
43 This method reads all .conf files inside the directory path given and
44 puts on its locations_list attribute all the Backup_location objects
45 created with the informations returned from the files parsing.
48 path -- Path of directory containing files to parse
51 for file in os.listdir(path):
52 if file.endswith(".conf"):
53 locations = self.locationsFromFile(os.path.join(path, file))
54 for location in locations:
55 self.addLocation(location)
57 def locationsFromFile(self, xml_file):
58 """Return a list with all locations objects inside the given file.
60 The file is parsed and all informations retrieved from it are used to
61 create Backup_location objects and these objects are appended to a list
62 that is returned to caller.
65 xml_file -- File to parse
68 locations -- List with all Backup_location objects created from file
71 locations_map = self._parser(xml_file)
73 number_of_locations = len(locations_map["types"])
74 for i in range(number_of_locations):
75 type = locations_map["types"][i]
76 category = locations_map["categories"][i]
77 path = locations_map["paths"][i]
78 path = self._fixPath(path)
79 new_loc = PcsBackupLocation(type, category, path)
80 locations.append(new_loc)
84 def _parser(self, xml_file):
85 # Parses the xml_file, divide each location element information on a
86 # dictonary with key to respective information.
88 # { "types": list of types in order of location appereance,
89 # "categories": list of categories ordered like types
90 # "paths" list of install paths in the same order as the others }
91 dom = xml.dom.minidom.parse(xml_file)
95 for node in dom.getElementsByTagName("location"):
96 type = node.getAttribute("type")
97 category = node.getAttribute("category")
98 path = node.childNodes[0].data.strip()
100 categories.append(category)
103 location_map = {"types": types, "categories": categories, "paths": paths}
106 def _fixPath(self, path):
107 # Fix any file path containing device specific constants, modifying
109 modifications = {"$HOME":"/home/user", "$USER":"user"}
110 for key in modifications.keys():
111 path = path.replace(key, modifications[key])
112 if not path.startswith("/"):
113 path = "/".join(path)