<commit_before># coding=utf-8
from __future__ import unicode_literals
import os
import sys

try:
    import ConfigParser as configparser
except ImportError:
    import configparser


__all__ = ('DMHY_URL', 'BGMI_PATH', 'DB_PATH', 'BGMI_SAVE_PATH',
           'BGMI_LX_PATH', 'DOWNLOAD_DELEGATE', 'CONFIG_FILE_PATH',
           'DETAIL_URL', 'FETCH_URL', 'IS_PYTHON3', 'MAX_PAGE',
           'BGMI_TMP_PATH', 'ARIA2_PATH')

__readonly__ = ('BGMI_PATH', 'DB_PATH', 'CONFIG_FILE_PATH',
                'IS_PYTHON3', 'DETAIL_URL', 'FETCH_URL')

__writeable__ = tuple([i for i in __all__ if i not in __readonly__])


# --------- Immutable ---------- #
BGMI_PATH = os.path.join(os.environ.get('HOME', '/tmp'), '.bgmi')
DB_PATH = os.path.join(BGMI_PATH, 'bangumi.db')
CONFIG_FILE_PATH = os.path.join(BGMI_PATH, 'bgmi.cfg')


def read_config():
    c = configparser.ConfigParser()
    if not os.path.exists(CONFIG_FILE_PATH):
        write_default_config()
        return
    c.read(CONFIG_FILE_PATH)

    for i in __writeable__:
        if c.has_option('bgmi', i):
            globals().update({i: c.get('bgmi', i)})


def print_config():
    c = configparser.ConfigParser()
    if not os.path.exists(CONFIG_FILE_PATH):
        return
    c.read(CONFIG_FILE_PATH)
    for i in __writeable__:
        print('{0}={1}'.format(i, c.get('bgmi', i)))


def write_default_config():
    c = configparser.ConfigParser()
    if not c.has_section('bgmi'):
        c.add_section('bgmi')

    for i in __writeable__:
        v = globals().get(i, None)
        c.set('bgmi', i, v)

    c.write(open(CONFIG_FILE_PATH, 'w'))


def write_config(config=None, value=None):
    c = configparser.ConfigParser()
    c.read(CONFIG_FILE_PATH)

    if not c.has_section('bgmi'):
        c.add_section('bgmi')

    if config is not None and config not in __writeable__:
        print('{0} is not exist or not writeable'.format(config))
        exit(1)

    for i in __writeable__:
        v = globals().get(i, None)
        c.set('bgmi', i, v)
<commit_msg>fix bug of config.py<commit_after># coding=utf-8
from __future__ import unicode_literals
import os
import sys

try:
    import ConfigParser as configparser
except ImportError:
    import configparser


__all__ = ('DMHY_URL', 'BGMI_PATH', 'DB_PATH', 'BGMI_SAVE_PATH',
           'BGMI_LX_PATH', 'DOWNLOAD_DELEGATE', 'CONFIG_FILE_PATH',
           'DETAIL_URL', 'FETCH_URL', 'IS_PYTHON3', 'MAX_PAGE',
           'BGMI_TMP_PATH', 'ARIA2_PATH')

__readonly__ = ('BGMI_PATH', 'DB_PATH', 'CONFIG_FILE_PATH',
                'IS_PYTHON3', 'DETAIL_URL', 'FETCH_URL')

__writeable__ = tuple([i for i in __all__ if i not in __readonly__])


# --------- Immutable ---------- #
BGMI_PATH = os.path.join(os.environ.get('HOME', '/tmp'), '.bgmi')
DB_PATH = os.path.join(BGMI_PATH, 'bangumi.db')
CONFIG_FILE_PATH = os.path.join(BGMI_PATH, 'bgmi.cfg')


def read_config():
    c = configparser.ConfigParser()
    if not os.path.exists(CONFIG_FILE_PATH):
        write_default_config()
        return
    c.read(CONFIG_FILE_PATH)

    for i in __writeable__:
        if c.has_option('bgmi', i):
            globals().update({i: c.get('bgmi', i)})


def print_config():
    c = configparser.ConfigParser()
    if not os.path.exists(CONFIG_FILE_PATH):
        return
    c.read(CONFIG_FILE_PATH)
    for i in __writeable__:
        print('{0}={1}'.format(i, c.get('bgmi', i)))


def write_default_config():
    c = configparser.ConfigParser()
    if not c.has_section('bgmi'):
        c.add_section('bgmi')

    for i in __writeable__:
        v = globals().get(i, None)
        c.set('bgmi', i, v)

    try:
        c.write(open(CONFIG_FILE_PATH, 'w'))
    except IOError:
        print('[-] write config file error and ignored')


def write_config(config=None, value=None):
    c = configparser.ConfigParser()
    c.read(CONFIG_FILE_PATH)

    if not c.has_section('bgmi'):
        c.add_section('bgmi')

    if config is not None and config not in __writeable__:
        print('{0} is not exist or not writeable'.format(config))
        exit(1)

    for i in __writeable__:
        v = globals().get(i, None)
        c.set('bgmi', i, v)

{'fraction_non_alphanumeric': 0.14627081824764662, 'fraction_numerical': 0.003861935795317403, 'mean_word_length': 3.062745098039216, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4574759', 'n_tokens_mistral': 1517, 'n_tokens_neox': 1476, 'n_words': 325}