diff --git a/src/zasd/config.py b/src/zasd/config.py index d8e3628..af783d5 100644 --- a/src/zasd/config.py +++ b/src/zasd/config.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 \ No newline at end of file diff --git a/src/zasd/config_loader.py b/src/zasd/config_loader.py index c601fd7..7f74c91 100644 --- a/src/zasd/config_loader.py +++ b/src/zasd/config_loader.py @@ -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('') diff --git a/src/zasd/repl.py b/src/zasd/repl.py index f747a9c..f704f6e 100644 --- a/src/zasd/repl.py +++ b/src/zasd/repl.py @@ -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() \ No newline at end of file