Skip to content

Commit ccb6d6a

Browse files
committed
refactor: 优化代码格式和结构
- 使用自动格式化工具重新格式化代码 - 调整缩进和换行方式 - 简化部分代码表达方式
1 parent 37491b6 commit ccb6d6a

31 files changed

+238
-247
lines changed

scripts/build_app.py

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,25 @@ def build_with_pdm_packer():
4040
print("Building with pdm-packer...")
4141
ensure_dist_dir()
4242
os.environ["PYTHONOPTIMIZE"] = "2"
43-
result = subprocess.run([
44-
shutil.which("pdm"),
45-
"pack",
46-
"-m", "vcf_generator_lite.__main__:main",
47-
"-o", os.path.join("dist", OUTPUT_BASE_NAME_TEMPLATE.format(
48-
version=APP_VERSION,
49-
platform=PLATFORM_PYTHON,
50-
distribution="zipapp"
51-
) + ".pyzw"),
52-
"--interpreter", f"/usr/bin/env python{PYTHON_VERSION}",
53-
"--compile",
54-
"--compress",
55-
"--no-py",
56-
])
43+
result = subprocess.run(
44+
[
45+
shutil.which("pdm"),
46+
"pack",
47+
"-m",
48+
"vcf_generator_lite.__main__:main",
49+
"-o",
50+
os.path.join(
51+
"dist",
52+
OUTPUT_BASE_NAME_TEMPLATE.format(version=APP_VERSION, platform=PLATFORM_PYTHON, distribution="zipapp")
53+
+ ".pyzw",
54+
),
55+
"--interpreter",
56+
f"/usr/bin/env python{PYTHON_VERSION}",
57+
"--compile",
58+
"--compress",
59+
"--no-py",
60+
]
61+
)
5762
print("Building finished.")
5863
return result.returncode
5964

@@ -66,29 +71,32 @@ def pack_with_innosetup() -> int:
6671
return result
6772

6873
os.environ["PATH"] += os.pathsep + "C:\\Program Files (x86)\\Inno Setup 6\\"
69-
result = subprocess.run([
70-
shutil.which("iscc"),
71-
"/D" + f"OutputBaseFilename={OUTPUT_BASE_NAME_TEMPLATE.format(
74+
result = subprocess.run(
75+
[
76+
shutil.which("iscc"),
77+
"/D"
78+
+ f"OutputBaseFilename={OUTPUT_BASE_NAME_TEMPLATE.format(
7279
version=APP_VERSION,
7380
platform=PLATFORM_NATIVE,
7481
distribution="setup"
7582
)}",
76-
"/D" + f"MyAppCopyright={APP_COPYRIGHT}",
77-
"/D" + f"MyAppVersion={APP_VERSION}",
78-
os.path.abspath('setup.iss'),
79-
])
83+
"/D" + f"MyAppCopyright={APP_COPYRIGHT}",
84+
"/D" + f"MyAppVersion={APP_VERSION}",
85+
os.path.abspath("setup.iss"),
86+
]
87+
)
8088
print("Packaging finished.")
8189
return result.returncode
8290

8391

8492
def pack_with_zipfile():
8593
print("Packaging with ZipFile...")
8694
require_pyinstaller_output()
87-
zip_path = os.path.join("dist", OUTPUT_BASE_NAME_TEMPLATE.format(
88-
version=APP_VERSION,
89-
platform=PLATFORM_NATIVE,
90-
distribution="portable"
91-
) + ".zip")
95+
zip_path = os.path.join(
96+
"dist",
97+
OUTPUT_BASE_NAME_TEMPLATE.format(version=APP_VERSION, platform=PLATFORM_NATIVE, distribution="portable")
98+
+ ".zip",
99+
)
92100
with ZipFile(zip_path, "w") as zip_file:
93101
for path, dirs, files in os.walk(os.path.join("dist", "vcf_generator_lite")):
94102
for file_path in [os.path.join(path, file) for file in files]:
@@ -99,11 +107,12 @@ def pack_with_zipfile():
99107
def main() -> int:
100108
parser = argparse.ArgumentParser()
101109
parser.add_argument(
102-
"-t", "--type",
110+
"-t",
111+
"--type",
103112
type=str,
104113
default="installer",
105114
choices=["installer", "portable", "zipapp"],
106-
help="应用打包类型(默认:%(default)s)"
115+
help="应用打包类型(默认:%(default)s)",
107116
)
108117
args = parser.parse_args()
109118

scripts/docs/generate_os_notices.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
},
1818
)
1919

