Package jsondata :: Module jsonpointer :: Class JSONPointer
[hide private]
[frames] | no frames]

Class JSONPointer

[apisource code


Represents exactly one JSONPointer in compliance with IETF RFC6901 and relative-pointer/draft-1/2018

Instance Methods [hide private]
new empty list
__init__(self, ptr, **kargs)
Normalizes and stores a JSONPointer.
[apisource code
 
__add__(self, x)
Appends a Pointer to self.
[apisource code
 
__call__(self, x, *args, **kargs)
Evaluates the pointer value on the document.
[apisource code
 
__delattr__(self, name)
x.__delattr__('name') <==> del x.name
[apisource code
 
__eq__(self, x)
Compares this pointer with x.
[apisource code
 
__ge__(self, x)
Checks containment(>=) of another pointer within this.
[apisource code
 
__gt__(self, x)
Checks containment(>) of another pointer or object within this.
[apisource code
 
__iadd__(self, x)
Add in place x to self, appends a path.
[apisource code
 
__le__(self, x)
Checks containment(<=) of this pointer within another.
[apisource code
 
__lt__(self, x)
Checks containment(<) of this pointer within another.
[apisource code
 
__ne__(self, x)
Compares this pointer with x.
[apisource code
 
__radd__(self, x)
Adds itself as the right-side-argument to the left.
[apisource code
 
__repr__(self)
Returns the attribute self.__raw, which is the raw input JSONPointer.
[apisource code
 
__setattr__(self, name, value)
x.__setattr__('name', value) <==> x.name = value
[apisource code
 
__str__(self)
Returns the string for the processed path.
[apisource code
 
check_node_or_value(self, jsondata, parent=False)
Checks the existence of the corresponding node within the JSON document.
[apisource code
 
copy(self, **kargs)
Creates a copy of self.
[apisource code
 
copy_path_list(self, parent=False)
Returns a deep copy of the objects pointer path list.
[apisource code
 
__deepcopy__(self, memo) [apisource code
 
__getattr__(self, name) [apisource code
 
get_key(self)
Get the resulting key for the pointer.
[apisource code
 
get_node_and_child(self, jsondata)
Returns a tuple containing the parent node and self as the child.
[apisource code
 
get_node_and_key(self, jsondata)
Returns a tuple containing the parent node and the key of current.
[apisource code
 
get_node_value(self, jsondata, cp=2, **kargs)
Gets the copy of the corresponding node.
[apisource code
 
get_node(self, jsondata)
Returns the existing node for the pointer, calls transparently *JSONPointer.__call__*.
[apisource code
 
get_node_exist(self, jsondata, parent=False)
Returns two parts, the exisitng node for valid part of the pointer, and the remaining part of the pointer for the non-existing sub-path.
[apisource code
 
get_path_list(self)
Gets for the corresponding path list of the object pointer for in-memory access on the data of the 'json' package.
[apisource code
 
get_path_list_and_key(self)
Gets for the corresponding path list of the object pointer for in-memory access on the data of the 'json' package.
[apisource code
 
get_pointer(self, jsondata=None, **kargs)
Gets the object pointer in compliance to RFC6901 or relative pointer/draft-01/2018.
[apisource code
 
get_pointer_and_key(self, jsondata=None, **kargs)
Get the resulting pointer and key from the processing of the pointer and the optional starting node *stratrel*.
[apisource code
 
get_pointer_str(self, jsondata=None, **kargs)
Gets the objects pointer string in compliance to RFC6901 or relative pointer/draft-01/2018.
[apisource code
 
get_raw(self)
Gets the objects raw 6901-pointer.
[apisource code
 
get_relupidx(self)
Returns the resulting integer prefix.
[apisource code
 
get_start(self)
Returns the resulting start pointer after the application of the integer prefix.
[apisource code
 
get_startrel(self)
Returns the raw start pointer.
[apisource code
 
evaluate(self, jsondata, parent=False)
Gets the value resulting from the current pointer.
[apisource code
 
isfragment(self)
Checks whether a http fragment.
[apisource code
 
isrelpathrequest(self)
Checks whether a path request.
[apisource code
 
isrel(self)
Checks whether a relative pointer.
[apisource code
 
isvalid_nodetype(self, x)
Checks valid node types of in-memory JSON data.
[apisource code
 
isvalrequest(self)
Checks whether a value request.
[apisource code
 
iter_path(self, jsondata=None, **kargs)
Iterator for the elements of the path pointer itself.
[apisource code
 
iter_path_nodes(self, jsondata, parent=False, rev=False)
Iterator for the elements the path pointer points to.
[apisource code
 
iter_path_subpaths(self, jsondata=None, parent=False, rev=False)
Successive iterator for the resulting sub-paths the path pointer itself.
[apisource code
 
iter_path_subpathdata(self, jsondata=None, parent=False, rev=False)
Successive iterator for the resulting sub-paths and the corresponding nodes.
[apisource code

Inherited from list: __contains__, __delitem__, __delslice__, __getattribute__, __getitem__, __getslice__, __imul__, __iter__, __len__, __mul__, __new__, __reversed__, __rmul__, __setitem__, __setslice__, __sizeof__, append, count, extend, index, insert, pop, remove, reverse, sort

Inherited from object: __format__, __reduce__, __reduce_ex__, __subclasshook__

Class Variables [hide private]
  VALID_INDEX = re.compile(r'0|[1-9][0-9]*$')
Regular expression for valid numerical index.

Inherited from list: __hash__

Instance Variables [hide private]
  __isrel
marks a relative pointer
  __isfragment
marks a uri fragment
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, ptr, **kargs)
(Constructor)

[apisource code 
Normalizes and stores a JSONPointer. The internal
representation depends on the type.

* absolute path:

  A list of ordered items representing
  the path items.

* relative path:

  Relative paths in addition provide
  a positive numeric offset of outer
  containers [RELPOINTER]_.

Processes the ABNF of a JSON Pointer from RFC6901
and/or a relative JSON Pointer(draft 2018).

Attributes:
    For details see manuals.

    * *isfragment*
    * *isrel*
    * *raw*
    * *start*
    * *startrel*

Args:
    **ptr**:
        A JSONPointer to be represented by this object. The
        supported formats are:

        .. parsed-literal::

           ptr := (
               JSONPointer                  # [RFC6901]_ or [RELPOINTER]_
               | <rfc6901-string>           # [RFC6901]_
               | <relative-pointer-string>  # [RELPOINTER]_
               | <pointer-items-list>       # non-URI-fragment pointer path items of [RFC6901]_
               )

        JSONPointer:
            A valid object, is copied into this object,
            see 'deep'. Supports *rfc6901* [RFC6901]_
            and *relative* pointers [RELPOINTER]_.

        *rfc6901-string*:
            A string i accordance to RFC6901 [RFC6901]_.

        *relative-pointer-string*:
            Draft standard, currently
            experimental [RELPOINTER]_.

        *pointer-items-list*:
            Expects a path list, where each item
            is processed for escape and unquote.
            Supports *rfc6901* pointers [RFC6901]_.

        Containing:

        * absolute JSON Pointer
        * relative JSON Pointer, requires the
          keyword argument *startrel*

    kargs:

        **debug**:
            Enable debugging.

        **deep**:
            Applies for copy operations on structured data
            'deep' when 'True', else 'shallow' only.
            Flat data types are copied by value in any case.

        **node**:
            Force to set the pointed node in the internal cache.

        **replace**:
            Replace masked characters, is applied onto the *ptr*
            parameter only. For the replacement of *startrel*
            create and pass an object *JSONPointer*. ::

               replace := (
                    True    # replaces rfc6901 escape sequences: ~0 and ~1
                  | False   # omit unescaping
               )

            .. note::

               Match operations on address strings are proceeded literally,
               thus the escaped characters should be consistent,
               see rfc6901, Section 3.

            default := False

        **startrel**:
            Start node for relative JSON Pointers. Is evaluated
            only in combination with a relative path, else
            ignored. ::

               startrel := (
                    JSONPointer           # supports [RFC6901]_ and [RELPOINTER]_
                  | <rfc6901-string>      # supports [RFC6901]_
                  | <rel-pointer-string>  # supports only relative to whole-document '0/...'
               )

            default := ""  # whole document

Returns:
    When successful returns *True*, else returns either *False*, or
    raises an exception.
    Success is the complete addition only, thus one failure returns
    *False*.

Raises:
    JSONPointerError:

Returns: new empty list
Overrides: object.__init__

__add__(self, x)
(Addition operator)

[apisource code 
Appends a Pointer to self.

Args:
    **x**:
        A valid JSONPointer of type: ::

           x := (
              JSONPointer - fragment
              | JSONPointer - relative-pointer
              | relative pointer
              )

Returns:
    A new object of JSONPointer

Raises:
    JSONPointerError:

Overrides: list.__add__

__call__(self, x, *args, **kargs)
(Call operator)

[apisource code 
Evaluates the pointer value on the document.

Args:
    **x**:
        A valid JSON document.

Returns:
    The resulting pointer value.

Raises:
    JSONPointerError

__delattr__(self, name)

[apisource code 

x.__delattr__('name') <==> del x.name

Overrides: object.__delattr__
(inherited documentation)

__eq__(self, x)
(Equality operator)

[apisource code 
Compares this pointer with x.

Args:
    **x**:
        A JSONPointer object.

Returns:
    True or False

Raises:
    JSONPointerError

Overrides: list.__eq__

__ge__(self, x)
(Greater-than-or-equals operator)

[apisource code 
Checks containment(>=) of another pointer within this.

The weight of contained entries is the criteria, though
the shorter is the bigger. This is true only in case of
a containment relation.

The number of equal path pointer items is compared.

Args:
    **x**:
        A valid Pointer.

Returns:
    True or False

Raises:
    JSONPointerError:

Overrides: list.__ge__

__gt__(self, x)
(Greater-than operator)

[apisource code 
Checks containment(>) of another pointer or object within this.

The number of equal items is compared.

Args:
    **x**:
        A valid Pointer.

Returns:
    True or False

Raises:
    JSONPointerError:

Overrides: list.__gt__

__iadd__(self, x)

[apisource code 
Add in place x to self, appends a path.

Args:
    **x**:
        A valid Pointer.

Returns:
    'self' with updated pointer attributes

Raises:
    JSONPointerError:

Overrides: list.__iadd__

__le__(self, x)
(Less-than-or-equals operator)

[apisource code 
Checks containment(<=) of this pointer within another.

The number of equal items is compared.

Args:
    **x**:
        A valid Pointer.

Returns:
    True or False

Raises:
    JSONPointerError:

Overrides: list.__le__

__lt__(self, x)
(Less-than operator)

[apisource code 
Checks containment(<) of this pointer within another.

The number of equal items is compared.

Args:
    **x**:
        A valid Pointer.

Returns:
    True or False

Raises:
    JSONPointerError:

Overrides: list.__lt__

__ne__(self, x)

[apisource code 
Compares this pointer with x.

Args:
    **x**:
        A valid Pointer.

Returns:
    True or False

Raises:
    JSONPointerError

Overrides: list.__ne__

__radd__(self, x)
(Right-side addition operator)

[apisource code 
Adds itself as the right-side-argument to the left.

This method appends 'self' to a path fragment on the left.
Therefore it adds the path separator on it's left side only.
The left side path fragment has to maintain to be in
accordance to RFC6901 by itself.

Once 'self' is added to the left side, it terminates it's
life cycle. Thus another simultaneous add operation is
handled by the resulting other element.

Args:
    **x**:
        A valid Pointer.

Returns:
    The updated input of type 'x' as 'x+S(x)'

Raises:
    JSONPointerError:

__repr__(self)
(Representation operator)

[apisource code 
Returns the attribute self.__raw, which is the raw input JSONPointer.

Args:
    None

Attributes:
    Evaluates *self.__isrel*

Returns:
    For relative paths: ::
       (<start-offset>, <pointer>)

       start-offset := [<self.startrel>]
       pointer := [<self>]

    For RFC6901 paths: ::

       <pointer>

       pointer := [<self>]

Raises:
    pass-through

Overrides: object.__repr__

__setattr__(self, name, value)

[apisource code 

x.__setattr__('name', value) <==> x.name = value

Overrides: object.__setattr__
(inherited documentation)

__str__(self)
(Informal representation operator)

[apisource code 
Returns the string for the processed path.

Args:
    None

Attributes:
    Evaluates *self.__isrel*

Returns:
    For relative paths: ::
       (<start-offset>, <pointer>)

       start-offset := [<self.startrel>]
       pointer := [<self>]

    For RFC6901 paths: ::

       <pointer>

       pointer := [<self>]

Raises:
    pass-through

Overrides: object.__str__

check_node_or_value(self, jsondata, parent=False)

[apisource code 
Checks the existence of the corresponding node
within the JSON document.

Args:
    **jsondata**:
        A valid JSON data node.

    **parent**:
        If *True* returns the parent node of the pointed value.

Returns:
    True or False

Raises:
    JSONPointerError:

    pass-through

copy(self, **kargs)

[apisource code 
Creates a copy of self.

Args:
    None

    kargs:
        **deep**:
            When *True* creates a deep copy,
            else shallow.

Returns:
    A copy of self.

Raises:
    pass-through

copy_path_list(self, parent=False)

[apisource code 
Returns a deep copy of the objects pointer path list.

Args:
    **parent**:
        The parent node of the pointer path.

Returns:
    A copy of the path list.

Raises:
    none

get_key(self)

[apisource code 

Get the resulting key for the pointer. In case of a relative pointer as resulting from the processing of the relative pointer and the starting node.

get_node_and_child(self, jsondata)

[apisource code 
Returns a tuple containing the parent node and self as the child.

Args:
    **jsondata**:
        A valid JSON data node.

Returns:
    The the tuple: ::

       (p, c):
          p: Node reference to parent container.
          c: Node reference to self as the child.

Raises:
    JSONPointerError:

    pass=through

get_node_and_key(self, jsondata)

[apisource code 
Returns a tuple containing the parent node and the key of current.

Args:
    **jsondata**:
        A valid JSON data node.

Returns:
    The the tuple: ::

       (n, k):
          n: Node reference to parent container.
          k: Key for self as the child entry:

             k := (
                  <list-index>
                | <dict-key>
                | None
             )

             list-index: 'int'
             dict-key: 'UTF-8'
             None: "for root-node"

Raises:
    JSONPointerError:

    pass-through

get_node_value(self, jsondata, cp=2, **kargs)

[apisource code 
Gets the copy of the corresponding node.
Relies on the standard package 'json'.

Args:
    **jsondata**:
        A valid JSON data node.

    **cp**:
        Type of returned copy. ::

           cp := (
                C_DEEP
              | C_REF
              | C_SHALLOW
           )

    kargs:
        **valtype**:
            Type of requested value.

Returns:
    The copy of the node, see option *copy*.

Raises:
    JSONPointerError

    pass-through

get_node(self, jsondata)

[apisource code 
Returns the existing node for the pointer,
calls transparently *JSONPointer.__call__*.

Args:
    **jsondata**:
        A valid JSON data node.

Returns:
    The node reference.

Raises:
    JSONPointerError:

    pass-through

get_node_exist(self, jsondata, parent=False)

[apisource code 
Returns two parts, the exisitng node for valid part of
the pointer, and the remaining part of the pointer for
the non-existing sub-path.

This method works similar to the 'evaluate' method, whereas it
handles partial valid path pointers, which may also include
a '-' in accordance to RFC6902.

Therefore the non-ambiguous part of the pointer is resolved,
and returned with the remaining part for a newly create.
Thus this method is in particular foreseen to support the
creation of new sub data structures.

The 'evaluate' method therefore returns a list of two elements,
the first is the node reference, the second the list of the
remaining path pointer components. The latter may be empty in
case of a fully valid pointer.


Args:
    **jsondata**:
        A valid JSON data node.

    **parent**:
        Return the parent node of the pointed value.

Returns:
    The node reference, and the remaining part.
    ret:=(node, [<remaining-path-components-list>])

Raises:
    JSONPointerError:
    forwarded from json

get_path_list(self)

[apisource code 
Gets for the corresponding path list of the object pointer for
in-memory access on the data of the 'json' package.

Args:
    none

Returns:
    The path list.

Raises:
    none

get_path_list_and_key(self)

[apisource code 
Gets for the corresponding path list of the object pointer for in-memory access on the data of the 'json' package.

Args:
    none

Returns:
    The path list.

Raises:
    none

get_pointer(self, jsondata=None, **kargs)

[apisource code 
Gets the object pointer in compliance to RFC6901
or relative pointer/draft-01/2018.

The result is by default the assigned pointer itself without
verification. Similar in case of a relative pointer the start
offset is ignored by default and no verification is performed.

The following options modify this behaviour:

* superpose - superposes the *startrel* offset with the pointer
* verify - verifies the actual existence of the nodes and/or
  intermediate nodes

The options could be applied combined.

Args:
    kargs:
        **returntype**:
            Defines the return type. ::

               returntype := (
                    RT_DEFAULT     | 'default'
                  | RT_LST         | 'list'      | list
                  | RT_JSONPOINTER | 'jpointer'
               )

        **superpose**:
            Is only relevant for relative paths. Superposes the offset
            *startrel* with the pointer into the resulting final pointer.
            By default nodes are not verified, see *verify* parameter.

            default := True

        **verify**:
            Verifies the "road" of the superposed pointers. ::

               verify := (
                    V_DEFAULT | 'default'
                  | V_NONE    | 'none'    | None    # no checks at all
                  | V_FINAL   | 'final'             # checks final result only
                  | V_STEPS   | 'steps'             # checks each intermediate directory
               )

            default := None
Returns:
    The new pointer in a choosen format, see *returntype*.

Raises:
    none

get_pointer_str(self, jsondata=None, **kargs)

[apisource code 
Gets the objects pointer string in compliance to RFC6901
or relative pointer/draft-01/2018.

The result is by default the assigned pointer itself without
verification. Similar in case of a relative pointer the start
offset is ignored by default and no verification is performed.

The following options modify this behaviour:

* superpose - superposes the *startrel* offset with the pointer
* verify - verifies the actual existence of the nodes and/or
  intermediate nodes

The options could be applied combined.

Args:
    kargs:
        **forcenotation**:
            Force the output notation for string representation to: ::

               forcenotation := (
                    NOTATION_NATIVE           # original format with unescape
                  | NOTATION_JSON             # transform to resulting pointer
                  | NOTATION_HTTP_FRAGMENT    # return a fragment with encoding
                  | NOTATION_JSON_REL         # resulting relative pointer
                  | NOTATION_RAW              # raw input
               )

               default := NOTATION_NATIVE

            **REMINDER**: Applicable for return type string only.

        **superpose**:
            Is only relevant for relative paths. Superposes the offset
            *startrel* with the pointer into the resulting final pointer.
            By default nodes are not verified, see *verify* parameter.

Returns:
    The new pointer in a choosen format, see *returntype*.

Raises:
    none

get_raw(self)

[apisource code 
Gets the objects raw 6901-pointer.

Args:
    none

Returns:
    The raw path.

Raises:
    none

evaluate(self, jsondata, parent=False)

[apisource code 
Gets the value resulting from the current pointer.

Args:
    **jsondata**:
        A JSON data node. ::

           jsondata := (
               JSONData
               | list
               | dict
               )

    **parent**:
        Return the parent node of the pointed value.
        When parent is selected, the pointed child node
        is not verified.

Returns:
    The referenced value.

Raises:
    JSONPointerError:

    pass-through

iter_path(self, jsondata=None, **kargs)

[apisource code 
Iterator for the elements of the path pointer itself.

Args:
    **jsondata**:
        If provided a valid JSON data node, the
        path components are successively verified on
        the provided document.

    kargs:
        **parent**:
            Uses the path pointer to parent node.

        **rev**:
            Reverse the order, start with last.

        **superpose**:
            Is only relevant for relative paths, for *rfc6901* defined
            paths the parameter is ignored. When *True* superposes
            the offset *startrel* with the pointer into the resulting
            final pointer. By default nodes are not verified,
            see *verify* parameter. ::

               superpose := (
                    True   # iterates resulting paths from *startrel*
                  | False  # iterates the path only
               )


            default := True

Returns:
    Yields the iterator for the current path pointer
    components.

Raises:
    JSONPointerError:
    forwarded from json

iter_path_nodes(self, jsondata, parent=False, rev=False)

[apisource code 
Iterator for the elements the path pointer points to.

Args:
    **jsondata**:
        A valid JSON data node.

    **parent**:
        Uses the path pointer to parent node.

    **rev**:
        Reverse the order, start with last.

Returns:
    Yields the iterator of the current node reference.

Raises:
    JSONPointerError:
    forwarded from json

iter_path_subpaths(self, jsondata=None, parent=False, rev=False)

[apisource code 
Successive iterator for the resulting sub-paths the
path pointer itself.

Args:
    **jsondata**:
        If provided a valid JSON data node, the
        path components are successively verified on
        the provided document.

    **parent**:
        Uses the path pointer to parent node.

    **rev**:
        Reverse the order, start with last.

Returns:
    Yields the iterator for the copy of the current
    slice of the path pointer.

Raises:
    JSONPointerError:
    forwarded from json

iter_path_subpathdata(self, jsondata=None, parent=False, rev=False)

[apisource code 
Successive iterator for the resulting sub-paths and the
corresponding nodes.

Args:
    **jsondata**:
        If provided a valid JSON data node, the
        path components are successively verified on
        the provided document.

    **parent**:
        Uses the path pointer to parent node.

    **rev**:
        Reverse the order, start with last.

Returns:
    Yields the iterator for the tuple of the current
    slice of the path pointer and the reference of the
    corresponding node. ::

       (<path-item>, <sub-path>, <node>)

       path-item: copy of the item
       sub-path:  copy of the current subpath
       node:      reference to the corresponding node

Raises:
    JSONPointerError:
    forwarded from json