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 copy
10from json_ntv.ntv import NtvSingle, NtvType, NtvList, Ntv
11
12class NtvComment:
13    '''this class includes comments and change management methods for NTV entities :
14
15    - `NtvComment.add_comment`
16    - `NtvComment.accept_comment`
17    - `NtvComment.reject_comment`
18    - `NtvComment.show_comment`
19    '''
20    def __init__(self, ntv):
21        ''' the parameter of the constructor is the Ntv entity'''
22        self._ntv = ntv
23
24    def add_comment(self, text, val=None, name=None, typ=None):
25        '''add a comment (text) and a proposal for a new NTV entity defined by (val, name and typ)'''
26        parent = self._ntv.parent
27        if (val, typ, name) == (None, None, None):
28            comment = NtvSingle(text, ntv_type='$comment')
29        else:            
30            if self._ntv.type_str == '$history':
31                new_self = copy.copy(self._ntv.ntv_value[0])
32            else:
33                new_self = copy.copy(self._ntv)
34            new_self.ntv_value = new_self.ntv_value if val is None else val 
35            new_self.ntv_type = new_self.ntv_type if typ is None else NtvType(typ) 
36            new_self.ntv_name = new_self.ntv_name if name is None else name
37            comment = NtvSingle(new_self, text)
38        if self._ntv.type_str == '$history':
39            self._ntv.ntv_value.append(comment)
40            return
41        com_val = [self._ntv]
42        com_val.append(comment)
43        com_list = NtvList(com_val, ntv_type=NtvType('$history'), ntv_name=self._ntv.name)
44        parent[parent.ntv_value.index(self._ntv)] = com_list
45        return
46    
47    def reject_comment(self):
48        '''delete all the comments'''
49        parent = self._ntv.parent
50        if self._ntv.type_str != '$history':   
51            return
52        old_ntv = self._ntv.ntv_value[0]
53        parent[parent.ntv_value.index(self._ntv)] = old_ntv
54
55    def accept_comment(self):
56        ''' replace the NTV entity by the last NTV proposal and delete all comments''' 
57        parent = self._ntv.parent
58        if self._ntv.type_str != '$history':   
59            return
60        for ntv in reversed(self._ntv.ntv_value):
61            if ntv.type_str != '$comment':
62                new_ntv = ntv
63                break
64        new_ntv = Ntv.obj(new_ntv.ntv_value) if new_ntv.type_str == 'ntv' else new_ntv
65        parent[parent.ntv_value.index(self._ntv)] = new_ntv
66
67    def show_comment(self):
68        ''' return a dict with comments for each commented node'''
69        return NtvList([node for node in self._ntv.tree if node.type_str == '$history']).to_obj()
class NtvComment:
13class NtvComment:
14    '''this class includes comments and change management methods for NTV entities :
15
16    - `NtvComment.add_comment`
17    - `NtvComment.accept_comment`
18    - `NtvComment.reject_comment`
19    - `NtvComment.show_comment`
20    '''
21    def __init__(self, ntv):
22        ''' the parameter of the constructor is the Ntv entity'''
23        self._ntv = ntv
24
25    def add_comment(self, text, val=None, name=None, typ=None):
26        '''add a comment (text) and a proposal for a new NTV entity defined by (val, name and typ)'''
27        parent = self._ntv.parent
28        if (val, typ, name) == (None, None, None):
29            comment = NtvSingle(text, ntv_type='$comment')
30        else:            
31            if self._ntv.type_str == '$history':
32                new_self = copy.copy(self._ntv.ntv_value[0])
33            else:
34                new_self = copy.copy(self._ntv)
35            new_self.ntv_value = new_self.ntv_value if val is None else val 
36            new_self.ntv_type = new_self.ntv_type if typ is None else NtvType(typ) 
37            new_self.ntv_name = new_self.ntv_name if name is None else name
38            comment = NtvSingle(new_self, text)
39        if self._ntv.type_str == '$history':
40            self._ntv.ntv_value.append(comment)
41            return
42        com_val = [self._ntv]
43        com_val.append(comment)
44        com_list = NtvList(com_val, ntv_type=NtvType('$history'), ntv_name=self._ntv.name)
45        parent[parent.ntv_value.index(self._ntv)] = com_list
46        return
47    
48    def reject_comment(self):
49        '''delete all the comments'''
50        parent = self._ntv.parent
51        if self._ntv.type_str != '$history':   
52            return
53        old_ntv = self._ntv.ntv_value[0]
54        parent[parent.ntv_value.index(self._ntv)] = old_ntv
55
56    def accept_comment(self):
57        ''' replace the NTV entity by the last NTV proposal and delete all comments''' 
58        parent = self._ntv.parent
59        if self._ntv.type_str != '$history':   
60            return
61        for ntv in reversed(self._ntv.ntv_value):
62            if ntv.type_str != '$comment':
63                new_ntv = ntv
64                break
65        new_ntv = Ntv.obj(new_ntv.ntv_value) if new_ntv.type_str == 'ntv' else new_ntv
66        parent[parent.ntv_value.index(self._ntv)] = new_ntv
67
68    def show_comment(self):
69        ''' return a dict with comments for each commented node'''
70        return NtvList([node for node in self._ntv.tree if node.type_str == '$history']).to_obj()

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

