catfood 1.0.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.
catfood-1.0.0/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
catfood-1.0.0/PKG-INFO ADDED
@@ -0,0 +1,76 @@
1
+ Metadata-Version: 2.4
2
+ Name: catfood
3
+ Version: 1.0.0
4
+ Summary: A collection of various commonly used functions. / 各种常用函数的集合。
5
+ Author: 鸭鸭「カモ」
6
+ License-Expression: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/DuckDuckStudio/catfood
8
+ Project-URL: Repository, https://github.com/DuckDuckStudio/catfood
9
+ Project-URL: Issues, https://github.com/DuckDuckStudio/catfood/issues
10
+ Classifier: Development Status :: 1 - Planning
11
+ Classifier: Environment :: Console
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Natural Language :: Chinese (Simplified)
14
+ Classifier: Operating System :: OS Independent
15
+ Classifier: Programming Language :: Python
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3.13
18
+ Classifier: Programming Language :: Python :: Implementation :: CPython
19
+ Classifier: Topic :: Software Development
20
+ Classifier: Topic :: Software Development :: Libraries
21
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
22
+ Description-Content-Type: text/markdown
23
+ License-File: LICENSE
24
+ Requires-Dist: colorama>=0.4.6
25
+ Requires-Dist: requests>=2.32.5
26
+ Requires-Dist: keyring>=25.6.0
27
+ Provides-Extra: typing-check
28
+ Requires-Dist: pyright; extra == "typing-check"
29
+ Provides-Extra: build-and-publish
30
+ Requires-Dist: build; extra == "build-and-publish"
31
+ Requires-Dist: twine; extra == "build-and-publish"
32
+ Dynamic: license-file
33
+
34
+ # 猫粮 🐱
35
+
36
+ 各种常用函数的集合。
37
+
38
+ ## 安装
39
+
40
+ 我自己仅在 CPython 3.13 (3.13.9) 测试过。
41
+
42
+ ### Pypi
43
+ ```bash
44
+ # https://pypi.org/project/catfood/
45
+ pip install catfood
46
+ ```
47
+
48
+ ### Test Pypi
49
+ ```bash
50
+ # https://test.pypi.org/project/catfood/
51
+ pip install -i https://test.pypi.org/simple/ catfood
52
+ ```
53
+
54
+ ### 从源安装
55
+ ```bash
56
+ git clone https://github.com/DuckDuckStudio/catfood.git
57
+ pip install ./catfood
58
+ ```
59
+
60
+ #### Build whl
61
+ ```bash
62
+ # Windows PowerShell
63
+ git clone https://github.com/DuckDuckStudio/catfood.git
64
+ cd catfood
65
+
66
+ python -m venv .venv
67
+ & ".venv/Scripts/Activate.ps1"
68
+ python.exe -m pip install pip --upgrade
69
+
70
+ pip install ".[build_and_publish]" # 包括构建和发布依赖 build 和 twine
71
+ python -m build
72
+ ls dist/
73
+
74
+ # 从 whl 安装
75
+ pip install dist/catfood-1.0.0-py3-none-any.whl
76
+ ```
@@ -0,0 +1,43 @@
1
+ # 猫粮 🐱
2
+
3
+ 各种常用函数的集合。
4
+
5
+ ## 安装
6
+
7
+ 我自己仅在 CPython 3.13 (3.13.9) 测试过。
8
+
9
+ ### Pypi
10
+ ```bash
11
+ # https://pypi.org/project/catfood/
12
+ pip install catfood
13
+ ```
14
+
15
+ ### Test Pypi
16
+ ```bash
17
+ # https://test.pypi.org/project/catfood/
18
+ pip install -i https://test.pypi.org/simple/ catfood
19
+ ```
20
+
21
+ ### 从源安装
22
+ ```bash
23
+ git clone https://github.com/DuckDuckStudio/catfood.git
24
+ pip install ./catfood
25
+ ```
26
+
27
+ #### Build whl
28
+ ```bash
29
+ # Windows PowerShell
30
+ git clone https://github.com/DuckDuckStudio/catfood.git
31
+ cd catfood
32
+
33
+ python -m venv .venv
34
+ & ".venv/Scripts/Activate.ps1"
35
+ python.exe -m pip install pip --upgrade
36
+
37
+ pip install ".[build_and_publish]" # 包括构建和发布依赖 build 和 twine
38
+ python -m build
39
+ ls dist/
40
+
41
+ # 从 whl 安装
42
+ pip install dist/catfood-1.0.0-py3-none-any.whl
43
+ ```
@@ -0,0 +1,32 @@
1
+ """
2
+ Cat Food - A collection of various commonly used functions.
3
+
4
+ 猫粮 🐱 - 各种常用函数的集合。
5
+ """
6
+
7
+ from .functions.print import 消息头
8
+ from .functions.files import open_file
9
+ from .exceptions.request import RequestException
10
+ from .functions.terminal import calculateCharactersDisplayed
11
+ from .functions.github.token import read_token, 这是谁的Token
12
+ from .functions.format.github import IssueNumber, ResolvesIssue
13
+ from .functions.github.api import 获取GitHub文件内容, 请求GitHubAPI
14
+ from .exceptions.operation import OperationFailed, TryOtherMethods, CancelOther, OperationNotSupported
15
+
16
+ __version__ = "1.0.0"
17
+ __all__ = [
18
+ "消息头",
19
+ "open_file",
20
+ "calculateCharactersDisplayed",
21
+ "IssueNumber",
22
+ "ResolvesIssue",
23
+ "获取GitHub文件内容",
24
+ "请求GitHubAPI",
25
+ "read_token",
26
+ "这是谁的Token",
27
+ "OperationFailed",
28
+ "TryOtherMethods",
29
+ "CancelOther",
30
+ "OperationNotSupported",
31
+ "RequestException",
32
+ ]
@@ -0,0 +1,10 @@
1
+ from .request import RequestException
2
+ from .operation import OperationFailed, TryOtherMethods, CancelOther, OperationNotSupported
3
+
4
+ __all__ = [
5
+ "OperationFailed",
6
+ "TryOtherMethods",
7
+ "CancelOther",
8
+ "OperationNotSupported",
9
+ "RequestException",
10
+ ]
@@ -0,0 +1,13 @@
1
+ """操作异常"""
2
+
3
+ class OperationFailed(Exception):
4
+ """当前操作失败"""
5
+
6
+ class TryOtherMethods(Exception):
7
+ """尝试当前方法失败,请尝试其他方法。"""
8
+
9
+ class CancelOther(Exception):
10
+ """取消后续操作"""
11
+
12
+ class OperationNotSupported(Exception):
13
+ """不支持的操作"""
@@ -0,0 +1,4 @@
1
+ """请求异常"""
2
+
3
+ class RequestException(Exception):
4
+ """请求时出现异常"""
@@ -0,0 +1,9 @@
1
+ from .print import 消息头
2
+ from .files import open_file
3
+ from .terminal import calculateCharactersDisplayed
4
+
5
+ __all__ = [
6
+ "open_file",
7
+ "消息头",
8
+ "calculateCharactersDisplayed"
9
+ ]
@@ -0,0 +1,34 @@
1
+ import os
2
+ import sys
3
+ import subprocess
4
+ from colorama import Fore
5
+ from functions.print import 消息头
6
+
7
+ def open_file(file: str) -> int:
8
+ try:
9
+ if not os.path.exists(file):
10
+ raise FileNotFoundError(f"未找到 {file}")
11
+
12
+ if (sys.platform != "win32") and os.path.isdir(file):
13
+ raise Exception("只能在 Windows 上打开目录")
14
+
15
+ if sys.platform == "win32":
16
+ if file.endswith(".yaml"):
17
+ os.startfile(file)
18
+ else:
19
+ subprocess.run(["code.cmd", file], check=True)
20
+ elif sys.platform == "linux":
21
+ try:
22
+ subprocess.run(["xdg-open", file], check=True)
23
+ except FileNotFoundError:
24
+ try:
25
+ subprocess.run(["nano", file], check=True)
26
+ except FileNotFoundError:
27
+ subprocess.run(["vim", file], check=True)
28
+ raise Exception("我实在不知道该用什么来打开它...")
29
+ else:
30
+ raise OSError(f"很抱歉,作者见识太少,不清楚如何在 {sys.platform} 上打开文件...")
31
+ return 0
32
+ except Exception as e:
33
+ print(f"{消息头.错误} 打开文件时发生异常:\n{Fore.RED}{e}{Fore.RESET}")
34
+ return 1
@@ -0,0 +1,6 @@
1
+ from .github import IssueNumber, ResolvesIssue
2
+
3
+ __all__ = [
4
+ "IssueNumber",
5
+ "ResolvesIssue"
6
+ ]
@@ -0,0 +1,52 @@
1
+ """
2
+ 将传入的字符统一为某种 GitHub 上的格式。
3
+ """
4
+
5
+ def IssueNumber(string: str | int | None) -> str | None:
6
+ """
7
+ 从给定的字符串中获取 Issue 或 PR 的编号。
8
+
9
+ :param string: 给定的字符串或整数或 None
10
+ :type string: str | int | None
11
+ :return: 返回字符串的编号,获取失败返回 None
12
+ :rtype: str | None
13
+ """
14
+
15
+ if not string:
16
+ return None
17
+ elif isinstance(string, int) and (string > 0):
18
+ return str(string)
19
+ elif isinstance(string, str):
20
+ string = string.strip()
21
+ if (not string) or (string == "0"):
22
+ return None
23
+ elif string.isdigit(): # 正整数
24
+ return string
25
+ elif string.startswith("#") and string[1:].isdigit():
26
+ return string[1:]
27
+ elif string.startswith("https://"):
28
+ for path in reversed(string.split("#", 1)[0].split("/")):
29
+ if path.isdigit():
30
+ return path
31
+
32
+ return None
33
+
34
+ def ResolvesIssue(string: str) -> str | None:
35
+ """
36
+ 将给定的字符串格式化为 GitHub PR 的 Resolves Issue 格式
37
+
38
+ GitHub Docs: https://docs.github.com/zh/issues/tracking-your-work-with-issues/using-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword
39
+
40
+ :param string: 给定的字符串
41
+ :type string: str
42
+ :return: 格式化成功返回字符串结果,失败返回 None
43
+ :rtype: str | None
44
+ """
45
+
46
+ num = IssueNumber(string)
47
+
48
+ if num:
49
+ # TODO: 支持 Fixes 等更多前缀
50
+ return f"- Resolves {num}"
51
+ else:
52
+ return None
@@ -0,0 +1,9 @@
1
+ from .token import read_token, 这是谁的Token
2
+ from .api import 获取GitHub文件内容, 请求GitHubAPI
3
+
4
+ __all__ = [
5
+ "获取GitHub文件内容",
6
+ "请求GitHubAPI",
7
+ "read_token",
8
+ "这是谁的Token"
9
+ ]
@@ -0,0 +1,61 @@
1
+ import base64
2
+ import requests
3
+ from typing import Any
4
+ from exceptions.request import RequestException
5
+
6
+ def 获取GitHub文件内容(repo: str, path: str, github_token: str | int | None = None) -> str | None:
7
+ """
8
+ 尝试通过 GitHub API 获取文本文件 base64,解码后返回。
9
+
10
+ :param repo: 文件所在的仓库,应为 `owner/repo` 的格式
11
+ :type repo: str
12
+ :param path: 需要获取的文件在仓库中的相对路径
13
+ :type path: str
14
+ :param github_token: 请求时附带的 GitHub Token
15
+ :type github_token: str | int | None
16
+ :return: UTF-8 编码解码后的文本文件字符串,获取失败返回 None
17
+ :rtype: str | None
18
+ """
19
+
20
+ try:
21
+ if (len(repo.split("/")) < 2) or (len(repo.split("/")) > 3):
22
+ raise ValueError("指定的仓库格式不对")
23
+
24
+ response = 请求GitHubAPI(
25
+ f"https://api.github.com/repos/{repo}/contents/{path.replace("\\", "/")}",
26
+ github_token
27
+ )
28
+
29
+ if not response:
30
+ raise RequestException("响应为空")
31
+
32
+ return base64.b64decode(response["content"]).decode("utf-8")
33
+ except Exception:
34
+ return None
35
+
36
+ def 请求GitHubAPI(api: str, github_token: str | int | None = None) -> Any | None:
37
+ """
38
+ 尝试向指定的 api 发送 GET 请求,返回响应的 json 内容
39
+
40
+ :param api: api 的 URL
41
+ :type api: str
42
+ :param github_token: 请求时附带的 GitHub Token
43
+ :type github_token: str | int | None
44
+ :return: 返回 `.json()` 后的响应,失败时返回 None
45
+ :rtype: Any | None
46
+ """
47
+
48
+ headers: dict[str, str] = {
49
+ "Authorization": f"token {github_token}",
50
+ "Accept": "application/vnd.github.v3+json"
51
+ }
52
+
53
+ if not github_token:
54
+ headers.pop("Authorization", None)
55
+
56
+ try:
57
+ response = requests.get(api, headers=headers)
58
+ response.raise_for_status()
59
+ return response.json()
60
+ except Exception:
61
+ return None
@@ -0,0 +1,55 @@
1
+ import keyring
2
+ from typing import cast, Any
3
+ from functions.print import 消息头
4
+ from functions.github.api import 请求GitHubAPI
5
+
6
+ def read_token(silent: bool = False) -> str | None:
7
+ """
8
+ 尝试从钥匙环中读取 github-access-token.glm 密钥 (aka glm 设置的 GitHub Token)
9
+
10
+ :param silent: 是否阻止输出
11
+ :type silent: bool
12
+ :return: 返回 str 的 GitHub Token,失败返回 None
13
+ :rtype: str | None
14
+ """
15
+
16
+ try:
17
+ token = keyring.get_password("github-access-token.glm", "github-access-token")
18
+ if token:
19
+ return token
20
+ elif not silent:
21
+ print("你可能还没设置 glm 的 Token, 请尝试使用以下命令设置 Token:\n glm config --token <YOUR-TOKEN>\n")
22
+ return None
23
+ except Exception as e:
24
+ if not silent:
25
+ print(f"{消息头.错误} 读取Token时出错:\n{e}")
26
+ return None
27
+
28
+ def 这是谁的Token(token: str | None) -> str | None:
29
+ """
30
+ 通过 GitHub API 来确认这个 Token 是谁的
31
+
32
+ :param token: 指定的 GitHub Token
33
+ :type token: str | None
34
+ :return: 返回 str 的所有者,失败返回 None
35
+ :rtype: str | None
36
+ """
37
+
38
+ if not isinstance(token, str):
39
+ return None
40
+
41
+ token = token.strip()
42
+ if not token:
43
+ return None
44
+
45
+ response: Any | None = 请求GitHubAPI(
46
+ "https://api.github.com/user", token
47
+ )
48
+
49
+ if isinstance(response, dict):
50
+ response = cast(dict[str, Any], response)
51
+ login: Any | None = response.get("login", None)
52
+ if isinstance(login, str):
53
+ return login
54
+
55
+ return None
@@ -0,0 +1,25 @@
1
+ from colorama import Fore
2
+
3
+ class 消息头():
4
+ # 特殊
5
+ 消息 = f"{Fore.BLUE}[!]{Fore.RESET}"
6
+ 问题 = f"{Fore.BLUE}?{Fore.RESET}"
7
+ 可选问题 = f"{Fore.BLUE}? (可选){Fore.RESET}"
8
+ # 日志输出
9
+ 信息 = f"{Fore.BLUE}INFO{Fore.RESET}"
10
+ 成功 = f"{Fore.GREEN}✓{Fore.RESET}"
11
+ 错误 = f"{Fore.RED}✕{Fore.RESET}"
12
+ 警告 = f"{Fore.YELLOW}WARN{Fore.RESET}"
13
+ 调试 = f"{Fore.CYAN}DEBUG{Fore.RESET}"
14
+ 提示 = f"{Fore.YELLOW}Hint{Fore.RESET}"
15
+ # 内部
16
+ 内部警告 = f"{Fore.YELLOW}WARN (内部){Fore.RESET}"
17
+ 内部错误 = f"{Fore.RED}✕ (内部){Fore.RESET}"
18
+
19
+ def 多行带头输出(内容: str, 头: str) -> None:
20
+ """
21
+ 输出多行带消息头的内容。
22
+ """
23
+
24
+ for 行 in 内容:
25
+ print(f"{头} {行}")
@@ -0,0 +1,29 @@
1
+ import re
2
+ import sys
3
+ from exceptions.operation import OperationNotSupported
4
+
5
+ def calculateCharactersDisplayed(content: str) -> int:
6
+ """
7
+ 计算内容在 Windows 终端上显示占多少字符的位置。
8
+
9
+ 方法请参阅我的文章: https://duckduckstudio.github.io/Articles/#/%E4%BF%A1%E6%81%AF%E9%80%9F%E6%9F%A5/Python/%E8%BE%93%E5%87%BA/%E8%AE%A1%E7%AE%97%E8%BE%93%E5%87%BA%E7%9A%84%E5%86%85%E5%AE%B9%E5%9C%A8Windows%E7%BB%88%E7%AB%AF%E4%B8%8A%E7%9A%84%E6%98%BE%E7%A4%BA%E5%8D%A0%E5%A4%9A%E5%B0%91%E5%AD%97%E7%AC%A6
10
+
11
+ :param content: 指定的内容
12
+ :type content: str
13
+ :return: 显示所占的字数
14
+ :rtype: int
15
+ """
16
+
17
+ if (sys.platform != "win32"):
18
+ raise OperationNotSupported("calculateCharactersDisplayed 仅在 Windows 终端中可用")
19
+
20
+ # 移除颜色转义
21
+ content = re.sub(r"\x1b\[[0-9;]*m", "", content)
22
+
23
+ total = 0
24
+ for char in content:
25
+ total += 1
26
+ if not ((ord(char) < 128) or (char in ["♪"])):
27
+ total += 1
28
+
29
+ return total
File without changes
@@ -0,0 +1,76 @@
1
+ Metadata-Version: 2.4
2
+ Name: catfood
3
+ Version: 1.0.0
4
+ Summary: A collection of various commonly used functions. / 各种常用函数的集合。
5
+ Author: 鸭鸭「カモ」
6
+ License-Expression: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/DuckDuckStudio/catfood
8
+ Project-URL: Repository, https://github.com/DuckDuckStudio/catfood
9
+ Project-URL: Issues, https://github.com/DuckDuckStudio/catfood/issues
10
+ Classifier: Development Status :: 1 - Planning
11
+ Classifier: Environment :: Console
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Natural Language :: Chinese (Simplified)
14
+ Classifier: Operating System :: OS Independent
15
+ Classifier: Programming Language :: Python
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3.13
18
+ Classifier: Programming Language :: Python :: Implementation :: CPython
19
+ Classifier: Topic :: Software Development
20
+ Classifier: Topic :: Software Development :: Libraries
21
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
22
+ Description-Content-Type: text/markdown
23
+ License-File: LICENSE
24
+ Requires-Dist: colorama>=0.4.6
25
+ Requires-Dist: requests>=2.32.5
26
+ Requires-Dist: keyring>=25.6.0
27
+ Provides-Extra: typing-check
28
+ Requires-Dist: pyright; extra == "typing-check"
29
+ Provides-Extra: build-and-publish
30
+ Requires-Dist: build; extra == "build-and-publish"
31
+ Requires-Dist: twine; extra == "build-and-publish"
32
+ Dynamic: license-file
33
+
34
+ # 猫粮 🐱
35
+
36
+ 各种常用函数的集合。
37
+
38
+ ## 安装
39
+
40
+ 我自己仅在 CPython 3.13 (3.13.9) 测试过。
41
+
42
+ ### Pypi
43
+ ```bash
44
+ # https://pypi.org/project/catfood/
45
+ pip install catfood
46
+ ```
47
+
48
+ ### Test Pypi
49
+ ```bash
50
+ # https://test.pypi.org/project/catfood/
51
+ pip install -i https://test.pypi.org/simple/ catfood
52
+ ```
53
+
54
+ ### 从源安装
55
+ ```bash
56
+ git clone https://github.com/DuckDuckStudio/catfood.git
57
+ pip install ./catfood
58
+ ```
59
+
60
+ #### Build whl
61
+ ```bash
62
+ # Windows PowerShell
63
+ git clone https://github.com/DuckDuckStudio/catfood.git
64
+ cd catfood
65
+
66
+ python -m venv .venv
67
+ & ".venv/Scripts/Activate.ps1"
68
+ python.exe -m pip install pip --upgrade
69
+
70
+ pip install ".[build_and_publish]" # 包括构建和发布依赖 build 和 twine
71
+ python -m build
72
+ ls dist/
73
+
74
+ # 从 whl 安装
75
+ pip install dist/catfood-1.0.0-py3-none-any.whl
76
+ ```
@@ -0,0 +1,22 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ catfood/__init__.py
5
+ catfood/py.typed
6
+ catfood.egg-info/PKG-INFO
7
+ catfood.egg-info/SOURCES.txt
8
+ catfood.egg-info/dependency_links.txt
9
+ catfood.egg-info/requires.txt
10
+ catfood.egg-info/top_level.txt
11
+ catfood/exceptions/__init__.py
12
+ catfood/exceptions/operation.py
13
+ catfood/exceptions/request.py
14
+ catfood/functions/__init__.py
15
+ catfood/functions/files.py
16
+ catfood/functions/print.py
17
+ catfood/functions/terminal.py
18
+ catfood/functions/format/__init__.py
19
+ catfood/functions/format/github.py
20
+ catfood/functions/github/__init__.py
21
+ catfood/functions/github/api.py
22
+ catfood/functions/github/token.py
@@ -0,0 +1,10 @@
1
+ colorama>=0.4.6
2
+ requests>=2.32.5
3
+ keyring>=25.6.0
4
+
5
+ [build_and_publish]
6
+ build
7
+ twine
8
+
9
+ [typing_check]
10
+ pyright
@@ -0,0 +1 @@
1
+ catfood
@@ -0,0 +1,62 @@
1
+ [build-system]
2
+ requires = [
3
+ "setuptools>=77.0.0",
4
+ "wheel"
5
+ ]
6
+ build-backend = "setuptools.build_meta"
7
+
8
+ [project]
9
+ name = "catfood"
10
+ version = "1.0.0"
11
+ description = "A collection of various commonly used functions. / 各种常用函数的集合。"
12
+ readme = "README.md"
13
+ authors = [
14
+ { name = "鸭鸭「カモ」" }
15
+ ]
16
+ license = "Apache-2.0"
17
+ license-files = [
18
+ "LICENSE"
19
+ ]
20
+ classifiers = [
21
+ "Development Status :: 1 - Planning",
22
+ "Environment :: Console",
23
+ "Intended Audience :: Developers",
24
+ "Natural Language :: Chinese (Simplified)",
25
+ "Operating System :: OS Independent",
26
+ "Programming Language :: Python",
27
+ "Programming Language :: Python :: 3",
28
+ "Programming Language :: Python :: 3.13",
29
+ "Programming Language :: Python :: Implementation :: CPython",
30
+ "Topic :: Software Development",
31
+ "Topic :: Software Development :: Libraries",
32
+ "Topic :: Software Development :: Libraries :: Python Modules"
33
+ ]
34
+ dependencies = [
35
+ "colorama>=0.4.6",
36
+ "requests>=2.32.5",
37
+ "keyring>=25.6.0"
38
+ ]
39
+
40
+ [project.urls]
41
+ Homepage = "https://github.com/DuckDuckStudio/catfood"
42
+ Repository = "https://github.com/DuckDuckStudio/catfood"
43
+ Issues = "https://github.com/DuckDuckStudio/catfood/issues"
44
+
45
+ [tool.setuptools.packages.find]
46
+ where = ["."]
47
+ include = [
48
+ "catfood*"
49
+ ]
50
+ exclude = [
51
+ "tests*",
52
+ "examples*"
53
+ ]
54
+
55
+ [project.optional-dependencies]
56
+ typing_check = [
57
+ "pyright"
58
+ ]
59
+ build_and_publish = [
60
+ "build",
61
+ "twine"
62
+ ]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+