forked from mueslo/pythonBits
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
79 lines (58 loc) · 2.32 KB
/
Copy pathconfig.py
File metadata and controls
79 lines (58 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# -*- coding: utf-8 -*-
from __future__ import (absolute_import, division,
print_function, unicode_literals)
from builtins import * # noqa: F401, F403
from io import open
from os import path, chmod, makedirs
import configparser
import getpass
import appdirs
from . import __title__ as appname
CONFIG_NAME = appname.lower() + '.cfg'
CONFIG_DIR = appdirs.user_config_dir(appname.lower())
CONFIG_PATH = path.join(CONFIG_DIR, CONFIG_NAME)
if not path.exists(CONFIG_DIR):
makedirs(CONFIG_DIR, 0o700)
class Config():
registry = {}
def __init__(self, config_path=None):
self.config_path = config_path or CONFIG_PATH
self._config = configparser.ConfigParser()
def register(self, section, option, query, ask=False, getpass=False):
self.registry[(section, option)] = {
'query': query, 'ask': ask, 'getpass': getpass}
# todo: ask to remember choice if save is declined
def _write(self):
with open(self.config_path, 'w') as configfile:
self._config.write(configfile)
chmod(self.config_path, 0o600)
def set(self, section, option, value):
self._config.set(section, option, value)
self._write()
def get(self, section, option, default='dontguessthis'):
self._config.read(self.config_path)
try:
return self._config.get(section, option)
except (configparser.NoSectionError, configparser.NoOptionError):
try:
reg_option = self.registry[(section, option)]
except KeyError:
if default != 'dontguessthis': # dirty hack
return default
else:
raise
if reg_option['getpass']:
value = getpass.getpass(reg_option['query'] + ": ")
else:
value = input(reg_option['query'] + ": ").strip()
if (reg_option['ask'] and
input(
'Would you like to save this value in {}? [Y/n] '.format(
self.config_path)).lower() == 'n'):
return value
if not self._config.has_section(section):
self._config.add_section(section)
self._config.set(section, option, value)
self._write()
return value
config = Config()