NtvComment(ntv)
21    def __init__(self, ntv):
22        ''' the parameter of the constructor is the Ntv entity'''
23        self._ntv = ntv

the parameter of the constructor is the Ntv entity

def add_comment(self, text, val=None, name=None, typ=None):
25    def add_comment(self, text, val=None, name=None, typ=None):
26        '''add a comment (text) and a proposal for a new NTV entity defined by (val, name and typ)'''
27        parent = self._ntv.parent
28        if (val, typ, name) == (None, None, None):
29            comment = NtvSingle(text, ntv_type='$comment')
30        else:            
31            if self._ntv.type_str == '$history':
32                new_self = copy.copy(self._ntv.ntv_value[0])
33            else:
34                new_self = copy.copy(self._ntv)
35            new_self.ntv_value = new_self.ntv_value if val is None else val 
36            new_self.ntv_type = new_self.ntv_type if typ is None else NtvType(typ) 
37            new_self.ntv_name = new_self.ntv_name if name is None else name
38            comment = NtvSingle(new_self, text)
39        if self._ntv.type_str == '$history':
40            self._ntv.ntv_value.append(comment)
41            return
42        com_val = [self._ntv]
43        com_val.append(comment)
44        com_list = NtvList(com_val, ntv_type=NtvType('$history'), ntv_name=self._ntv.name)
45        parent[parent.ntv_value.index(self._ntv)] = com_list
46        return

add a comment (text) and a proposal for a new NTV entity defined by (val, name and typ)

def reject_comment(self):
48    def reject_comment(self):
49        '''delete all the comments'''
50        parent = self._ntv.parent
51        if self._ntv.type_str != '$history':   
52            return
53        old_ntv = self._ntv.ntv_value[0]
54        parent[parent.ntv_value.index(self._ntv)] = old_ntv

delete all the comments

def accept_comment(self):
56    def accept_comment(self):
57        ''' replace the NTV entity by the last NTV proposal and delete all comments''' 
58        parent = self._ntv.parent
59        if self._ntv.type_str != '$history':   
60            return
61        for ntv in reversed(self._ntv.ntv_value):
62            if ntv.type_str != '$comment':
63                new_ntv = ntv
64                break
65        new_ntv = Ntv.obj(new_ntv.ntv_value) if new_ntv.type_str == 'ntv' else new_ntv
66        parent[parent.ntv_value.index(self._ntv)] = new_ntv

replace the NTV entity by the last NTV proposal and delete all comments

def show_comment(self):
68    def show_comment(self):
69        ''' return a dict with comments for each commented node'''
70        return NtvList([node for node in self._ntv.tree if node.type_str == '$history']).to_obj()

return a dict with comments for each commented node