#!/usr/bin/python3.6
from aiohttp import web
import argparse

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from database.dictionary import Base as dictionary_base
from api.dictionary import get_dictionary
from api.dictionary import entry, definition, translation, configuration

parser = argparse.ArgumentParser(description='Dictionary service')
parser.add_argument('--db-file', dest='STORAGE', required=False)
args = parser.parse_args()

if args.STORAGE:
    STORAGE = args.STORAGE
else:
    DATABASE_STORAGE_INFO_FILE = 'data/word_database_storage_info'
    with open(DATABASE_STORAGE_INFO_FILE) as storage_file:
        STORAGE = storage_file.read()

ENGINE = create_engine('sqlite:///%s' % STORAGE)
dictionary_base.metadata.create_all(ENGINE)
SessionClass = sessionmaker(bind=ENGINE)

routes = web.RouteTableDef()

app = web.Application()
app['database_session'] = SessionClass
app['session_instance'] = SessionClass()
app['autocommit'] = True


app.router.add_route('GET', '/definition/{definition_id}', definition.get_definition)
app.router.add_route('POST', '/definition/search', definition.search_definition)
app.router.add_route('DELETE', '/definition/{definition_id}/delete', definition.delete_definition)

app.router.add_route('GET', '/dictionary/{language}', get_dictionary)

app.router.add_route('GET', '/entry/{language}/{word}', entry.get_entry)
app.router.add_route('POST', '/entry/{language}/create', entry.add_entry)
app.router.add_route('PUT', '/entry/{word_id}/edit', entry.edit_entry)
app.router.add_route('DELETE', '/entry/{word_id}/delete', entry.delete_entry)

app.router.add_route('GET', '/translations/{origin}/{target}/{word}', translation.get_translation)
app.router.add_route('GET', '/translations/{origin}/{word}', translation.get_all_translations)
app.router.add_route('GET', '/word/{word_id}', entry.get_word_by_id)

app.router.add_route('GET', '/ping', configuration.pong)
app.router.add_route('POST', '/commit', configuration.do_commit)
app.router.add_route('POST', '/rollback', configuration.do_rollback)
app.router.add_route('PUT', '/configure', configuration.configure_service)


if __name__ == '__main__':
    try:
        app.router.add_routes(routes)
        web.run_app(app, host="0.0.0.0", port=8001)
    finally:
        app['session_instance'].flush()
        app['session_instance'].close()

@@ -482,24 +482,29 @@
 TORAGE:%0A    
+WORD_
 STORAGE = ar
@@ -654,16 +654,21 @@
         
+WORD_
 STORAGE 
@@ -690,16 +690,21 @@
 read()%0A%0A
+WORD_
 ENGINE =
@@ -735,16 +735,21 @@
 //%25s' %25 
+WORD_
 STORAGE)
@@ -785,24 +785,29 @@
 ate_all(
+WORD_
 ENGINE)%0A
 SessionC
@@ -798,16 +798,20 @@
 ENGINE)%0A
+Word
 SessionC
@@ -835,16 +835,21 @@
 er(bind=
+WORD_
 ENGINE)%0A
@@ -925,24 +925,28 @@
 session'%5D = 
+Word
 SessionClass
@@ -972,16 +972,20 @@
 nce'%5D = 
+Word
 SessionC

change names