# -*- coding: utf-8 -*-

"""Connection configuration for PyBEL."""

import json
import logging
import os

from .version import VERSION

__all__ = [
    'config',
    'connection',
    'PYBEL_MINIMUM_IMPORT_VERSION',
]

log = logging.getLogger(__name__)

#: The last PyBEL version where the graph data definition changed
PYBEL_MINIMUM_IMPORT_VERSION = 0, 13, 0

config = {}

PYBEL_CACHE_DIRECTORY = 'PYBEL_CACHE_DIRECTORY'
DEFAULT_CACHE_DIRECTORY = os.path.join(os.path.expanduser('~'), '.pybel')
#: The default directory where PyBEL files, including logs and the  default cache, are stored. Created if not exists.
CACHE_DIRECTORY = os.environ.get(PYBEL_CACHE_DIRECTORY, DEFAULT_CACHE_DIRECTORY)

DEFAULT_CACHE_NAME = 'pybel_{}.{}.{}_cache.db'.format(*PYBEL_MINIMUM_IMPORT_VERSION)
DEFAULT_CACHE_PATH = os.path.join(CACHE_DIRECTORY, DEFAULT_CACHE_NAME)
#: The default cache connection string uses sqlite.
DEFAULT_CACHE_CONNECTION = 'sqlite:///' + DEFAULT_CACHE_PATH

PYBEL_CONFIG_DIRECTORY = 'PYBEL_CONFIG_DIRECTORY'
PYBEL_DEV_CONFIG_DIRECTORY = 'PYBEL_DEV_CONFIG_DIRECTORY'
DEFAULT_CONFIG_DIRECTORY = os.path.join(os.path.expanduser('~'), '.config')
if VERSION.endswith('-dev'):
    CONFIG_DIRECTORY = os.environ.get(PYBEL_DEV_CONFIG_DIRECTORY, os.path.join(DEFAULT_CONFIG_DIRECTORY, 'pybel-dev'))
else:
    CONFIG_DIRECTORY = os.environ.get(PYBEL_CONFIG_DIRECTORY, os.path.join(DEFAULT_CONFIG_DIRECTORY, 'pybel'))

PYBEL_CONFIG_PATH = 'PYBEL_CONFIG_PATH'
DEFAULT_CONFIG_PATH = os.path.join(CONFIG_DIRECTORY, 'config.json')
CONFIG_PATH = os.environ.get(PYBEL_CONFIG_PATH, DEFAULT_CONFIG_PATH)

if os.path.exists(CONFIG_PATH):
    with open(CONFIG_PATH) as file:
        config.update(json.load(file))

#: The environment variable that contains the default SQL connection information for the PyBEL cache
PYBEL_CONNECTION = 'PYBEL_CONNECTION'

if PYBEL_CONNECTION in os.environ:
    connection = os.environ[PYBEL_CONNECTION]
    log.info('got environment-defined connection: %s', connection)
elif PYBEL_CONNECTION in config:
    connection = config[PYBEL_CONNECTION]
    log.info('getting configured connection: %s', connection)
else:  # This means that there will have to be a cache directory created
    os.makedirs(CACHE_DIRECTORY, exist_ok=True)
    connection = DEFAULT_CACHE_CONNECTION
    log.info('no configuration found, using default sqlite connection %s', connection)

@@ -61,16 +61,36 @@
 EL.%22%22%22%0A%0A
+import configparser%0A
 import j
@@ -93,16 +93,16 @@
 rt json%0A
-
 import l
@@ -983,142 +983,33 @@
 TH%0A%0A
-PYBEL_
 CONFIG_
-DIRECTORY = 'PYBEL_CONFIG_DIRECTORY'%0APYBEL_DEV_CONFIG_DIRECTORY = 'PYBEL_DEV_CONFIG_DIRECTORY'%0ADEFAULT_CONFIG_DIRECTORY =
+FILE_PATHS = %5B%0A   
  os.
@@ -1056,559 +1056,581 @@
 fig'
-)%0Aif VERSION.endswith('-dev'):
+, 'pybel.ini'),
 %0A    
-CONFIG_DIRECTORY = os.environ.get(PYBEL_DEV_CONFIG_DIRECTORY, os.path.join(DEFAULT_CONFIG_DIRECTORY, 'pybel-dev'))%0Aelse:%0A    CONFIG_DIRECTORY = os.environ.get(PYBEL_CONFIG_DIRECTORY, os.path.join(DEFAULT_CONFIG_DIRECTORY, 'pybel'))%0A%0APYBEL_CONFIG_PATH = 'PYBEL_CONFIG_PATH'%0ADEFAULT_CONFIG_PATH = os.path.join(CONFIG_DIRECTORY, 'config.json')%0ACONFIG_PATH = os.environ.get(PYBEL_CONFIG_PATH, DEFAULT_CONFIG
+os.path.join(os.path.expanduser('~'), '.config', 'pybel.cfg'),%0A    os.path.join(os.path.expanduser('~'), '.config', 'pybel', 'pybel.ini'),%0A    os.path.join(os.path.expanduser('~'), '.config', 'pybel', 'pybel.cfg'),%0A    os.path.join(os.path.expanduser('~'), '.config', 'pybel', 'config.ini'),%0A%5D%0Aconfig_parser = configparser.ConfigParser()%0Aconfig_parser.read(CONFIG_FILE
 _PATH
+S
 )
-%0A
 %0Aif 
-os.path.exists(CONFIG_PATH):%0A    with open(CONFIG_PATH) as file:%0A        config.update(json.load(file)
+'pybel' in config_parser:%0A    config.update(config_parser%5B'pybel'%5D)%0Aif VERSION.endswith('-dev') and 'pybel-dev' in config_parser:%0A    config.update(config_parser%5B'pybel-dev'%5D
 )%0A%0A#
@@ -1921,32 +1921,28 @@
 n)%0Aelif 
-PYBEL_CONNECTION
+'connection'
  in conf
@@ -1969,32 +1969,28 @@
  config%5B
-PYBEL_CONNECTION
+'connection'
 %5D%0A    lo

Update loading of configuration to use INI instead of JSON