예제 #1
0
    def getDocumentation(self) -> str:
        """Get a formatted version of the documentation"""
        meta_tree_node = self.getMetaTreeNode()
        if meta_tree_node._properties is None:
            raise TreeException(meta_tree_node, "Missing properties")
        if meta_tree_node._properties._doc is None:
            raise TreeException(meta_tree_node, "Missing documentation")

        return meta_tree_node._properties._doc.format_(self)
예제 #2
0
 def getPath(self) -> str:
     """Return the formatted tree path of the node"""
     if self._parent is not None:
         return "%s.%s" % (self._parent.getPath(), self._name)
     if self._name is None:
         raise TreeException(self, "Encountered an unnamed node")
     return self._name
예제 #3
0
 def validate(self, value):
     # type: (Any) -> None
     if not isinstance(value._value, self._type):
         raise TreeException(
             value,
             "Value type mismatch: Expected %s, obtained %s" %
             (str(self._type), str(type(value._value))),
         )
예제 #4
0
 def validate(self, node):
     # type: (Any) -> None
     types_defined: Set[Type_] = set()
     for arg in self._args:
         # Pylint wants us to use isinstance, which is inappropriate here
         if type(arg) in types_defined:  # pylint: disable=unidiomatic-typecheck
             raise TreeException(
                 node, "Property type '%s' ambiguously defined" % type(arg))
         types_defined.add(type(arg))
예제 #5
0
    def __setattr__(self, name: str, value: ValueType) -> None:
        """An overloaded attribute setter method that takes care of notifying the associated
        meta tree node of the value update.
        """
        super().__setattr__(name, value)

        meta_tree_node = self.getMetaTreeNode()
        if name not in meta_tree_node._values.keys():
            raise TreeException(
                meta_tree_node,
                f"Trying to assign to undefined member '{name}'")

        if value is not None:
            self.getValue(name).validate()
예제 #6
0
    def parseOptionalArgs(self, *args: Any) -> None:
        """Parse any optional arguments"""
        for opt_arg in args:
            if isinstance(opt_arg, Properties):
                if self._properties is not None:
                    raise TreeException(self, "Properties ambiguously defined")
                self._properties = opt_arg
                self._properties.validate(self)

            elif issubclass(type(opt_arg), Node):
                node: Node = opt_arg
                self._children[node._name] = node
            elif isinstance(opt_arg, Value):
                value: Value = opt_arg
                self._values[value._name] = value
                value._parent = self
            elif isinstance(opt_arg, Multiple):
                multi_node: Multiple = opt_arg
                spawned = multi_node.spawn()
                # Pass the spawned nodes and values as if they would have been added
                # to this node's constructor
                self.parseOptionalArgs(*spawned)
예제 #7
0
    def format_(self, meta_tree_entity):
        # type: (Any) -> str
        """Evaluate based to a meta tree entity"""

        if self._doc is None:
            return ""

        if callable(self._doc):
            doc_callable: Callable = self._doc
            doc_string = doc_callable(meta_tree_entity)
        elif isinstance(self._doc, str):
            doc_string = self._doc
        else:
            raise TreeException(
                self,
                "Strange _doc property type. Must either be str or callable")

        if doc_string is not None:
            formatted_doc_string = " [%s]" % doc_string
        else:
            formatted_doc_string = ""

        return formatted_doc_string
예제 #8
0
 def getDocumentation(self) -> str:
     """Get a formatted version of the documentation"""
     if self._meta_tree_value._properties._doc is None:
         raise TreeException(self._meta_tree_value, "Missing documentation")
     return self._meta_tree_value._properties._doc.format_(
         self._meta_tree_value)