Package jsondata :: Module jsonpatch :: Class JSONPatch
[hide private]
[frames] | no frames]

Class JSONPatch

[apisource code


Representation of a JSONPatch task list for RFC6902.

Contains the defined methods from standards:

* add
* remove
* replace
* move
* copy
* test

Attributes:
    **patch**:
        List of patch items.

Instance Methods [hide private]
 
__init__(self, p=None, **kargs)
List of patch tasks.
[apisource code
 
__add__(self, x=None)
Creates and adds patch job to the task queue.
[apisource code
 
__call__(self, jdata, x=None)
Evaluates the related task for the provided index.
[apisource code
 
__eq__(self, x)
Compares this pointer with x.
[apisource code
 
__getitem__(self, key)
Support of slices, for 'iterator' refer to self.__iter__.
[apisource code
 
__iadd__(self, x=None)
Adds patch jobs to the task queue in place.
[apisource code
 
__isub__(self, x)
Removes the patch job from the task queue in place.
[apisource code
 
__iter__(self)
Provides an iterator foreseen for large amounts of in-memory patches.
[apisource code
 
__len__(self)
The number of outstanding patches.
[apisource code
 
__ne__(self, x)
Compares this pointer with x.
[apisource code
 
__radd__(self, x=None)
Adds a copy of xto the task queue.
[apisource code
 
__repr__(self)
Prints the representation format of a JSON patch list.
[apisource code
 
__str__(self)
Prints the display format.
[apisource code
 
__sub__(self, x)
Removes the patch job from the task queue.
[apisource code
 
apply(self, jsondata, **kargs)
Applies the JSONPatch task.
[apisource code
 
getpatchitem(self, x=None)
Gets the reference to a single patch item.
[apisource code
 
getpatchitems(self, *args, **kargs)
Gets a list of references of patch items.
[apisource code
 
gettree(self, *args, **kargs)
Gets the resulting logical JSON data structure constructed from the patch items of the current set.
[apisource code
 
patch_export(self, patchfile, schema=None, **kargs)
Exports the current task list.
[apisource code
 
patch_import(self, patchfile, schemafile=None, **kargs)
Imports a task list.
[apisource code
 
repr_export(self)
Prints the export representation format of a JSON patch list.
[apisource code
 
str_export(self)
Pretty prints the export representation format of a JSON patch list.
[apisource code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __setattr__, __sizeof__, __subclasshook__

Instance Variables [hide private]
  deep
Defines copy operations, True:=deep, False:=swallow
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, p=None, **kargs)
(Constructor)

[apisource code 
List of patch tasks.

Args:
    **p**:
        Patch list. ::

           p := (
                JSONPatch
              | <list-of-patch-items>
           )

    kargs:
        **replace**:
            Replace masked characters in *target* specification. ::

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

            .. note::
            
               Match operations are proceeded literally, thus the
               escaped characters should be consistent,
               see rfc6901, Section 3.

            default := False

Returns:
    self.

Raises:
    pass-through

Overrides: object.__init__

__add__(self, x=None)
(Addition operator)

[apisource code 
Creates and adds patch job to the task queue.

Args:
    **x**:
        Extension of pathc job, eithe a *JSONPatch*,
        or a *JSONPatchItem*.

Returns:
    Returns a patch job, or raises Exception. 

Raises:
    JSONDataPatchError

__call__(self, jdata, x=None)
(Call operator)

[apisource code 
Evaluates the related task for the provided index.

Args:
    **x**: 
        Task index.

    **jdata**: 
        JSON data the task has to be 
        applied on.

Returns:
    Returns a tuple of: ::

       (n, lerr): 

       n:    number of present active entries
       lerr: list of failed entries

Raises:
    JSONDataPatchError:

__eq__(self, x)
(Equality operator)

[apisource code 
Compares this pointer with x.

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

Returns:
    *True* or *False*

Raises:
    JSONPointerError

__getitem__(self, key)
(Indexing operator)

[apisource code 

Support of slices, for 'iterator' refer to self.__iter__.

#. self[key]

#. self[i:j:k]

#. x in self

#. for x in self

__isub__(self, x)

[apisource code 
Removes the patch job from the task queue in place. 

Removes one of the following type(x) variants:

    *int*:
        The patch job with given index.

    *JSONPatchItem*:
        The first matching entry from 
        the task queue. 

Args:
    **x**:
        Item to be removed. ::

           x := (
                int
              | JSONPatchItem
           ) 

Returns:
    Returns resulting list without x.

Raises:
    JSONDataPatchError:

__ne__(self, x)

[apisource code 
Compares this pointer with x.

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

Returns:
    *True* or *False*

Raises:
    JSONPointerError

__repr__(self)
(Representation operator)

[apisource code 

Prints the representation format of a JSON patch list.

Overrides: object.__repr__

__str__(self)
(Informal representation operator)

[apisource code 

Prints the display format.

Overrides: object.__str__

__sub__(self, x)
(Subtraction operator)

[apisource code 
Removes the patch job from the task queue. 

Removes one of the following type(x) variants:

    *int*:
        The patch job with given index.

    *JSONPatchItem*:
        The first matching entry from 
        the task queue. 

Args:
    **x**:
        Item to be removed. ::

           x := (
                int
              | JSONPatchItem
           ) 

Returns:
    Returns resulting list without x.

Raises:
    JSONDataPatchError:

apply(self, jsondata, **kargs)

[apisource code 
Applies the JSONPatch task.

Args:
    **jsondata**:
        JSON data the joblist has to be applied on.

Returns:
    Returns a tuple of: ::

       (n, lerr): 

       n:    number of present active entries
       lerr: list of failed entries

Raises:
    JSONDataPatchError:

getpatchitem(self, x=None)

[apisource code 
Gets the reference to a single patch item.

Args:
    **x**:
        Requested item. ::

           x := (
                int
              | JSONPatchItem
           ) 

           int:           index of patch item
           JSONPatchItem: the reference to the patch item
    
Returns:
    The selected patch item.

Raises:
    None

getpatchitems(self, *args, **kargs)

[apisource code 
Gets a list of references of patch items.

Args:
    args:
        Requested items. ::

           *args := (
              <item>
              | <item-list>
              | None
           )
           item-list := <item>[, <item-list>]
           item := (
                int
              | JSONPatchItem
           )
           None := "all items of the current patch list"

           int:           index of patch item
           JSONPatchItem: the reference to the patch item
    
    kargs:
        **idxlist**:
            Print with index: ::

               idxlist := (
                    True     # format: [{<index>: <JSONPatchItem>}]
                  | False    # format: [<JSONPatchItem>]
               )

        **copydata**:
            Creates a copy of each resulting item. ::

               copydata := (C_DEEP | C_SHALLOW | C_REF)

            default := C_REF  # no copy
        
Returns:
    A list of the selected patch items.

Raises:
    None

gettree(self, *args, **kargs)

[apisource code 
Gets the resulting logical JSON data structure
constructed from the patch items of the current set.

Args:
    args:
        Requested items. ::

           *args := (
              <item>
              | <item-list>
              | None
           )
           item-list := <item>[, <item-list>]
           item := (
                int
              | JSONPatchItem
           )
           None := "all items of the current patch list"

           int:           index of patch item
           JSONPatchItem: the reference to the patch item
    
    kargs:
        **data**:
            An optional JSON data structure, when provided
            the actual data as selected by the patch list
            is returned. Else the paths only.

            default := None

        **scope**:
            Defines the source scope of the data structure. ::

               scope := (
                    "in"   # input data, e.g. source for "copy"
                  | "out"  # output data, e.g. target for "copy
               )

            default := "out"

Returns:
    The combined list of the selected patch items contained
    in an object JSONData.

Raises:
    None

patch_export(self, patchfile, schema=None, **kargs)

[apisource code 
Exports the current task list.

Args:
    **patchfile**:
        JSON patch for export.

    **schema**:
        JSON-Schema for validation of the patch list.

    kargs:
        **validator**: [default, draft3, off, ]
            Sets schema validator for the data file.
            The values are: ::

               default = validate
               draft3  = Draft3Validator
               off     = None

            default:= validate

        **pretty**:
            If True exports as tree format, 
            else all as one line.

Returns:
    When successful returns 'True', else raises an exception.

Raises:
    JSONDataPatchError:

patch_import(self, patchfile, schemafile=None, **kargs)

[apisource code 
Imports a task list.

Args:
    **patchfile**:
        JSON patch filename containing the list of patch operations.

    **schemafile**:
        JSON-Schema filename for validation of the patch list.

    kargs:
        **replace**:
            Replace masked characters in *target* specification. ::

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

            .. note::
            
               Match operations are proceeded literally, thus the
               escaped characters should be consistent,
               see rfc6901, Section 3.

            default := False

        **validator**:
            Sets schema validator for the data file.
            Curren release relies on *jsonschema*, which
            supports at the time of writing draft-03 and
            draft-04.

            The values are: ::

                validator := (
                      MS_DRAFT3           | 'draft3'
                    | MS_DRAFT4           | 'draft4'
                    | MS_ON               | 'on'
                    | MS_OFF              | 'off'
                    | MODE_SCHEMA_DEFAULT | 'default'
                )

            default:= MS_OFF

Returns:
    When successful returns 'True', else raises an exception.

Raises:
    JSONDataPatchError: