# Path: src/mcsafetyfeed-server/mcsafetyfeedserver/models.py
# class Users(Base):
# class AgencyTypes(Base):
# class Agencies(Base):
# class DispatchTypes(Base):
# class Groups(Base):
# class GroupDispatchTypes(Base):
# class Statuses(Base):
# class Incidents(Base):
# class IncidentsDispatches(Base):
# class Dispatches(Base):
# class APICalls(Base):
# class CurrentDispatches(Base):
# class Runs(Base):
#     def get_from_code(cls, session, code):
#     def create_new_agency_type(cls, session, code, description):
#     def get_from_guid(cls, session, guid):
#     def create_new_agency(cls, session, agency_code, agency_name,
#             type_id, description, website):
#     def get_all(cls, session):
#     def get_from_dispatch_text(cls, session, dispatch_text):
#     def create_new_dispatch_type(cls, session, dispatch_text, description):
#     def get_all(cls, session):
#     def get_from_dispatch_text(cls, session, dispatch_text):
#     def create_new_status(cls, session, status_text, description):
#     def get_from_status_text(cls, session, status_text):
#     def create_new_incident(cls, session, dispatch_text):
#     def get_count(cls, session):
#     def check_exists(cls, session, guid, status_text):
#     def add_dispatch(cls, session, run_id, status_text, short_address,
#             guid, dispatch_text, dispatch_datetime, source_lat, source_lng,
#             geocode_lat, geocode_lng, full_address, geocode_successful):
#     def get_by_guid(cls, session, guid):
#     def close_dispatch(cls, session, run_id, guid):
#     def get_by_date(cls, session, target_datetime, start, count):
#     def get_count_by_date(cls, session, target_datetime):
#     def add_api_call(cls, session, api_key, query_time, api_call, api_version):
#     def get_count(cls, session):
#     def add_current_dispatch(cls, session, guid):
#     def get_current_dispatch_guids(cls, session):
#     def remove_current_dispatch(cls, session, guid):
#     def get_count(cls, session):
#     def new_run(cls, session):
#     def update_run(cls, session, run, successful, error_text, new_dispatches):


import os
import sys
import transaction
from sqlalchemy import engine_from_config
from pyramid.paster import (
    get_appsettings,
    setup_logging,
    )
from pyramid.scripts.common import parse_vars
from ..models import (
    DBSession,
    #MyModel,
    Base,
    AgencyTypes,
    Agencies,
    )
def main(argv=sys.argv):
    if len(argv) < 2:
        usage(argv)
    config_uri = argv[1]
    options = parse_vars(argv[2:])
    setup_logging(config_uri)
    settings = get_appsettings(config_uri, options=options)
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    Base.metadata.create_all(engine)
    #with transaction.manager:
    #    model = MyModel(name='one', value=1)
    #    DBSession.add(model)

    with transaction.manager:
        with open('agencies.csv','r') as f:
            agencies = f.read().split('\n')

        for agency in agencies:
            if agency.strip() != '':
                # agencyid, shortname, longname, type, description, websiteurl
                parts = agency.split('\t')
                agency_type = AgencyTypes.get_from_code(DBSession, parts[3])
                a = Agencies(
                    agency_code = parts[1],
                    agency_name = parts[2],
                    type_id = agency_type.id,
                    description = parts[4],
                    website = parts[5],
                )

# Path: src/mcsafetyfeed-server/mcsafetyfeedserver/models.py
# class Users(Base):
# class AgencyTypes(Base):
# class Agencies(Base):
# class DispatchTypes(Base):
# class Groups(Base):
# class GroupDispatchTypes(Base):
# class Statuses(Base):
# class Incidents(Base):
# class IncidentsDispatches(Base):
# class Dispatches(Base):
# class APICalls(Base):
# class CurrentDispatches(Base):
# class Runs(Base):
#     def get_from_code(cls, session, code):
#     def create_new_agency_type(cls, session, code, description):
#     def get_from_guid(cls, session, guid):
#     def create_new_agency(cls, session, agency_code, agency_name,
#             type_id, description, website):
#     def get_all(cls, session):
#     def get_from_dispatch_text(cls, session, dispatch_text):
#     def create_new_dispatch_type(cls, session, dispatch_text, description):
#     def get_all(cls, session):
#     def get_from_dispatch_text(cls, session, dispatch_text):
#     def create_new_status(cls, session, status_text, description):
#     def get_from_status_text(cls, session, status_text):
#     def create_new_incident(cls, session, dispatch_text):
#     def get_count(cls, session):
#     def check_exists(cls, session, guid, status_text):
#     def add_dispatch(cls, session, run_id, status_text, short_address,
#             guid, dispatch_text, dispatch_datetime, source_lat, source_lng,
#             geocode_lat, geocode_lng, full_address, geocode_successful):
#     def get_by_guid(cls, session, guid):
#     def close_dispatch(cls, session, run_id, guid):
#     def get_by_date(cls, session, target_datetime, start, count):
#     def get_count_by_date(cls, session, target_datetime):
#     def add_api_call(cls, session, api_key, query_time, api_call, api_version):
#     def get_count(cls, session):
#     def add_current_dispatch(cls, session, guid):
#     def get_current_dispatch_guids(cls, session):
#     def remove_current_dispatch(cls, session, guid):
#     def get_count(cls, session):
#     def new_run(cls, session):
#     def update_run(cls, session, run, successful, error_text, new_dispatches):


# Path: src/mcsafetyfeed-server/mcsafetyfeedserver/scripts/initializedb.py
import os
import sys
import transaction
from sqlalchemy import engine_from_config
from pyramid.paster import (
    get_appsettings,
    setup_logging,
    )
from pyramid.scripts.common import parse_vars
from ..models import (
    DBSession,
    #MyModel,
    Base,
    AgencyTypes,
    Agencies,
    )

def main(argv=sys.argv):
    if len(argv) < 2:
        usage(argv)
    config_uri = argv[1]
    options = parse_vars(argv[2:])
    setup_logging(config_uri)
    settings = get_appsettings(config_uri, options=options)
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    Base.metadata.create_all(engine)
    #with transaction.manager:
    #    model = MyModel(name='one', value=1)
    #    DBSession.add(model)

    with transaction.manager:
        with open('agencies.csv','r') as f:
            agencies = f.read().split('\n')

        for agency in agencies:
            if agency.strip() != '':
                # agencyid, shortname, longname, type, description, websiteurl
                parts = agency.split('\t')
                agency_type = AgencyTypes.get_from_code(DBSession, parts[3])
                a = Agencies(
                    agency_code = parts[1],
                    agency_name = parts[2],
                    type_id = agency_type.id,
                    description = parts[4],
                    website = parts[5],
                )

                DBSession.add(a)