# return a configuration objects with default values
def myCar_read_config_defaults(config):
    import configparser
    if not config.sections():
    # there is nothing, lets setup default values
        config['paths'] = { 'app' : '/opt/myCar',
                            'log' : 'data/log',
                            'record' : 'data/rec',
                            'export' : 'data/exp' }


        config['connections'] = { 'bt_dev' : '/dev/rfcomm',
                                  'gps_dev' : '/dev/ttyS1',
                                  'wifi_pwd' : 'myCar123' }

        config['startup'] = { 'bt_connect' : True,
                              'autorecord' : True }

        config['metrics'] = { 'temperature' : 'C',
                              'speed' : 'km/h' }

    # return a valid config object either 
    # with or without default values
    return config

def myCar_save_config(config):
    import configparser, logging
    
    with open('data/etc/myCar.conf', 'w') as configfile:
        config.write(configfile)
        logging.info('configuration saved')

# a method to check for the existence of paths 
def myCar_save_pathcreate(approot,directory):
    # first we need to know if directory starts with a /
    if directory.startswith("/"):
        path=directory
    else:
        path=approot + "/" + directory


    import logging
    from pathlib import Path
    try:
        Path(path).mkdir(parents=True,exist_ok=True)
        retval=True
        logging.info(path + " created")
    except PermissionError:
        # the case where the permissions are not met
        logging.warning(path + " could not be created")
        retval=False

    return retval
