|
1 | | -''' |
2 | | -@author: Kirill Python |
3 | | -@contact: http://vk.com/python273 |
4 | | -@license Apache License, Version 2.0, see LICENSE file |
5 | | -
|
6 | | -Copyright (C) 2013 |
7 | | -''' |
8 | | - |
9 | | -# -*- coding: utf-8 -*- |
10 | | -import os |
11 | | -import json |
12 | | - |
13 | | - |
14 | | -class config: |
15 | | - def __init__(self, section, filename='config'): |
16 | | - self.section = section # Секция настроек |
17 | | - self.filename = filename # Файл с настройками |
18 | | - self.all = self.parse() # Все настройки |
19 | | - self.settings = self.all.get(section, {}) # Настройки секции |
20 | | - |
21 | | - def __getitem__(self, item): |
22 | | - return self.settings.get(item, {}) |
23 | | - |
24 | | - def __setitem__(self, key, value): |
25 | | - self.settings.update({key: value}) |
26 | | - self.all.update({self.section: self.settings}) |
27 | | - self.update(self.all) |
28 | | - |
29 | | - def parse(self): |
30 | | - fileis = os.path.exists(self.filename) |
31 | | - if fileis: |
32 | | - settings = json.load(open(self.filename, 'r')) |
33 | | - return settings |
34 | | - else: |
35 | | - self.update() |
36 | | - return {} |
37 | | - |
38 | | - def update(self, settings=None): |
39 | | - if not settings: |
40 | | - settings = {} |
41 | | - |
42 | | - json.dump(settings, open(self.filename, 'w')) |
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +''' |
| 4 | +@author: Kirill Python |
| 5 | +@contact: http://vk.com/python273 |
| 6 | +@license Apache License, Version 2.0, see LICENSE file |
| 7 | +
|
| 8 | +Copyright (C) 2013 |
| 9 | +''' |
| 10 | + |
| 11 | +import os |
| 12 | +import json |
| 13 | + |
| 14 | + |
| 15 | +class Config(object): |
| 16 | + def __init__(self, section, filename='config'): |
| 17 | + self.section = section # Секция настроек |
| 18 | + self.filename = filename # Файл с настройками |
| 19 | + self.all = self.parse() # Все настройки |
| 20 | + self.settings = self.all.get(section, {}) # Настройки секции |
| 21 | + |
| 22 | + def __getitem__(self, item): |
| 23 | + return self.settings.get(item, {}) |
| 24 | + |
| 25 | + def __setitem__(self, key, value): |
| 26 | + self.settings.update({key: value}) |
| 27 | + self.all.update({self.section: self.settings}) |
| 28 | + self.update(self.all) |
| 29 | + |
| 30 | + def parse(self): |
| 31 | + fileis = os.path.exists(self.filename) |
| 32 | + if fileis: |
| 33 | + settings = json.load(open(self.filename, 'r')) |
| 34 | + return settings |
| 35 | + else: |
| 36 | + self.update() |
| 37 | + return {} |
| 38 | + |
| 39 | + def update(self, settings=None): |
| 40 | + if not settings: |
| 41 | + settings = {} |
| 42 | + |
| 43 | + json.dump(settings, open(self.filename, 'w')) |
0 commit comments