|
12 | 12 | CONFIG_NAME = appname.lower() + '.cfg' |
13 | 13 | CONFIG_DIR = appdirs.user_config_dir(appname.lower()) |
14 | 14 | CONFIG_PATH = path.join(CONFIG_DIR, CONFIG_NAME) |
| 15 | +CONFIG_VERSION = 1 |
15 | 16 |
|
16 | 17 | if not path.exists(CONFIG_DIR): |
17 | 18 | makedirs(CONFIG_DIR, 0o700) |
@@ -71,20 +72,43 @@ def get(self, section, option, default='dontguessthis'): |
71 | 72 | return value |
72 | 73 |
|
73 | 74 |
|
74 | | -def migrate_config(config): |
75 | | - if config.get('Imgur', 'client_id', None) is not None: |
76 | | - log.notice('Migrating config.') |
| 75 | +def backup(config): |
| 76 | + from datetime import datetime |
| 77 | + t = datetime.now() |
77 | 78 |
|
78 | | - from datetime import datetime |
79 | | - t = datetime.now() |
80 | | - p = config.config_path |
81 | | - config.config_path = p + "." + t.strftime("%Y-%m-%dT%H-%M-%S") + '.bak' |
82 | | - config._write() |
83 | | - log.notice('Old config backed up at {}', config.config_path) |
84 | | - config.config_path = p |
| 79 | + p = config.config_path |
| 80 | + config.config_path = (config.config_path + "." + |
| 81 | + t.strftime("%Y-%m-%dT%H-%M-%S") + '.bak') |
| 82 | + config._write() |
| 83 | + log.notice('Old config backed up at {}', config.config_path) |
| 84 | + config.config_path = p |
85 | 85 |
|
| 86 | + |
| 87 | +def imgur_api_change(config): |
| 88 | + if config.get('Imgur', 'client_id', None) is not None: |
86 | 89 | config._config.remove_section('Imgur') |
87 | 90 | config._write() |
| 91 | + else: |
| 92 | + log.warning('section already removed') |
| 93 | + |
| 94 | + |
| 95 | +def migrate_config(config): |
| 96 | + migrations = {0: (1, imgur_api_change)} |
| 97 | + version_args = lambda v: ('General', 'version', v) # noqa: E731 |
| 98 | + |
| 99 | + cur = int(config.get(*version_args(0))) |
| 100 | + if cur in migrations: |
| 101 | + backup(config) |
| 102 | + while True: |
| 103 | + cur = int(config.get(*version_args(0))) |
| 104 | + try: |
| 105 | + new, mig = migrations[cur] |
| 106 | + except KeyError: |
| 107 | + break |
| 108 | + else: |
| 109 | + log.notice('Migrating config from {} to {}'.format(cur, new)) |
| 110 | + mig(config) |
| 111 | + config.set(*version_args(str(new))) |
88 | 112 |
|
89 | 113 |
|
90 | 114 | config = Config() |
|
0 commit comments