Skip to content

Commit 59cafc7

Browse files
committed
feat: use local template data instead of remote API
1 parent 1531a16 commit 59cafc7

2 files changed

Lines changed: 101 additions & 40 deletions

File tree

QuickProject/Qpro.py

Lines changed: 100 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -118,54 +118,115 @@ def _create_empty_project(project_name):
118118

119119

120120
def _search_supported_languages(is_CN=using_gitee):
121+
# 本地模板数据库
122+
# 格式: {显示名称: [GitHub URL, Gitee URL, 替换字符串]}
123+
QPRO_TEMPLATES = {
124+
"C - QuickProject's C Template": [
125+
"https://github.com/Rhythmicc/QproCTemplate",
126+
"https://gitee.com/RhythmLian/QproCTemplate",
127+
"QproCTemplate"
128+
],
129+
"C++ - QuickProject's cpp": [
130+
"https://github.com/Rhythmicc/QproCppTemplate",
131+
"https://gitee.com/RhythmLian/QproCppTemplate",
132+
"QproCppTemplate"
133+
],
134+
"Teaching Assistant - 助教模板": [
135+
"https://github.com/Rhythmicc/QproTeachingAssistantTemplate",
136+
"https://gitee.com/RhythmLian/QproTeachingAssistantTemplate",
137+
"QproTeachingAssistantTemplate"
138+
],
139+
"Socket Server - Qpro SockServer Template": [
140+
"https://github.com/Rhythmicc/QproSockServerTemplate",
141+
"https://gitee.com/RhythmLian/QproSockServerTemplate",
142+
"QproSockServerTemplate"
143+
],
144+
"Commander - Qpro Commander Template": [
145+
"https://github.com/Rhythmicc/QproCommanderTemplate",
146+
"https://gitee.com/RhythmLian/QproCommanderTemplate",
147+
"QproCommanderTemplate"
148+
],
149+
"Verilog - A Qpro Verilog Template": [
150+
"https://github.com/Rhythmicc/QproVerilogTemplate",
151+
"https://gitee.com/RhythmLian/QproVerilogTemplate",
152+
"QproVerilogTemplate"
153+
],
154+
"Python2 - QuickProject's Python2 Template": [
155+
"https://github.com/Rhythmicc/QproPythonTemplate",
156+
"https://gitee.com/RhythmLian/QproPythonTemplate",
157+
"QproPythonTemplate"
158+
],
159+
"Python3 - QuickProject's Python3 Template": [
160+
"https://github.com/Rhythmicc/QproPython3Template",
161+
"https://gitee.com/RhythmLian/QproPython3Template",
162+
"QproPython3Template"
163+
],
164+
"RISC-V - Qpro RISC-V Template": [
165+
"https://github.com/Rhythmicc/QproRiscVTemplate",
166+
"https://gitee.com/RhythmLian/QproRiscVTemplate",
167+
"QproRiscVTemplate"
168+
],
169+
"Pypi - Qpro Pypi lib template": [
170+
"https://github.com/Rhythmicc/QproPypiTemplate",
171+
"https://gitee.com/RhythmLian/QproPypiTemplate",
172+
"QproPypiTemplate"
173+
],
174+
"Scala - Qpro Scala Template": [
175+
"https://github.com/Rhythmicc/QproScalaTemplate",
176+
"https://gitee.com/RhythmLian/QproScalaTemplate",
177+
"QproScalaTemplate"
178+
],
179+
"Java - QuickProject's Java Template": [
180+
"https://github.com/Rhythmicc/QproJavaTemplate",
181+
"https://gitee.com/RhythmLian/QproJavaTemplate",
182+
"QproJavaTemplate"
183+
],
184+
"DCU - QproDCUTemplate": [
185+
"https://github.com/Rhythmicc/QproDCUTemplate",
186+
"https://gitee.com/RhythmLian/QproDCUTemplate",
187+
"QproDCUTemplate"
188+
],
189+
"Pypi Commander - Qpro Pypi Commander Template": [
190+
"https://github.com/Rhythmicc/QproPypiCommanderTemplate",
191+
"https://gitee.com/RhythmLian/QproPypiCommanderTemplate",
192+
"QproPypiCommanderTemplate"
193+
],
194+
}
195+
121196
kw = _ask(
122197
{
123198
"type": "input",
124199
"message": _lang["askKeyword"],
125200
}
126201
)
127202

128-
QproDefaultStatus(_lang["SearchingTemplate"])
129-
QproDefaultStatus.start()
203+
# 本地搜索匹配
204+
matched_templates = {}
205+
kw_lower = kw.lower()
206+
for name, urls in QPRO_TEMPLATES.items():
207+
if kw_lower in name.lower():
208+
matched_templates[name] = urls
130209

131-
import time
132-
import json
133-
import requests
134-
from requests.exceptions import ProxyError
135-
136-
retry = 3
210+
if not matched_templates:
211+
QproDefaultConsole.print(QproErrorString, f"未找到匹配 '{kw}' 的模板")
212+
return None
137213

138-
while retry:
139-
try:
140-
res = json.loads(
141-
requests.get(
142-
f"https://qpro-lang.rhythm.icu/?keyword={kw}&is_CN={str(is_CN).lower()}"
143-
).text
144-
)
145-
if not res["status"]:
146-
QproDefaultConsole.print(QproErrorString, res["message"])
147-
return None
148-
data = {i[0]: i[1:] for i in res["data"]}
149-
QproDefaultStatus.stop()
150-
return data[
151-
_ask(
152-
{
153-
"type": "list",
154-
"message": _lang["ChooseSupportedTemplate"],
155-
"choices": list(data.keys()),
156-
}
157-
)
158-
]
159-
except ProxyError:
160-
retry -= 1
161-
time.sleep(3)
162-
except Exception as e:
163-
QproDefaultConsole.print(QproErrorString, repr(e))
164-
return None
165-
166-
QproDefaultStatus.stop()
167-
QproDefaultConsole.print(QproErrorString, _lang["TemplateServerError"])
168-
return None
214+
# 选择模板
215+
selected_name = _ask(
216+
{
217+
"type": "list",
218+
"message": _lang["ChooseSupportedTemplate"],
219+
"choices": list(matched_templates.keys()),
220+
}
221+
)
222+
223+
template_info = matched_templates[selected_name]
224+
# 返回 [git_url, replace_string]
225+
# 根据是否使用国内源选择 URL
226+
git_url = template_info[1] if is_CN else template_info[0]
227+
replace_string = template_info[2]
228+
229+
return [git_url, replace_string]
169230

170231

171232
def _external_create(project_name: str, key: str = ""):

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "qpro"
7-
version = "0.14.2"
7+
version = "0.14.3"
88
description = "Small but powerful command line APP framework"
99
readme = {file = "README.md", content-type = "text/markdown"}
1010
license = {text = "MIT"}

0 commit comments

Comments
 (0)