Source code for aj.config

import os
import pwd
import stat
import yaml
from jadi import service

[docs]class BaseConfig(object): """ A base class for config implementations. Your implementation must be able to save arbitrary mixture of ``dict``, ``list``, and scalar values. .. py:attribute:: data currenly loaded config content """ def __init__(self): = None
[docs] def load(self): """ Should load config content into :attr:`data`. """ raise NotImplementedError()
[docs] def save(self): """ Should save config content from :attr:`data`. """ raise NotImplementedError()
[docs] def ensure_structure(self):'name', None)'max_sessions', 99)'language', 'en')'restricted_user', 'nobody')'auth', {})['auth'].setdefault('emails', {})['auth'].setdefault('provider', 'os')'ssl', {})['ssl'].setdefault('enable', False)['ssl'].setdefault('certificate', None)['ssl'].setdefault('client_auth', {})['ssl']['client_auth'].setdefault('enable', False)['ssl']['client_auth'].setdefault('force', False)['ssl']['client_auth'].setdefault('certificates', {})
[docs]class UserConfig(BaseConfig): def __init__(self, context): BaseConfig.__init__(self) username = pwd.getpwuid(os.getuid())[0] _dir = os.path.expanduser('~%s/.config' % username) if not os.path.exists(_dir): os.makedirs(_dir) self.path = os.path.join(_dir, 'ajenti.yml') if os.path.exists(self.path): self.load() else: = {}
[docs] def load(self): = yaml.load(open(self.path))
[docs] def harden(self): os.chmod(self.path, stat.S_IRWXU)
[docs] def save(self): with open(self.path, 'w') as f: f.write(yaml.safe_dump(, default_flow_style=False, encoding='utf-8', allow_unicode=True )) self.harden()


comments powered by Disqus