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
/
old_xml_import.py
60 lines (44 loc) · 1.61 KB
/
old_xml_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
#!/usr/bin/env python
import re
from flask import flash
from lxml import objectify
from _import import add_children, normalize_move, parse_samples, postprocess_move
from model import Move, Device
from model import db
def parse_move(tree):
move = Move()
add_children(move, tree.header)
normalize_move(move)
return move
def old_xml_import(xmlfile, user, request_form):
filename = xmlfile.name
data = xmlfile.readlines()
if isinstance(data[0], bytes):
data = [str(x.decode('utf-8')) for x in data]
data[0] = data[0] + "<sml>"
data.append("</sml>")
filematch = re.match(r'log-([A-F0-9]{16})-\d{4}-\d{2}-\d{2}T\d{2}_\d{2}_\d{2}-\d+\.xml', filename)
if not filematch:
flash("illegal filename: '%s'" % filename, 'error')
return
serial_number = filematch.group(1)
tree = objectify.fromstring("\n".join(data).encode('utf-8'))
move = parse_move(tree)
move.source = filename
move.import_module = __name__
device = Device.query.filter_by(serial_number=serial_number).scalar()
if not device:
device = Device()
device.serial_number = serial_number
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)
for sample in parse_samples(tree.Samples.iterchildren(), move):
db.session.add(sample)
postprocess_move(move)
db.session.commit()
return move