# Path: imports.py
# 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):
#             if 'swimming' in events and events['swimming']['type'] == 'Stroke':
#                 stroke_count += 1
# 
#         if 'swimming' in move.activity:
#             assert stroke_count > 0
# 
#         if stroke_count > 0:
#             move.stroke_count = stroke_count
# 
#         db.session.commit()
#         return move
# 
# 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):


import xkcdpass.xkcd_password as xp
from flask_script import Command, Option
from imports import move_import
from model import db, User, Move, Sample
    """ Deletes a move from the database """

    def __init__(self, app_context):
        super(DeleteMove, self).__init__()
        self.app_context = app_context

    def get_options(self):
        return [
            Option('--moveid', '-m', dest='move_id', required=True),
        ]

    def run(self, move_id):
        move = Move.query.filter_by(id=move_id).one()
        Sample.query.filter_by(move=move).delete()
        db.session.delete(move)
        db.session.commit()
        print("deleted move %d" % move.id)


class ListMoves(Command):
    """ Lists all moves in the database """

    def __init__(self, app_context):
        super(ListMoves, self).__init__()
        self.app_context = app_context

    def get_options(self):
        return []

    def run(self):

# Path: imports.py
# 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):
#             if 'swimming' in events and events['swimming']['type'] == 'Stroke':
#                 stroke_count += 1
# 
#         if 'swimming' in move.activity:
#             assert stroke_count > 0
# 
#         if stroke_count > 0:
#             move.stroke_count = stroke_count
# 
#         db.session.commit()
#         return move
# 
# 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: commands.py
import xkcdpass.xkcd_password as xp
from flask_script import Command, Option
from imports import move_import
from model import db, User, Move, Sample

    """ Deletes a move from the database """

    def __init__(self, app_context):
        super(DeleteMove, self).__init__()
        self.app_context = app_context

    def get_options(self):
        return [
            Option('--moveid', '-m', dest='move_id', required=True),
        ]

    def run(self, move_id):
        move = Move.query.filter_by(id=move_id).one()
        Sample.query.filter_by(move=move).delete()
        db.session.delete(move)
        db.session.commit()
        print("deleted move %d" % move.id)


class ListMoves(Command):
    """ Lists all moves in the database """

    def __init__(self, app_context):
        super(ListMoves, self).__init__()
        self.app_context = app_context

    def get_options(self):
        return []

    def run(self):

        for move in Move.query: