- if not self.has_facet(facet) or not self.get(facet) == value:
- if self.old_style:
- if self.key not in self.origin.data:
- self.origin.data[self.key] = {}
- self.origin.data[self.key][facet] = value
- else:
- node = ".".join((self.key, facet))
- self.origin.data[node] = value
- self.facethash[node] = self.origin.data[node]
+ if not self.origin.is_writeable() and not self.universe.loading:
+ # break if there is an attempt to update an element from a
+ # read-only file, unless the universe is in the midst of loading
+ # updated data from files
+ raise PermissionError("Altering elements in read-only files is "
+ "disallowed")
+ # Coerce some values to appropriate data types
+ # TODO(fungi) Move these to a separate validation mechanism
+ if facet in ["loglevel"]:
+ value = int(value)
+ elif facet in ["administrator"]:
+ value = bool(value)
+
+ # The canonical node for this facet within its origin
+ node = ".".join((self.key, facet))
+
+ if node not in self.origin.data or self.origin.data[node] != value:
+ # Be careful to only update the origin's contents when required,
+ # since that affects whether the backing file gets written
+ self.origin.data[node] = value