diff --git a/Changelog.md b/Changelog.md index 95def24..24ac75a 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,3 +1,10 @@ +# 0.1.0 + +## 新特性 + ++ schema字段现在支持`title`和`$comment`字段了 ++ schema中定义的`title`字段可以用于定义命令行的缩写 + # 0.0.9 ## 新特性 diff --git a/README.md b/README.md index eaa3b8a..d1f9aaf 100644 --- a/README.md +++ b/README.md @@ -114,13 +114,13 @@ class Test_A(EntryPoint): "properties": { "type": "object", "minProperties": 1, - "additionalProperties": false, + "additionalProperties": False, "patternProperties": { "^\\w+$": { "oneOf": [ { "type": "object", - "additionalProperties": false, + "additionalProperties": False, "required": ["type"], "properties": { "type": { @@ -135,6 +135,13 @@ class Test_A(EntryPoint): }, "description": { "type": "string" + }, + "$comment": { + "type": "string" + }, + "title": { + "type": "string", + "pattern": "^[a-b]|[d-z]$" } } }, @@ -175,6 +182,13 @@ class Test_A(EntryPoint): }, "description": { "type": "string" + }, + "$comment": { + "type": "string" + }, + "title": { + "type": "string", + "pattern": r"^[a-b]|[d-z]$" } } }, @@ -215,6 +229,13 @@ class Test_A(EntryPoint): }, "description": { "type": "string" + }, + "$comment": { + "type": "string" + }, + "title": { + "type": "string", + "pattern": "^[a-b]|[d-z]$" } } }, @@ -255,6 +276,13 @@ class Test_A(EntryPoint): }, "description": { "type": "string" + }, + "$comment": { + "type": "string" + }, + "title": { + "type": "string", + "pattern": "^[a-b]|[d-z]$" } } }, @@ -276,19 +304,26 @@ class Test_A(EntryPoint): "items": { "type": "object", "required": ["type"], - "additionalProperties":false, + "additionalProperties": false, "properties": { "type": { "type": "string", "enum": ["string", "number", "integer"] }, "enum":{ - "type": "array" + "type": "array" } } }, - "description":{ + "description": { + "type": "string" + }, + "$comment": { "type": "string" + }, + "title": { + "type": "string", + "pattern": "^[a-b]|[d-z]$" } } } @@ -320,6 +355,7 @@ class Test_A(EntryPoint): 3. 字段类型只能是`string`,`boolean`,`number`,`integer`,`array`之一 4. 字段类型如果为`array`则内部必须要有`items`且`items`中必须有`type`字段,且该`type`字段的值必须为`string`,`number`,`integer`之一 + 如果我们不想校验,那么可以设置`verify_schema`为`False`强行关闭这个功能. #### 从定义的schema中获取默认配置 @@ -438,6 +474,7 @@ class Test_A(EntryPoint): #### 从命令行参数中获取配置参数 当我们定义好`schema`后所有schema中定义好的参数都可以以`--xxxx`的形式从命令行中读取,需要注意schema中定义的字段中`_`会被修改为`-`. +如果定义的字段模式中含有`title`字段,则使用title字段作为命令行缩写即`-x`的形式 这个命令行读取是使用的标准库`argparse`,构造出的解析器中`useage`,`epilog`和`description`会由类中定义的`usage`,`epilog`和docstring决定;`argv`则为传到节点处时剩下的命令行参数(每多一个节点就会从左侧摘掉一个命令行参数). @@ -462,6 +499,8 @@ class Test_A(EntryPoint): } ``` +命令行中默认使用`-c`/`--config`来指定读取配置文件,它的读取行为受上面介绍的从自定义配置文件中读取配置的设置影响. + #### 配置的读取顺序 配置的读取顺序为`schema中定义的default值`->`配置指定的配置文件路径`->`命令行指定的配置文件`->`环境变量`->`命令行参数`,而覆盖顺序则是反过来. diff --git a/schema_entry/protocol.py b/schema_entry/protocol.py index 88c46b8..6b3498c 100644 --- a/schema_entry/protocol.py +++ b/schema_entry/protocol.py @@ -30,6 +30,13 @@ }, "description": { "type": "string" + }, + "$comment": { + "type": "string" + }, + "title": { + "type": "string", + "pattern": r"^[a-b]|[d-z]$" } } }, @@ -70,6 +77,13 @@ }, "description": { "type": "string" + }, + "$comment": { + "type": "string" + }, + "title": { + "type": "string", + "pattern": r"^[a-b]|[d-z]$" } } }, @@ -110,6 +124,13 @@ }, "description": { "type": "string" + }, + "$comment": { + "type": "string" + }, + "title": { + "type": "string", + "pattern": r"^[a-b]|[d-z]$" } } }, @@ -150,6 +171,13 @@ }, "description": { "type": "string" + }, + "$comment": { + "type": "string" + }, + "title": { + "type": "string", + "pattern": r"^[a-b]|[d-z]$" } } }, @@ -184,6 +212,13 @@ }, "description": { "type": "string" + }, + "$comment": { + "type": "string" + }, + "title": { + "type": "string", + "pattern": r"^[a-b]|[d-z]$" } } } diff --git a/schema_entry/utils.py b/schema_entry/utils.py index c15cf91..f85e009 100644 --- a/schema_entry/utils.py +++ b/schema_entry/utils.py @@ -88,7 +88,11 @@ def _argparse_base_handdler(_type: Any, key: str, schema: Dict[str, Any], parser if noflag: parser.add_argument(f"{key}", **kwargs) else: - parser.add_argument(f"--{key}", **kwargs) + if schema.get("title"): + short = schema["title"][0] + parser.add_argument(f"-{short}", f"--{key}", **kwargs) + else: + parser.add_argument(f"--{key}", **kwargs) return parser @@ -117,7 +121,11 @@ def _argparse_boolean_handdler(key: str, schema: Dict[str, Any], parser: argpars kwargs.update({ "help": _description }) - parser.add_argument(f"--{key}", **kwargs) + if schema.get("title"): + short = schema["title"][0] + parser.add_argument(f"-{short}", f"--{key}", **kwargs) + else: + parser.add_argument(f"--{key}", **kwargs) return parser @@ -169,7 +177,11 @@ def _argparse_array_handdler(key: str, schema: Dict[str, Any], parser: argparse. kwargs.update({ "action": "append" }) - parser.add_argument(f"--{key}", **kwargs) + if schema.get("title"): + short = schema["title"][0] + parser.add_argument(f"-{short}", f"--{key}", **kwargs) + else: + parser.add_argument(f"--{key}", **kwargs) return parser diff --git a/setup.cfg b/setup.cfg index c4ed7c6..bc91707 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = schema_entry -version = 0.0.9 +version = 0.1.0 url = https://github.com/Python-Tools/schema_entry author = hsz author_email = hsz1273327@gmail.com diff --git a/tests/test_entrypoint.py b/tests/test_entrypoint.py index 73eb2b3..89f0d56 100644 --- a/tests/test_entrypoint.py +++ b/tests/test_entrypoint.py @@ -290,10 +290,11 @@ def _2(a: int, b: int, c: int, d: int) -> None: root([]) def test_load_configfile_with_custom_parser_in_class(self) -> None: - def test_other_config2_parser( p: Path) -> Dict[str, Any]: + def test_other_config2_parser(p: Path) -> Dict[str, Any]: with open(p) as f: temp = json.load(f) return {k.lower(): v for k, v in temp.items()} + class Test_AC(EntryPoint): load_all_config_file = True default_config_file_paths = [ @@ -305,7 +306,6 @@ class Test_AC(EntryPoint): "test_other_config2.json": test_other_config2_parser } - root = Test_AC() @root.as_main @@ -380,6 +380,30 @@ def _(a_a: float) -> None: "a_a": 321.5 }) + def test_load_short_cmd_config(self) -> None: + class Test_A(EntryPoint): + schema = { + "$schema": "http://json-schema.org/draft-07/schema#", + "type": "object", + "properties": { + "a_a": { + "type": "number", + "title": "a" + } + }, + "required": ["a_a"] + } + root = Test_A() + + @root.as_main + def _(a_a: float) -> None: + pass + + root(["-a", "321.5"]) + self.assertDictEqual(root.config, { + "a_a": 321.5 + }) + def test_load_cmd_noflag_config(self) -> None: class Test_A(EntryPoint): default_config_file_paths = [