2d292af3e81527750e46a2167d30efe840ac58ca
Marco Ricci Move typing classes into se...

Marco Ricci authored 4 months ago

src/derivepassphrase/types.py    1) # SPDX-FileCopyrightText: 2024 Marco Ricci <m@the13thletter.info>
src/derivepassphrase/types.py    2) #
src/derivepassphrase/types.py    3) # SPDX-License-Identifier: MIT
src/derivepassphrase/types.py    4) 
Marco Ricci Update documentation to use...

Marco Ricci authored 3 months ago

src/derivepassphrase/_types.py   5) """Types used by derivepassphrase."""
Marco Ricci Move typing classes into se...

Marco Ricci authored 4 months ago

src/derivepassphrase/types.py    6) 
src/derivepassphrase/types.py    7) from __future__ import annotations
src/derivepassphrase/types.py    8) 
Marco Ricci Consolidate `types` submodu...

Marco Ricci authored 3 months ago

src/derivepassphrase/_types.py   9) import enum
Marco Ricci Allow all textual strings,...

Marco Ricci authored 2 months ago

src/derivepassphrase/_types.py  10) from typing import Literal, NamedTuple, TypeGuard
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 3 months ago

src/derivepassphrase/types.py   11) 
Marco Ricci Support Python 3.10 and PyP...

Marco Ricci authored 3 months ago

src/derivepassphrase/types.py   12) from typing_extensions import (
Marco Ricci Fix style issues with ruff...

Marco Ricci authored 3 months ago

src/derivepassphrase/types.py   13)     Any,
src/derivepassphrase/types.py   14)     NotRequired,
src/derivepassphrase/types.py   15)     Required,
src/derivepassphrase/types.py   16)     TypedDict,
Marco Ricci Support Python 3.10 and PyP...

Marco Ricci authored 3 months ago

src/derivepassphrase/types.py   17) )
Marco Ricci Move typing classes into se...

Marco Ricci authored 4 months ago

src/derivepassphrase/types.py   18) 
Marco Ricci Consolidate `types` submodu...

Marco Ricci authored 3 months ago

src/derivepassphrase/_types.py  19) __all__ = (
src/derivepassphrase/_types.py  20)     'SSH_AGENT',
src/derivepassphrase/_types.py  21)     'SSH_AGENTC',
src/derivepassphrase/_types.py  22)     'KeyCommentPair',
src/derivepassphrase/_types.py  23)     'VaultConfig',
src/derivepassphrase/_types.py  24)     'is_vault_config',
src/derivepassphrase/_types.py  25) )
Marco Ricci Move typing classes into se...

Marco Ricci authored 4 months ago

src/derivepassphrase/types.py   26) 
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

src/derivepassphrase/types.py   27) 
Marco Ricci Move typing classes into se...

Marco Ricci authored 4 months ago

src/derivepassphrase/types.py   28) class VaultConfigGlobalSettings(TypedDict, total=False):
src/derivepassphrase/types.py   29)     r"""Configuration for vault: global settings.
src/derivepassphrase/types.py   30) 
src/derivepassphrase/types.py   31)     Attributes:
src/derivepassphrase/types.py   32)         key:
src/derivepassphrase/types.py   33)             The base64-encoded ssh public key to use, overriding the
src/derivepassphrase/types.py   34)             master passphrase. Optional.
src/derivepassphrase/types.py   35)         phrase:
src/derivepassphrase/types.py   36)             The master passphrase. Optional.
Marco Ricci Allow all textual strings,...

Marco Ricci authored 2 months ago

src/derivepassphrase/_types.py  37)         unicode_normalization_form:
src/derivepassphrase/_types.py  38)             The preferred Unicode normalization form; we warn the user
src/derivepassphrase/_types.py  39)             if textual passphrases do not match their normalized forms.
src/derivepassphrase/_types.py  40)             Optional, and a `derivepassphrase` extension.
Marco Ricci Move typing classes into se...

Marco Ricci authored 4 months ago

src/derivepassphrase/types.py   41) 
src/derivepassphrase/types.py   42)     """
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

src/derivepassphrase/types.py   43) 
Marco Ricci Move typing classes into se...

Marco Ricci authored 4 months ago