20-
NoticesConfig = TypedDict(
21-
"NoticesConfig", {"template": str, "output": str, "notices": list[Notice]}
22-
)
20+
NoticesConfig = TypedDict("NoticesConfig", {"template": str, "output": str, "notices": list[Notice]})
2321

2422

2523
def format_url(url: str, notice: Notice) -> str:
@@ -29,10 +27,7 @@ def format_url(url: str, notice: Notice) -> str:
2927

3028

3129
def generate_notices(config: NoticesConfig):
32-
return [
33-
{**notice, "license-url": format_url(notice["license-url"], notice=notice)}
34-
for notice in config["notices"]
35-
]
30+
return [{**notice, "license-url": format_url(notice["license-url"], notice=notice)} for notice in config["notices"]]
3631

3732

3833
def main() -> int:

scripts/prepare_innosetup_extensions.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@
44

55
import requests
66

7-
URL_CHINESE_SIMPLIFIED_ISL_URL = "https://raw.github.com/jrsoftware/issrc/main/Files/Languages/Unofficial/ChineseSimplified.isl"
8-
URL_CHINESE_SIMPLIFIED_ISL_LATEST = "https://github.com/kira-96/Inno-Setup-Chinese-Simplified-Translation/raw/refs/heads/main/ChineseSimplified.isl"
7+
URL_CHINESE_SIMPLIFIED_ISL_URL = (
8+
"https://raw.github.com/jrsoftware/issrc/main/Files/Languages/Unofficial/ChineseSimplified.isl"
9+
)
10+
URL_CHINESE_SIMPLIFIED_ISL_LATEST = (
11+
"https://github.com/kira-96/Inno-Setup-Chinese-Simplified-Translation/raw/refs/heads/main/ChineseSimplified.isl"
12+
)
913
# 国内使用 GitCode 加速下载
10-
URL_CHINESE_SIMPLIFIED_ISL_GITCODE = "https://raw.gitcode.com/gh_mirrors/is/issrc/raw/main/Files/Languages/Unofficial/ChineseSimplified.isl"
14+
URL_CHINESE_SIMPLIFIED_ISL_GITCODE = (
15+
"https://raw.gitcode.com/gh_mirrors/is/issrc/raw/main/Files/Languages/Unofficial/ChineseSimplified.isl"
16+
)
1117

1218
PATH_INNOSETUP_EXTENSION = "./.innosetup"
1319
PATH_CHINESE_SIMPLIFIED = os.path.join(PATH_INNOSETUP_EXTENSION, "Languages", "ChineseSimplified.isl")
@@ -33,11 +39,12 @@ def prepare_innosetup_extensions(download_url: str) -> int:
3339
def main() -> int:
3440
parser = argparse.ArgumentParser()
3541
parser.add_argument(
36-
"-m", "--mirror",
42+
"-m",
43+
"--mirror",
3744
type=str,
3845
default="latest",
3946
choices=["github", "gitcode", "latest"],
40-
help="文件下载镜像(默认:%(default)s)"
47+
help="文件下载镜像(默认:%(default)s)",
4148
)
4249
args = parser.parse_args()
4350
match args.mirror:

scripts/version.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@ def get_exe_style_version(version: str):
1515

1616

1717
def change_version(
18-
file_name: str,
19-
content_pattern: Pattern[str],
20-
content_formatter: str,
21-
version: str,
22-
encoding="utf-8"
18+
file_name: str, content_pattern: Pattern[str], content_formatter: str, version: str, encoding="utf-8"
2319
):
2420
with open(file_name, "r", encoding=encoding) as f:
2521
origin_content = f.read()
@@ -34,27 +30,26 @@ def change_pyproject_version(version: str):
3430
file_name="pyproject.toml",
3531
content_pattern=re.compile(r'^ *version *= *".*" *$', flags=re.M),
3632
content_formatter='version = "%s"',
37-
version=version
33+
version=version,
3834
)
3935

4036

4137
def change_version_info(version: str):
4238
pyinstaller_versionfile.create_versionfile_from_input_file(
43-
output_file="versionfile.txt",
44-
input_file="metadata.yml",
45-
version=get_exe_style_version(version)
39+
output_file="versionfile.txt", input_file="metadata.yml", version=get_exe_style_version(version)
4640
)
4741
print("Change version to %s in %s." % (version, "versionfile.txt"))
4842

4943

5044
def print_version():
5145
from vcf_generator_lite.__version__ import __version__
46+
5247
print(__version__)
5348

5449

5550
def main() -> int:
5651
parser = argparse.ArgumentParser()
57-
parser.add_argument("version", nargs='?', type=str)
52+
parser.add_argument("version", nargs="?", type=str)
5853
args = parser.parse_args()
5954

6055
version = args.version

