Initial commit (Vesion 0.1)
[tablet-suite] / src / backup / .svn / text-base / pcsbackupparser.py.svn-base
1 # Module used to parse osso-backup xml conf files, retrieving categories and
2 # backup paths information
3
4 import os.path
5 import xml.dom
6 import xml.dom.minidom
7
8 from pcsbackuplocation import *
9
10
11 class PcsBackupParser:
12     """Holds a list of Backup_location objects
13
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.
18
19     """
20     def __init__(self):
21         self.locationsDict = {}
22         
23     def getLocationsDict(self):
24         return self.locationsDict
25         
26     def addLocation(self, location):
27         """Add a location to locations_list attribute of theis class.
28
29         Arguments:
30         location -- the location object to be added
31
32         """
33         category = location.category
34         if category in self.locationsDict.keys():
35             self.locationsDict[category].append(location)
36         else:
37             self.locationsDict[category] = [location]
38
39     def fillLocationsDict(self, path):
40         """Add all locations that can be found inside xml files of the given
41         path.
42
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.
46
47         Arguments:
48         path -- Path of directory containing files to parse
49
50         """
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)
56
57     def locationsFromFile(self, xml_file):
58         """Return a list with all locations objects inside the given file.
59
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.
63
64         Arguments:
65         xml_file -- File to parse
66
67         Returns:
68         locations -- List with all Backup_location objects created from file
69
70         """
71         locations_map = self._parser(xml_file)
72         locations = []
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)
81         return locations
82     
83
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.
87         # Dictonary format:
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)
92         types = []
93         categories = []
94         paths = []
95         for node in dom.getElementsByTagName("location"):
96             type = node.getAttribute("type")
97             category = node.getAttribute("category")
98             path = node.childNodes[0].data.strip()
99             types.append(type)
100             categories.append(category)
101             paths.append(path)
102
103         location_map = {"types": types, "categories": categories, "paths": paths}
104         return location_map
105
106     def _fixPath(self, path):
107         # Fix any file path containing device specific constants, modifying
108         # them to its values
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)
114         return path
115