import os
from kombu import Queue
from lib.irma.configuration.ini import TemplatedConfiguration

# ______________________________________________________________________________ TEMPLATE

template_frontend_config = {
                         'mongodb': [('host', TemplatedConfiguration.string, None),
                                      ('port', TemplatedConfiguration.integer, 27017),
                                      ('dbname', TemplatedConfiguration.string, None),
                                    ],
                         'collections': [('scan_info', TemplatedConfiguration.string, None),
                                          ('scan_results', TemplatedConfiguration.string, None),
                                          ('scan_files', TemplatedConfiguration.string, None),
                                          ('scan_file_fs', TemplatedConfiguration.string, None),
                                         ],
                         'celery_brain': [('timeout', TemplatedConfiguration.integer, 10),
                                          ],
                         'celery_frontend': [('timeout', TemplatedConfiguration.integer, 10),
                                             ],
                         'broker_brain': [
                                    ('host', TemplatedConfiguration.string, None),
                                    ('port', TemplatedConfiguration.integer, 5672),
                                    ('vhost', TemplatedConfiguration.string, None),
                                    ('username', TemplatedConfiguration.string, None),
                                    ('password' , TemplatedConfiguration.string, None),
                                    ('queue' , TemplatedConfiguration.string, None),
                                    ],
                         'broker_frontend': [
                                    ('host', TemplatedConfiguration.string, None),
                                    ('port', TemplatedConfiguration.integer, 5672),
                                    ('vhost', TemplatedConfiguration.string, None),
                                    ('username', TemplatedConfiguration.string, None),
                                    ('password' , TemplatedConfiguration.string, None),
                                    ('queue' , TemplatedConfiguration.string, None),
                                    ],
                         'backend_brain': [
                                   ('host', TemplatedConfiguration.string, None),
                                   ('port', TemplatedConfiguration.integer, 6379),
                                   ('db', TemplatedConfiguration.integer, None),
                                   ],
                         'backend_frontend': [
                                   ('host', TemplatedConfiguration.string, None),
                                   ('port', TemplatedConfiguration.integer, 6379),
                                   ('db', TemplatedConfiguration.integer, None),
                                   ],
                         'ftp_brain': [
                                    ('host', TemplatedConfiguration.string, None),
                                    ('port', TemplatedConfiguration.integer, 21),
                                    ('username', TemplatedConfiguration.string, None),
                                    ('password' , TemplatedConfiguration.string, None),
                                    ],
                         }


frontend_config = TemplatedConfiguration("frontend.ini", template_frontend_config)

# ______________________________________________________________________________ CELERY HELPERS



def _conf_celery(app, broker, backend, queue=None):
    app.conf.update(
                     BROKER_URL=broker,
                     CELERY_RESULT_BACKEND=backend,
                     CELERY_ACCEPT_CONTENT=['json'],
                     CELERY_TASK_SERIALIZER='json',
                     CELERY_RESULT_SERIALIZER='json'
                     )

    if queue:
        app.conf.update(
                        CELERY_DEFAULT_QUEUE=queue,
                        # delivery_mode=1 enable transient mode (don't survive to a server restart)
                        CELERY_QUEUES=(
                                       Queue(queue, routing_key=queue, delivery_mode=1),
                                       )
                        )
    return

def conf_brain_celery(app):
    broker = get_brain_broker_uri()
    backend = get_brain_backend_uri()
    queue = frontend_config.broker_brain.queue
    _conf_celery(app, broker, backend, queue)

def conf_frontend_celery(app):
    broker = get_frontend_broker_uri()
    backend = get_frontend_backend_uri()
    queue = frontend_config.broker_frontend.queue
    _conf_celery(app, broker, backend, queue)



def get_db_uri():
    return "mongodb://%s:%s/" % (frontend_config.mongodb.host, frontend_config.mongodb.port)

def get_brain_celery_timeout():
    return frontend_config.celery_brain.timeout

def get_frontend_celery_timeout():
    return frontend_config.celery_admin.timeout
# ______________________________________________________________________________ BACKEND HELPERS

def _get_backend_uri(backend_config):
    return "redis://%s:%s/%s" % (backend_config.host, backend_config.port, backend_config.db)

def get_brain_backend_uri():
    return _get_backend_uri(frontend_config.backend_brain)

def get_frontend_backend_uri():
    return _get_backend_uri(frontend_config.backend_frontend)

# ______________________________________________________________________________ BROKER HELPERS

def _get_broker_uri(broker_config):
    return  "amqp://%s:%s@%s:%s/%s" % (broker_config.username, broker_config.password, broker_config.host, broker_config.port, broker_config.vhost)

def get_brain_broker_uri():
    return _get_broker_uri(frontend_config.broker_brain)

def get_frontend_broker_uri():
    return _get_broker_uri(frontend_config.broker_frontend)

@@ -2732,339 +2732,8 @@
  %5D,%0A
-                         'backend_frontend': %5B%0A                                   ('host', TemplatedConfiguration.string, None),%0A                                   ('port', TemplatedConfiguration.integer, 6379),%0A                                   ('db', TemplatedConfiguration.integer, None),%0A                                   %5D,%0A
     
@@ -3392,24 +3392,29 @@
 ker, backend
+=None
 , queue=None
@@ -3481,60 +3481,8 @@
 er,%0A
-                     CELERY_RESULT_BACKEND=backend,%0A
     
@@ -3662,20 +3662,115 @@
   )%0A
-%0A    if queu
+    if backend is not None:%0A        app.conf.update(CELERY_RESULT_BACKEND=backend)%0A%0A    if queue is not Non
 e:%0A 
@@ -4058,25 +4058,8 @@
 ueue
-, delivery_mode=1
 ),%0A 
@@ -4406,49 +4406,8 @@
 i()%0A
-    backend = get_frontend_backend_uri()%0A
     
@@ -4478,33 +4478,30 @@
 pp, broker, 
-backend, 
+queue=
 queue)%0A%0A%0A%0Ade
@@ -4497,17 +4497,16 @@
 ueue)%0A%0A%0A
-%0A
 def get_
@@ -5097,103 +5097,8 @@
 n)%0A%0A
-def get_frontend_backend_uri():%0A    return _get_backend_uri(frontend_config.backend_frontend)%0A%0A
 # __

remove frontend backend