feature: allowed slicing for item retrieval from 'SSHKeyCollection' instances
This commit is contained in:
@@ -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:
|
||||
|
||||
if isinstance(key, int):
|
||||
if int(self.__current) == key:
|
||||
return self.__current
|
||||
else:
|
||||
while int(self.__current) != key:
|
||||
self.__current = next(self.__current)
|
||||
|
||||
if self.__current is None:
|
||||
raise KeyError
|
||||
|
||||
return self.__current
|
||||
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
|
||||
|
||||
self.__current = next(self.__current)
|
||||
|
||||
# print(test_coll)
|
||||
result = sshkcoll
|
||||
|
||||
return result
|
||||
|
||||
def __len__(self) -> int:
|
||||
if self.__indices is None:
|
||||
@@ -364,16 +399,19 @@ 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 isinstance(key, int):
|
||||
if int(self.__current) == key:
|
||||
if self.__current() is None or len(self.__current()) < 1:
|
||||
self.__current(*value)
|
||||
else:
|
||||
@@ -381,19 +419,22 @@ class SSHKeyCollection(Sequence):
|
||||
return self.__current(*value)
|
||||
|
||||
while int(self.__current) != key:
|
||||
self.__current = next(self.__current)
|
||||
|
||||
if self.__current is None:
|
||||
raise KeyError
|
||||
|
||||
self.__current(*value)
|
||||
self.__current = next(self.__current)
|
||||
|
||||
def __delitem__(self, key: int) -> None | Never:
|
||||
self.__current(*value)
|
||||
elif isinstance(key, slice):
|
||||
raise NotImplementedError
|
||||
|
||||
def __delitem__(self, key: int | slice) -> None | Never:
|
||||
self.__current = self.__first
|
||||
|
||||
if self.__current is None:
|
||||
raise KeyError
|
||||
|
||||
if isinstance(key, int):
|
||||
if key == -1:
|
||||
if self.__last is not None:
|
||||
self.__last._SSHKey__prev._SSHKey__next = None
|
||||
@@ -422,6 +463,8 @@ class SSHKeyCollection(Sequence):
|
||||
self.__current._SSHKey__idx = count
|
||||
self.__current = next(self.__current)
|
||||
count += 1
|
||||
elif isinstance(key, slice):
|
||||
raise NotImplementedError
|
||||
|
||||
@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])
|
||||
|
||||
Reference in New Issue
Block a user