[go: up one dir, main page]

Menu

[d66369]: / src / user_config.py  Maximize  Restore  History

Download this file

107 lines (77 with data), 4.1 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
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
# -*- coding: utf-8 -*-
'''
Copyright 2010 Vitaly Volkov
This file is part of Reggata.
Reggata is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Reggata is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Reggata. If not, see <http://www.gnu.org/licenses/>.
Created on 16.10.2010
@author: vlkv
'''
from pyjavaproperties import Properties
import consts
import os
import codecs
#TODO Кстати, можно было бы использовать класс QSettings
class UserConfig(object):
'''
Класс для упрощения доступа к содержимому конфиг-файла в домашней директории пользователя.
Этот класс реализован по паттерну одиночка (singleton pattern).
'''
_instance = None
_props = None
def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super(UserConfig, cls).__new__(cls, *args, **kwargs)
cls._props = Properties()
if not os.path.exists(consts.USER_CONFIG_DIR):
os.makedirs(consts.USER_CONFIG_DIR)
if not os.path.exists(consts.USER_CONFIG_FILE):
#Создаем файл
#os.mknod(consts.USER_CONFIG_FILE) #По всей видимости os.mknod() недоступна под Windows!
#Записываем в этот файл комментарий
reggata_conf = '''
# This is Reggata configuration file "reggata.conf".
# See "reggata.conf.template" for example of configuration.
'''
#Файл будет создан, если его еще нет
f = codecs.open(consts.USER_CONFIG_FILE, "w", "utf-8")
f.write(reggata_conf)
f.close()
#Если файла не существует, из load() вылетает исключение
cls._props.load(codecs.open(consts.USER_CONFIG_FILE, "r", "utf-8"))
return cls._instance
def __init__(self):
pass
def __getitem__(self, key):
'''Операция [], возвращает значение параметра с ключом key. Если ключ key не существует, то выкидывается исключение.'''
return self._props[key]
def get(self, key, default=None):
'''Возвращает значение параметра с ключом key. Если ключа key не существует, то
возвращает значение аргумента default. Если default не задан, то его значение по
умолчанию равно None.'''
return self._props.get(key, default)
def store(self, key, value):
self._props[key] = str(value)
self._props.store(codecs.open(consts.USER_CONFIG_FILE, 'w', "utf-8"))
def storeAll(self, d):
if type(d) != dict:
raise TypeError("This is not a dict instance.")
for key in d.keys():
self._props[key] = str(d[key])
self._props.store(codecs.open(consts.USER_CONFIG_FILE, 'w', "utf-8"))
def refresh(self):
self._props.load(codecs.open(consts.USER_CONFIG_FILE, "r", "utf-8"))
#Это тестовый код
if __name__ == "__main__":
uc1 = UserConfig()
uc2 = UserConfig()
print(uc1 == uc2)
print(uc1 is uc2)