Skip to content

Commit d460ab9

Browse files
committed
update cache storing
1 parent ab27c32 commit d460ab9

File tree

2 files changed

+23
-35
lines changed

2 files changed

+23
-35
lines changed

src/wechaty/config.py

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@
2929
get_logger
3030
)
3131

32-
from wechaty.exceptions import WechatyConfigurationError
3332

3433
log = get_logger('Config')
3534

3635
# log.debug('test logging debug')
3736
# log.info('test logging info')
3837

3938

39+
# TODO(wj-Mcat): there is no reference usage, so need to be removed
4040
_FILE_PATH = os.path.dirname(os.path.realpath(__file__))
4141
DATA_PATH = os.path.realpath(
4242
os.path.join(
@@ -90,40 +90,23 @@ def valid_api_host(api_host: str) -> bool:
9090

9191
class Config:
9292
"""
93-
store python-wechaty configuration
93+
get the configuration from the environment variables
9494
"""
95-
# pylint: disable=R0913
96-
def __init__(self,
97-
api_host: Optional[str] = None,
98-
token: Optional[str] = None,
99-
protocol: Optional[str] = None,
100-
http_port: Optional[int] = None,
101-
name: str = 'python-wechaty',
102-
debug: bool = True,
103-
docker: bool = False):
104-
"""
105-
initialize the configuration
106-
"""
107-
self.default = DefaultSetting
108-
109-
self.api_host = api_host if api_host is not None \
110-
else DefaultSetting.default_api_host
111-
112-
self.http_port = http_port if http_port is not None \
113-
else DefaultSetting.default_port
114-
115-
self.protocol = protocol if protocol is not None \
116-
else DefaultSetting.default_protocol
95+
def __init__(self) -> None:
96+
self._cache_dir: Optional[str] = None
11797

118-
if token is None:
119-
raise WechatyConfigurationError('token can"t be None')
98+
@property
99+
def cache_dir(self) -> str:
100+
"""get the cache dir in the lazy loading mode
120101
121-
self.name = name
122-
self.debug = debug
123-
self.docker = docker
124-
125-
if self.api_host is not None and not valid_api_host(self.api_host):
126-
raise WechatyConfigurationError(f'api host %s is not valid {self.api_host}')
102+
Returns:
103+
str: the path of cache dir
104+
"""
105+
if self._cache_dir is not None:
106+
return self._cache_dir
107+
self._cache_dir = os.environ.get("CACHE_DIR", '.wechaty')
108+
assert self._cache_dir is not None
109+
return self._cache_dir
127110

128111

129112
# export const CHATIE_OFFICIAL_ACCOUNT_ID = 'gh_051c89260e5d'
@@ -141,3 +124,6 @@ def qr_code_for_chatie() -> FileBox:
141124
chatie_official_account_qr_code: str = \
142125
'http://weixin.qq.com/r/qymXj7DEO_1ErfTs93y5'
143126
return FileBox.from_qr_code(chatie_official_account_qr_code)
127+
128+
129+
config = Config()

src/wechaty/plugin.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
ScanStatus
6363
)
6464

65+
from .config import config
66+
6567
from .exceptions import (
6668
WechatyPluginError,
6769
)
@@ -182,7 +184,7 @@ class WechatyPluginOptions:
182184
@dataclass
183185
class WechatySchedulerOptions:
184186
"""options for wechaty scheduler"""
185-
job_store: Union[str, SQLAlchemyJobStore] = 'sqlite:///.wechaty/job.db'
187+
job_store: Union[str, SQLAlchemyJobStore] = f'sqlite:///{config.cache_dir}/job.db'
186188
job_store_alias: str = 'wechaty-scheduler'
187189

188190

@@ -198,7 +200,7 @@ class WechatySchedulerMixin:
198200
_scheduler_field: str = "_scheduler"
199201

200202
scheduler_job_alias: str = 'wechaty_scheduler'
201-
scheduler_db_file: str = '.wechaty/job.db'
203+
scheduler_db_file: str = f'{config.cache_dir}/job.db'
202204

203205
@property
204206
def scheduler(self) -> AsyncIOScheduler:
@@ -429,7 +431,7 @@ def cache_dir(self) -> str:
429431
430432
this is friendly for code typing
431433
"""
432-
_cache_dir = os.path.join('.wechaty', self.name)
434+
_cache_dir = os.path.join(config.cache_dir, self.name)
433435
os.makedirs(_cache_dir, exist_ok=True)
434436
return _cache_dir
435437

0 commit comments

Comments
 (0)