NTV.json_ntv.ntv_comment

Created on Sun Oct 2 22:24:59 2022

@author: philippe@loco-labs.io

The NTV.json_ntv.ntv_comment module contains the NtvComment class.

  1# -*- coding: utf-8 -*-
  2"""
  3Created on Sun Oct  2 22:24:59 2022
  4
  5@author: philippe@loco-labs.io
  6
  7The `NTV.json_ntv.ntv_comment` module contains the `NtvComment` class.
  8"""
  9import json
 10from json_ntv.ntv_patch import NtvPatch
 11
 12
 13class NtvComment:
 14    '''This class includes comments and change management methods for NTV entities :
 15
 16    *Attributes :*
 17
 18    - **_ntv** : Ntv entity being commented on
 19    - **_comments**:  list of NtvPatch to apply to the NTV entity
 20
 21    *dynamic values (@property)*
 22    - `comments`
 23    - `ntv`
 24
 25    *instance method*
 26    - `add`
 27    - `accept`
 28    - `reject`
 29    - `json`
 30    '''
 31
 32    def __init__(self, ntv, comments=None):
 33        ''' constructor
 34
 35        *Parameters*
 36
 37        - **ntv**: Ntv - NTV entity being commented on
 38        - **comments**: list of NtvPatch (default None) - comments applied to the entity
 39        '''
 40        self._ntv = ntv
 41        self._comments = []
 42        if not comments:
 43            return
 44        if comments.__class__.__name__ in ('NtvPatch', 'NtvOp'):
 45            self._comments = [NtvPatch(comments)]
 46        elif isinstance(comments, list):
 47            self._comments = [NtvPatch(comment) for comment in comments]
 48
 49    def __repr__(self):
 50        ''' string representation'''
 51        if not self._comments:
 52            return 'no comments'
 53        return json.dumps(self.json())
 54
 55    def __eq__(self, other):
 56        ''' equal if _comments and _ntv are equal'''
 57        return self.__class__.__name__ == other.__class__.__name__ and\
 58            self._ntv == other._ntv and self._comments == other._comments
 59
 60    @property
 61    def comments(self):
 62        '''getters _comments'''
 63        return self._comments
 64
 65    @property
 66    def ntv(self):
 67        '''getters _ntv'''
 68        return self._ntv
 69
 70    def add(self, comment=None):
 71        '''add comment in the list of comments
 72
 73        *Parameters*
 74
 75        - **comment**: NtvPatch (default None) - new comment to apply
 76        '''
 77        #comment = comment if isinstance(comment, (NtvPatch, NtvOp)) else NtvPatch(comment)
 78        self._comments.append(NtvPatch(comment))
 79        return len(self._comments)-1
 80
 81    def reject(self, all_comment=False):
 82        '''remove the last or all comments
 83
 84        *Parameters*
 85
 86        - **all_comment**: Boolean (default False) - if False, remove the last comment
 87
 88        *return*: resulting NtvComment
 89        '''
 90        if all_comment or len(self._comments) <= 1:
 91            return NtvComment(self._ntv, [])
 92        return NtvComment(self._ntv, self._comments[:-1])
 93
 94    def accept(self, all_comment=True):
 95        '''apply the first or all comments
 96
 97        *Parameters*
 98
 99        - **all_comment**: Boolean (default True) - if False, apply the first comment
100
101        *return*: resulting NtvComment
102        '''
103        if not self._comments:
104            return NtvComment(self._ntv, self._comments)
105        apply = self._comments if all_comment else [self._comments[0]]
106        ntv = self._ntv
107        for comment in apply:
108            ntv = comment.exe(ntv)
109        return NtvComment(ntv, [] if all_comment else self._comments[1:])
110
111    def json(self, ntv=False, comments=True):
112        ''' return json-value.
113
114        *Parameters*
115
116        - **ntv**: Boolean (default False) - if True, include ntv
117        - **comments**: Boolean (default True) - if True, include comments
118
119        *return*: resulting json-value
120        '''
121        jsn = {}
122        if self._comments and comments:
123            jsn['comments'] = [comment.json for comment in self._comments]
124        if ntv:
125            jsn['ntv'] = self._ntv.to_obj()
126        return jsn
class NtvComment:
 14class NtvComment:
 15    '''This class includes comments and change management methods for NTV entities :
 16
 17    *Attributes :*
 18
 19    - **_ntv** : Ntv entity being commented on
 20    - **_comments**:  list of NtvPatch to apply to the NTV entity
 21
 22    *dynamic values (@property)*
 23    - `comments`
 24    - `ntv`
 25
 26    *instance method*
 27    - `add`
 28    - `accept`
 29    - `reject`
 30    - `json`
 31    '''
 32
 33    def __init__(self, ntv, comments=None):
 34        ''' constructor
 35
 36        *Parameters*
 37
 38        - **ntv**: Ntv - NTV entity being commented on
 39        - **comments**: list of NtvPatch (default None) - comments applied to the entity
 40        '''
 41        self._ntv = ntv
 42        self._comments = []
 43        if not comments:
 44            return
 45        if comments.__class__.__name__ in ('NtvPatch', 'NtvOp'):
 46            self._comments = [NtvPatch(comments)]
 47        elif isinstance(comments, list):
 48            self._comments = [NtvPatch(comment) for comment in comments]
 49
 50    def __repr__(self):
 51        ''' string representation'''
 52        if not self._comments:
 53            return 'no comments'
 54        return json.dumps(self.json())
 55
 56    def __eq__(self, other):
 57        ''' equal if _comments and _ntv are equal'''
 58        return self.__class__.__name__ == other.__class__.__name__ and\
 59            self._ntv == other._ntv and self._comments == other._comments
 60
 61    @property
 62    def comments(self):
 63        '''getters _comments'''
 64        return self._comments
 65
 66    @property
 67    def ntv(self):
 68        '''getters _ntv'''
 69        return self._ntv
 70
 71    def add(self, comment=None):
 72        '''add comment in the list of comments
 73
 74        *Parameters*
 75
 76        - **comment**: NtvPatch (default None) - new comment to apply
 77        '''
 78        #comment = comment if isinstance(comment, (NtvPatch, NtvOp)) else NtvPatch(comment)
 79        self._comments.append(NtvPatch(comment))
 80        return len(self._comments)-1
 81
 82    def reject(self, all_comment=False):
 83        '''remove the last or all comments
 84
 85        *Parameters*
 86
 87        - **all_comment**: Boolean (default False) - if False, remove the last comment
 88
 89        *return*: resulting NtvComment
 90        '''
 91        if all_comment or len(self._comments) <= 1:
 92            return NtvComment(self._ntv, [])
 93        return NtvComment(self._ntv, self._comments[:-1])
 94
 95    def accept(self, all_comment=True):
 96        '''apply the first or all comments
 97
 98        *Parameters*
 99
100        - **all_comment**: Boolean (default True) - if False, apply the first comment
101
102        *return*: resulting NtvComment
103        '''
104        if not self._comments:
105            return NtvComment(self._ntv, self._comments)
106        apply = self._comments if all_comment else [self._comments[0]]
107        ntv = self._ntv
108        for comment in apply:
109            ntv = comment.exe(ntv)
110        return NtvComment(ntv, [] if all_comment else self._comments[1:])
111
112    def json(self, ntv=False, comments=True):
113        ''' return json-value.
114
115        *Parameters*
116
117        - **ntv**: Boolean (default False) - if True, include ntv
118        - **comments**: Boolean (default True) - if True, include comments
119
120        *return*: resulting json-value
121        '''
122        jsn = {}
123        if self._comments and comments:
124            jsn['comments'] = [comment.json for comment in self._comments]
125        if ntv:
126            jsn['ntv'] = self._ntv.to_obj()
127        return jsn

