From e8be20dcb51c2cb75465be2f30eb5bae632b7fba Mon Sep 17 00:00:00 2001 From: Sean Breckenridge Date: Sun, 16 May 2021 16:28:26 -0700 Subject: [PATCH] core: add tmp_dir for global access to a tmp dir (#173) * core: add tmp_dir for global access to a tmp dir --- my/core/core_config.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/my/core/core_config.py b/my/core/core_config.py index a883db4..cc8b527 100644 --- a/my/core/core_config.py +++ b/my/core/core_config.py @@ -42,6 +42,14 @@ class Config(user_config): NOTE: you shouldn't use this attribute in HPI modules directly, use Config.get_cache_dir()/cachew.cache_dir() instead ''' + tmp_dir: Optional[PathIsh] = None + ''' + Path to a temporary directory. + This can be used temporarily while extracting zipfiles etc... + - if None , uses default determined by tempfile.gettempdir + 'HPI' + - otherwise , use the specified directory as the base temporary directory + ''' + enabled_modules : Optional[Sequence[str]] = None ''' list of regexes/globs @@ -62,7 +70,20 @@ class Config(user_config): from .cachew import _appdirs_cache_dir return _appdirs_cache_dir() else: - return Path(cdir) + return Path(cdir).expanduser() + + def get_tmp_dir(self) -> Path: + tdir: Optional[PathIsh] = self.tmp_dir + tpath: Path + # use tempfile if unset + if tdir is None: + import tempfile + tpath = Path(tempfile.gettempdir()) / 'HPI' + else: + tpath = Path(tdir) + tpath = tpath.expanduser() + tpath.mkdir(parents=True, exist_ok=True) + return tpath def _is_module_active(self, module: str) -> Optional[bool]: # None means the config doesn't specify anything