# -*- coding: utf-8 -*-
# YOU NEED TO COPY THIS FILE TO config.py so that the program uses those values here
from __future__ import print_function
import os


config = {}

def updateConfig(newConfig):
    global config
    for k in newConfig:
        config[k] = newConfig[k]


def loadConfig():
    # caches are only used temporarily - if a download fails, sometimes it can help to delete
    # all cachedata - the only negative point in deleting all cachedata is, that the program may be a bit slower on next start
    cache_dir = 'cache'             # here we write normal html-downloads
    cache_dir_for_flv = 'cache2'    # here we write flashdownloads

    flash_dir = 'flash' # all finished flashdownloads go in this directory and will be deleted from the cachedir


    dl_instances = 6  # how many parallel downloads you will start

    # VALUES below this aren't interesting for you

    # dl_* can be changed through config and use those values then as default
    dl_title = 'tmp'
    dl_name = 'tmp'

    link = None # you can set a default url as starturl.. but commandline-option will overwrite this var

    extractStart = 0 # how many media files should be skipped when using extract all
    extractAmount = 999999 # how many media files should be extracted when using extract all

    cachePort = 0 #9123
    preferHypertable=False
    preferFileCache = False

    config = {
        'cache_dir': cache_dir,
        'cache_dir_for_flv': cache_dir_for_flv,
        'flash_dir': flash_dir,
        'dl_instances': dl_instances,
        'dl_title': dl_title,
        'dl_name': dl_name,
        'link': link,
        'extractStart': extractStart,
        'extractAmount': extractAmount,
        'cachePort': cachePort,
        'preferHypertable': preferHypertable,
        'preferFileCache': preferFileCache,
            }
    updateConfig(config)
    return config

# -*- coding: utf-8 -*-
try:
    import ConfigParser as configparser
except ImportError:
    import configparser
import os


config = {}

def updateConfig(newConfig):
    global config
    for k in newConfig:
        if k in ['cache_dir', 'cache_dir_for_flv', 'flash_dir']:
            config[k] = os.path.expanduser(newConfig[k])
        else:
            config[k] = newConfig[k]

def loadConfig():
    global config
    configFiles = [os.path.join('etc', 'flashget.cfg'), os.path.expanduser(os.path.join('~', '.flashget.cfg')), '.flashget.cfg']
    if not os.path.isfile(configFiles[1]):
        createConfigFile(configFiles[1])
    for filepath in configFiles:
        if os.path.isfile(filepath):
            configP = configparser.SafeConfigParser(allow_no_value = True)
            configP.read(filepath)
            # TODO parse to int and boolean
            updateConfig(dict(configP.items("DEFAULT")))
    return config

def createConfigFile(path):
    config = configparser.SafeConfigParser(allow_no_value = True)
    config.add_section('')
    config.set('', '; temporary caches for http-data - you have to clean them by hand since flashget won\'t purge them')
    config.set('', '; cache for html pages')
    config.set('', 'cache_dir', os.path.join('~', 'flashget', 'cacheHtml'))
    config.set('', '; cache for the large streamdownloads')
    config.set('', 'cache_dir_for_flv', os.path.join('~', 'flashget', 'cacheFlv'))
    config.set('', '; all finished flashdownloads go in this directory and will be deleted from the cachedir')
    config.set('', 'flash_dir', os.path.join('~', 'flashget', 'downloads'))
    config.set('', '; how many downloads will be processed in parallel')
    config.set('', '; this only works for one instance of this program')
    config.set('', 'dl_instances', "6")
    config.set('', '; default download title, if you download directly from a stream - but better use commandline argument')
    config.set('', 'dl_title', "tmp")
    config.set('', '; default download name, if you download directly from a stream - but better use commandline argument')
    config.set('', 'dl_name', "tmp")
    config.set('', '; default url from where to start - let it be ;)')
    config.set('', 'link', None)
    config.set('', '; how many media files should be skipped when using extract all')
    config.set('', 'extractStart', "0")
    config.set('', '; how many media files should be extracted when using extract all')
    config.set('', 'extractAmount', "999999")
    config.set('', '; if you run the caching as client/server (when you have multiple parallel instances) - configure the port here')
    config.set('', 'cachePort', "0")
    config.set('', 'preferHypertable', "False")
    config.set('', 'preferFileCache', "False")

    with open(path, 'w+') as fp:
        config.write(fp)

use configparser for config