src/derivepassphrase/types.py   44)     key: NotRequired[str]
src/derivepassphrase/types.py   45)     phrase: NotRequired[str]
Marco Ricci Allow all textual strings,...

Marco Ricci authored 2 months ago

src/derivepassphrase/_types.py  46)     unicode_normalization_form: NotRequired[
src/derivepassphrase/_types.py  47)         Literal['NFC', 'NFD', 'NFKC', 'NFKD']
src/derivepassphrase/_types.py  48)     ]
Marco Ricci Move typing classes into se...

Marco Ricci authored 4 months ago

src/derivepassphrase/types.py   49) 
src/derivepassphrase/types.py   50) 
src/derivepassphrase/types.py   51) class VaultConfigServicesSettings(VaultConfigGlobalSettings, total=False):
src/derivepassphrase/types.py   52)     r"""Configuration for vault: services settings.
src/derivepassphrase/types.py   53) 
src/derivepassphrase/types.py   54)     Attributes:
src/derivepassphrase/types.py   55)         notes:
src/derivepassphrase/types.py   56)             Optional notes for this service, to display to the user when
src/derivepassphrase/types.py   57)             generating the passphrase.
src/derivepassphrase/types.py   58)         length:
src/derivepassphrase/types.py   59)             Desired passphrase length.
src/derivepassphrase/types.py   60)         repeat:
src/derivepassphrase/types.py   61)             The maximum number of immediate character repetitions
src/derivepassphrase/types.py   62)             allowed in the passphrase.  Disabled if set to 0.
src/derivepassphrase/types.py   63)         lower:
src/derivepassphrase/types.py   64)             Optional constraint on ASCII lowercase characters.  If
src/derivepassphrase/types.py   65)             positive, include this many lowercase characters
src/derivepassphrase/types.py   66)             somewhere in the passphrase.  If 0, avoid lowercase
src/derivepassphrase/types.py   67)             characters altogether.
src/derivepassphrase/types.py   68)         upper:
src/derivepassphrase/types.py   69)             Same as `lower`, but for ASCII uppercase characters.
src/derivepassphrase/types.py   70)         number:
src/derivepassphrase/types.py   71)             Same as `lower`, but for ASCII digits.
src/derivepassphrase/types.py   72)         space:
src/derivepassphrase/types.py   73)             Same as `lower`, but for the space character.
src/derivepassphrase/types.py   74)         dash:
src/derivepassphrase/types.py   75)             Same as `lower`, but for the hyphen-minus and underscore
src/derivepassphrase/types.py   76)             characters.
src/derivepassphrase/types.py   77)         symbol:
src/derivepassphrase/types.py   78)             Same as `lower`, but for all other hitherto unlisted
src/derivepassphrase/types.py   79)             ASCII printable characters (except backquote).
src/derivepassphrase/types.py   80) 
src/derivepassphrase/types.py   81)     """
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

src/derivepassphrase/types.py   82) 
Marco Ricci Move typing classes into se...

Marco Ricci authored 4 months ago

src/derivepassphrase/types.py   83)     notes: NotRequired[str]
src/derivepassphrase/types.py   84)     length: NotRequired[int]
src/derivepassphrase/types.py   85)     repeat: NotRequired[int]
src/derivepassphrase/types.py   86)     lower: NotRequired[int]
src/derivepassphrase/types.py   87)     upper: NotRequired[int]
src/derivepassphrase/types.py   88)     number: NotRequired[int]
src/derivepassphrase/types.py   89)     space: NotRequired[int]
src/derivepassphrase/types.py   90)     dash: NotRequired[int]
src/derivepassphrase/types.py   91)     symbol: NotRequired[int]
src/derivepassphrase/types.py   92) 
src/derivepassphrase/types.py   93) 
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

src/derivepassphrase/types.py   94) _VaultConfig = TypedDict(
src/derivepassphrase/types.py   95)     '_VaultConfig',
src/derivepassphrase/types.py   96)     {'global': NotRequired[VaultConfigGlobalSettings]},
src/derivepassphrase/types.py   97)     total=False,
src/derivepassphrase/types.py   98) )
src/derivepassphrase/types.py   99) 
src/derivepassphrase/types.py  100) 
Marco Ricci Move typing classes into se...

