<commit_before>from core import internals

import logging
import yaml
import argparse

import os
import sys


_LOG_LEVELS_STR = ['INFO', 'WARNING', 'ERROR', 'DEBUG']

default_conf = { 'spotify-downloader':
                 { 'manual'                 : False,
                   'no-metadata'            : False,
                   'avconv'                 : False,
                   'folder'                 : internals.get_music_dir(),
                   'overwrite'              : 'prompt',
                   'input-ext'              : '.m4a',
                   'output-ext'             : '.mp3',
                   'download-only-metadata' : False,
                   'dry-run'                : False,
                   'music-videos-only'      : False,
                   'no-spaces'              : False,
                   'file-format'            : '{artist} - {track_name}',
                   'search-format'          : '{artist} - {track_name} lyrics',
                   'youtube-api-key'        : None,
                   'log-level'              : 'INFO' }
               }


def log_leveller(log_level_str):
    loggin_levels = [logging.INFO, logging.WARNING, logging.ERROR, logging.DEBUG]
    log_level_str_index = _LOG_LEVELS_STR.index(log_level_str)
    loggin_level = loggin_levels[log_level_str_index]
    return loggin_level


def merge(default, config):
    """ Override default dict with config dict. """
    merged = default.copy()
    merged.update(config)
    return merged


def get_config(config_file):
    try:
        with open(config_file, 'r') as ymlfile:
            cfg = yaml.load(ymlfile)
    except FileNotFoundError:
        with open(config_file, 'w') as ymlfile:
            yaml.dump(default_conf, ymlfile, default_flow_style=False)
            cfg = default_conf

    return cfg['spotify-downloader']


def override_config(config_file, parser, raw_args=None):
    """ Override default dict with config dict passed as comamnd line argument. """
    config_file = os.path.realpath(config_file)
    config = merge(default_conf['spotify-downloader'], get_config(config_file))

    parser.set_defaults(manual=config['manual'])
    parser.set_defaults(no_metadata=config['no-metadata'])
    parser.set_defaults(avconv=config['avconv'])
    parser.set_defaults(folder=os.path.relpath(config['folder'], os.getcwd()))
    parser.set_defaults(overwrite=config['overwrite'])
    parser.set_defaults(input_ext=config['input-ext'])
    parser.set_defaults(output_ext=config['output-ext'])
    parser.set_defaults(download_only_metadata=config['download-only-metadata'])
    parser.set_defaults(dry_run=config['dry-run'])<commit_msg>First run config info (#273)<commit_after>from core import internals, const

log = const.log

import logging
import yaml
import argparse

import os
import sys


_LOG_LEVELS_STR = ['INFO', 'WARNING', 'ERROR', 'DEBUG']

default_conf = { 'spotify-downloader':
                 { 'manual'                 : False,
                   'no-metadata'            : False,
                   'avconv'                 : False,
                   'folder'                 : internals.get_music_dir(),
                   'overwrite'              : 'prompt',
                   'input-ext'              : '.m4a',
                   'output-ext'             : '.mp3',
                   'download-only-metadata' : False,
                   'dry-run'                : False,
                   'music-videos-only'      : False,
                   'no-spaces'              : False,
                   'file-format'            : '{artist} - {track_name}',
                   'search-format'          : '{artist} - {track_name} lyrics',
                   'youtube-api-key'        : None,
                   'log-level'              : 'INFO' }
               }


def log_leveller(log_level_str):
    loggin_levels = [logging.INFO, logging.WARNING, logging.ERROR, logging.DEBUG]
    log_level_str_index = _LOG_LEVELS_STR.index(log_level_str)
    loggin_level = loggin_levels[log_level_str_index]
    return loggin_level


def merge(default, config):
    """ Override default dict with config dict. """
    merged = default.copy()
    merged.update(config)
    return merged


def get_config(config_file):
    try:
        with open(config_file, 'r') as ymlfile:
            cfg = yaml.load(ymlfile)
    except FileNotFoundError:
        log.info('Writing default configuration to {0}:'.format(config_file))
        with open(config_file, 'w') as ymlfile:
            yaml.dump(default_conf, ymlfile, default_flow_style=False)
            cfg = default_conf

        for line in yaml.dump(default_conf['spotify-downloader'], default_flow_style=False).split('\n'):
            if line.strip():
                log.info(line.strip())
        log.info('Please note that command line arguments have higher priority '
                 'than their equivalents in the configuration file')

    return cfg['spotify-downloader']


def override_config(config_file, parser, raw_args=None):
    """ Override default dict with config dict passed as comamnd line argument. """
    config_file = os.path.realpath(config_file)
    config = merge(default_conf['spotify-downloader'], get_config(config_file))

    parser.set_defaults(manual=config['manual'])
    parser.set_defaults(no_metadata=config['no-metadata'])
    parser.set_defaults(avconv=config['avconv'])
    parser.set_defaults(folder=os.path.relpath(config['folder'], os.getcwd()))
    parser.set_defaults(overwrite=config['overwrite'])
    parser.set_defaults(input_ext=config['input-ext'])
    parser.set_defaults(output_ext=config['output-ext'])
    parser.set_defaults(download_only_metadata=config['download-only-metadata'])
    parser.set_defaults(dry_run=config['dry-run'])
{'fraction_non_alphanumeric': 0.11700468018720749, 'fraction_numerical': 0.0013867221355520888, 'mean_word_length': 2.172072567344695, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 15, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6149285', 'n_tokens_mistral': 1705, 'n_tokens_neox': 1624, 'n_words': 305}