forked from vemel/handsdown
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport_string.py
More file actions
206 lines (142 loc) · 4.4 KB
/
Copy pathimport_string.py
File metadata and controls
206 lines (142 loc) · 4.4 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
"""Wrapper for python import strings."""
from typing import TypeVar
from handsdown.exceptions import ImportStringError
_R = TypeVar("_R", bound="ImportString")
class ImportString:
"""
Wrapper for python import strings.
Arguments:
value -- Import string.
"""
def __init__(self, value: str) -> None:
self.value = value
def __str__(self) -> str:
"""
Get string value.
Examples::
str(ImportString("my_module"))
"my_module"
Returns:
Original import string.
"""
return self.value
def __hash__(self) -> int:
return hash(self.value)
def __add__(self, other: str) -> "ImportString":
"""
Add new import part.
Examples::
ImportString("my_module") + "MyClass"
ImportString("my_module.MyClass")
ImportString("") + "MyClass"
ImportString("MyClass")
Arguments:
other -- Import string part.
Returns:
A new `ImportString` instance.
"""
if self.value:
return ImportString(f"{self.value}.{other}")
return ImportString(other)
def __bool__(self) -> bool:
"""
Check if not empty.
Examples::
bool(ImportString("my_module"))
True
bool(ImportString(""))
False
Returns:
True if not empty.
"""
return bool(self.value)
def __eq__(self, other: object) -> bool:
"""
Compare to another `ImportString` or a string.
Examples::
ImportString("my_module.MyClass") == ImportString("my_module.MyClass")
True
ImportString("my_module.MyClass") == ImportString("my_module.OtherClass")
False
ImportString("my_module.MyClass") == "my_module.MyClass"
True
ImportString("my_module.MyClass") == "my_module"
False
ImportString("my_module.MyClass") == b"my_module.MyClass"
False
Arguments:
other - ImportString instance or a string.
Returns:
True if import strings are equal.
"""
if isinstance(other, str):
return self.value == other
if isinstance(other, ImportString):
return self.value == other.value
return False
@property
def parts(self) -> list[str]:
"""
Parts of import string splitted by dots.
Examples::
ImportString("my_module.MyClass")
["my_module", "MyClass"]
ImportString("")
[]
Returns:
A list of import string parts.
"""
return self.value.split(".")
def is_top_level(self) -> bool:
"""
Check if import string has no parents.
Returns:
True if it has no parents.
"""
return "." not in self.value
@property
def parent(self: _R) -> _R:
"""
Parent import string.
Returns:
A new `ImportString` instance.
"""
if self.is_top_level():
msg = "Import string is top level and has no parents."
raise ImportStringError(msg)
parent_import_string_parts = self.value.split(".")[:-1]
return self.__class__(".".join(parent_import_string_parts))
def startswith(self: _R, import_string: _R) -> bool:
"""
Check if it starts with `import_string`.
Returns:
True if it is a child.
"""
return self.value.startswith(f"{import_string}.")
def get_parents(self: _R) -> list[_R]:
"""
Get all parents.
Returns:
A list of `ImportString` instances.
"""
if self.is_top_level():
return []
parents = []
import_string = self
while not import_string.is_top_level():
parents.append(import_string.parent)
import_string = import_string.parent
parents.reverse()
return parents
@property
def length(self) -> int:
"""
Length of import string parts.
Returns:
Length of import string.
"""
return len(self.parts)
@property
def name(self) -> str:
"""Last part of the import string."""
return self.parts[-1] if self.parts else "empty"