anime-downloader/anime_downloader/config.py

64 lines
1.4 KiB
Python
Raw Normal View History

2018-05-31 04:04:47 -07:00
import click
import os
import errno
2018-06-02 11:18:05 -07:00
import json
2018-05-31 04:04:47 -07:00
2018-06-02 11:18:05 -07:00
APP_NAME = 'anime downloader'
APP_DIR = click.get_app_dir(APP_NAME)
2018-05-31 04:04:47 -07:00
DEFAULT_CONFIG = {
'dl': {
'url': False,
'player': None,
2018-06-02 11:18:05 -07:00
'skip_download': False,
2018-05-31 04:04:47 -07:00
'download_dir': '.',
'quality': '720p',
2018-06-02 11:18:05 -07:00
'force_download': False,
2018-06-02 12:11:43 -07:00
'log_level': 'INFO',
},
'watch': {
'quality': '720p',
'log_level': 'INFO',
2018-05-31 04:04:47 -07:00
}
}
2018-06-02 11:18:05 -07:00
class _Config:
CONFIG_FILE = os.path.join(APP_DIR, 'config.json')
def __init__(self):
try:
os.makedirs(APP_DIR)
except OSError as e:
if e.errno != errno.EEXIST:
raise
if not os.path.exists(self.CONFIG_FILE):
self._write_default_config()
self._CONFIG = DEFAULT_CONFIG
else:
self._CONFIG = self._read_config()
@property
def CONTEXT_SETTINGS(self):
return dict(
default_map=self._CONFIG
)
2018-06-09 08:24:42 -07:00
def __getitem__(self, attr):
return self._CONFIG[attr]
2018-06-02 11:18:05 -07:00
def _write_config(self, config_dict):
with open(self.CONFIG_FILE, 'w') as configfile:
json.dump(config_dict, configfile, indent=4, sort_keys=True)
2018-05-31 04:04:47 -07:00
2018-06-02 11:18:05 -07:00
def _read_config(self):
with open(self.CONFIG_FILE, 'r') as configfile:
conf = json.load(configfile)
return conf
2018-05-31 04:04:47 -07:00
2018-06-02 11:18:05 -07:00
def _write_default_config(self):
self._write_config(DEFAULT_CONFIG)
2018-05-31 04:04:47 -07:00
2018-06-02 11:18:05 -07:00
Config = _Config()