Skip to content

Commit 898eb9f

Browse files
committed
update
1 parent e39cbfc commit 898eb9f

File tree

8 files changed

+75
-16
lines changed

8 files changed

+75
-16
lines changed

Changelog.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
# 0.2.1
2+
3+
## bug修复
4+
5+
+ 修复多层节点情况下无法返回返回值
6+
7+
## 改进
8+
9+
+ 在有返回值时由__call__中返回的值会包含节点信息
10+
111
# 0.2.0
212

313
## 新增特性

build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python -m build --wheel

schema_entry/entrypoint.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
from .protocol import SUPPORT_SCHEMA
2626
from .utils import get_parent_tree, parse_value_string_by_schema, parse_schema_as_cmd, pydantic_schema_to_protocol
27-
from .entrypoint_base import SchemaType, PropertyType, EntryPointABC, PydanticModelLike
27+
from .entrypoint_base import SchemaType, PropertyType, EntryPointABC, PydanticModelLike, CallerReturnType
2828

2929

3030
class EntryPoint(EntryPointABC):
@@ -185,7 +185,7 @@ def with_schema(self, schemaObj: Union[str, dict, PydanticModelLike]) -> Union[s
185185
self.__doc__ = schemaObj.__doc__
186186
return schemaObj
187187

188-
def __call__(self, argv: Sequence[str]) -> Optional[Any]:
188+
def __call__(self, argv: Sequence[str]) -> Optional[CallerReturnType]:
189189
if not self.usage:
190190
if len(self._subcmds) == 0:
191191
self.usage = f"{self.prog} [options]"
@@ -215,23 +215,27 @@ def __call__(self, argv: Sequence[str]) -> Optional[Any]:
215215
description=self.__doc__,
216216
usage=self.usage,
217217
formatter_class=argparse.RawDescriptionHelpFormatter)
218-
self.pass_args_to_sub(parser, argv)
219-
return None
218+
return self.pass_args_to_sub(parser, argv)
219+
220220
else:
221221
parser = argparse.ArgumentParser(
222222
prog=self.prog,
223223
epilog=self.epilog,
224224
description=self.__doc__,
225225
usage=self.usage)
226-
return self.parse_args(parser, argv)
226+
result = self.parse_args(parser, argv)
227+
if result is None:
228+
return result
229+
else:
230+
return {"caller": self.name, "result": result}
227231

228-
def pass_args_to_sub(self, parser: argparse.ArgumentParser, argv: Sequence[str]) -> None:
232+
def pass_args_to_sub(self, parser: argparse.ArgumentParser, argv: Sequence[str]) -> Optional[CallerReturnType]:
229233
scmds = list(self._subcmds.keys())
230234
scmdss = ",".join(scmds)
231235
parser.add_argument('subcmd', help=f'执行子命令,可选的子命有{scmdss}')
232236
args = parser.parse_args(argv[0:1])
233237
if self._subcmds.get(args.subcmd):
234-
self._subcmds[args.subcmd](argv[1:])
238+
return self._subcmds[args.subcmd](argv[1:])
235239
else:
236240
print(f'未知的子命令 `{argv[0]}`')
237241
parser.print_help()

schema_entry/entrypoint_base.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ def model_json_schema(self, *args: Any, **kwargs: Any) -> Dict[str, Any]:
1616
...
1717

1818

19+
class CallerReturnType(TypedDict):
20+
caller: str
21+
result: Any
22+
23+
1924
class ItemType(TypedDict):
2025
type: str
2126
enum: List[Union[int, float, str]]
@@ -148,7 +153,7 @@ def with_schema(self, schemaObj: Union[str, dict, PydanticModelLike]) -> Union[s
148153
"""
149154

150155
@abc.abstractmethod
151-
def __call__(self, argv: Sequence[str]) -> Optional[Any]:
156+
def __call__(self, argv: Sequence[str]) -> Optional[CallerReturnType]:
152157
"""执行命令.
153158
154159
如果当前的命令节点不是终点(也就是下面还有子命令)则传递参数到下一级;
@@ -160,7 +165,7 @@ def __call__(self, argv: Sequence[str]) -> Optional[Any]:
160165
161166
"""
162167
@abc.abstractmethod
163-
def pass_args_to_sub(self, parser: argparse.ArgumentParser, argv: Sequence[str]) -> None:
168+
def pass_args_to_sub(self, parser: argparse.ArgumentParser, argv: Sequence[str]) -> Optional[CallerReturnType]:
164169
"""解析复杂命令行参数并将参数传递至下一级."""
165170

166171
@abc.abstractmethod

schema_entry/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.2.0"
1+
__version__ = "0.2.1"

test.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
python -m coverage run --source=schema_entry -m unittest discover -v -s .
2+
python -m coverage report

tests/test_entrypoint.py

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ class Test_A(EntryPoint):
3838
root = Test_A()
3939
assert root.name == "test_b"
4040

41-
4241
def test_default_entry_usage(self) -> None:
4342
class Test_A(EntryPoint):
4443
schema = {
@@ -78,10 +77,13 @@ class Test_A(EntryPoint):
7877
root = Test_A()
7978
config = root(["--a-a=4.2"])
8079
target = {
81-
"a_a": 4.2
80+
"caller": "test_a",
81+
"result": {
82+
"a_a": 4.2
83+
}
8284
}
83-
self.assertDictEqual(config,target)
84-
85+
self.assertDictEqual(config, target)
86+
8587
def test_main_return(self) -> None:
8688
class Test_A(EntryPoint):
8789
schema = {
@@ -95,13 +97,14 @@ class Test_A(EntryPoint):
9597
},
9698
"required": ["a_a"]
9799
}
100+
98101
def do_main(self) -> str:
99102
return "a test"
100103
root = Test_A()
101104
get_value = root(["--a-a=4.2"])
102-
target = "a test"
105+
target = {"caller": "test_a", "result": "a test"}
103106
assert get_value == target
104-
107+
105108
def test_override_do_main(self) -> None:
106109
class Test_A(EntryPoint):
107110
schema = {
@@ -177,6 +180,38 @@ def _(a: int) -> None:
177180
"a": 2
178181
})
179182

183+
def test_subcmd_return(self) -> None:
184+
class A(EntryPoint):
185+
pass
186+
187+
class B(EntryPoint):
188+
pass
189+
190+
class C(EntryPoint):
191+
schema = {
192+
"$schema": "http://json-schema.org/draft-07/schema#",
193+
"type": "object",
194+
"properties": {
195+
"a": {
196+
"type": "integer"
197+
}
198+
},
199+
"required": ["a"]
200+
}
201+
root = A()
202+
a_b_c = root.regist_sub(B).regist_sub(C)
203+
204+
@a_b_c.as_main
205+
def _(a: int) -> int:
206+
return a
207+
os.environ['A_B_C_A'] = "2"
208+
call_result = root(["b", "c"])
209+
210+
self.assertDictEqual(call_result, {
211+
"caller": "c",
212+
"result": 2
213+
})
214+
180215

181216
class LoadConfigTest(unittest.TestCase):
182217
@classmethod

typecheck.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
python -m pycodestyle --max-line-length=140 --ignore=E501 --first --statistics schema_entry
2+
python -m mypy --ignore-missing-imports --show-column-numbers --follow-imports=silent --check-untyped-defs --disallow-untyped-defs --no-implicit-optional --warn-unused-ignores schema_entry

0 commit comments

Comments
 (0)