This class includes comments and change management methods for NTV entities :

Attributes :

  • _ntv : Ntv entity being commented on
  • _comments: list of NtvPatch to apply to the NTV entity

dynamic values (@property)

instance method

NtvComment(ntv, comments=None)
33    def __init__(self, ntv, comments=None):
34        ''' constructor
35
36        *Parameters*
37
38        - **ntv**: Ntv - NTV entity being commented on
39        - **comments**: list of NtvPatch (default None) - comments applied to the entity
40        '''
41        self._ntv = ntv
42        self._comments = []
43        if not comments:
44            return
45        if comments.__class__.__name__ in ('NtvPatch', 'NtvOp'):
46            self._comments = [NtvPatch(comments)]
47        elif isinstance(comments, list):
48            self._comments = [NtvPatch(comment) for comment in comments]

constructor

Parameters

  • ntv: Ntv - NTV entity being commented on
  • comments: list of NtvPatch (default None) - comments applied to the entity
comments

getters _comments

ntv

getters _ntv

def add(self, comment=None):
71    def add(self, comment=None):
72        '''add comment in the list of comments
73
74        *Parameters*
75
76        - **comment**: NtvPatch (default None) - new comment to apply
77        '''
78        #comment = comment if isinstance(comment, (NtvPatch, NtvOp)) else NtvPatch(comment)
79        self._comments.append(NtvPatch(comment))
80        return len(self._comments)-1

add comment in the list of comments

Parameters

  • comment: NtvPatch (default None) - new comment to apply
def reject(self, all_comment=False):
82    def reject(self, all_comment=False):
83        '''remove the last or all comments
84
85        *Parameters*
86
87        - **all_comment**: Boolean (default False) - if False, remove the last comment
88
89        *return*: resulting NtvComment
90        '''
91        if all_comment or len(self._comments) <= 1:
92            return NtvComment(self._ntv, [])
93        return NtvComment(self._ntv, self._comments[:-1])

remove the last or all comments

Parameters

  • all_comment: Boolean (default False) - if False, remove the last comment

return: resulting NtvComment

def accept(self, all_comment=True):
 95    def accept(self, all_comment=True):
 96        '''apply the first or all comments
 97
 98        *Parameters*
 99
100        - **all_comment**: Boolean (default True) - if False, apply the first comment
101
102        *return*: resulting NtvComment
103        '''
104        if not self._comments:
105            return NtvComment(self._ntv, self._comments)
106        apply = self._comments if all_comment else [self._comments[0]]
107        ntv = self._ntv
108        for comment in apply:
109            ntv = comment.exe(ntv)
110        return NtvComment(ntv, [] if all_comment else self._comments[1:])

apply the first or all comments

Parameters

  • all_comment: Boolean (default True) - if False, apply the first comment

return: resulting NtvComment

def json(self, ntv=False, comments=True):
112    def json(self, ntv=False, comments=True):
113        ''' return json-value.
114
115        *Parameters*
116
117        - **ntv**: Boolean (default False) - if True, include ntv
118        - **comments**: Boolean (default True) - if True, include comments
119
120        *return*: resulting json-value
121        '''
122        jsn = {}
123        if self._comments and comments:
124            jsn['comments'] = [comment.json for comment in self._comments]
125        if ntv:
126            jsn['ntv'] = self._ntv.to_obj()
127        return jsn

return json-value.

Parameters

  • ntv: Boolean (default False) - if True, include ntv
  • comments: Boolean (default True) - if True, include comments

return: resulting json-value