<filename>nafparserpy/layers/utils.py<gh_stars>1-10
from dataclasses import dataclass, field

from lxml import etree


class AttributeGetter:
    """Provides an attribute getter"""
    def get(self, attribute):
        """Get attribute from `attrs` field

        Parameters
        ----------
        attribute : str
            attribute name

        Raises
        ------
        KeyError: if the layer has no such attribute"""
        if self.has(attribute):
            return self.attrs[attribute]
        else:
            raise KeyError('Attribute {} is not present in layer:\n{}'.format(attribute, self))

    def has(self, attribute):
        """Test if attribute appears in `attrs` field"""
        return self.attrs is not None and attribute in self.attrs.keys()


class IdrefGetter:
    """Provides a target ids getter for layers with a `span` field"""
    def target_ids(self):
        """Return list of target ids covered by the layer's span"""
        if self.span is None:
            return []
        return [t.id for t in self.span.targets]


@dataclass
class AttributeLayer(AttributeGetter):
    """A layer containing only attributes"""
    layer: str
    attrs: dict = field(default_factory=dict)
    """optional attributes (keys are subclass dependent)"""

    def node(self):
        """Create etree node from object"""
        return create_node(self.layer, None, [], self.attrs)

    @staticmethod
    def object(layer_name, node):
        """Create object from etree node"""
        return AttributeLayer(layer_name, node.attrib)


def create_node(layer, text, children, attributes):
    """Create an etree Element node from the text, children and attributes of NAF objects

    Parameters
    ----------
    layer : str
        layer name
    text : str
        text of node
    children : list
        list of NAF objects to add as subelements in the node
    attributes : dict
        node attributes (whether compulsory or optional)
    """
    node = etree.Element(layer)
    if text is not None:
        node.text = etree.CDATA(text)
    for child in children:
        # create node for each child and append to node subelements
        node.append(child.node())
    for k, v in attributes.items():
        node.set(k, v)
    return node

<filename>nafparserpy/layers/utils.py<gh_stars>1-10
from dataclasses import dataclass, field

from lxml import etree


class AttributeGetter:
    """Provides an attribute getter"""
    def get(self, attribute):
        """Get attribute from `attrs` field

        Parameters
        ----------
        attribute : str
            attribute name

        Raises
        ------
        KeyError: if the layer has no such attribute"""
        if self.has(attribute):
            return self.attrs[attribute]
        else:
            raise KeyError('Attribute {} is not present in layer:\n{}'.format(attribute, self))

    def has(self, attribute):
        """Test if attribute appears in `attrs` field"""
        return self.attrs is not None and attribute in self.attrs.keys()


class IdrefGetter:
    """Provides a target ids getter for layers with a `span` field"""
    def target_ids(self):
        """Return list of target ids covered by the layer's span"""
        if self.span is None:
            return []
        return [t.id for t in self.span.targets]


@dataclass
class AttributeLayer(AttributeGetter):
    """A layer containing only attributes"""
    layer: str
    attrs: dict = field(default_factory=dict)
    """optional attributes (keys are subclass dependent)"""

    def node(self):
        """Create etree node from object"""
        return create_node(self.layer, None, [], self.attrs)

    @staticmethod
    def object(layer_name, node):
        """Create object from etree node"""
        return AttributeLayer(layer_name, node.attrib)


def create_node(layer, text, children, attributes):
    """Create an etree Element node from the text, children and attributes of NAF objects

    Parameters
    ----------
    layer : str
        layer name
    text : str
        text of node
    children : list
        list of NAF objects to add as subelements in the node
    attributes : dict
        node attributes (whether compulsory or optional)
    """
    node = etree.Element(layer)
    if text is not None:
        node.text = etree.CDATA(text)
    for child in children:
        # create node for each child and append to node subelements
        node.append(child.node())
    for k, v in attributes.items():
        node.set(k, v)
    return node

Provides an attribute getter Get attribute from `attrs` field          Parameters         ----------         attribute : str             attribute name          Raises         ------         KeyError: if the layer has no such attribute Test if attribute appears in `attrs` field Provides a target ids getter for layers with a `span` field Return list of target ids covered by the layer's span A layer containing only attributes optional attributes (keys are subclass dependent) Create etree node from object Create object from etree node Create an etree Element node from the text, children and attributes of NAF objects      Parameters     ----------     layer : str         layer name     text : str         text of node     children : list         list of NAF objects to add as subelements in the node     attributes : dict         node attributes (whether compulsory or optional) # create node for each child and append to node subelements