Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
hsz1273327 committed Feb 2, 2021
1 parent f98ca25 commit 9b1d0c4
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 11 deletions.
7 changes: 7 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# 0.1.0

## 新特性

+ schema字段现在支持`title``$comment`字段了
+ schema中定义的`title`字段可以用于定义命令行的缩写

# 0.0.9

## 新特性
Expand Down
49 changes: 44 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -135,6 +135,13 @@ class Test_A(EntryPoint):
},
"description": {
"type": "string"
},
"$comment": {
"type": "string"
},
"title": {
"type": "string",
"pattern": "^[a-b]|[d-z]$"
}
}
},
Expand Down Expand Up @@ -175,6 +182,13 @@ class Test_A(EntryPoint):
},
"description": {
"type": "string"
},
"$comment": {
"type": "string"
},
"title": {
"type": "string",
"pattern": r"^[a-b]|[d-z]$"
}
}
},
Expand Down Expand Up @@ -215,6 +229,13 @@ class Test_A(EntryPoint):
},
"description": {
"type": "string"
},
"$comment": {
"type": "string"
},
"title": {
"type": "string",
"pattern": "^[a-b]|[d-z]$"
}
}
},
Expand Down Expand Up @@ -255,6 +276,13 @@ class Test_A(EntryPoint):
},
"description": {
"type": "string"
},
"$comment": {
"type": "string"
},
"title": {
"type": "string",
"pattern": "^[a-b]|[d-z]$"
}
}
},
Expand All @@ -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]$"
}
}
}
Expand Down Expand Up @@ -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中获取默认配置
Expand Down Expand Up @@ -438,6 +474,7 @@ class Test_A(EntryPoint):
#### 从命令行参数中获取配置参数

当我们定义好`schema`后所有schema中定义好的参数都可以以`--xxxx`的形式从命令行中读取,需要注意schema中定义的字段中`_`会被修改为`-`.
如果定义的字段模式中含有`title`字段,则使用title字段作为命令行缩写即`-x`的形式

这个命令行读取是使用的标准库`argparse`,构造出的解析器中`useage`,`epilog``description`会由类中定义的`usage`,`epilog`和docstring决定;`argv`则为传到节点处时剩下的命令行参数(每多一个节点就会从左侧摘掉一个命令行参数).

Expand All @@ -462,6 +499,8 @@ class Test_A(EntryPoint):
}
```

命令行中默认使用`-c`/`--config`来指定读取配置文件,它的读取行为受上面介绍的从自定义配置文件中读取配置的设置影响.

#### 配置的读取顺序

配置的读取顺序为`schema中定义的default值`->`配置指定的配置文件路径`->`命令行指定的配置文件`->`环境变量`->`命令行参数`,而覆盖顺序则是反过来.
Expand Down
35 changes: 35 additions & 0 deletions schema_entry/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
},
"description": {
"type": "string"
},
"$comment": {
"type": "string"
},
"title": {
"type": "string",
"pattern": r"^[a-b]|[d-z]$"
}
}
},
Expand Down Expand Up @@ -70,6 +77,13 @@
},
"description": {
"type": "string"
},
"$comment": {
"type": "string"
},
"title": {
"type": "string",
"pattern": r"^[a-b]|[d-z]$"
}
}
},
Expand Down Expand Up @@ -110,6 +124,13 @@
},
"description": {
"type": "string"
},
"$comment": {
"type": "string"
},
"title": {
"type": "string",
"pattern": r"^[a-b]|[d-z]$"
}
}
},
Expand Down Expand Up @@ -150,6 +171,13 @@
},
"description": {
"type": "string"
},
"$comment": {
"type": "string"
},
"title": {
"type": "string",
"pattern": r"^[a-b]|[d-z]$"
}
}
},
Expand Down Expand Up @@ -184,6 +212,13 @@
},
"description": {
"type": "string"
},
"$comment": {
"type": "string"
},
"title": {
"type": "string",
"pattern": r"^[a-b]|[d-z]$"
}
}
}
Expand Down
18 changes: 15 additions & 3 deletions schema_entry/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -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 = [email protected]
Expand Down
28 changes: 26 additions & 2 deletions tests/test_entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -305,7 +306,6 @@ class Test_AC(EntryPoint):
"test_other_config2.json": test_other_config2_parser
}


root = Test_AC()

@root.as_main
Expand Down Expand Up @@ -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 = [
Expand Down

0 comments on commit 9b1d0c4

Please sign in to comment.