pyxsd.transforms ================ .. py:module:: pyxsd.transforms .. autoapi-nested-parse:: Transform framework for pyxsd. Custom transforms subclass :class:`Transform` (or :class:`Displayer` for transforms that write output) from this package:: from pyxsd.transforms import Transform The shipped transforms are :class:`~pyxsd.transforms.print_data.PrintData`, :class:`~pyxsd.transforms.to_dict.ToDict`, and :class:`~pyxsd.transforms.to_data.ToData`. The historical crystallography library (CellSizer and friends) moved to ``examples/legacy/`` in the repository. Classes ------- .. autoapisummary:: pyxsd.transforms.Displayer pyxsd.transforms.ToData pyxsd.transforms.ToDict pyxsd.transforms.Transform Functions --------- .. autoapisummary:: pyxsd.transforms.iter_tree Package Contents ---------------- .. py:class:: Displayer(root) The base abstract class for all transforms. All methods should mix into the usable transform classes. Contains methods to retrieve elements from the tree. Subclasses must accept the tree root in their ``__init__``; that makes the class abstract until it does, so framework-only subclasses (like :class:`~pyxsd.transforms.displayer.Displayer`) cannot be instantiated by accident. Subclasses should call ``super().__init__(root)``, which stores the root as ``self.root``. .. py:attribute:: root :type: Any .. py:method:: openFile(fileName = None) .. py:method:: writeTree(file) .. py:method:: makeTempFileOfTree() .. py:class:: ToData(root = None, *, format = 'pyxsd', fidelity = 'best-effort', typed = True, always_list = False) Exports the bound tree under an explicit JSON data contract. Usable both as ``doc.transform(ToData(**options))`` and from the CLI as ``--transform ToData(**options)``. The CLI constructs a transform with the tree root, while ``Document.transform`` calls it with the root; ``__call__`` therefore accepts an optional explicit root and otherwise uses the one given at construction. Keyword arguments pass through to the export codec. The CLI supplies document context for schema-directed formats, including RELAX NG. Native ``format="pyxsd"`` also works on a RELAX NG lexical tree and produces the generic grouped export; it never manufactures RELAX NG typed wrappers or tags. A root-only call cannot export a RELAX NG tree in a schema-directed format; use the CLI or the document's ``to_data`` method. .. py:attribute:: kwargs :type: dict[str, Any] .. py:class:: ToDict(root = None, **kwargs) Exports the bound tree as a plain dict. Usable both as ``doc.transform(ToDict(**options))`` and from the CLI as ``--transform to_dict(**options)``. The CLI constructs a transform with the tree root, while ``Document.transform`` calls it with the root; ``__call__`` therefore accepts an optional explicit root and otherwise uses the one given at construction. Keyword arguments pass through to :func:`pyxsd.dict_export.bound_to_dict`. .. py:attribute:: kwargs .. py:class:: Transform(root) The base abstract class for all transforms. All methods should mix into the usable transform classes. Contains methods to retrieve elements from the tree. Subclasses must accept the tree root in their ``__init__``; that makes the class abstract until it does, so framework-only subclasses (like :class:`~pyxsd.transforms.displayer.Displayer`) cannot be instantiated by accident. Subclasses should call ``super().__init__(root)``, which stores the root as ``self.root``. .. py:attribute:: root .. py:method:: makeElemObj(name) Creates a new element that contains the proper tree structure. .. py:method:: makeCommentElem(comment) Makes a comment element. .. py:method:: iter_tree(instance) Yield every tree node at or below ``instance``, depth-first. Method form of the module-level :func:`iter_tree`; see there for semantics. - ``instance``: a tree node, or a list/dict of them. .. py:method:: walk(instance, visitor, *args, **kwargs) Walks through the tree structure and runs a provided visitor function on all elements. The visitor is called as ``visitor(node, attrNames, elemNames, *args, **kwargs)`` where ``node`` is the tree node being visited, ``attrNames`` is the list of its attribute names, and ``elemNames`` is the list of its children's names. Traversal is driven by :meth:`iter_tree`. .. py:method:: classCollector(instance, attrNames, elemNames, collectorDict) Visitor function to make a dictionary that associates a class with its instances. The class name is the key, and the value is the list of associated instances. See ``getInstancesByClassName``. .. py:method:: tagCollector(instance, attrNames, elemNames, collectorDict) A visitor function that is used to make a dictionary that associates a tag name with its children. See ``getAllSubElements``. .. py:method:: tagFinder(instance, attrNames, elemNames, collection, name) A visitor function to collect all tags with a particular name and put them into a list. See ``getElementsByName``. .. py:method:: getInstancesByClassName(root) Uses the ``walk`` function with the ``classCollector`` visitor function to associate a class name with the class's instances. .. py:method:: getAllSubElements(root) Uses the ``walk`` function with the ``tagCollector`` visitor function to make a dictionary that associates all elements with their sub-elements. .. py:method:: getElementsByName(root, name) Uses the ``walk`` function with the ``tagFinder`` visitor function to make a list containing all elements with a particular name. .. py:method:: find(tagName, baseElem) Finds an element from a given tagName. Returns the first one found, or returns None. This function is an alternative to the walk/visitor functions. See ``getElementsByName``. .. py:method:: findAll(tagName, baseElem) Finds all elements with a given tagName. Returns a list of elements or None. This function is an alternative to the walk/visitor functions. See ``getElementsByName``. .. py:function:: iter_tree(instance) Yield every tree node at or below ``instance``, depth-first. Lists (and tuples) are descended into item by item and dictionaries by value; anything without both ``_children_`` and ``_attribs_`` is skipped. Each yielded node is visited before its children (pre-order). This generator powers :meth:`~pyxsd.transforms.transform.Transform.walk` and is the supported way to iterate a tree directly:: for node in iter_tree(root): ... - ``instance``: a tree node, or a list/dict of them.