# Path: filters.py
# def radian_to_degree(value):
#     return math.degrees(value)
# 
# Path: model.py
# class Sample(db.Model):
#     __tablename__ = 'sample'
#     id = db.Column(db.Integer, name='id', primary_key=True)
# 
#     move_id = db.Column(db.Integer, db.ForeignKey(Move.id), name='move_id', nullable=False)
#     move = db.relationship(Move, backref=db.backref('samples', lazy='dynamic'))
# 
#     sample_type = db.Column(db.String, name='sample_type')
#     time = db.Column(db.Interval, name='time')
#     utc = db.Column(db.DateTime, name='utc')
# 
#     distance = db.Column(db.Float, name='distance')
#     speed = db.Column(db.Float, name='speed')
#     temperature = db.Column(db.Float, name='temperature')
# 
#     hr = db.Column(db.Float, name='hr')
#     energy_consumption = db.Column(db.Float, name='energy_consumption')
#     relative_performance_level = db.Column(db.Float, name='relative_performance_level')
# 
#     vertical_speed = db.Column(db.Float, name='vertical_speed')
#     sea_level_pressure = db.Column(db.Float, name='sea_level_pressure')
# 
#     gps_altitude = db.Column(db.Float, name='gps_altitude')
#     gps_heading = db.Column(db.Float, name='gps_heading')
#     gps_speed = db.Column(db.Float, name='gps_speed')
#     gps_hdop = db.Column(db.Float, name='gps_hdop')
# 
#     number_of_satellites = db.Column(db.Integer, name='number_of_satellites')
# 
#     latitude = db.Column(db.Float, name='latitude')
#     longitude = db.Column(db.Float, name='longitude')
# 
#     altitude = db.Column(db.Integer, name='altitude')
# 
#     ehpe = db.Column(db.Float, name='ehpe')  # Estimated Horizontal Position Error
# 
#     cadence = db.Column(db.Float, name='cadence')
# 
#     nav_type = db.Column(db.Integer, name='nav_type')
#     nav_valid = db.Column(db.String, name='nav_valid')
#     nav_type_explanation = db.Column(db.String, name='nav_type_explanation')
# 
#     events = db.Column(JsonEncodedDict(4096), name='events')
#     satellites = db.Column(JsonEncodedDict(4096), name='satellites')
#     apps_data = db.Column(JsonEncodedDict(4096), name='apps_data')


from datetime import timedelta, datetime
from geopy.geocoders import Nominatim
from filters import radian_to_degree
from model import Sample
import re
import sqlalchemy
def set_attr(move, attr, value):
    prop = getattr(type(move), attr).property
    assert len(prop.columns) == 1

    value = _convert_attr(type(prop.columns[0].type), value, attr, "illegal value '%s' for '%s'" % (value, attr))
    setattr(move, attr, value)


def add_children(move, element):
    for child in element.iterchildren():
        tag = normalize_tag(child.tag)

        if tag in ('speed', 'hr', 'cadence', 'temperature', 'altitude'):
            for sub_child in child.iterchildren():
                sub_tag = tag + '_' + normalize_tag(sub_child.tag)
                set_attr(move, sub_tag, value=sub_child.text)
        else:
            set_attr(move, tag, value=child.text)


def _parse_recursive(node):
    if node.countchildren() > 0:
        events = {}
        for child in node.iterchildren():
            tag = remove_namespace(child.tag)
            if len(tag) > 2 and tag.upper() != tag:
                tag = tag[0].lower() + tag[1:]

            data = _parse_recursive(child)
            if tag in events:

# Path: filters.py
# def radian_to_degree(value):
#     return math.degrees(value)
# 
# Path: model.py
# class Sample(db.Model):
#     __tablename__ = 'sample'
#     id = db.Column(db.Integer, name='id', primary_key=True)
# 
#     move_id = db.Column(db.Integer, db.ForeignKey(Move.id), name='move_id', nullable=False)
#     move = db.relationship(Move, backref=db.backref('samples', lazy='dynamic'))
# 
#     sample_type = db.Column(db.String, name='sample_type')
#     time = db.Column(db.Interval, name='time')
#     utc = db.Column(db.DateTime, name='utc')
# 
#     distance = db.Column(db.Float, name='distance')
#     speed = db.Column(db.Float, name='speed')
#     temperature = db.Column(db.Float, name='temperature')
# 
#     hr = db.Column(db.Float, name='hr')
#     energy_consumption = db.Column(db.Float, name='energy_consumption')
#     relative_performance_level = db.Column(db.Float, name='relative_performance_level')
# 
#     vertical_speed = db.Column(db.Float, name='vertical_speed')
#     sea_level_pressure = db.Column(db.Float, name='sea_level_pressure')
# 
#     gps_altitude = db.Column(db.Float, name='gps_altitude')
#     gps_heading = db.Column(db.Float, name='gps_heading')
#     gps_speed = db.Column(db.Float, name='gps_speed')
#     gps_hdop = db.Column(db.Float, name='gps_hdop')
# 
#     number_of_satellites = db.Column(db.Integer, name='number_of_satellites')
# 
#     latitude = db.Column(db.Float, name='latitude')
#     longitude = db.Column(db.Float, name='longitude')
# 
#     altitude = db.Column(db.Integer, name='altitude')
# 
#     ehpe = db.Column(db.Float, name='ehpe')  # Estimated Horizontal Position Error
# 
#     cadence = db.Column(db.Float, name='cadence')
# 
#     nav_type = db.Column(db.Integer, name='nav_type')
#     nav_valid = db.Column(db.String, name='nav_valid')
#     nav_type_explanation = db.Column(db.String, name='nav_type_explanation')
# 
#     events = db.Column(JsonEncodedDict(4096), name='events')
#     satellites = db.Column(JsonEncodedDict(4096), name='satellites')
#     apps_data = db.Column(JsonEncodedDict(4096), name='apps_data')


# Path: _import.py
from datetime import timedelta, datetime
from geopy.geocoders import Nominatim
from filters import radian_to_degree
from model import Sample
import re
import sqlalchemy

def set_attr(move, attr, value):
    prop = getattr(type(move), attr).property
    assert len(prop.columns) == 1

    value = _convert_attr(type(prop.columns[0].type), value, attr, "illegal value '%s' for '%s'" % (value, attr))
    setattr(move, attr, value)


def add_children(move, element):
    for child in element.iterchildren():
        tag = normalize_tag(child.tag)

        if tag in ('speed', 'hr', 'cadence', 'temperature', 'altitude'):
            for sub_child in child.iterchildren():
                sub_tag = tag + '_' + normalize_tag(sub_child.tag)
                set_attr(move, sub_tag, value=sub_child.text)
        else:
            set_attr(move, tag, value=child.text)


def _parse_recursive(node):
    if node.countchildren() > 0:
        events = {}
        for child in node.iterchildren():
            tag = remove_namespace(child.tag)
            if len(tag) > 2 and tag.upper() != tag:
                tag = tag[0].lower() + tag[1:]

            data = _parse_recursive(child)
            if tag in events:

                if not isinstance(events[tag], list):