Marco Ricci authored 4 months ago

src/derivepassphrase/types.py  101) class VaultConfig(TypedDict, _VaultConfig, total=False):
src/derivepassphrase/types.py  102)     r"""Configuration for vault.
src/derivepassphrase/types.py  103) 
src/derivepassphrase/types.py  104)     Usually stored as JSON.
src/derivepassphrase/types.py  105) 
src/derivepassphrase/types.py  106)     Attributes:
src/derivepassphrase/types.py  107)         global (NotRequired[VaultConfigGlobalSettings]):
src/derivepassphrase/types.py  108)             Global settings.
src/derivepassphrase/types.py  109)         services (Required[dict[str, VaultConfigServicesSettings]]):
src/derivepassphrase/types.py  110)             Service-specific settings.
src/derivepassphrase/types.py  111) 
src/derivepassphrase/types.py  112)     """
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

src/derivepassphrase/types.py  113) 
Marco Ricci Move typing classes into se...

Marco Ricci authored 4 months ago

src/derivepassphrase/types.py  114)     services: Required[dict[str, VaultConfigServicesSettings]]
src/derivepassphrase/types.py  115) 
Marco Ricci Reformat everything with ruff

Marco Ricci authored 3 months ago

src/derivepassphrase/types.py  116) 
Marco Ricci Apply new ruff ruleset to c...

Marco Ricci authored 2 months ago

src/derivepassphrase/_types.py 117) def is_vault_config(obj: Any) -> TypeGuard[VaultConfig]:  # noqa: ANN401,C901,PLR0911,PLR0912
Marco Ricci Move typing classes into se...

Marco Ricci authored 4 months ago

src/derivepassphrase/types.py  118)     """Check if `obj` is a valid vault config, according to typing.
src/derivepassphrase/types.py  119) 
src/derivepassphrase/types.py  120)     Args:
src/derivepassphrase/types.py  121)         obj: The object to test.
src/derivepassphrase/types.py  122) 
src/derivepassphrase/types.py  123)     Returns:
src/derivepassphrase/types.py  124)         True if this is a vault config, false otherwise.
src/derivepassphrase/types.py  125) 
src/derivepassphrase/types.py  126)     """
src/derivepassphrase/types.py  127)     if not isinstance(obj, dict):
src/derivepassphrase/types.py  128)         return False
src/derivepassphrase/types.py  129)     if 'global' in obj:
src/derivepassphrase/types.py  130)         o_global = obj['global']
src/derivepassphrase/types.py  131)         if not isinstance(o_global, dict):
src/derivepassphrase/types.py  132)             return False
Marco Ricci Allow all textual strings,...

Marco Ricci authored 2 months ago

src/derivepassphrase/_types.py 133)         for key in ('key', 'phrase', 'unicode_normalization_form'):
Marco Ricci Move typing classes into se...

Marco Ricci authored 4 months ago

src/derivepassphrase/types.py  134)             if key in o_global and not isinstance(o_global[key], str):
src/derivepassphrase/types.py  135)                 return False
src/derivepassphrase/types.py  136)         if 'key' in o_global and 'phrase' in o_global:
src/derivepassphrase/types.py  137)             return False
src/derivepassphrase/types.py  138)     if not isinstance(obj.get('services'), dict):
src/derivepassphrase/types.py  139)         return False
src/derivepassphrase/types.py  140)     for sv_name, service in obj['services'].items():
src/derivepassphrase/types.py  141)         if not isinstance(sv_name, str):
src/derivepassphrase/types.py  142)             return False
src/derivepassphrase/types.py  143)         if not isinstance(service, dict):
src/derivepassphrase/types.py  144)             return False
src/derivepassphrase/types.py  145)         for key, value in service.items():
src/derivepassphrase/types.py  146)             match key:
src/derivepassphrase/types.py  147)                 case 'notes' | 'phrase' | 'key':
src/derivepassphrase/types.py  148)                     if not isinstance(value, str):
src/derivepassphrase/types.py  149)                         return False
src/derivepassphrase/types.py  150)                 case 'length':
src/derivepassphrase/types.py  151)                     if not isinstance(value, int) or value < 1:
src/derivepassphrase/types.py  152)                         return False
src/derivepassphrase/types.py  153)                 case _:
src/derivepassphrase/types.py  154)                     if not isinstance(value, int) or value < 0:
src/derivepassphrase/types.py  155)                         return False
src/derivepassphrase/types.py  156)         if 'key' in service and 'phrase' in service:
src/derivepassphrase/types.py  157)             return False
src/derivepassphrase/types.py  158)     return True
Marco Ricci Consolidate `types` submodu...

