"""
Copyright [2009-2016] EMBL-European Bioinformatics Institute
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
     http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

'''
TO DO: Need to generalize this to make the scripts independent and enable
       connecting to multiple databases simultaneously. Convert this to a class
'''

# ---------------------------------IMPORTS-------------------------------------
import mysql.connector
from mysql.connector import errorcode

from config.rfam_config import RFAMLIVEPUB  # rfam_live on public host
from config.rfam_config import RFAMLIVE  # rfam_live on curation host
from config.rfam_config import RFAMLIVELOC  # local instance of rfam_live
from config.rfam_config import XFAMDEV

# -----------------------------------------------------------------------------
# need to generalize this to enable DB setting upon implementation 
db_conf = XFAMDEV

# -----------------------------------------------------------------------------


def connect():
    """
    Connects to a specific database and returns a mysql connection object
    """

    try:
        cnx = mysql.connector.connect(user=db_conf["user"],
                                      password=db_conf["pwd"],
                                      host=db_conf["host"],
                                      database=db_conf["db"],
                                      port=db_conf["port"])

    except mysql.connector.Error as err:

        if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
            print "Wrong username or password"

        elif err.errno == errorcode.ER_BAD_DB_ERROR:
            print "Database does not exist"

        else:
            print err

    return cnx

# -----------------------------------------------------------------------------


def disconnect(cnx):
    """
    Closes a database connection

    cnx: MySQL connection object
    """

    try:
        cnx.close()

    except:
        print "Error closing database connection"

# -----------------------------------------------------------------------------

@@ -958,24 +958,25 @@
 public host%0A
+#
 from config.
@@ -1107,16 +1107,17 @@
 am_live%0A
+%0A
 from con
@@ -1139,32 +1139,194 @@
  import XFAMDEV%0A
+from config.rfam_config import RFAMLOCAL%0Afrom config.rfam_config import RFAMLIVE%0Afrom config.rfam_config import RFAMLIVEPUB%0Afrom config.rfam_config import RFAMREL
 %0A# -------------
@@ -1460,28 +1460,109 @@
 tion
- %0Adb_conf = XFAMDEV%0A
+%0A#db_conf = RFAMLOCAL%0A#db_conf = RFAMLIVEPUB%0Adb_conf = RFAMLIVE%0A#db_conf = XFAMDEV%0A#db_conf = RFAMREL
 %0A# -

Update Database options