-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodes.py
107 lines (77 loc) · 3.35 KB
/
nodes.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from abc import ABC, abstractmethod
class NodeBase(ABC):
_type_display_name = "Node Base"
"""A string representing a human-readable name for the type of node."""
def __init__(self, parent: "NodeBase" = None) -> None:
super().__init__()
self.parent = parent
def get_depth(self) -> int:
"""Returns an integer representing the amount of nodes that were processed before to lead to this node."""
return 0 if self.parent is None else self.parent.get_depth() + 1
@abstractmethod
def equals(self, other) -> bool:
"""Returns True if the given node is equal to this node, False otherwise."""
pass
@staticmethod
@abstractmethod
def parse(*args):
"""Returns a new instance of the node based on the given arguments."""
pass
@abstractmethod
def __repr__(self):
pass
class GenericText(NodeBase):
_type_display_name = "Generic Text"
def __init__(self, text: str, parent: NodeBase = None) -> None:
super().__init__(parent)
self.text = text
def equals(self, other) -> bool: return self.text == other.text if type(other) == GenericText else False
def __repr__(self): return self.text
@staticmethod
def parse(*args): return GenericText(args[0])
class Username(NodeBase):
_type_display_name = "Username"
def __init__(self, username: str, parent: NodeBase = None) -> None:
super().__init__(parent)
self.username = username
def equals(self, other): return self.username == other.username if type(other) == Username else False
def __repr__(self): return self.username
@staticmethod
def parse(*args): return Username(args[0])
class RealName(NodeBase):
_type_display_name = "Real Name"
def __init__(self, name: str, parent: NodeBase = None) -> None:
super().__init__(parent)
self.name = name
def equals(self, other): return self.name == other.name if type(other) == RealName else False
def __repr__(self): return self.name
@staticmethod
def parse(*args): return RealName(args[0])
class Email(NodeBase):
_type_display_name = "Email Address"
def __init__(self, email: str, parent: NodeBase = None) -> None:
super().__init__(parent)
self.email = email
def equals(self, other): return self.email == other.email if type(other) == Email else False
def __repr__(self): return self.email
@staticmethod
def parse(*args): return Email(args[0])
class Website(NodeBase):
_type_display_name = "Website"
def __init__(self, url: str, parent: NodeBase = None) -> None:
super().__init__(parent)
self.url = url
self.domain = url.split("//")[1].split("/")[0] if "//" in url else url.split("/")[0]
def equals(self, other) -> bool: return self.domain == other.domain if type(other) == Website else False
def __repr__(self): return self.domain
@staticmethod
def parse(*args): return Website(args[0])
class Location(NodeBase):
_type_display_name = "Location"
def __init__(self, location: str, parent: NodeBase = None) -> None:
super().__init__(parent)
self.location = location
def equals(self, other) -> bool: return self.location == other.location if type(other) == Location else False
def __repr__(self): return self.location
@staticmethod
def parse(*args): return Location(args[0])