X-Git-Url: https://vcs.maemo.org/git/?a=blobdiff_plain;f=src%2Fbackup%2Fpcsbackupparser.py;fp=src%2Fbackup%2Fpcsbackupparser.py;h=c950a6e2f94122ad5c6946e0f0f791122d309948;hb=256d6db84797e58f32185f042154a7c0fc54163e;hp=0000000000000000000000000000000000000000;hpb=7d8224c9f08712280a22af40325d59a19f4fb1e1;p=tablet-suite diff --git a/src/backup/pcsbackupparser.py b/src/backup/pcsbackupparser.py new file mode 100644 index 0000000..c950a6e --- /dev/null +++ b/src/backup/pcsbackupparser.py @@ -0,0 +1,115 @@ +# Module used to parse osso-backup xml conf files, retrieving categories and +# backup paths information + +import os.path +import xml.dom +import xml.dom.minidom + +from pcsbackuplocation import * + + +class PcsBackupParser: + """Holds a list of Backup_location objects + + Can parse .conf xml files with osso-backup format at the given path with + fill_locations_list, creating Backup_location objects based on type, + category and path of each location inside each xml file and then holding + all objects in the locations_list attribute. + + """ + def __init__(self): + self.locationsDict = {} + + def getLocationsDict(self): + return self.locationsDict + + def addLocation(self, location): + """Add a location to locations_list attribute of theis class. + + Arguments: + location -- the location object to be added + + """ + category = location.category + if category in self.locationsDict.keys(): + self.locationsDict[category].append(location) + else: + self.locationsDict[category] = [location] + + def fillLocationsDict(self, path): + """Add all locations that can be found inside xml files of the given + path. + + This method reads all .conf files inside the directory path given and + puts on its locations_list attribute all the Backup_location objects + created with the informations returned from the files parsing. + + Arguments: + path -- Path of directory containing files to parse + + """ + for file in os.listdir(path): + if file.endswith(".conf"): + locations = self.locationsFromFile(os.path.join(path, file)) + for location in locations: + self.addLocation(location) + + def locationsFromFile(self, xml_file): + """Return a list with all locations objects inside the given file. + + The file is parsed and all informations retrieved from it are used to + create Backup_location objects and these objects are appended to a list + that is returned to caller. + + Arguments: + xml_file -- File to parse + + Returns: + locations -- List with all Backup_location objects created from file + + """ + locations_map = self._parser(xml_file) + locations = [] + number_of_locations = len(locations_map["types"]) + for i in range(number_of_locations): + type = locations_map["types"][i] + category = locations_map["categories"][i] + path = locations_map["paths"][i] + path = self._fixPath(path) + new_loc = PcsBackupLocation(type, category, path) + locations.append(new_loc) + return locations + + + def _parser(self, xml_file): + # Parses the xml_file, divide each location element information on a + # dictonary with key to respective information. + # Dictonary format: + # { "types": list of types in order of location appereance, + # "categories": list of categories ordered like types + # "paths" list of install paths in the same order as the others } + dom = xml.dom.minidom.parse(xml_file) + types = [] + categories = [] + paths = [] + for node in dom.getElementsByTagName("location"): + type = node.getAttribute("type") + category = node.getAttribute("category") + path = node.childNodes[0].data.strip() + types.append(type) + categories.append(category) + paths.append(path) + + location_map = {"types": types, "categories": categories, "paths": paths} + return location_map + + def _fixPath(self, path): + # Fix any file path containing device specific constants, modifying + # them to its values + modifications = {"$HOME":"/home/user", "$USER":"user"} + for key in modifications.keys(): + path = path.replace(key, modifications[key]) + if not path.startswith("/"): + path = "/".join(path) + return path +