def parseSampleNode(sampleNode): sample = Sample() events = [] for node in sampleNode: if node.tag == 'depth': sample.depth = parseFloat(node) elif node.tag == 'time': sample.time = parseDuration(node) elif node.tag == 'temperature': sample.temperature = parseFloat(node) elif node.tag == 'bearing': pass elif node.tag == 'vendor': pass elif node.tag == 'event': event = Event() event.time = int(node.get('time')) # time within sample event.time += sample.time # add sample time to get actual time in dive event.text = node.text if event.text != 'unknown': events.append(event) else: logging.warn("unknown: " + node.toxml()) pass return sample, events
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