import configparser
import logging

from simple_button import SimpleButton
from two_button_control import TwoButtonControl
import  function_calls
from signal import pause
from rotary_encoder import RotaryEncoder

logger = logging.getLogger(__name__)

def getFunctionCall(function_name):
    try:
        if function_name != 'None':
            return getattr(function_calls, function_name)
    except AttributeError:
        logger.error('Could not find FunctionCall {function_name}'.format(function_name=function_name))
    return lambda *args: None


class VolumeControl:
    def __new__(self, config):
        if config.get('Type') == 'TwoButtonControl':
            return TwoButtonControl(
                config.getint('pinUp'),
                config.getint('pinDown'),
                getFunctionCall(config.get('functionCallUp')),
                getFunctionCall(config.get('functionCallDown')),
                functionCallTwoBtns=getFunctionCall(config.get('functionCallTwoButtons')),
                pull_up=config.getboolean('pull_up', fallback=True),
                hold_repeat=config.getboolean('hold_repeat', fallback=True),
                hold_time=config.getfloat('hold_time', fallback=0.3),
                name='VolumeControl'
            )
        elif config.get('Type') == 'RotaryEncoder':
            return RotaryEncoder(
                config.getint('pinUp'),
                config.getint('pinDown'),
                getFunctionCall(config.get('functionCallUp')),
                getFunctionCall(config.get('functionCallDown')),
                config.getfloat('timeBase',fallback=0.1),
                name='RotaryVolumeControl')






def generate_device(config, deviceName):
    print(deviceName)
    device_type = config.get('Type')
    if deviceName.lower() == 'VolumeControl'.lower():
        return VolumeControl(config)
    elif device_type == 'TwoButtonControl':
        return TwoButtonControl(
            config.getint('Pin1'),
            config.getint('Pin2'),
            getFunctionCall(config.get('functionCall1')),
            getFunctionCall(config.get('functionCall2')),
            functionCallTwoBtns=getFunctionCall(config.get('functionCallTwoButtons')),
            pull_up=config.getboolean('pull_up',fallback=True),
            hold_repeat=config.getboolean('hold_repeat', False),
            hold_time=config.getfloat('hold_time',fallback=0.3),
            name=deviceName)
    elif device_type in ('Button', 'SimpleButton'):
        return SimpleButton(config.getint('Pin'),
                            action=getFunctionCall(config.get('functionCall')),
                            name=deviceName,
                            bouncetime=config.getint('bouncetime', fallback=500),
                            edge=config.get('edge',fallback='FALLING'),
                            hold_repeat=config.getboolean('hold_repeat', False),
                            hold_time=config.getfloat('hold_time',fallback=0.3))
    logger.warning('cannot find {}'.format(deviceName))
    return None


def get_all_devices(config):
    devices = []
    logger.info(config.sections())
    for section in config.sections():
        if config.getboolean(section, 'enabled', fallback=False):
            logger.info('adding GPIO-Device, {}'.format(section))
            device = generate_device(config[section], section)
            if device is not None:
                devices.append(device)
            else:
                logger.warning('Could not add Device {} with {}'.format(section, config.items(section)))
        else:
            logger.info('Device {} not enabled'.format(section))
    for dev in devices:
        print(dev)


if __name__ == "__main__":

    logging.basicConfig(level='DEBUG')
    logger = logging.getLogger()
    logger.setLevel('DEBUG')

    config = configparser.ConfigParser()
    config.read('../../settings/gpio_settings.ini')
    devices = get_all_devices(config)
    print(devices)
    pause()

@@ -13,16 +13,26 @@
 gparser%0A
+import os%0A
 import l
@@ -1666,20 +1666,16 @@
 rol')%0A%0A%0A
-%0A%0A%0A%0A
 def gene
@@ -3680,16 +3680,35 @@
 nt(dev)%0A
+    return devices%0A
 %0A%0Aif __n
@@ -3760,21 +3760,20 @@
 (level='
-DEBUG
+INFO
 ')%0A    l
@@ -3825,13 +3825,12 @@
 el('
-DEBUG
+INFO
 ')%0A%0A
@@ -3884,115 +3884,311 @@
 nfig
-.read('../../settings/gpio_settings.ini')%0A    devices = get_all_devices(config)%0A    print(devices)%0A    pause(
+_path = os.path.expanduser('~/.config/phoniebox/gpio_settings.ini')%0A    config.read(config_path)%0A%0A    devices = get_all_devices(config)%0A    print(devices)%0A    logger.info('Ready for taking actions')%0A    try:%0A        pause()%0A    except KeyboardInterrupt:%0A        pass%0A    logger.info('Exiting GPIO Control'
 )%0A

change config file location, updated logging