-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnode.py
72 lines (50 loc) · 1.88 KB
/
node.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import uuid
class Node(object):
(ADD, DELETE, INSERT) = range(3)
def __init__(self, tag=None, identifier=None, expanded=True):
self.tag = tag
self._identifier = self.set_identifier(identifier)
self.expanded = expanded
self._bpointer = []
self._fpointer = []
@classmethod
def sanitize_id(cls, identifier):
return str(identifier).strip().replace(" ", "_")
def set_identifier(self, identifier):
if identifier is None:
return str(uuid.uuid1())
else:
return self.sanitize_id(identifier)
@property
def identifier(self):
return self._identifier
@property
def bpointer(self):
return self._bpointer
@bpointer.setter
def bpointer(self, value):
if value is not None and isinstance(value, list):
self._bpointer = value
@property
def fpointer(self):
return self._fpointer
@fpointer.setter
def fpointer(self, value):
if value is not None and isinstance(value, list):
self._fpointer = value
def update_fpointer(self, identifier, mode=ADD):
if mode is self.ADD:
if self.sanitize_id(identifier) not in self._fpointer:
self._fpointer.append(self.sanitize_id(identifier))
elif mode is self.DELETE:
self._fpointer.remove(self.sanitize_id(identifier))
elif mode is self.INSERT:
self._fpointer = [self.sanitize_id(identifier)]
def update_bpointer(self, identifier, mode=ADD):
if mode is self.ADD:
if self.sanitize_id(identifier) not in self._bpointer:
self._bpointer.append(self.sanitize_id(identifier))
elif mode is self.DELETE:
self._bpointer.remove(self.sanitize_id(identifier))
elif mode is self.INSERT:
self._bpointer = [self.sanitize_id(identifier)]