zt-oawf-cli 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,123 @@
1
+ Metadata-Version: 2.4
2
+ Name: zt-oawf-cli
3
+ Version: 0.1.0
4
+ Summary: ZT OAWF CLI scaffold
5
+ Requires-Python: >=3.10
6
+ Description-Content-Type: text/markdown
7
+ Requires-Dist: playwright>=1.40.0
8
+ Requires-Dist: requests>=2.31.0
9
+ Requires-Dist: click>=8.1.0
10
+ Requires-Dist: pyyaml>=6.0
11
+ Provides-Extra: dev
12
+ Requires-Dist: pytest>=7.0; extra == "dev"
13
+
14
+ # zt-oawf-cli
15
+
16
+ ZT OAWF(`oawf.ztn.cn`)命令行工具:登录缓存 Cookie,查询流程列表。
17
+
18
+ ## 环境要求
19
+
20
+ - Python `>=3.10`
21
+ - 可用浏览器(首次登录会打开)
22
+
23
+ ## 安装
24
+
25
+ ### 开发安装(源码)
26
+
27
+ ```bash
28
+ pip install -e ".[dev]"
29
+ python -m playwright install chromium
30
+ ```
31
+
32
+ ### 打包发布
33
+
34
+ ```bash
35
+ python -m pip install --upgrade build twine
36
+ python -m build
37
+ twine upload dist/*
38
+ ```
39
+
40
+ ## 快速开始
41
+
42
+ ### 1) 登录并缓存 Cookie
43
+
44
+ 推荐有头登录(SSO / 验证码更稳):
45
+
46
+ ```bash
47
+ zt-oawf-cli login --headed
48
+ ```
49
+
50
+ 已设置 SSO 环境变量时也可无头:
51
+
52
+ ```bash
53
+ zt-oawf-cli login
54
+ ```
55
+
56
+ 登录成功后 Cookie 写入 `~/.zt-oawf-cli/cookies.json`(需含 `JSESSIONID`、`OaToken`)。
57
+
58
+ ### 2) 查询流程列表
59
+
60
+ 对应 `kmReviewIndex.do?method=list`:
61
+
62
+ ```bash
63
+ # 全部
64
+ zt-oawf-cli wf list
65
+ zt-oawf-cli -o human wf list
66
+
67
+ # 待我审批
68
+ zt-oawf-cli wf approval
69
+ zt-oawf-cli -o human wf approval
70
+
71
+ # 我发起的
72
+ zt-oawf-cli wf created
73
+ zt-oawf-cli -o human wf created
74
+
75
+ # 我已审核
76
+ zt-oawf-cli wf approved
77
+ zt-oawf-cli -o human wf approved
78
+
79
+ # 流程详情(fdId)
80
+ zt-oawf-cli wf detail 1a1f8e767ae0b5854254ddb42acb763d
81
+ zt-oawf-cli -o human wf detail 1a1f8e767ae0b5854254ddb42acb763d
82
+ ```
83
+
84
+ ### 3) 脚手架占位
85
+
86
+ ```bash
87
+ zt-oawf-cli wf ping
88
+ ```
89
+
90
+ ## 全局参数
91
+
92
+ - `-o, --output`:`json`(默认)/ `human`
93
+ - `--version`:查看版本
94
+
95
+ ## 配置说明
96
+
97
+ 默认配置目录:`~/.zt-oawf-cli`
98
+
99
+ - Cookie 缓存:`~/.zt-oawf-cli/cookies.json`
100
+ - 配置文件:`~/.zt-oawf-cli/config.yaml`
101
+
102
+ 可选:
103
+
104
+ ```yaml
105
+ browser_channel: chromium
106
+ ```
107
+
108
+ ## 环境变量(可选)
109
+
110
+ - `ZT_SSO_USERNAME`
111
+ - `ZT_SSO_PASSWORD`
112
+
113
+ 未设置时请执行 `zt-oawf-cli login --headed`。
114
+
115
+ ## 命令一览
116
+
117
+ - `zt-oawf-cli login [--headed]`
118
+ - `zt-oawf-cli wf list [--mydoc] [--path] [--orderby] [--ordertype]` — 全部
119
+ - `zt-oawf-cli wf approval` — 待我审批
120
+ - `zt-oawf-cli wf created` — 我发起的
121
+ - `zt-oawf-cli wf approved` — 我已审核
122
+ - `zt-oawf-cli wf detail <fdId>` — 流程详情
123
+ - `zt-oawf-cli wf ping`
@@ -0,0 +1,110 @@
1
+ # zt-oawf-cli
2
+
3
+ ZT OAWF(`oawf.ztn.cn`)命令行工具:登录缓存 Cookie,查询流程列表。
4
+
5
+ ## 环境要求
6
+
7
+ - Python `>=3.10`
8
+ - 可用浏览器(首次登录会打开)
9
+
10
+ ## 安装
11
+
12
+ ### 开发安装(源码)
13
+
14
+ ```bash
15
+ pip install -e ".[dev]"
16
+ python -m playwright install chromium
17
+ ```
18
+
19
+ ### 打包发布
20
+
21
+ ```bash
22
+ python -m pip install --upgrade build twine
23
+ python -m build
24
+ twine upload dist/*
25
+ ```
26
+
27
+ ## 快速开始
28
+
29
+ ### 1) 登录并缓存 Cookie
30
+
31
+ 推荐有头登录(SSO / 验证码更稳):
32
+
33
+ ```bash
34
+ zt-oawf-cli login --headed
35
+ ```
36
+
37
+ 已设置 SSO 环境变量时也可无头:
38
+
39
+ ```bash
40
+ zt-oawf-cli login
41
+ ```
42
+
43
+ 登录成功后 Cookie 写入 `~/.zt-oawf-cli/cookies.json`(需含 `JSESSIONID`、`OaToken`)。
44
+
45
+ ### 2) 查询流程列表
46
+
47
+ 对应 `kmReviewIndex.do?method=list`:
48
+
49
+ ```bash
50
+ # 全部
51
+ zt-oawf-cli wf list
52
+ zt-oawf-cli -o human wf list
53
+
54
+ # 待我审批
55
+ zt-oawf-cli wf approval
56
+ zt-oawf-cli -o human wf approval
57
+
58
+ # 我发起的
59
+ zt-oawf-cli wf created
60
+ zt-oawf-cli -o human wf created
61
+
62
+ # 我已审核
63
+ zt-oawf-cli wf approved
64
+ zt-oawf-cli -o human wf approved
65
+
66
+ # 流程详情(fdId)
67
+ zt-oawf-cli wf detail 1a1f8e767ae0b5854254ddb42acb763d
68
+ zt-oawf-cli -o human wf detail 1a1f8e767ae0b5854254ddb42acb763d
69
+ ```
70
+
71
+ ### 3) 脚手架占位
72
+
73
+ ```bash
74
+ zt-oawf-cli wf ping
75
+ ```
76
+
77
+ ## 全局参数
78
+
79
+ - `-o, --output`:`json`(默认)/ `human`
80
+ - `--version`:查看版本
81
+
82
+ ## 配置说明
83
+
84
+ 默认配置目录:`~/.zt-oawf-cli`
85
+
86
+ - Cookie 缓存:`~/.zt-oawf-cli/cookies.json`
87
+ - 配置文件:`~/.zt-oawf-cli/config.yaml`
88
+
89
+ 可选:
90
+
91
+ ```yaml
92
+ browser_channel: chromium
93
+ ```
94
+
95
+ ## 环境变量(可选)
96
+
97
+ - `ZT_SSO_USERNAME`
98
+ - `ZT_SSO_PASSWORD`
99
+
100
+ 未设置时请执行 `zt-oawf-cli login --headed`。
101
+
102
+ ## 命令一览
103
+
104
+ - `zt-oawf-cli login [--headed]`
105
+ - `zt-oawf-cli wf list [--mydoc] [--path] [--orderby] [--ordertype]` — 全部
106
+ - `zt-oawf-cli wf approval` — 待我审批
107
+ - `zt-oawf-cli wf created` — 我发起的
108
+ - `zt-oawf-cli wf approved` — 我已审核
109
+ - `zt-oawf-cli wf detail <fdId>` — 流程详情
110
+ - `zt-oawf-cli wf ping`
@@ -0,0 +1,28 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61.0"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "zt-oawf-cli"
7
+ version = "0.1.0"
8
+ description = "ZT OAWF CLI scaffold"
9
+ readme = "README.md"
10
+ requires-python = ">=3.10"
11
+ dependencies = [
12
+ "playwright>=1.40.0",
13
+ "requests>=2.31.0",
14
+ "click>=8.1.0",
15
+ "pyyaml>=6.0",
16
+ ]
17
+
18
+ [project.optional-dependencies]
19
+ dev = ["pytest>=7.0"]
20
+
21
+ [project.scripts]
22
+ zt-oawf-cli = "zt_oawf_cli.cli:main"
23
+
24
+ [tool.setuptools.packages.find]
25
+ where = ["src"]
26
+
27
+ [tool.pytest.ini_options]
28
+ testpaths = ["tests"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1 @@
1
+ __version__ = "0.1.0"
@@ -0,0 +1,226 @@
1
+ """OAWF API 请求封装"""
2
+ import time
3
+ from urllib.parse import urlparse
4
+
5
+ import requests
6
+
7
+ from .auth import get_auth_cookies, get_auth_headers
8
+ from .config import config
9
+ from .detail import parse_review_detail, review_detail_url
10
+
11
+
12
+ class OawfApiError(RuntimeError):
13
+ """API 或登录态错误。"""
14
+
15
+
16
+ def _is_sso_url(url: str) -> bool:
17
+ parsed = urlparse(url or "")
18
+ hostname = (parsed.hostname or "").lower()
19
+ path = parsed.path.lower()
20
+ return (
21
+ hostname.startswith("sso.")
22
+ or "/cas/login" in path
23
+ or path.rstrip("/").endswith("/login")
24
+ )
25
+
26
+
27
+ def _is_html_response(response: requests.Response) -> bool:
28
+ content_type = response.headers.get("Content-Type", "").lower()
29
+ text = (response.text or "").lstrip().lower()
30
+ return "text/html" in content_type or text.startswith(("<!doctype html", "<html"))
31
+
32
+
33
+ def _is_auth_error(
34
+ response: requests.Response,
35
+ expected_url: str | None = None,
36
+ ) -> bool:
37
+ if response.status_code in (401, 403):
38
+ return True
39
+
40
+ actual_url = getattr(response, "url", "") or ""
41
+ expected_host = (urlparse(expected_url or "").hostname or "").lower()
42
+ actual_host = (urlparse(actual_url).hostname or "").lower()
43
+ if expected_host and actual_host and actual_host != expected_host:
44
+ return True
45
+
46
+ if _is_sso_url(actual_url):
47
+ return True
48
+ for previous in getattr(response, "history", []):
49
+ if _is_sso_url(previous.headers.get("Location", "")):
50
+ return True
51
+
52
+ if _is_html_response(response):
53
+ text = (response.text or "").lower()
54
+ if any(keyword in text for keyword in ("login", "sso", "sign in", "登录")):
55
+ return True
56
+
57
+ content_type = response.headers.get("Content-Type", "").lower()
58
+ if "application/json" in content_type:
59
+ try:
60
+ data = response.json()
61
+ except ValueError:
62
+ return False
63
+ if isinstance(data, dict):
64
+ msg = str(data.get("msg") or data.get("message") or "").lower()
65
+ if any(
66
+ keyword in msg
67
+ for keyword in ("login", "登录", "未授权", "认证", "session")
68
+ ):
69
+ return True
70
+
71
+ return False
72
+
73
+
74
+ def _response_excerpt(response: requests.Response, limit: int = 500) -> str:
75
+ text = " ".join((response.text or "").split())
76
+ return text[:limit]
77
+
78
+
79
+ class OawfApi:
80
+ """OAWF API 客户端"""
81
+
82
+ def __init__(self):
83
+ self.base_url = config.BASE_URL
84
+
85
+ def list_reviews(
86
+ self,
87
+ *,
88
+ mydoc: str = "all",
89
+ j_path: str = "/listAll",
90
+ orderby: str = "docCreateTime",
91
+ ordertype: str = "down",
92
+ paging_setting: str = "",
93
+ ):
94
+ """查询流程列表(对应 kmReviewIndex.do?method=list)。"""
95
+ return self.request(
96
+ "GET",
97
+ "/km/review/km_review_index/kmReviewIndex.do",
98
+ params={
99
+ "method": "list",
100
+ "pagingSetting": paging_setting,
101
+ "q.mydoc": mydoc,
102
+ "q.j_path": j_path,
103
+ "orderby": orderby,
104
+ "ordertype": ordertype,
105
+ "__seq": str(int(time.time() * 1000)),
106
+ "s_ajax": "true",
107
+ },
108
+ )
109
+
110
+ def get_review_detail(self, fd_id: str, *, _retried: bool = False) -> dict:
111
+ """获取流程详情(kmReviewMain.do?method=view,解析 HTML 表单)。"""
112
+ if not fd_id or not str(fd_id).strip():
113
+ raise OawfApiError("fdId 不能为空")
114
+
115
+ fd_id = str(fd_id).strip()
116
+ url = review_detail_url(self.base_url, fd_id)
117
+ cookies = get_auth_cookies()
118
+ headers = get_auth_headers(cookies)
119
+ headers = {
120
+ **headers,
121
+ "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
122
+ }
123
+
124
+ try:
125
+ response = requests.get(
126
+ url,
127
+ headers=headers,
128
+ cookies=cookies,
129
+ timeout=60,
130
+ allow_redirects=True,
131
+ )
132
+ except requests.RequestException as exc:
133
+ raise OawfApiError(f"请求流程详情失败: {exc}") from exc
134
+
135
+ actual_url = getattr(response, "url", "") or url
136
+ if _is_sso_url(actual_url) or any(
137
+ _is_sso_url(prev.headers.get("Location", ""))
138
+ for prev in getattr(response, "history", [])
139
+ ):
140
+ if _retried:
141
+ raise OawfApiError(
142
+ "登录态已失效,请执行 `zt-oawf-cli login --headed` 后重试。"
143
+ )
144
+ try:
145
+ get_auth_cookies(force_refresh=True)
146
+ except Exception as exc:
147
+ raise OawfApiError(
148
+ f"登录态已失效,自动重新登录失败: {exc}。"
149
+ "请先执行 `zt-oawf-cli login --headed`。"
150
+ ) from exc
151
+ return self.get_review_detail(fd_id, _retried=True)
152
+
153
+ if response.status_code >= 400:
154
+ raise OawfApiError(
155
+ f"流程详情错误: {response.status_code} {_response_excerpt(response)}"
156
+ )
157
+
158
+ html = response.text or ""
159
+ # 登录页误判:无表单且明显是登录
160
+ if "kmReviewMainForm" not in html:
161
+ lower = html.lower()
162
+ if any(k in lower for k in ("/cas/login", "sso.", "请登录", "用户登录")):
163
+ if _retried:
164
+ raise OawfApiError(
165
+ "登录态已失效,请执行 `zt-oawf-cli login --headed` 后重试。"
166
+ )
167
+ get_auth_cookies(force_refresh=True)
168
+ return self.get_review_detail(fd_id, _retried=True)
169
+ raise OawfApiError(
170
+ f"未获取到流程详情,请确认 fdId 是否有效: {fd_id}"
171
+ )
172
+
173
+ try:
174
+ return parse_review_detail(html, fd_id=fd_id, url=url)
175
+ except ValueError as exc:
176
+ raise OawfApiError(str(exc)) from exc
177
+
178
+ def request(self, method: str, path: str, *, _retried: bool = False, **kwargs):
179
+ cookies = get_auth_cookies()
180
+ headers = get_auth_headers(cookies)
181
+ url = f"{self.base_url}/{path.lstrip('/')}"
182
+
183
+ try:
184
+ response = requests.request(
185
+ method=method,
186
+ url=url,
187
+ headers=headers,
188
+ cookies=cookies,
189
+ **kwargs,
190
+ )
191
+ except requests.RequestException as exc:
192
+ raise OawfApiError(f"请求失败: {exc}") from exc
193
+
194
+ if _is_auth_error(response, expected_url=url):
195
+ if _retried:
196
+ raise OawfApiError(
197
+ "登录态已失效,自动重新登录后仍未通过认证。"
198
+ "请执行 `zt-oawf-cli login --headed` 检查登录流程。"
199
+ )
200
+
201
+ try:
202
+ get_auth_cookies(force_refresh=True)
203
+ except Exception as exc:
204
+ raise OawfApiError(
205
+ f"登录态已失效,自动重新登录失败: {exc}。"
206
+ "请先执行 `zt-oawf-cli login --headed` 完成登录。"
207
+ ) from exc
208
+ return self.request(method, path, _retried=True, **kwargs)
209
+
210
+ if response.status_code >= 400:
211
+ raise OawfApiError(
212
+ f"API 错误: {response.status_code} {_response_excerpt(response)}"
213
+ )
214
+
215
+ if _is_html_response(response):
216
+ raise OawfApiError(
217
+ "API 返回了 HTML 页面,已拒绝将其作为数据输出。"
218
+ "请检查登录态或站点网关配置。"
219
+ )
220
+
221
+ try:
222
+ return response.json()
223
+ except ValueError as exc:
224
+ raise OawfApiError(
225
+ f"API 返回了非 JSON 响应: {_response_excerpt(response)}"
226
+ ) from exc