|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 | # coding:utf8 |
3 | | -import json |
| 3 | +import json |
4 | 4 | import sys |
5 | 5 | import pkg_resources |
6 | 6 |
|
| 7 | + |
7 | 8 | class Config: |
8 | 9 |
|
9 | | - def __init__(self, args): |
10 | | - if args.restore: |
11 | | - self.__restore() |
12 | | - return |
13 | | - |
14 | | - conf = self.__load() |
15 | | - self.suffix = args.suffix if args.suffix else conf['suffix'] |
16 | | - self.comment = args.comment if args.comment else conf['comment'] |
17 | | - self.ignore = args.ignore if args.ignore else conf['ignore'] |
| 10 | + def __init__(self): |
| 11 | + conf = self.__load() |
| 12 | + |
| 13 | + self.suffix = conf['suffix'] |
| 14 | + self.comment = conf['comment'] |
| 15 | + self.ignore = conf['ignore'] |
| 16 | + |
| 17 | + def invoke(self, args): |
| 18 | + if args.restore: |
| 19 | + self.__restore() |
| 20 | + else: |
| 21 | + if any([args.suffix_add, args.comment_add, args.ignore_add]): |
| 22 | + self.__append_config(args.suffix_add, args.comment_add, args.ignore_add) |
| 23 | + if any([args.suffix_reset, args.comment_reset, args.ignore_reset]): |
| 24 | + self.__reset_config(args.suffix_reset, args.comment_reset, args.ignore_reset) |
| 25 | + if args.list: |
| 26 | + self.show() |
| 27 | + |
| 28 | + def show(self): |
| 29 | + print(json.dumps(self.__dict__, indent=4)) |
| 30 | + |
| 31 | + def __confirm(self, tips): |
| 32 | + check = input(tips) |
| 33 | + return check.strip().lower() == 'y' |
| 34 | + |
| 35 | + def __append_config(self, suffix_add, comment_add, ignore_add): |
| 36 | + if suffix_add: |
| 37 | + if self.__confirm("'suffix' will be appended with {} (y/n)".format(suffix_add)): |
| 38 | + self.suffix.extend(suffix_add) |
| 39 | + if comment_add: |
| 40 | + if self.__confirm("'comment' will be appended with {} (y/n)".format(comment_add)): |
| 41 | + self.comment.extend(comment_add) |
| 42 | + if ignore_add: |
| 43 | + if self.__confirm("'ignore' will be appended with {} (y/n)".format(ignore_add)): |
| 44 | + self.ignore.extend(ignore_add) |
| 45 | + |
| 46 | + self.__update() |
18 | 47 |
|
19 | | - if any([args.suffix_add, args.comment_add, args.ignore_add]): |
20 | | - self.__append_config(args.suffix_add, args.comment_add, args.ignore_add) |
| 48 | + def __reset_config(self, suffix_reset, comment_reset, ignore_reset): |
| 49 | + if suffix_reset: |
| 50 | + if self.__confirm("'suffix' will be replaced with {} (y/n)".format(suffix_reset)): |
| 51 | + self.suffix = suffix_reset |
| 52 | + if comment_reset: |
| 53 | + if self.__confirm("'comment' will be replaced with {} (y/n)".format(comment_reset)): |
| 54 | + self.comment = comment_reset |
| 55 | + if ignore_reset: |
| 56 | + if self.__confirm("'ignore' will be replaced with {} (y/n)".format(ignore_reset)): |
| 57 | + self.ignore = ignore_reset |
21 | 58 |
|
22 | | - if any([args.suffix_save, args.comment_save, args.ignore_save]): |
23 | | - self.__save_config(args.suffix_save, args.comment_save, args.ignore_save) |
24 | | - |
25 | | - def show(self): |
26 | | - print(json.dumps(self.__dict__, indent=4)) |
27 | | - |
28 | | - def __confirm(self,tips): |
29 | | - check = input(tips) |
30 | | - return check.strip().lower() == 'y' |
| 59 | + self.__update() |
31 | 60 |
|
32 | | - def __append_config(self, suffix_add, comment_add, ignore_add): |
33 | | - if suffix_add: |
34 | | - if self.__confirm("'suffix' will be appended with {} (y/n)".format(suffix_add)): |
35 | | - self.suffix.extend(suffix_add) |
36 | | - if comment_add: |
37 | | - if self.__confirm("'comment' will be appended with {} (y/n)".format(comment_add)): |
38 | | - self.comment.extend(comment_add) |
39 | | - if ignore_add: |
40 | | - if self.__confirm("'ignore' will be appended with {} (y/n)".format(ignore_add)): |
41 | | - self.ignore.extend(ignore_add) |
| 61 | + def __load(self): |
| 62 | + filename = pkg_resources.resource_filename(__name__, 'config.json') |
| 63 | + with open(filename, 'r') as config: |
| 64 | + conf = json.load(config) |
| 65 | + return conf |
42 | 66 |
|
43 | | - self.__update() |
44 | | - |
45 | | - def __save_config(self, suffix_save, comment_save, ignore_save): |
46 | | - if suffix_save: |
47 | | - if self.__confirm("'suffix' will be replaced with {} (y/n)".format(suffix_save)): |
48 | | - self.suffix = suffix_save |
49 | | - if comment_save: |
50 | | - if self.__confirm("'comment' will be replaced with {} (y/n)".format(comment_save)): |
51 | | - self.comment = comment_save |
52 | | - if ignore_save: |
53 | | - if self.__confirm("'ignore' will be replaced with {} (y/n)".format(ignore_save)): |
54 | | - self.ignore = ignore_save |
| 67 | + def __update(self): |
| 68 | + filename = pkg_resources.resource_filename(__name__, 'config.json') |
| 69 | + with open(filename, 'w') as config: |
| 70 | + json.dump(self.__dict__, config, indent=4) |
55 | 71 |
|
56 | | - self.__update() |
57 | | - |
58 | | - def __load(self): |
59 | | - filename = pkg_resources.resource_filename(__name__, 'config.json') |
60 | | - with open(filename, 'r') as config: |
61 | | - conf = json.load(config) |
62 | | - return conf |
63 | | - |
64 | | - def __update(self): |
65 | | - filename = pkg_resources.resource_filename(__name__, 'config.json') |
66 | | - with open(filename, 'w') as config: |
67 | | - json.dump(self.__dict__, config, indent=4) |
68 | | - |
69 | | - def __restore(self): |
70 | | - self.suffix = ["c", "cc", "clj", "cpp", "cs", "cu", "cuh", "dart", "go", "h", |
71 | | - "hpp", "java", "jl", "js", "kt", "lisp", "lua", "pde", "m", "php", |
72 | | - "py", "R", "rb", "rs", "rust", "sh", "scala", "swift", "ts", "vb"] |
73 | | - self.comment = ["#", "//", "/*", "*", "*/", ":", ";", '""""'] |
74 | | - self.ignore = ["out", "venv", ".git", ".idea", "build", "target", "node_modules", ".vscode", "dist"] |
| 72 | + def __restore(self): |
| 73 | + self.suffix = ["c", "cc", "clj", "cpp", "cs", "cu", "cuh", "dart", "go", "h", |
| 74 | + "hpp", "java", "jl", "js", "kt", "lisp", "lua", "pde", "m", "php", |
| 75 | + "py", "R", "rb", "rs", "rust", "sh", "scala", "swift", "ts", "vb"] |
| 76 | + self.comment = ["#", "//", "/*", "*", "*/", ":", ";", '""""'] |
| 77 | + self.ignore = ["out", "venv", ".git", ".idea", "build", "target", "node_modules", ".vscode", "dist"] |
75 | 78 |
|
76 | | - if self.__confirm('Default configuration will be restored (y/n)?'): |
77 | | - self.__update() |
| 79 | + if self.__confirm('Default configuration will be restored (y/n)?'): |
| 80 | + self.__update() |
0 commit comments