This commit is contained in:
shenjack 2023-07-11 18:19:00 +08:00
parent 4e9a908a97
commit 2ea0138e90
5 changed files with 143 additions and 61 deletions

View File

@ -0,0 +1,40 @@
from dataclasses import dataclass, field
from typing import Set, List
class Parsed:
...
@dataclass
class Option:
name: str
shortcuts: List[str]
optional: bool
types: Set[type] = field(default_factory=lambda: {str})
@dataclass
class OptionGroup:
options: List[Option]
optional: bool = True
exclusive: bool = False
@dataclass
class Argument:
name: str
types: Set[type] = field(default_factory=lambda: {str})
@dataclass
class Flag:
name: str
shortcuts: List[str]
@dataclass
class FlagGroup:
flags: List[Flag]
exclusive: bool = False

View File

@ -1,2 +1,2 @@
class IllegalArgumentName(Exception):
"""参数名称或快捷名不合法"""
class IllegalName(Exception):
"""名称或快捷名不合法"""

View File

@ -5,18 +5,18 @@
# -------------------------------
import re
from dataclasses import dataclass
from typing import Callable, List, Optional, Union
from typing import Callable, List, Optional, Union, Set
from .data import Option, Argument, Flag, Parsed
from .descriptor import CallBackDescriptor
try:
from typing import Self
except ImportError:
from typing import TypeVar
Self = TypeVar("Self")
Self = TypeVar("Self") # NOQA
from .exception import IllegalArgumentName
from .exception import IllegalName
CallBack = Union[Callable[[str], None], str] # Equals to `Callable[[str], None] | str`
# 可调用对象或字符串作为回调
@ -29,21 +29,6 @@ ParseArgFunc = Callable[[str], Optional[type]]
EMPTY_WORDS = re.compile(r"\s", re.I)
def check_once(cls, name) -> None:
"""
Check whether the attribute has been set.
if so, it will raise exception `AttributeError`.
检查属性是否已经被设置
如果已经被设置将会抛出 `AttributeError` 异常
:param cls: class object
:param name: attribute name
:return: None
"""
if hasattr(cls, name):
if getattr(cls, name) is not None:
raise AttributeError(f"Attribute '{name}' has been set.")
def check_name(name: Union[str, List[str]]) -> None:
"""
Check the name or shortcuts of argument(s) or flag(s).
@ -56,31 +41,13 @@ def check_name(name: Union[str, List[str]]) -> None:
:return: None
"""
if isinstance(name, str) and EMPTY_WORDS.search(name):
raise IllegalArgumentName("The name of argument must not contains empty words.")
raise IllegalName("The name of argument must not contains empty words.")
elif isinstance(name, list) and all((not isinstance(i, str)) and EMPTY_WORDS.search(i) for i in name):
raise IllegalArgumentName("The name of shortcut must be 'str', and must not contains empty words.")
raise IllegalName("The name of shortcut must be 'str', and must not contains empty words.")
else:
raise TypeError("The type of name must be 'str' or 'list[str]'.")
class Parsed:
...
@dataclass
class Argument:
name: str
shortcuts: List[str]
optional: bool
type: type
@dataclass
class Flag:
name: str
shortcuts: List[str]
class Literal:
_tip = CallBackDescriptor("_tip")
_func = CallBackDescriptor("_func")
@ -89,13 +56,13 @@ class Literal:
def __init__(self, name: str):
self.name: str = name
self.sub: List[Self] = []
self._doc: Optional[str] = None
self._tip: Optional[CallBack] = None
self._func: Optional[CallBack] = None
self._err_callback: Optional[CallBack] = None
self._opts: List[Option] = []
self._args: List[Argument] = []
self._flags: List[Argument]
self._flags: List[Flag] = []
def __call__(self, *nodes) -> Self:
self.sub += nodes
@ -105,32 +72,40 @@ class Literal:
attrs = (k for k in self.__dict__ if not (k.startswith("__") and k.endswith("__")))
return f"{self.__class__.__name__}({', '.join(f'{k}={v!r}' for k in attrs if (v := self.__dict__[k]))})"
def arg(
def arg(self, name: str, types: Optional[Set[type]] = None) -> Self:
Argument(name=name, types=types)
return self
def opt(
self,
name: str,
shortcuts: Optional[List[str]] = None,
optional: bool = True,
type: Optional[type] = None
types: Optional[Set[type]] = None
) -> Self:
check_name(name)
if shortcuts is not None and len(shortcuts) != 0:
check_name(shortcuts)
Argument(name=name, shortcuts=shortcuts, optional=optional, type=type)
self._opts.append(
Option(name=name, shortcuts=shortcuts, optional=optional, types=types)
)
return self
def arg_group(self, args: List[Argument], exclusive: bool = False):
def opt_group(self, opts: List[Option], exclusive: bool = False):
...
def flag(self, name: str, shortcuts: Optional[List[str]] = None) -> Self:
check_name(name)
if shortcuts is not None and len(shortcuts) != 0:
check_name(shortcuts)
# Flag(...)
Flag(name=name, shortcuts=shortcuts)
...
return self
def flag_group(self, flags: List[Flag], exclusive: bool = False):
def flag_group(self, flags: List[Flag], exclusive: bool = False) -> Self:
...
return self
def error(self, callback: CallBack) -> Self:
self._err_callback = callback
@ -149,8 +124,6 @@ class Literal:
def to_doc(self) -> str:
...
self._doc = ...
return self._doc
def builder(node: Literal) -> Literal:

View File

@ -1,10 +1,83 @@
# This file is @generated by PDM.
# It is not intended for manual editing.
[[package]]
name = "nepattern"
version = "0.5.10"
requires_python = ">=3.8"
summary = "a complex pattern, support typing"
dependencies = [
"tarina>=0.3.3",
"typing-extensions>=4.5.0",
]
[[package]]
name = "tarina"
version = "0.3.3"
requires_python = ">=3.8"
summary = "A collection of common utils for Arclet"
dependencies = [
"typing-extensions>=4.4.0",
]
[[package]]
name = "typing-extensions"
version = "4.7.1"
requires_python = ">=3.7"
summary = "Backported and Experimental Type Hints for Python 3.7+"
[metadata]
lock_version = "4.2"
cross_platform = true
groups = ["default"]
content_hash = "sha256:cb30ff0b06924f6f0d5f726b84c255686a2e277a4180b00b7b6e427c05ca202b"
content_hash = "sha256:73e5bcd71ae7bae870ea57f3a7aefed66821d6b549ec2ca2e8cf202e645c2aa3"
[metadata.files]
"nepattern 0.5.10" = [
{url = "https://files.pythonhosted.org/packages/5b/07/295a685764d31e171ce0551f1d46c289d5279a80544629e86facaae946b1/nepattern-0.5.10-py3-none-any.whl", hash = "sha256:cfeae4906ed94ed4a5198da102b0f730a1c61793f7d0bfd0e63ca9dbd325e9d6"},
{url = "https://files.pythonhosted.org/packages/ec/a3/e3c59a4a4878787913637ac5c3327912fe3bdc3afb997902b55cfcf0eb2a/nepattern-0.5.10.tar.gz", hash = "sha256:70648453e94aee75575e4e4ee13ba876d11ac20d8a80fe5eb7dc7e43573e0918"},
]
"tarina 0.3.3" = [
{url = "https://files.pythonhosted.org/packages/11/37/7948ddeb644212875fe18a8bb4e68dc3f1fc16fc18e4ecf51b79a0036a8d/tarina-0.3.3-cp38-cp38-musllinux_1_1_i686.whl", hash = "sha256:934d970d4f355fe0042325f1c50c4ff885fa6d114c9c4b062a293f8d378b70f8"},
{url = "https://files.pythonhosted.org/packages/11/d5/d686f2ae369c58c70a0eced645a9e6bd4fb4cf4f38cebb7a5cbd91f878f8/tarina-0.3.3-cp38-cp38-macosx_10_9_universal2.whl", hash = "sha256:3cfb1cb0b6eabcd745d083d6031bab3319e46da94ca1df62cea1bbcc1abe3159"},
{url = "https://files.pythonhosted.org/packages/12/22/a5ef2ad9cec1c23fce161817f10596c03c01af70cf248c7060702dbc7321/tarina-0.3.3.tar.gz", hash = "sha256:ac9faf7569021cfa137a9cee1be5185bae9ff4e4e6a5b93c66abdf056d1e04fa"},
{url = "https://files.pythonhosted.org/packages/19/a3/3f79a4afaaac21c52785b60101a89ec79dbb645daa51e3b7f08dffc0c659/tarina-0.3.3-cp38-cp38-musllinux_1_1_x86_64.whl", hash = "sha256:d2c3c7808a3984f00b16f101c36443a042d3abc8ff5d5e84b9cb3fabebb330b1"},
{url = "https://files.pythonhosted.org/packages/1b/a6/3005c325cdd7ba5eaa9b619b182cf8b75264ea409759dadf6d261d615c6d/tarina-0.3.3-cp311-cp311-macosx_10_9_universal2.whl", hash = "sha256:e1d28e974fe5915dde245019430f248ddbdd91e8df1dec53306448cc24f4d9a8"},
{url = "https://files.pythonhosted.org/packages/1c/ad/376ad6150ad45039876f61984420535cdfe5c5db3433804d68dae995a167/tarina-0.3.3-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:3efce03c7a46118afd009dc56169030dc8b698f6da20fce25cd0139530525d80"},
{url = "https://files.pythonhosted.org/packages/20/4f/774bef8e456859529cb1bcabaf1639cc33fb8f5dd4c024f8a27a2f7a5e48/tarina-0.3.3-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:3c4cec6381869116c7a8f7d9b0cb7433967f5b324faa9fc2e4784fdb9bab79f8"},
{url = "https://files.pythonhosted.org/packages/2b/2c/2d1501948a95a45187d5add6f468ea46f5ec23e5fb713661fb5a3ed5cc10/tarina-0.3.3-cp39-cp39-macosx_10_9_universal2.whl", hash = "sha256:5195c7f9fa2313bdf422c24b8ed0b8d9549c4e46afc0c96eeaefdf1d2caa30e9"},
{url = "https://files.pythonhosted.org/packages/2c/1f/124aeff1910a9393b9823594f17468fcd591e1e85e60c5f8be27a7f12e07/tarina-0.3.3-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:788001d26cb84a752df569aef900387ac4d9b2273b38b9ed26f6d56b4644aa06"},
{url = "https://files.pythonhosted.org/packages/2c/f0/bdb27f86f0cb2ece0acb3e626ceaee508dcc261e16722a43f688058dfbe0/tarina-0.3.3-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:2c643c1e384e206af3ba2eb7142f6b3680d51d0b51e01a61bbf1f3350040c791"},
{url = "https://files.pythonhosted.org/packages/2e/07/e03e5fec539279d7cca66f883fc1e13fc4fb188c1df3f107b6b74de98e5c/tarina-0.3.3-py3-none-any.whl", hash = "sha256:0547c75e3e53ca202c420e6d829d5bcf5758e478fc5964b92f9de2f838cfb12a"},
{url = "https://files.pythonhosted.org/packages/3a/39/abeb158b7c1b94542b4b68f0a07f7739ca0ff0be7064bfcbb138f85282fa/tarina-0.3.3-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:d33bcfaad022214a5a534c065b69c14ad9f2e53aa03198d6de0216e173fec98b"},
{url = "https://files.pythonhosted.org/packages/3a/8d/bb611259872326280f5b97fbb864c2739a76399a3c52e4d633d3dbc67b78/tarina-0.3.3-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:aa2897e7cad66be833c2e392181ecff33e59038bf85cf18000c3e48c2ee85fde"},
{url = "https://files.pythonhosted.org/packages/42/11/97465d907f8b2e26358b07d3b67b568557990efc04995a0e2a303dca6e1b/tarina-0.3.3-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:dd778f324c512403afa3973661c480d6529a403435d205ee7bf7fc586894f4c1"},
{url = "https://files.pythonhosted.org/packages/51/7a/6cb5efb75d526e72e9846e6fb71dae96e6e27470a965b0770c29a3c5a83a/tarina-0.3.3-cp310-cp310-macosx_10_9_universal2.whl", hash = "sha256:de2429a632f31b09bdb2d5f668311d7de4882f1ac3ebc5022decf647f900af5a"},
{url = "https://files.pythonhosted.org/packages/5b/6a/be6bf22769ef3e55e8afc4c256080613a5ec97d7a6f6787c9414c86b0061/tarina-0.3.3-cp310-cp310-win32.whl", hash = "sha256:ca4b1978614dbb5f30c4378be1ad4f4aa6fb227cdf32740964ee9760d6a8e3a9"},
{url = "https://files.pythonhosted.org/packages/5e/32/30fae8ac68095dd919c0435c8ee744ed50a7f4b3942288a3294a8f3fa3f2/tarina-0.3.3-cp39-cp39-win32.whl", hash = "sha256:f6b7fc1799a70cc14548086602a2343562de722c490649c4bf2e74eb7c542c04"},
{url = "https://files.pythonhosted.org/packages/62/cd/2f3722c02cf93cf68d92567efa7c5f632af3d15a686127241b52216b207a/tarina-0.3.3-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:a14079252a54501285956f1e96004477af8b9ae64a575f561b99bfa778c01707"},
{url = "https://files.pythonhosted.org/packages/67/67/e4f5bbe1bc4c696ac09a1325bcc3a9a464185914f0a2302e695e68f42757/tarina-0.3.3-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:4dcc7112b927a023a635528566b62beb7a99f72f2b9e88fde29fafb61136a579"},
{url = "https://files.pythonhosted.org/packages/69/ef/b24dc2963cb1b788e3331bc703f7c1d1963fc8b4a8c99b82f78c78fc0528/tarina-0.3.3-cp311-cp311-musllinux_1_1_i686.whl", hash = "sha256:fbcaa4a6c57528ed23199f778445d20215cc2421c1d494dedbe671164bd29dc5"},
{url = "https://files.pythonhosted.org/packages/6e/4b/f0bacb8118a30193150dd7d078a398bac5739ffbc3270652492dd656193d/tarina-0.3.3-cp38-cp38-win32.whl", hash = "sha256:5ec78f0d8bc5b529502e1be20bb8dc628ff1a6c5e31941f9572c105c6fe591b1"},
{url = "https://files.pythonhosted.org/packages/72/7f/17d999c886905a0672337adb9501400c8a2ab209feaae0bb3eba7627cba1/tarina-0.3.3-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:35dc66fb6baac12fca363380ff12fd27164481f281f82ac2282e500b53fef961"},
{url = "https://files.pythonhosted.org/packages/78/92/b24dddc1b836beaa6733c9b2caf51410a6cce0873ae6d55344f2d22db26b/tarina-0.3.3-cp39-cp39-musllinux_1_1_i686.whl", hash = "sha256:1f78d6f75da0d34061ade10d0e1aeec3018a4d36836568085a4eb79fef65bb7e"},
{url = "https://files.pythonhosted.org/packages/80/20/2c8303ab730af65a79a8cae869b463936bac2236519595c5e4259b72f4b8/tarina-0.3.3-cp39-cp39-win_amd64.whl", hash = "sha256:9f8a0a505a9b226d1d45483fac105c55407c13a95449e15562601128dc2359d1"},
{url = "https://files.pythonhosted.org/packages/8c/06/b864bceb1597ba142d729c34798aa6251879bb276d3b3f965b18c673fcad/tarina-0.3.3-cp310-cp310-win_amd64.whl", hash = "sha256:efa6e1ab03c1948e3efd4645b8960dd312407e029462726a9e77822261a6be3d"},
{url = "https://files.pythonhosted.org/packages/8f/ca/472e18aa2bbef0bb83526089012f2628a69a8b0213c1bb6cbd51c11f736c/tarina-0.3.3-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:ccb1090fccd3dea3596bdea5ac80b7379663741dd5023b97840dca968f4081bd"},
{url = "https://files.pythonhosted.org/packages/91/1f/1d7a52e8558eac6d5dd9ade3e88a2458e224178e018d9e602896cea20b05/tarina-0.3.3-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:ecb4cbe65fe9b4e4c91ededb9d6941aae80b6827f07827e12e7ba3f7c3109c02"},
{url = "https://files.pythonhosted.org/packages/c5/55/65ab39d0fd1c6010c7ae874d8c659b7d95ef415b5dd2489cda246ceff669/tarina-0.3.3-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:f56b904a451e287e6251c1312be628afddf670bfa87841b5b1c32d324a1c7c43"},
{url = "https://files.pythonhosted.org/packages/c6/f5/1180b80fe98bf86e0e7e7defdbf6fa7859795b77f7bf91cdb368e8d5b0cf/tarina-0.3.3-cp311-cp311-win_amd64.whl", hash = "sha256:730a8fc6dc71adf09197a73ef1e39978e5fe7ec3b7be9a5060905fe48baf27ec"},
{url = "https://files.pythonhosted.org/packages/d2/8a/7a922a79e2f6ae6bf0f3bac63e5f1cdfa39abf987b5a122a70bc327144e6/tarina-0.3.3-cp310-cp310-musllinux_1_1_i686.whl", hash = "sha256:9b9a99acff1e6391c593f449ae76848471d34688f04eecf7d3ba0725157563c7"},
{url = "https://files.pythonhosted.org/packages/d3/eb/bbbc5e5666382a55ab6e5d7c4dc68833af6126db2eb96a2836b8afe86d3c/tarina-0.3.3-cp311-cp311-win32.whl", hash = "sha256:87da7c87f448180712c02f6345c686742d9e755d58bb32f70c1bf680e1ae0684"},
{url = "https://files.pythonhosted.org/packages/d8/55/2f96d10417cf96a6667d3a5d68bcfd9cf0dde7452d69a8970694529ae7eb/tarina-0.3.3-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:6471aef032461a6917a24af1fb9952bfb5c9bbe26999ea87455bf43157687097"},
{url = "https://files.pythonhosted.org/packages/de/14/2224da3ba13fb3d63d9a1da7747a55c43f4f3436344e03cd91bcdecb2fed/tarina-0.3.3-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:201e31c7b694132ce67662f63142b33e45835223a753fd5f4d301da5ac021b5e"},
{url = "https://files.pythonhosted.org/packages/e6/21/78f9911125401d8256563bd94cb0d8b5a4a63fac1ecbdcfd5c4b9bff9c03/tarina-0.3.3-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:c5e2e0f5a3760823b23cc3fe49d01c0a75d482560c60934764e2142d187e553b"},
{url = "https://files.pythonhosted.org/packages/ec/d4/9643018bb37a2c4e87331633d7c232d2fab3d6017c1f6fb580c87c10a988/tarina-0.3.3-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:7546e4cefadcc5f163611b0af8af2391b711c337d7a0d32a3977ec0bf3e8c87d"},
{url = "https://files.pythonhosted.org/packages/f6/a1/afd77bdf28621f3e9771288a9c866f5685610fc6ae4bdc6b4a34ea74d3e2/tarina-0.3.3-cp38-cp38-win_amd64.whl", hash = "sha256:ff5e49a37d6d72a392f8a84c6808f7cc00494352d9e459a0a3921e587b2ecb3f"},
{url = "https://files.pythonhosted.org/packages/f8/b8/a7ab3d3e0281c2135c53ffb43e703653359374a5696057c55fbff8bc779a/tarina-0.3.3-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:ae87c40c09a62b0898267fb4cbeaece3ca2d7bd8cd2f50c8ecb19583cca3fa64"},
{url = "https://files.pythonhosted.org/packages/f8/cd/be747301ea89ee45b5c38b0144a70ac5ed0abc0c3fe98842f528f8797da5/tarina-0.3.3-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:6c1ff19bf0b6e554d2fb3d6db35d74553a02b3781167370d11bc950f92fb33ba"},
]
"typing-extensions 4.7.1" = [
{url = "https://files.pythonhosted.org/packages/3c/8b/0111dd7d6c1478bf83baa1cab85c686426c7a6274119aceb2bd9d35395ad/typing_extensions-4.7.1.tar.gz", hash = "sha256:b75ddc264f0ba5615db7ba217daeb99701ad295353c45f9e95963337ceeeffb2"},
{url = "https://files.pythonhosted.org/packages/ec/6b/63cc3df74987c36fe26157ee12e09e8f9db4de771e0f3404263117e75b95/typing_extensions-4.7.1-py3-none-any.whl", hash = "sha256:440d5dd3af93b060174bf433bccd69b0babc3b15b1a8dca43789fd7f61514b36"},
]

View File

@ -1,22 +1,22 @@
[project]
version = "0.1.0"
name = "lib-not-dr"
description = "A python lib created from Difficult Rocket development"
readme = "README.md"
authors = [
{name = "shenjackyuanjie", email = "3695888@qq.com"}
]
requires-python = ">=3.8"
classifiers = [
"Programming Language :: Python :: 3",
"Operating System :: OS Independent",
]
license = { file = "LICENSE" }
dependencies = [
"nepattern>=0.5.10",
]
[project.urls]
Homepage = "https://github.com/shenjackyuanjie/lib-not-dr"
Repository = "https://github.com/shenjackyuanjie/lib-not-dr"
@ -24,13 +24,9 @@ Changelog = "https://github.com/shenjackyuanjie/lib-not-dr/blob/main/docs/change
[tool.ruff]
target-version = "py38"
line-length = 150
src = [
"lib_not_dr"
]
format = "grouped"