This repository has been archived by the owner on Jan 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
sml_import.py
66 lines (51 loc) · 1.89 KB
/
sml_import.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
import os
from flask import flash
from lxml import objectify
from _import import add_children, set_attr, normalize_tag, normalize_move, parse_samples, postprocess_move
from model import Move, Device
from model import db
def parse_move(tree):
device_log = tree.DeviceLog
move = Move()
add_children(move, device_log.Header)
for child in device_log.Device.Info.iterchildren():
tag = normalize_tag(child.tag)
attr = "device_info_%s" % tag.lower()
set_attr(move, attr, child.text)
normalize_move(move)
return move
def parse_device(tree):
device = Device()
device.name = tree.DeviceLog.Device.Name.text
device.serial_number = tree.DeviceLog.Device.SerialNumber.text
return device
def sml_import(xmlfile, user, request_form):
filename = xmlfile.name
tree = objectify.parse(xmlfile).getroot()
move = parse_move(tree)
move.source = os.path.abspath(filename)
move.import_module = __name__
device = parse_device(tree)
persistent_device = Device.query.filter_by(serial_number=device.serial_number).scalar()
if persistent_device:
if not persistent_device.name:
flash("update device name to '%s'" % device.name)
persistent_device.name = device.name
else:
assert device.name == persistent_device.name
device = persistent_device
else:
db.session.add(device)
if Move.query.filter_by(user=user, date_time=move.date_time, device=device).scalar():
flash("%s at %s already exists" % (move.activity, move.date_time), 'warning')
else:
move.user = user
move.device = device
db.session.add(move)
samples = tree.DeviceLog.Samples.iterchildren()
for sample in parse_samples(samples, move):
db.session.add(sample)
postprocess_move(move)
db.session.commit()
return move