feature: removed dict arg option for publish method while added repr magic method in/for SSHKeyCollection
This commit is contained in:
47
sshkey.py
47
sshkey.py
@@ -4,9 +4,10 @@ from pathlib import Path, PurePath
|
|||||||
from custtypes import ExecutedPath, IdlePath
|
from custtypes import ExecutedPath, IdlePath
|
||||||
from enum import StrEnum, auto
|
from enum import StrEnum, auto
|
||||||
from random import choice as gamble
|
from random import choice as gamble
|
||||||
from collections.abc import Sequence
|
# from collections.abc import Sequence, Iterable
|
||||||
from typing import Never, Self, Callable
|
from typing import Never, Self, Callable, Iterable, Sequence
|
||||||
from whereami import USER_PATH
|
from whereami import USER_PATH
|
||||||
|
from itertools import chain
|
||||||
# import os
|
# import os
|
||||||
|
|
||||||
class SSHKeyType(StrEnum):
|
class SSHKeyType(StrEnum):
|
||||||
@@ -37,7 +38,6 @@ class SSHKey:
|
|||||||
def __int__(self) -> int:
|
def __int__(self) -> int:
|
||||||
return self.__idx
|
return self.__idx
|
||||||
|
|
||||||
|
|
||||||
def update_status(self) -> None:
|
def update_status(self) -> None:
|
||||||
if isinstance(self.__value, tuple):
|
if isinstance(self.__value, tuple):
|
||||||
privkey_present = False
|
privkey_present = False
|
||||||
@@ -69,7 +69,7 @@ class SSHKey:
|
|||||||
return "🔑" + key_basename
|
return "🔑" + key_basename
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return "SSHKey(" + str(self.__value) + ")"
|
return "%s(%r)" % (self.__class__.__name__, self.__value)
|
||||||
|
|
||||||
def __nonzero__(self) -> bool:
|
def __nonzero__(self) -> bool:
|
||||||
return True
|
return True
|
||||||
@@ -282,6 +282,8 @@ class SSHKey:
|
|||||||
result = self
|
result = self
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def prev(arg):
|
def prev(arg):
|
||||||
if isinstance(arg, SSHKey):
|
if isinstance(arg, SSHKey):
|
||||||
return arg._SSHKey__prev__()
|
return arg._SSHKey__prev__()
|
||||||
@@ -312,7 +314,8 @@ class SSHKeyCollection(Sequence):
|
|||||||
self.__last: SSHKey | None = None
|
self.__last: SSHKey | None = None
|
||||||
self.__indices: range | None = None
|
self.__indices: range | None = None
|
||||||
|
|
||||||
# @TODO have other item magic methods mimic this one for slicing purposes
|
# @TODO allow initialization with unpacked parameter or sequence/iterable argument
|
||||||
|
|
||||||
def __getitem__(self, key: int | slice) -> SSHKey | Never:
|
def __getitem__(self, key: int | slice) -> SSHKey | Never:
|
||||||
self.__current = self.__first
|
self.__current = self.__first
|
||||||
|
|
||||||
@@ -573,7 +576,6 @@ class SSHKeyCollection(Sequence):
|
|||||||
def count(self, query: RegEx | str) -> int | Never:
|
def count(self, query: RegEx | str) -> int | Never:
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
# @TODO make sure to implement below method
|
|
||||||
def pull(self, query: RegEx | str = "*") -> None:
|
def pull(self, query: RegEx | str = "*") -> None:
|
||||||
if isinstance(query, RegEx):
|
if isinstance(query, RegEx):
|
||||||
keypaths = self.__ssh_path.glob("*")
|
keypaths = self.__ssh_path.glob("*")
|
||||||
@@ -617,11 +619,12 @@ class SSHKeyCollection(Sequence):
|
|||||||
def index(self, item: str | ExecutedPath) -> Never:
|
def index(self, item: str | ExecutedPath) -> Never:
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def publish(self, category: SSHKeyType | str = SSHKeyType.pubkey, pref: int | None = None, datatype = dict):
|
def publish(self, category: SSHKeyType | str | None = SSHKeyType.pubkey, pref: int | None = None, datatype = list):
|
||||||
privkey = list()
|
privkey = list()
|
||||||
pubkey = list()
|
pubkey = list()
|
||||||
self.__current = self.__first
|
self.__current = self.__first
|
||||||
|
|
||||||
|
# @TODO create conditional case that publishes all keys
|
||||||
if datatype == list:
|
if datatype == list:
|
||||||
while self.__current is not None:
|
while self.__current is not None:
|
||||||
# print(self.__current)
|
# print(self.__current)
|
||||||
@@ -648,30 +651,10 @@ class SSHKeyCollection(Sequence):
|
|||||||
else:
|
else:
|
||||||
return (privkey, pubkey, preference)
|
return (privkey, pubkey, preference)
|
||||||
elif datatype == dict:
|
elif datatype == dict:
|
||||||
result = dict()
|
# @TODO have result var equal to instance of a yaml.YAMLObject class from parse module
|
||||||
|
raise NotImplementedError
|
||||||
while self.__current is not None:
|
|
||||||
# print(self.__current)
|
|
||||||
if self.__current.category == SSHKeyType.privkey.name.lower():
|
|
||||||
privkey.append(str(self.__current._SSHKey__value))
|
|
||||||
elif self.__current.category == SSHKeyType.pubkey.name.lower():
|
|
||||||
pubkey.append(self.__current._SSHKey__value.read_text())
|
|
||||||
elif self.__current.category == SSHKeyType.dual.name.lower():
|
|
||||||
privkey.append(str(self.__current._SSHKey__value[0]))
|
|
||||||
pubkey.append(self.__current._SSHKey__value[1].read_text())
|
|
||||||
self.__current = next(self.__current)
|
|
||||||
# print("publish running...")
|
|
||||||
|
|
||||||
if category.name.lower() == SSHKeyType.pubkey.name.lower():
|
|
||||||
result["ssh_authorized_keys"]: list[str] = pubkey
|
|
||||||
|
|
||||||
if category.name.lower() == SSHKeyType.privkey.name.lower():
|
|
||||||
result["ssh_private_key_paths"]: list[str] = privkey
|
|
||||||
result["ssh_private_key_path_pref"]: int = pref if pref is not None else gamble(range(len(privkey)))
|
|
||||||
|
|
||||||
if category.name.lower() == SSHKeyType.dual.name.lower():
|
|
||||||
result["ssh_authorized_keys"]: list[str] = pubkey
|
|
||||||
result["ssh_private_key_paths"]: list[str] = privkey
|
|
||||||
result["ssh_private_key_path_pref"]: int = pref if pref is not None else gamble(range(len(privkey)))
|
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
def __repr__(self) -> str:
|
||||||
|
return "%s()" % (self.__class__.__name__)
|
||||||
|
|||||||
Reference in New Issue
Block a user