refactor,feature: simplified item method for 'SSHKeyCollection', added iteration and str methods to it, and refined str methods for 'SSHKey'
This commit is contained in:
@@ -46,7 +46,8 @@ class SSHKey:
|
|||||||
return self.__idx
|
return self.__idx
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
return str(self.__value)
|
key_basename = Path(str(self.__value)).name
|
||||||
|
return "🔑" + key_basename
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return "SSHKey(" + str(self.__value) + ")"
|
return "SSHKey(" + str(self.__value) + ")"
|
||||||
@@ -61,7 +62,7 @@ class SSHKey:
|
|||||||
case "int":
|
case "int":
|
||||||
return str(self.__idx)
|
return str(self.__idx)
|
||||||
case _:
|
case _:
|
||||||
return str(self.__value)
|
return str(self)
|
||||||
|
|
||||||
def __next__(self) -> ExecutedPath | tuple[ExecutedPath]:
|
def __next__(self) -> ExecutedPath | tuple[ExecutedPath]:
|
||||||
return self.__next
|
return self.__next
|
||||||
@@ -118,15 +119,13 @@ class SSHKey:
|
|||||||
if isinstance(self.__value, tuple):
|
if isinstance(self.__value, tuple):
|
||||||
raise ValueError
|
raise ValueError
|
||||||
|
|
||||||
if isinstance(other, Self):
|
if isinstance(other, (str, ExecutedPath)):
|
||||||
|
result = self.update(self.__value, other)
|
||||||
|
else:
|
||||||
if isinstance(other.__SSHKey__value, tuple):
|
if isinstance(other.__SSHKey__value, tuple):
|
||||||
raise ValueError
|
raise ValueError
|
||||||
|
|
||||||
result = self.update(self.__value, other._SSHKey__value)
|
result = self.update(self.__value, other._SSHKey__value)
|
||||||
elif isinstance(other, (str, ExecutedPath)):
|
|
||||||
result = self.update(self.__value, other)
|
|
||||||
else:
|
|
||||||
raise TypeError
|
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@@ -134,15 +133,13 @@ class SSHKey:
|
|||||||
if isinstance(self.__value, tuple):
|
if isinstance(self.__value, tuple):
|
||||||
raise ValueError
|
raise ValueError
|
||||||
|
|
||||||
if isinstance(other, Self):
|
if isinstance(other, (str, ExecutedPath)):
|
||||||
|
result = self.update(other, self.__value)
|
||||||
|
else:
|
||||||
if isinstance(other.__SSHKey__value, tuple):
|
if isinstance(other.__SSHKey__value, tuple):
|
||||||
raise ValueError
|
raise ValueError
|
||||||
|
|
||||||
result = self.update(other._SSHKey__value, self.__value)
|
result = self.update(other._SSHKey__value, self.__value)
|
||||||
elif isinstance(other, (str, ExecutedPath)):
|
|
||||||
result = self.update(other, self.__value)
|
|
||||||
else:
|
|
||||||
raise TypeError
|
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@@ -262,7 +259,7 @@ class SSHKeyCollection(Sequence):
|
|||||||
self.__last: SSHKey | None = None
|
self.__last: SSHKey | None = None
|
||||||
self.__indices: range | None = None
|
self.__indices: range | None = None
|
||||||
|
|
||||||
# @TODO adjust item magic methods for slicing purposes
|
# @TODO have other item magic methods mimic this one for slicing purposes
|
||||||
def __getitem__(self, key: int | slice) -> SSHKey | Never:
|
def __getitem__(self, key: int | slice) -> SSHKey | Never:
|
||||||
self.__current = self.__first
|
self.__current = self.__first
|
||||||
|
|
||||||
@@ -283,29 +280,38 @@ class SSHKeyCollection(Sequence):
|
|||||||
elif isinstance(key, slice):
|
elif isinstance(key, slice):
|
||||||
step = key.step
|
step = key.step
|
||||||
sshkcoll = SSHKeyCollection()
|
sshkcoll = SSHKeyCollection()
|
||||||
range_args = []
|
if hasattr(key, "start"):
|
||||||
if hasattr(key, "start") and getattr(key, "start") is not None:
|
if getattr(key, "start") is None:
|
||||||
range_args.append(key.start)
|
start = 0
|
||||||
if hasattr(key, "stop") and getattr(key, "stop") is not None:
|
else:
|
||||||
range_args.append(key.stop)
|
start = key.start
|
||||||
if hasattr(key, "step") and getattr(key, "step") is not None:
|
if hasattr(key, "stop"):
|
||||||
range_args.append(key.step)
|
if getattr(key, "stop") is None:
|
||||||
indices = range(*range_args)
|
stop = len(self.__indices)
|
||||||
|
else:
|
||||||
|
stop = key.stop
|
||||||
|
if hasattr(key, "step"):
|
||||||
|
if getattr(key, "step") is None:
|
||||||
|
step = 1
|
||||||
|
else:
|
||||||
|
step = key.step
|
||||||
|
indices = range(start, stop, step)
|
||||||
# test_coll = []
|
# test_coll = []
|
||||||
while int(self.__current) < key.stop:
|
while int(self.__current) < stop:
|
||||||
if self.__current is None:
|
if int(self.__current) < start:
|
||||||
raise KeyError
|
|
||||||
elif int(self.__current) < key.start:
|
|
||||||
continue
|
continue
|
||||||
elif int(self.__current) >= key.start:
|
elif int(self.__current) >= start:
|
||||||
if int(self.__current) in indices:
|
if int(self.__current) in indices:
|
||||||
sshkcoll.append(self.__current)
|
sshkcoll.append(self.__current)
|
||||||
test_coll.append(self.__current)
|
# test_coll.append(self.__current)
|
||||||
else:
|
else:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
self.__current = next(self.__current)
|
self.__current = next(self.__current)
|
||||||
|
|
||||||
|
if self.__current is None:
|
||||||
|
break
|
||||||
|
|
||||||
# print(test_coll)
|
# print(test_coll)
|
||||||
result = sshkcoll
|
result = sshkcoll
|
||||||
|
|
||||||
@@ -484,11 +490,15 @@ class SSHKeyCollection(Sequence):
|
|||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def __next__(self):
|
def __next__(self):
|
||||||
raise NotImplementedError
|
self.__current = next(self.__current)
|
||||||
|
if self.__current is not None:
|
||||||
|
return self.__current
|
||||||
|
raise StopIteration
|
||||||
|
|
||||||
def __iter__(self) -> Self | Never:
|
def __iter__(self) -> Self | Never:
|
||||||
self.__Current = self.__first
|
self.__current = self.__first
|
||||||
return self.__current
|
# return self.__current
|
||||||
|
return self
|
||||||
|
|
||||||
def count(self, query: RegEx | str) -> int | Never:
|
def count(self, query: RegEx | str) -> int | Never:
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
@@ -515,6 +525,22 @@ class SSHKeyCollection(Sequence):
|
|||||||
def sort(self, key: Callable = (lambda e: e), reverse: bool = False) -> None | Never:
|
def sort(self, key: Callable = (lambda e: e), reverse: bool = False) -> None | Never:
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
prefix = "[("
|
||||||
|
postfix = "|]"
|
||||||
|
|
||||||
|
self.__current = self.__first
|
||||||
|
|
||||||
|
concat = lambda s: str(s)[1:] + ", "
|
||||||
|
content = str()
|
||||||
|
count = 0
|
||||||
|
while self.__current is not None:
|
||||||
|
content += str(count) + ">" + concat(self.__current)
|
||||||
|
self.__current = next(self.__current)
|
||||||
|
count += 1
|
||||||
|
content = content[0:len(content)-2]
|
||||||
|
|
||||||
|
return prefix + content + postfix
|
||||||
|
|
||||||
# @TODO maybe move to separate module for classes for handling users and groups
|
# @TODO maybe move to separate module for classes for handling users and groups
|
||||||
class UserSSH:
|
class UserSSH:
|
||||||
@@ -527,5 +553,8 @@ class UserSSH:
|
|||||||
|
|
||||||
sshkey_coll = SSHKeyCollection()
|
sshkey_coll = SSHKeyCollection()
|
||||||
sshkey_coll.pull()
|
sshkey_coll.pull()
|
||||||
# print(len(sshkey_coll) + 2)
|
print(len(sshkey_coll))
|
||||||
print(sshkey_coll[0:4][3])
|
print(sshkey_coll[0:len(sshkey_coll)])
|
||||||
|
|
||||||
|
for k in sshkey_coll:
|
||||||
|
print(k)
|
||||||
|
|||||||
Reference in New Issue
Block a user