Skip to content

Commit 0454a46

Browse files
committed
perf: simplify and enhance config initialization
Signed-off-by: l.feng <43399351+msclock@users.noreply.github.com>
1 parent c207eb8 commit 0454a46

6 files changed

Lines changed: 44 additions & 27 deletions

File tree

src/_core/include/pysubconverter.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace _core {
77

8-
void init_config(const std::string &configDir);
8+
void init_config();
99

1010
void update_config(const std::map<std::string, std::string> &arguments);
1111

src/_core/src/pybind.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ PYBIND11_MODULE(_core, m) {
2727

2828
m.def("init_config",
2929
_core::init_config,
30-
py::arg("dir"),
3130
py::doc(R"pbdoc(initialize the configuration directory from subconverter.)pbdoc"));
3231

3332
m.def("subconverter", _core::subconverter, py::arg("arguments"), py::doc(R"pbdoc(convert to subscription format

src/_core/src/pysubconverter.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,7 @@
1010
namespace fs = std::filesystem;
1111

1212
namespace _core {
13-
void cd(const std::string &dir) {
14-
try {
15-
fs::current_path(fs::canonical(dir));
16-
}
17-
catch (const fs::filesystem_error &e) {
18-
throw std::runtime_error(e.what());
19-
}
20-
}
21-
22-
void init_config(const std::string &configDir) {
23-
cd(configDir);
13+
void init_config() {
2414
if (!fileExist(global.prefPath)) {
2515
if (fileExist("pref.toml"))
2616
global.prefPath = "pref.toml";

src/_core/tests/test_core.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ using _core_rc_test = test::utils::rc_dir_test;
1414

1515
TEST_F(_core_rc_test, subconverter) {
1616
auto config_dir = this->test_data_dir_ / "config";
17-
_core::init_config(config_dir.string());
17+
std::filesystem::current_path(config_dir);
18+
_core::init_config();
1819
std::vector<std::string> urls = {
1920
"ss://YWVzLTI1Ni1nY206VEV6amZBWXEySWp0dW9T@127.0.0.1:0123#fake 1",
2021
"ss://YWVzLTI1Ni1nY206VEV6amZBWXEySWp0dW9T@127.0.0.1:0123#fake 2",

src/pysubconverter/__init__.py

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from __future__ import annotations
88

9+
import os
910
import shutil
1011
from contextlib import contextmanager
1112
from pathlib import Path
@@ -62,21 +63,47 @@ def config_context(
6263
renew: bool = False,
6364
) -> Generator[None, Any, None]:
6465
"""
65-
A context manager to initialize and configure the config directory.
66+
Context manager for initializing and configuring the config directory with enhanced safety.
67+
68+
Args:
69+
cache_dir: Directory path for caching configurations
70+
renew: Force renew configuration files when True
71+
72+
Yields:
73+
None: Context manager protocol
74+
75+
Raises:
76+
FileNotFoundError: If package config directory not found
77+
RuntimeError: If directory operations fail
6678
"""
6779
config_lock = FileLock(Path(cache_dir) / "config.lock")
6880
config_dir = Path(cache_dir) / "config"
81+
6982
with config_lock:
70-
pkg_config = distribution(__package__).locate_file(__package__) / "config"
71-
if not config_dir.exists():
72-
shutil.copytree(str(pkg_config), config_dir)
83+
try:
84+
pkg_config = distribution(__package__).locate_file(__package__) / "config"
85+
if not pkg_config.exists():
86+
raise FileNotFoundError(
87+
"Package config directory not found at" + str(pkg_config)
88+
)
7389

74-
if renew:
75-
shutil.rmtree(config_dir)
76-
shutil.copytree(str(pkg_config), config_dir)
90+
if renew and config_dir.exists():
91+
shutil.rmtree(config_dir)
7792

78-
init_config(str(config_dir))
79-
try:
80-
yield
81-
finally:
82-
pass
93+
if not config_dir.exists():
94+
shutil.copytree(str(pkg_config), config_dir)
95+
96+
origin_dir = Path.cwd().resolve()
97+
try:
98+
os.chdir(config_dir)
99+
init_config()
100+
yield
101+
except Exception as e:
102+
raise RuntimeError(
103+
"Failed to initialize config in " + str(config_dir) + " : " + str(e)
104+
) from e
105+
finally:
106+
os.chdir(origin_dir)
107+
108+
except (shutil.Error, OSError) as e:
109+
raise RuntimeError("Configuration management failed: " + str(e)) from e

src/pysubconverter/_core.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def get_ruleset(arguments: dict[str, str]) -> str:
6363
get the ruleset from subconverter.
6464
"""
6565

66-
def init_config(dir: str) -> None:
66+
def init_config() -> None:
6767
"""
6868
initialize the configuration directory from subconverter.
6969
"""

0 commit comments

Comments
 (0)