# Path: model.py
# class JsonEncodedDict(TypeDecorator):
# class Device(db.Model):
# class User(db.Model):
# class UserPreference(db.Model):
# class Move(db.Model):
# class Sample(db.Model):
# class MoveEdit(db.Model):
# class AlembicVersion(db.Model):
#     def process_bind_param(self, value, dialect):
#     def process_result_value(self, value, dialect):
#     def is_authenticated(self):
#     def is_active(self):
#     def is_anonymous(self):
#     def has_strava(self):
#     def get_strava_access_token(self):
#     def get_id(self):
#     def __init__(self, key, value):
# 
# Path: old_xml_import.py
# 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
# 
# Path: sml_import.py
# 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


import gzip
from flask import flash
from sqlalchemy.sql import func
from gpx_import import gpx_import
from model import db, Sample
from old_xml_import import old_xml_import
from sml_import import sml_import
#!/usr/bin/env python





def move_import(xmlfile, filename, user, request_form):
    if filename.endswith('.gz'):
        xmlfile = gzip.GzipFile(fileobj=xmlfile, mode='rb', filename=filename)
        filename = filename[:-len('.gz')]

    extension = filename[-4:]
    import_functions = {
        '.xml': old_xml_import,
        '.sml': sml_import,
        '.gpx': gpx_import,
    }

    if extension not in import_functions:
        flash("unknown fileformat: '%s'" % xmlfile.name, 'error')
        return

    import_function = import_functions[extension]
    move = import_function(xmlfile, user, request_form)
    if move:
        move.temperature_avg, = db.session.query(func.avg(Sample.temperature)).filter(Sample.move == move, Sample.temperature > 0).one()

        stroke_count = 0

# Path: model.py
# class JsonEncodedDict(TypeDecorator):
# class Device(db.Model):
# class User(db.Model):
# class UserPreference(db.Model):
# class Move(db.Model):
# class Sample(db.Model):
# class MoveEdit(db.Model):
# class AlembicVersion(db.Model):
#     def process_bind_param(self, value, dialect):
#     def process_result_value(self, value, dialect):
#     def is_authenticated(self):
#     def is_active(self):
#     def is_anonymous(self):
#     def has_strava(self):
#     def get_strava_access_token(self):
#     def get_id(self):
#     def __init__(self, key, value):
# 
# Path: old_xml_import.py
# 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
# 
# Path: sml_import.py
# 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


# Path: imports.py
import gzip
from flask import flash
from sqlalchemy.sql import func
from gpx_import import gpx_import
from model import db, Sample
from old_xml_import import old_xml_import
from sml_import import sml_import

#!/usr/bin/env python





def move_import(xmlfile, filename, user, request_form):
    if filename.endswith('.gz'):
        xmlfile = gzip.GzipFile(fileobj=xmlfile, mode='rb', filename=filename)
        filename = filename[:-len('.gz')]

    extension = filename[-4:]
    import_functions = {
        '.xml': old_xml_import,
        '.sml': sml_import,
        '.gpx': gpx_import,
    }

    if extension not in import_functions:
        flash("unknown fileformat: '%s'" % xmlfile.name, 'error')
        return

    import_function = import_functions[extension]
    move = import_function(xmlfile, user, request_form)
    if move:
        move.temperature_avg, = db.session.query(func.avg(Sample.temperature)).filter(Sample.move == move, Sample.temperature > 0).one()

        stroke_count = 0

        for events, in db.session.query(Sample.events).filter(Sample.move == move, Sample.events != None):