Source code for jgdv.cli.builder_mixin

  1#!/usr/bin/env python3
  2"""
  3
  4"""
  5# Imports:
  6from __future__ import annotations
  7
  8# ##-- stdlib imports
  9import datetime
 10import builtins
 11import enum
 12import functools as ftz
 13import itertools as itz
 14import logging as logmod
 15import pathlib as pl
 16import re
 17import time
 18import types
 19import collections
 20import contextlib
 21import hashlib
 22from copy import deepcopy
 23from uuid import UUID, uuid1
 24from weakref import ref
 25import atexit # for @atexit.register
 26import faulthandler
 27# ##-- end stdlib imports
 28
 29from .param_spec import ParamSpec
 30from .param_spec.core import ToggleParam, KeyParam, AssignParam, PositionalParam
 31from .param_spec.defaults import HelpParam, VerboseParam, SeparatorParam
 32
 33# ##-- types
 34# isort: off
 35import abc
 36import collections.abc
 37from typing import TYPE_CHECKING, cast, assert_type, assert_never
 38from typing import Generic, NewType
 39# Protocols:
 40from typing import Protocol, runtime_checkable
 41# Typing Decorators:
 42from typing import no_type_check, final, override, overload
 43
 44if TYPE_CHECKING:
 45    from jgdv import Maybe
 46    from typing import Final
 47    from typing import ClassVar, Any, LiteralString
 48    from typing import Never, Self, Literal
 49    from typing import TypeGuard
 50    from collections.abc import Iterable, Iterator, Callable, Generator
 51    from collections.abc import Sequence, Mapping, MutableMapping, Hashable
 52
 53##--|
 54
 55# isort: on
 56# ##-- end types
 57
 58##-- logging
 59logging = logmod.getLogger(__name__)
 60##-- end logging
 61
 62# Vars:
 63
 64# Body:
 65
[docs] 66def _remap_type(data:dict) -> type: 67 """ 68 Get a specific type of parameter, using provided data 69 70 needs to handle: 71 - using separator, prefix and type to select toggle/key/assign/positional 72 - TODO using type to select toggle, literal 73 - TODO using count to select repeatable 74 - TODO using choice to select choice params 75 """ 76 77 match data: 78 case {"separator": str() as x } if bool(x): 79 return AssignParam 80 case {"prefix" : int() }: 81 return PositionalParam 82 case {"type": x } if x is not bool: 83 return KeyParam 84 case _: 85 return ToggleParam
86 87##--| 88
[docs] 89class ParamSpecMaker_m: 90 __slots__ = () 91
[docs] 92 @staticmethod 93 def build_param(**kwargs:Any) -> ParamSpec: 94 """ Utility method for easily making paramspecs """ 95 data = dict(kwargs) 96 # Parse the name 97 match ParamSpec._processor.parse_name(data['name']): 98 case None: 99 pass 100 case dict() as parsed: 101 data.update(parsed) 102 103 # remap to a specific paramspec type 104 refined = _remap_type(data) 105 # build it, using original kwargs 106 return refined(**kwargs)