def parse_full(path): dives = [] with open(path, 'r') as file: for event, node in cElementTree.iterparse(file): if node.tag == "dive": dive = Dive() dive.number = int(node.get('number')) dive.date_time = _datetime(node.get('date'), node.get('time')) dive.duration = _duration(node.get('duration')) location = None samples = [] events = [] for child in node: if child.tag == 'depth': dive.max_depth = _depth(child.get('max')) dive.avg_depth = _depth(child.get('mean')) elif child.tag == 'temperature': dive.temperature = _temp(child.get('water')) elif child.tag == 'location': location = child.text elif child.tag == 'sample': sample = Sample() sample.time = _duration(child.get('time')) sample.depth = _depth(child.get('depth')) # Parse the temperature and pressure (or copy from # last sample if not present) temperature = child.get('temp') pressure = child.get('pressure') if temperature: sample.temperature = _temp(temperature) elif samples: sample.temperature = samples[-1].temperature if pressure: sample.pressure = _pressure(pressure) elif samples: sample.pressure = samples[-1].pressure samples.append(sample) dives.append((dive, samples, events, location)) return dives
def parseDiveNode(diveNode): ''' Parses a single dive from the dive log. @param xml: The dive data as an XML string (one <dive> node). @param parse_samples: If set to False, samples will not be parsed - quicker ''' samples = [] events = [] dive = Dive() for node in diveNode: if node.tag == 'number': dive.number = parseInt(node) elif node.tag == 'fingerprint': dive.fingerprint = node.text elif node.tag == 'size': dive.size = parseInt(node) elif node.tag == 'maxdepth': dive.max_depth = parseFloat(node) elif node.tag == 'datetime': dive.date_time = parseDateTime(node) elif node.tag == 'divetime': dive.duration = parseDuration(node) elif node.tag == 'sample': sample, new_events = parseSampleNode(node) samples.append(sample) events += new_events elif node.tag == 'gasmix': pass else: logging.warning("unknown node: %s" % node.toxml()) # For some computers, temperatures are inserted on change only, so some # samples don't have a temperature. In those cases, copy the previous # sample's temparature. previous = None for sample in samples: if sample.temperature == None and previous != None: sample.temperature = previous.temperature previous = sample return (dive, samples, events)