diff --git a/sshkey_man.py b/sshkey_man.py index e7af3a8..898b727 100644 --- a/sshkey_man.py +++ b/sshkey_man.py @@ -10,7 +10,7 @@ from typing import TypedDict as Dict from glob import glob as globbify from whereami import USER_PATH from softman import Softs -import os +# import os class RootFate(Enum): disposal = 0 @@ -225,7 +225,8 @@ class SSHKey: def reverse(self) -> Never: if isinstance(self.__value, tuple): - v1, v2 = *self.__value + v1 = self.__value[0] + v2 = self.__value[1] result = self.update(v2, v1) else: result = self @@ -253,6 +254,7 @@ def stream_unequal(s1, s2) -> bool | Never: # @TODO create unit tests for below class class SSHKeyCollection(Sequence): __user_path: ExecutedPath = USER_PATH + __ssh_path: ExecutedPath = __user_path / ".ssh" def __init__(self): self.__current: SSHKey | None = None @@ -260,21 +262,54 @@ class SSHKeyCollection(Sequence): self.__last: SSHKey | None = None self.__indices: range | None = None - def __getitem__(self, key: int) -> SSHKey | Never: + # @TODO adjust item magic methods for slicing purposes + def __getitem__(self, key: int | slice) -> SSHKey | Never: self.__current = self.__first - + if self.__current is None: raise KeyError - elif int(self.__current) == key: - return self.__current - else: - while int(self.__current) != key: - self.__current = next(self.__current) + if isinstance(key, int): + if int(self.__current) == key: + return self.__current + else: + while int(self.__current) != key: + if self.__current is None: + raise KeyError + + self.__current = next(self.__current) + + result = self.__current + 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) + # test_coll = [] + while int(self.__current) < key.stop: if self.__current is None: raise KeyError + elif int(self.__current) < key.start: + continue + elif int(self.__current) >= key.start: + if int(self.__current) in indices: + sshkcoll.append(self.__current) + test_coll.append(self.__current) + else: + continue - return self.__current + self.__current = next(self.__current) + + # print(test_coll) + result = sshkcoll + + return result def __len__(self) -> int: if self.__indices is None: @@ -364,64 +399,72 @@ class SSHKeyCollection(Sequence): #print(self.__current) return self.__current - def __setitem__(self, key: int, *value: ExecutedPath | str) -> None | Never: + def __setitem__(self, key: int | slice, *value: ExecutedPath | str) -> None | Never: if len(value) < 1 or len(value) > 2: raise ValueError value = tuple(map(lambda s: Path(s) if isinstance(s, str) else s, value)) self.__current = self.__first + if self.__current is None: raise KeyError - elif int(self.__current) == key: - if self.__current() is None or len(self.__current()) < 1: - self.__current(*value) - else: + + if isinstance(key, int): if int(self.__current) == key: - return self.__current(*value) + if self.__current() is None or len(self.__current()) < 1: + self.__current(*value) + else: + if int(self.__current) == key: + return self.__current(*value) - while int(self.__current) != key: - self.__current = next(self.__current) + while int(self.__current) != key: + if self.__current is None: + raise KeyError - if self.__current is None: - raise KeyError + self.__current = next(self.__current) - self.__current(*value) + self.__current(*value) + elif isinstance(key, slice): + raise NotImplementedError - def __delitem__(self, key: int) -> None | Never: + def __delitem__(self, key: int | slice) -> None | Never: self.__current = self.__first if self.__current is None: raise KeyError - if key == -1: - if self.__last is not None: - self.__last._SSHKey__prev._SSHKey__next = None - self.__last = self.__last._SSHKey__prev + if isinstance(key, int): + if key == -1: + if self.__last is not None: + self.__last._SSHKey__prev._SSHKey__next = None + self.__last = self.__last._SSHKey__prev - self.__current = self.__last + self.__current = self.__last + else: + self.__first = None + return past + elif key <= -2: + raise NotImplementedError else: - self.__first = None - return past - elif key <= -2: + while int(self.__current) != key: + self.__current = next(self.__current) + + if self.__current is None: + raise KeyError + + count = self.__current._SSHKey__idx + prior = self.__current._SSHKey__prev + posterior = self.__current._SSHKey__next + posterior._SSHKey__prev = prior + posterior._SSHKey__prev._SSHKey__next = posterior + self.__current = posterior + while self.__current is not None: + self.__current._SSHKey__idx = count + self.__current = next(self.__current) + count += 1 + elif isinstance(key, slice): raise NotImplementedError - else: - while int(self.__current) != key: - self.__current = next(self.__current) - - if self.__current is None: - raise KeyError - - count = self.__current._SSHKey__idx - prior = self.__current._SSHKey__prev - posterior = self.__current._SSHKey__next - posterior._SSHKey__prev = prior - posterior._SSHKey__prev._SSHKey__next = posterior - self.__current = posterior - while self.__current is not None: - self.__current._SSHKey__idx = count - self.__current = next(self.__current) - count += 1 @property def head(self) -> SSHKey | None: @@ -440,15 +483,31 @@ class SSHKeyCollection(Sequence): def __missing__(self) -> Never: raise NotImplementedError - def __iter__(self) -> Self | Never: + def __next__(self): raise NotImplementedError + def __iter__(self) -> Self | Never: + self.__Current = self.__first + return self.__current + def count(self, query: RegEx | str) -> int | Never: raise NotImplementedError # @TODO make sure to implement below method def pull(self, query: RegEx | str = "*") -> Never: - raise NotImplementedError + if isinstance(query, RegEx): + keypaths = self.__ssh_path.glob("*") + + for p in keypaths: + if query.fullmatch(p.name): + self.append(p) + else: + continue + else: + keypaths = self.__ssh_path.glob(query) + + for p in keypaths: + self.append(p) def reverse(self) -> None | Never: raise NotImplementedError @@ -465,3 +524,8 @@ class UserSSH: self.keys = keys self.password = password self.fate = fate + +sshkey_coll = SSHKeyCollection() +sshkey_coll.pull() +# print(len(sshkey_coll) + 2) +print(sshkey_coll[0:4][3])