From 67e645a3bdcbe58b101b9aba9f8c26d6f228195c Mon Sep 17 00:00:00 2001 From: Alex Tavarez Date: Wed, 14 Jan 2026 19:36:27 -0500 Subject: [PATCH] refactor,feature: simplified item method for 'SSHKeyCollection', added iteration and str methods to it, and refined str methods for 'SSHKey' --- sshkey_man.py | 93 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 61 insertions(+), 32 deletions(-) diff --git a/sshkey_man.py b/sshkey_man.py index 898b727..f2e0444 100644 --- a/sshkey_man.py +++ b/sshkey_man.py @@ -46,7 +46,8 @@ class SSHKey: return self.__idx def __str__(self) -> str: - return str(self.__value) + key_basename = Path(str(self.__value)).name + return "🔑" + key_basename def __repr__(self) -> str: return "SSHKey(" + str(self.__value) + ")" @@ -61,7 +62,7 @@ class SSHKey: case "int": return str(self.__idx) case _: - return str(self.__value) + return str(self) def __next__(self) -> ExecutedPath | tuple[ExecutedPath]: return self.__next @@ -118,15 +119,13 @@ class SSHKey: if isinstance(self.__value, tuple): raise ValueError - if isinstance(other, Self): + if isinstance(other, (str, ExecutedPath)): + result = self.update(self.__value, other) + else: if isinstance(other.__SSHKey__value, tuple): raise ValueError result = self.update(self.__value, other._SSHKey__value) - elif isinstance(other, (str, ExecutedPath)): - result = self.update(self.__value, other) - else: - raise TypeError return result @@ -134,15 +133,13 @@ class SSHKey: if isinstance(self.__value, tuple): raise ValueError - if isinstance(other, Self): + if isinstance(other, (str, ExecutedPath)): + result = self.update(other, self.__value) + else: if isinstance(other.__SSHKey__value, tuple): raise ValueError result = self.update(other._SSHKey__value, self.__value) - elif isinstance(other, (str, ExecutedPath)): - result = self.update(other, self.__value) - else: - raise TypeError return result @@ -262,7 +259,7 @@ class SSHKeyCollection(Sequence): self.__last: SSHKey | 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: self.__current = self.__first @@ -283,29 +280,38 @@ class SSHKeyCollection(Sequence): elif isinstance(key, slice): step = key.step sshkcoll = SSHKeyCollection() - range_args = [] - if hasattr(key, "start") and getattr(key, "start") is not None: - range_args.append(key.start) - if hasattr(key, "stop") and getattr(key, "stop") is not None: - range_args.append(key.stop) - if hasattr(key, "step") and getattr(key, "step") is not None: - range_args.append(key.step) - indices = range(*range_args) + if hasattr(key, "start"): + if getattr(key, "start") is None: + start = 0 + else: + start = key.start + if hasattr(key, "stop"): + if getattr(key, "stop") is None: + 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 = [] - while int(self.__current) < key.stop: - if self.__current is None: - raise KeyError - elif int(self.__current) < key.start: + while int(self.__current) < stop: + if int(self.__current) < start: continue - elif int(self.__current) >= key.start: + elif int(self.__current) >= start: if int(self.__current) in indices: sshkcoll.append(self.__current) - test_coll.append(self.__current) + # test_coll.append(self.__current) else: continue self.__current = next(self.__current) + if self.__current is None: + break + # print(test_coll) result = sshkcoll @@ -484,11 +490,15 @@ class SSHKeyCollection(Sequence): raise NotImplementedError 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: - self.__Current = self.__first - return self.__current + self.__current = self.__first + # return self.__current + return self def count(self, query: RegEx | str) -> int | Never: raise NotImplementedError @@ -515,6 +525,22 @@ class SSHKeyCollection(Sequence): def sort(self, key: Callable = (lambda e: e), reverse: bool = False) -> None | Never: 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 class UserSSH: @@ -527,5 +553,8 @@ class UserSSH: sshkey_coll = SSHKeyCollection() sshkey_coll.pull() -# print(len(sshkey_coll) + 2) -print(sshkey_coll[0:4][3]) +print(len(sshkey_coll)) +print(sshkey_coll[0:len(sshkey_coll)]) + +for k in sshkey_coll: + print(k)