PCN: allow adding new properties at run-time

This commit is contained in:
miruka 2020-11-14 11:29:46 -04:00
parent 9348df32f7
commit fbd3f6f4a2

View File

@ -81,16 +81,15 @@ class Section(MutableMapping):
self._edited[name] = value self._edited[name] = value
return return
if name in self.properties: if name in self.sections or isinstance(value, Section):
return raise NotImplementedError(f"cannot set section {name!r}")
if name in self.sections: if name in self.methods or callable(value):
raise NotImplementedError(f"cannot overwrite section {name!r}") raise NotImplementedError(f"cannot set method {name!r}")
if name in self.methods: self._set_property(name, "Any", "None")
raise NotImplementedError(f"cannot overwrite method {name!r}") getattr(type(self), name).value_override = value
self._edited[name] = value
raise NotImplementedError(f"cannot add new attribute {name!r}")
def __delattr__(self, name: str) -> None: def __delattr__(self, name: str) -> None:
@ -307,16 +306,17 @@ class Section(MutableMapping):
def deep_merge_edits( def deep_merge_edits(
self, edits: Dict[str, Any], has_expressions: bool = True, self, edits: Dict[str, Any], has_expressions: bool = True,
) -> None: ) -> bool:
changes = False
if not self.parent: # this is Root if not self.parent: # this is Root
edits = edits.get("set", {}) edits = edits.get("set", {})
for name, value in edits.items(): for name, value in edits.copy().items():
if name not in self: if isinstance(self.get(name), Section) and isinstance(value, dict):
continue if self[name].deep_merge_edits(value, has_expressions):
changes = True
if isinstance(self[name], Section) and isinstance(value, dict):
self[name].deep_merge_edits(value, has_expressions)
elif not has_expressions: elif not has_expressions:
self[name] = value self[name] = value
@ -324,8 +324,16 @@ class Section(MutableMapping):
elif isinstance(value, (tuple, list)): elif isinstance(value, (tuple, list)):
user_expression, gui_value = value user_expression, gui_value = value
if getattr(type(self), name).expression == user_expression: if not hasattr(type(self), name):
self[name] = gui_value self[name] = gui_value
elif getattr(type(self), name).expression == user_expression:
self[name] = gui_value
else:
# If user changed their config file, discard the GUI edit
del edits[name]
changes = True
return changes
@classmethod @classmethod