def load_persistent(self): try: file = open(os.path.join(self.settingsdir,'persist.xml'), 'r') doc = xml.parse(file) if doc.documentElement.tagName == 'session': waypoint_list = doc.getElementsByTagName('waypoints')[0] sources = waypoint_list.getElementsByTagName('source') for source in sources: currentSource = Source(source.getAttribute('type')) if currentSource.name == "Manual Waypoints": self.manualSource = currentSource sourceIter = self.wpList.append(None,(currentSource,)) waypoints=source.getElementsByTagName('wp') for waypoint in waypoints: wp=Waypoint() name_element = \ waypoint.getElementsByTagName('name')[0] lat_element = \ waypoint.getElementsByTagName('latitude')[0] lon_element = \ waypoint.getElementsByTagName('longitude')[0] alt_element = \ waypoint.getElementsByTagName('altitude')[0] wp.name = string.strip(name_element.firstChild.data) wp.lat=float(lat_element.firstChild.data) wp.lon=float(lon_element.firstChild.data) wp.alt=float(alt_element.firstChild.data) #print '---'+wp.name #Append Waypoint to correct Source Object self.wpList.append(sourceIter,(wp,)) #if document is empty or contains garbage raise IOError after cleaning up else: doc.unlink() file.close() raise IOError doc.unlink() file.close() return True except(IOError): #The File is not available for reading so insert standard Source into the list currentSource = Source('Manual Waypoints') self.manualSource = currentSource self.wpList.append(None,(currentSource,)) return False
def load_from_file(self, filename): try: file = open(filename, 'r') doc = xml.parse(file) currentSource = Source(os.path.basename(filename)) sourceIter = self.wpList.append(None,(currentSource,)) if doc.documentElement.tagName == 'gpx': waypoints = doc.getElementsByTagName('wpt') for waypoint in waypoints: wp=Waypoint() wp.lat=float(waypoint.getAttribute('lat')) wp.lon=float(waypoint.getAttribute('lon')) alt_element = \ waypoint.getElementsByTagName('ele') if alt_element.length > 0: wp.alt=float(alt_element[0].firstChild.data) name_element = \ waypoint.getElementsByTagName('name') if name_element.length > 0: wp.name = string.strip(name_element[0].firstChild.data) #Append Waypoint to correct Source Object self.wpList.append(sourceIter,(wp,)) #if document is empty or contains garbage raise IOError after cleaning up else: doc.unlink() file.close() raise IOError doc.unlink() file.close() return True except(IOError): return False