Marco Ricci authored 3 months ago

src/derivepassphrase/_types.py 159) 
src/derivepassphrase/_types.py 160) 
src/derivepassphrase/_types.py 161) class KeyCommentPair(NamedTuple):
src/derivepassphrase/_types.py 162)     """SSH key plus comment pair.  For typing purposes.
src/derivepassphrase/_types.py 163) 
src/derivepassphrase/_types.py 164)     Attributes:
src/derivepassphrase/_types.py 165)         key: SSH key.
src/derivepassphrase/_types.py 166)         comment: SSH key comment.
src/derivepassphrase/_types.py 167) 
src/derivepassphrase/_types.py 168)     """
src/derivepassphrase/_types.py 169) 
src/derivepassphrase/_types.py 170)     key: bytes | bytearray
src/derivepassphrase/_types.py 171)     comment: bytes | bytearray
src/derivepassphrase/_types.py 172) 
src/derivepassphrase/_types.py 173) 
src/derivepassphrase/_types.py 174) class SSH_AGENTC(enum.Enum):  # noqa: N801
src/derivepassphrase/_types.py 175)     """SSH agent protocol numbers: client requests.
src/derivepassphrase/_types.py 176) 
src/derivepassphrase/_types.py 177)     Attributes:
src/derivepassphrase/_types.py 178)         REQUEST_IDENTITIES:
src/derivepassphrase/_types.py 179)             List identities.  Expecting `SSH_AGENT.IDENTITIES_ANSWER`.
src/derivepassphrase/_types.py 180)         SIGN_REQUEST:
src/derivepassphrase/_types.py 181)             Sign data.  Expecting `SSH_AGENT.SIGN_RESPONSE`.
src/derivepassphrase/_types.py 182) 
src/derivepassphrase/_types.py 183)     """
src/derivepassphrase/_types.py 184) 
src/derivepassphrase/_types.py 185)     REQUEST_IDENTITIES: int = 11
src/derivepassphrase/_types.py 186)     SIGN_REQUEST: int = 13
src/derivepassphrase/_types.py 187) 
src/derivepassphrase/_types.py 188) 
src/derivepassphrase/_types.py 189) class SSH_AGENT(enum.Enum):  # noqa: N801
src/derivepassphrase/_types.py 190)     """SSH agent protocol numbers: server replies.
src/derivepassphrase/_types.py 191) 
src/derivepassphrase/_types.py 192)     Attributes:
Marco Ricci Add a specific error class...

Marco Ricci authored 2 months ago

src/derivepassphrase/_types.py 193)         FAILURE:
src/derivepassphrase/_types.py 194)             Generic failure code.
src/derivepassphrase/_types.py 195)         SUCCESS:
src/derivepassphrase/_types.py 196)             Generic success code.
Marco Ricci Consolidate `types` submodu...

Marco Ricci authored 3 months ago

src/derivepassphrase/_types.py 197)         IDENTITIES_ANSWER:
src/derivepassphrase/_types.py 198)             Successful answer to `SSH_AGENTC.REQUEST_IDENTITIES`.
src/derivepassphrase/_types.py 199)         SIGN_RESPONSE:
src/derivepassphrase/_types.py 200)             Successful answer to `SSH_AGENTC.SIGN_REQUEST`.
src/derivepassphrase/_types.py 201) 
src/derivepassphrase/_types.py 202)     """
src/derivepassphrase/_types.py 203) 
Marco Ricci Add a specific error class...

Marco Ricci authored 2 months ago

src/derivepassphrase/_types.py 204)     FAILURE: int = 5
src/derivepassphrase/_types.py 205)     SUCCESS: int = 6