first commit
This commit is contained in:
14
jconfig/__init__.py
Normal file
14
jconfig/__init__.py
Normal file
@@ -0,0 +1,14 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
:authors: python273
|
||||
:license: Apache License, Version 2.0, see LICENSE file
|
||||
|
||||
:copyright: (c) 2019 python273
|
||||
"""
|
||||
|
||||
__author__ = 'python273'
|
||||
__version__ = '3.0'
|
||||
__email__ = 'vk_api@python273.pw'
|
||||
|
||||
from .jconfig import Config
|
||||
from .memory import MemoryConfig
|
51
jconfig/base.py
Normal file
51
jconfig/base.py
Normal file
@@ -0,0 +1,51 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
:authors: python273
|
||||
:license: Apache License, Version 2.0, see LICENSE file
|
||||
|
||||
:copyright: (c) 2019 python273
|
||||
"""
|
||||
|
||||
|
||||
class BaseConfig(object):
|
||||
""" Абстрактный базовый класс конфигурации.
|
||||
У наследуемых классов должен быть определен `__slots__`
|
||||
|
||||
:param section: имя подкатегории в конфиге
|
||||
:param \*\*kwargs: будут переданы в :func:`load`
|
||||
"""
|
||||
|
||||
__slots__ = ('section_name', '_settings', '_section')
|
||||
|
||||
def __init__(self, section, **kwargs):
|
||||
self.section_name = section
|
||||
|
||||
self._settings = self.load(**kwargs)
|
||||
self._section = self._settings.setdefault(section, {})
|
||||
|
||||
def __getattr__(self, name):
|
||||
return self._section.get(name)
|
||||
|
||||
__getitem__ = __getattr__
|
||||
|
||||
def __setattr__(self, name, value):
|
||||
try:
|
||||
super(BaseConfig, self).__setattr__(name, value)
|
||||
except AttributeError:
|
||||
self._section[name] = value
|
||||
|
||||
__setitem__ = __setattr__
|
||||
|
||||
def setdefault(self, k, d=None):
|
||||
return self._section.setdefault(k, d)
|
||||
|
||||
def clear_section(self):
|
||||
self._section.clear()
|
||||
|
||||
def load(self, **kwargs):
|
||||
"""Абстрактный метод, должен возвращать dict с конфигом"""
|
||||
raise NotImplementedError
|
||||
|
||||
def save(self):
|
||||
"""Абстрактный метод, должен сохранять конфиг"""
|
||||
raise NotImplementedError
|
41
jconfig/jconfig.py
Normal file
41
jconfig/jconfig.py
Normal file
@@ -0,0 +1,41 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
:authors: python273
|
||||
:license: Apache License, Version 2.0, see LICENSE file
|
||||
|
||||
:copyright: (c) 2019 python273
|
||||
"""
|
||||
|
||||
|
||||
import json
|
||||
|
||||
from .base import BaseConfig
|
||||
|
||||
|
||||
class Config(BaseConfig):
|
||||
""" Класс конфигурации в файле
|
||||
|
||||
:param filename: имя файла
|
||||
"""
|
||||
|
||||
__slots__ = ('_filename',)
|
||||
|
||||
def __init__(self, section, filename='.jconfig'):
|
||||
self._filename = filename
|
||||
|
||||
super(Config, self).__init__(section, filename=filename)
|
||||
|
||||
def load(self, filename, **kwargs):
|
||||
try:
|
||||
with open(filename, 'r') as f:
|
||||
settings = json.load(f)
|
||||
except (IOError, ValueError):
|
||||
settings = {}
|
||||
|
||||
settings.setdefault(self.section_name, {})
|
||||
|
||||
return settings
|
||||
|
||||
def save(self):
|
||||
with open(self._filename, 'w') as f:
|
||||
json.dump(self._settings, f, indent=2, sort_keys=True)
|
24
jconfig/memory.py
Normal file
24
jconfig/memory.py
Normal file
@@ -0,0 +1,24 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
:authors: python273
|
||||
:license: Apache License, Version 2.0, see LICENSE file
|
||||
|
||||
:copyright: (c) 2019 python273
|
||||
"""
|
||||
|
||||
from .base import BaseConfig
|
||||
|
||||
|
||||
class MemoryConfig(BaseConfig):
|
||||
""" Класс конфигурации в памяти
|
||||
|
||||
:param settings: существующий dict с конфигом
|
||||
"""
|
||||
|
||||
__slots__ = tuple()
|
||||
|
||||
def load(self, settings=None, **kwargs):
|
||||
return {} if settings is None else settings
|
||||
|
||||
def save(self):
|
||||
pass
|
Reference in New Issue
Block a user