How to use the pglast.node.Base function in pglast

To help you get started, we’ve selected a few pglast examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github lelit / pglast / tests / test_node.py View on Github external
def test_bad_base_construction():
    pytest.raises(ValueError, Base, {})
    pytest.raises(ValueError, Base, [])
    pytest.raises(ValueError, Base, {'Foo': 'bar', 'Bar': 'foo'})
    pytest.raises(ValueError, Base, {'Foo': {'some': 'value'}}, parent=1.0)
    pytest.raises(ValueError, Base, {'Foo': {'some': 'value'}}, name=1.0)
    pytest.raises(ValueError, Base, set())
github lelit / pglast / tests / test_node.py View on Github external
def test_bad_base_construction():
    pytest.raises(ValueError, Base, {})
    pytest.raises(ValueError, Base, [])
    pytest.raises(ValueError, Base, {'Foo': 'bar', 'Bar': 'foo'})
    pytest.raises(ValueError, Base, {'Foo': {'some': 'value'}}, parent=1.0)
    pytest.raises(ValueError, Base, {'Foo': {'some': 'value'}}, name=1.0)
    pytest.raises(ValueError, Base, set())
github lelit / pglast / pglast / node.py View on Github external
    @property
    def string_value(self):
        if self.node_tag != 'String':
            raise TypeError('%r is not a String node' % self)
        return self.str.value

    def traverse(self):
        "A generator that recursively traverse all attributes of the node."

        yield self
        for item in self:
            yield from item.traverse()


class Scalar(Base):
    "Represent a single scalar value."

    __slots__ = Base.__slots__ + ('_value',)

    def _init(self, value, parent, name):
        if not isinstance(value, (NoneType, bool, float, int, str)):
            raise ValueError("Unexpected value for 'value', must be either None or a"
                             " bool|float|int|str instance, got %r" % type(value))
        super()._init(parent, name)
        self._value = value

    def __and__(self, other):
        if isinstance(self._value, int) and isinstance(other, int):
            return self._value & other
        else:
            raise ValueError("Wrong operands for __and__: %r & %r"
github lelit / pglast / pglast / node.py View on Github external
return '%s=%r' % (aname, self)

    @property
    def parent_node(self):
        "The parent :class:`Node` of this element."

        return self._parent_node

    @property
    def parent_attribute(self):
        "The *attribute* in the parent :class:`Node` referencing this element."

        return self._parent_attribute


class List(Base):
    """Represent a sequence of :class:`Node` instances.

    :type items: list
    :param items: a list of items, usually :class:`Node` instances
    :type parent: ``None`` or :class:`Node` instance
    :param parent: ``None`` to indicate that the node is the *root* of the parse tree,
                   otherwise it is the immediate parent of the new node
    :type name: str or tuple
    :param name: the name of the attribute in the `parent` node that *points* to this one;
                 it may be a tuple (name, position) when ``parent[name]`` is actually a list of
                 nodes
    """

    __slots__ = Base.__slots__ + ('_items',)

    def _init(self, items, parent, name):
github lelit / pglast / pglast / node.py View on Github external
class Node(Base):
    """Represent a single entry in a *parse tree* returned by :func:`~.parser.parse_sql()`
    (or :func:`~.parser.parse_plpgsql()`).

    :type details: dict
    :param details: the *parse tree* of the node
    :type parent: ``None`` or :class:`Node` instance
    :param parent: ``None`` to indicate that the node is the *root* of the parse tree,
                   otherwise it is the immediate parent of the new node
    :type name: str or tuple
    :param name: the name of the attribute in the `parent` node that *points* to this one;
                 it may be a tuple (name, position) when ``parent[name]`` is actually a list of
                 nodes
    """

    __slots__ = Base.__slots__ + ('_node_tag', '_parse_tree')

    def _init(self, details, parent=None, name=None):
        if not isinstance(details, dict) or len(details) != 1:
            raise ValueError("Unexpected value for 'details', must be a dict with"
                             " exactly one key, got %r" % type(details))
        super()._init(parent, name)
        (self._node_tag, self._parse_tree), *_ = details.items()

    def __repr__(self):
        return '{%s}' % self._node_tag

    def __getattr__(self, attr):
        try:
            value = self._parse_tree[attr]
        except KeyError:
            return Missing
github lelit / pglast / pglast / node.py View on Github external
def __getitem__(self, index):
        return Base(self._items[index], self.parent_node, (self.parent_attribute, index))
github lelit / pglast / pglast / node.py View on Github external
def __iter__(self):
        pnode = self.parent_node
        aname = self.parent_attribute
        for idx, item in enumerate(self._items):
            yield Base(item, pnode, (aname, idx))
github lelit / pglast / pglast / node.py View on Github external
def __iter__(self):
        value = self._parse_tree
        for attr in sorted(value.keys()):
            yield Base(value[attr], self, attr)
github lelit / pglast / pglast / node.py View on Github external
def __getattr__(self, attr):
        try:
            value = self._parse_tree[attr]
        except KeyError:
            return Missing
        else:
            return Base(value, self, attr)
github lelit / pglast / pglast / node.py View on Github external
def string_value(self):
        if len(self) != 1:
            raise TypeError('%r does not contain a single String node' % self)
        node = self[0]
        if node.node_tag != 'String':
            raise TypeError('%r does not contain a single String node' % self)
        return node.str.value

    def traverse(self):
        "A generator that recursively traverse all the items in the list."

        for item in self:
            yield from item.traverse()


class Node(Base):
    """Represent a single entry in a *parse tree* returned by :func:`~.parser.parse_sql()`
    (or :func:`~.parser.parse_plpgsql()`).

    :type details: dict
    :param details: the *parse tree* of the node
    :type parent: ``None`` or :class:`Node` instance
    :param parent: ``None`` to indicate that the node is the *root* of the parse tree,
                   otherwise it is the immediate parent of the new node
    :type name: str or tuple
    :param name: the name of the attribute in the `parent` node that *points* to this one;
                 it may be a tuple (name, position) when ``parent[name]`` is actually a list of
                 nodes
    """

    __slots__ = Base.__slots__ + ('_node_tag', '_parse_tree')