From d9ff0443dce5ffc8fa1f74cac7312d083ef24e2b Mon Sep 17 00:00:00 2001 From: Alex Tavarez Date: Thu, 22 Jan 2026 17:13:14 -0500 Subject: [PATCH] feature: removed dict arg option for publish method while added repr magic method in/for SSHKeyCollection --- sshkey.py | 47 +++++++++++++++-------------------------------- 1 file changed, 15 insertions(+), 32 deletions(-) diff --git a/sshkey.py b/sshkey.py index 15721f5..1d19eeb 100644 --- a/sshkey.py +++ b/sshkey.py @@ -4,9 +4,10 @@ from pathlib import Path, PurePath from custtypes import ExecutedPath, IdlePath from enum import StrEnum, auto from random import choice as gamble -from collections.abc import Sequence -from typing import Never, Self, Callable +# from collections.abc import Sequence, Iterable +from typing import Never, Self, Callable, Iterable, Sequence from whereami import USER_PATH +from itertools import chain # import os class SSHKeyType(StrEnum): @@ -37,7 +38,6 @@ class SSHKey: def __int__(self) -> int: return self.__idx - def update_status(self) -> None: if isinstance(self.__value, tuple): privkey_present = False @@ -69,7 +69,7 @@ class SSHKey: return "🔑" + key_basename def __repr__(self) -> str: - return "SSHKey(" + str(self.__value) + ")" + return "%s(%r)" % (self.__class__.__name__, self.__value) def __nonzero__(self) -> bool: return True @@ -282,6 +282,8 @@ class SSHKey: result = self return result + + def prev(arg): if isinstance(arg, SSHKey): return arg._SSHKey__prev__() @@ -312,7 +314,8 @@ class SSHKeyCollection(Sequence): self.__last: SSHKey | 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: self.__current = self.__first @@ -573,7 +576,6 @@ class SSHKeyCollection(Sequence): def count(self, query: RegEx | str) -> int | Never: raise NotImplementedError - # @TODO make sure to implement below method def pull(self, query: RegEx | str = "*") -> None: if isinstance(query, RegEx): keypaths = self.__ssh_path.glob("*") @@ -617,11 +619,12 @@ class SSHKeyCollection(Sequence): def index(self, item: str | ExecutedPath) -> Never: 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() pubkey = list() self.__current = self.__first + # @TODO create conditional case that publishes all keys if datatype == list: while self.__current is not None: # print(self.__current) @@ -648,30 +651,10 @@ class SSHKeyCollection(Sequence): else: return (privkey, pubkey, preference) elif datatype == dict: - result = dict() - - 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))) + # @TODO have result var equal to instance of a yaml.YAMLObject class from parse module + raise NotImplementedError return result + + def __repr__(self) -> str: + return "%s()" % (self.__class__.__name__)