src/vcf_generator_lite/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def fix_home_env():
1111
"""
1212
修复 Tkinter 在 Windows 中无法获取 HOME 的问题
1313
"""
14-
os.environ['HOME'] = os.path.expanduser("~")
14+
os.environ["HOME"] = os.path.expanduser("~")
1515

1616

1717
def setup_logging():

src/vcf_generator_lite/constants.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
URL_REPOSITORY = "https://gitee.com/HelloTool/VCFGeneratorLiteForTkinter"
1010
URL_REPORT = "https://gitee.com/HelloTool/VCFGeneratorLiteForTkinter/issues/new/choose"
1111
URL_LICENSE = f"https://gitee.com/HelloTool/VCFGeneratorLiteForTkinter/blob/{SOURCE_REFERENCE}/LICENSE"
12-
URL_OS_NOTICES = f"https://gitee.com/HelloTool/VCFGeneratorLiteForTkinter/blob/{SOURCE_REFERENCE}/docs/legal/os_notices.md"
12+
URL_OS_NOTICES = (
13+
f"https://gitee.com/HelloTool/VCFGeneratorLiteForTkinter/blob/{SOURCE_REFERENCE}/docs/legal/os_notices.md"
14+
)
1315

1416
EMAIL_JESSE205 = "[email protected]"

src/vcf_generator_lite/layouts/vertical_dialog_layout.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,10 @@ def _create_widgets(self, parent: Misc):
2222
footer.pack(fill=X, side=BOTTOM)
2323

2424
@abstractmethod
25-
def _create_header(self, parent: Misc) -> Optional[Widget]:
26-
...
25+
def _create_header(self, parent: Misc) -> Optional[Widget]: ...
2726

2827
@abstractmethod
29-
def _create_content(self, parent: Misc) -> Optional[Widget]:
30-
...
28+
def _create_content(self, parent: Misc) -> Optional[Widget]: ...
3129

3230
@abstractmethod
33-
def _create_actions(self, parent: Misc) -> Optional[Widget]:
34-
...
31+
def _create_actions(self, parent: Misc) -> Optional[Widget]: ...

src/vcf_generator_lite/models/contact.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def parse_contact(person_text: str):
2323
if is_china_mobile_phone(part):
2424
phone = part
2525
name = " ".join(parts[:i])
26-
note_parts = parts[i + 1:]
26+
note_parts = parts[i + 1 :]
2727
note = " ".join(note_parts) if note_parts else None
2828
break
2929
else:

src/vcf_generator_lite/services/vcf_generator.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def serialize_to_vcard(contact: Contact):
4040
items: list[str] = [
4141
"VERSION:2.1",
4242
f"FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:{utf8_to_qp(contact.name)}",
43-
f"TEL;CELL:{contact.phone}"
43+
f"TEL;CELL:{contact.phone}",
4444
]
4545
if contact.note:
4646
items.append(f"NOTE;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:{utf8_to_qp(contact.note)}")
@@ -55,19 +55,13 @@ def __init__(
5555
executor: ThreadPoolExecutor,
5656
progress_listener: Optional[Callable[[float, bool], None]],
5757
input_text: str,
58-
output_io: IO
58+
output_io: IO,
5959
):
6060
self._executor = executor
6161
self._progress_listener = progress_listener
6262
self._input_text = input_text
6363
self._output_io = output_io
64-
self._state = VCardGeneratorState(
65-
total=0,
66-
processed=0,
67-
progress=0.0,
68-
invalid_lines=[],
69-
exceptions=[]
70-
)
64+
self._state = VCardGeneratorState(total=0, processed=0, progress=0.0, invalid_lines=[], exceptions=[])
7165
self._write_queue = Queue()
7266

7367
def start(self) -> Future[GenerateResult]:
@@ -84,10 +78,7 @@ def _process(self) -> GenerateResult:
8478
for future in done:
8579
if exception := future.exception():
8680
self._state.exceptions.append(exception)
87-
return GenerateResult(
88-
invalid_lines=self._state.invalid_lines,
89-
exceptions=self._state.exceptions
90-
)
81+
return GenerateResult(invalid_lines=self._state.invalid_lines, exceptions=self._state.exceptions)
9182

9283
def _parse_input(self):
9384
lines = [line.strip() for line in self._input_text.split("\n")]

src/vcf_generator_lite/themes/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
def create_platform_theme() -> EnhancedTheme:
88
if is_windows:
99
from vcf_generator_lite.themes.widows_theme import WindowsTheme
10+
1011
return WindowsTheme()
1112

1213
from vcf_generator_lite.themes.clam_theme import ClamTheme
14+
1315
return ClamTheme()

0 commit comments

Comments
 (0)