Commit changes

oop-refactor
Thor 4 years ago
parent 4086e22bce
commit 75c3913563
  1. 3
      src/zasd/config.py
  2. 10
      src/zasd/config_loader.py
  3. 53
      src/zasd/repl.py

@ -21,3 +21,6 @@ def get(*keys: str, default: Any = None, cursor: Mapping = None):
return (get(rest, default=default, cursor=value)
if value is not None and rest else value)
def reference():
return _config

@ -142,11 +142,11 @@ async def load_config():
log.debug('Loaded configuration')
if _config.get('log_level') <= logging.DEBUG:
log.debug('')
for line in pprint.pformat(_config).split('\n'):
logging.debug(_config.get('tab_size') * ' ' + line)
log.debug('')
#if _config.get('log_level') <= logging.DEBUG:
log.debug('')
for line in pprint.pformat(_config.reference()).split('\n'):
logging.debug(_config.get('tab_size') * ' ' + line)
log.debug('')
async def _warn_default():
log.warning('')

@ -1,11 +1,62 @@
''' Interactive Python interpreter '''
from typing import MutableMapping, Any
import asyncio
import ptpython.repl
import re
import zasd.config as config
class DictObject:
def __init__(self, dictionary: MutableMapping[str, Any], name):
DictObject._dictionary = dictionary
DictObject._name = name
def __getattribute__(self, name):
if not name in DictObject._dictionary:
raise AttributeError("name '{}' is not defined".format(name))
return DictObject._dictionary[name]
def __setattr__(self, name, value):
DictObject._dictionary[name] = value
def __delattr__(self, name):
if not name in DictObject._dictionary:
raise AttributeError("name {} is not defined".format(name))
del self._dictionary[name]
def __dir__(self):
return DictObject._dictionary.keys()
def __repr__(self):
return DictObject.make_repr(DictObject._dictionary, DictObject._name)
@staticmethod
def make_repr(obj, name, level=0, path=[]):
if not isinstance(obj, dict):
repstr = repr(obj)
if re.search(r'^<.+>$', repstr):
repstr = repstr[1:-1]
return '{}.{} = {}'.format(name, '.'.join(path), repstr)
else:
keys = sorted(obj.keys())
strs = list()
for key in keys:
value = DictObject.make_repr(
obj[key], name, level + 1, path = path + [key])
strs.append(value)
return '\n'.join(strs)
@asyncio.coroutine
def repl():
globals = dict(
config = DictObject(config.reference(), 'config')
)
locals = dict()
try:
yield from ptpython.repl.embed(
globals=globals(), locals=(), return_asyncio_coroutine=True,
globals=globals, locals=locals, return_asyncio_coroutine=True,
patch_stdout=True, vi_mode=True)
except EOFError:
asyncio.get_event_loop().stop()
Loading…
Cancel
Save