ogn-python/ogn/commands/database.py

50 wiersze
1.3 KiB
Python
Czysty Zwykły widok Historia

2015-12-01 22:04:13 +00:00
from ogn.commands.dbutils import engine, session
from ogn.model import Base, AddressOrigin
from ogn.utils import get_ddb
from ogn.collect.database import update_devices
from manager import Manager
manager = Manager()
2015-11-15 18:31:58 +00:00
@manager.command
def init():
"""Initialize the database."""
from alembic.config import Config
from alembic import command
Base.metadata.create_all(engine)
2016-01-12 17:35:33 +00:00
alembic_cfg = Config("alembic.ini")
command.stamp(alembic_cfg, "head")
print("Done.")
2016-01-12 17:36:08 +00:00
@manager.command
def drop(sure='n'):
"""Drop all tables."""
if sure == 'y':
Base.metadata.drop_all(engine)
print('Dropped all tables.')
else:
print("Add argument '--sure y' to drop all tables.")
@manager.command
def import_ddb():
"""Import registered devices from the DDB."""
2015-11-24 07:20:28 +00:00
print("Import registered devices fom the DDB...")
counter = update_devices(session, AddressOrigin.ogn_ddb, get_ddb())
2015-11-15 11:10:20 +00:00
print("Imported %i devices." % counter)
2015-12-01 22:04:13 +00:00
@manager.command
def import_file(path='tests/custom_ddb.txt'):
"""Import registered devices from a local file."""
# (flushes previously manually imported entries)
2015-12-01 22:04:13 +00:00
print("Import registered devices from '{}'...".format(path))
counter = update_devices(session, AddressOrigin.user_defined, get_ddb(path))
print("Imported %i devices." % counter)