ZFS Automatic Snapshot Daemon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

82 lines
2.5 KiB

''' Interactive Python interpreter '''
from typing import MutableMapping, Set, Any
import sys
import re
import ptpython.repl
import zasd.entrypoint
import zasd.config as config
from zasd.filesystem import FilesystemRegistry
async def repl():
''' Launch an asynchronous ptpython REPL '''
glo: MutableMapping = dict(
sys = sys,
config = DictObject(config.reference(), 'config'),
registry = FilesystemRegistry)
loc: MutableMapping = dict()
def configure(ptrepl):
ptrepl.confirm_exit = False
ptrepl.highlight_matching_parenthesis = True
ptrepl.insert_blank_line_after_output = False
try:
await ptpython.repl.embed(
globals=glo,
locals=loc,
configure=configure,
return_asyncio_coroutine=True,
patch_stdout=True)
except EOFError:
zasd.entrypoint.stop()
class DictObject():
''' Class for wrapping dictionaries in objects '''
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) -> Set[str]:
return DictObject._dictionary.keys()
def __repr__(self) -> str:
return DictObject.serialise(DictObject._dictionary, DictObject._name)
@staticmethod
def serialise(obj, name, level=0, path=None) -> str:
''' Turn an object into a string recursively. '''
if not isinstance(obj, dict):
# Output variables in 'object.name = value' format
repstr = repr(obj)
if re.search(r'^<.+>$', repstr):
repstr = repstr[1:-1]
return '{}.{} = {}'.format(name, '.'.join(path or []), repstr)
else:
# Separate dictionary entries with newlines
keys = sorted(obj.keys())
strs = list()
for key in keys:
value = DictObject.serialise(
obj[key], name, level + 1, path = (path or []) + [key])
strs.append(value)
return '\n'.join(strs)