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.
62 lines
1.4 KiB
62 lines
1.4 KiB
|
|
from panflute import Doc
|
|
|
|
class Context:
|
|
def __init__(self, doc: Doc=None, parent: 'Context'=None):
|
|
self.parent = parent
|
|
self._commands = {}
|
|
self._flags = {}
|
|
self.doc = doc
|
|
|
|
def get_command(self, command: str):
|
|
if command in self._commands:
|
|
return self._commands[command]
|
|
elif self.parent:
|
|
return self.parent.get_command(command)
|
|
else:
|
|
return None
|
|
|
|
def set_command(self, command: str, val):
|
|
self._commands[command] = val
|
|
|
|
def unset_command(self, command: str):
|
|
del self._commands[command]
|
|
|
|
def is_flag_set(self, flag: str):
|
|
if flag in self._flags and self._flags[flag]:
|
|
return True
|
|
elif self.parent:
|
|
return self.parent.is_set(flag)
|
|
else:
|
|
return False
|
|
|
|
def set_flag(self, flag: str, val: bool):
|
|
self._flags[flag] = val
|
|
|
|
def unset_flag(self, flag):
|
|
del self._flags[flag]
|
|
|
|
def get_metadata(self, key, simple=True):
|
|
value = self.doc.get_metadata(key, None, simple)
|
|
if value is not None:
|
|
return value
|
|
elif self.parent:
|
|
return self.parent.get_metadata(key)
|
|
else:
|
|
return None
|
|
|
|
def set_metadata(self, key, value):
|
|
meta = self.doc.metadata
|
|
key = key.split(".")
|
|
for k in key[:-1]:
|
|
meta = meta[k]
|
|
meta[key[-1]] = value
|
|
|
|
def unset_metadata(self, key):
|
|
meta = self.doc.metadata
|
|
key = key.split(".")
|
|
for k in key[:-1]:
|
|
meta = meta[k]
|
|
print(type(meta))
|
|
del meta.content[key[-1]] # A hack because MetaMap doesn't have a __delitem__
|
|
|
|
|