core: add tmp_dir for global access to a tmp dir (#173)

* core: add tmp_dir for global access to a tmp dir
This commit is contained in:
Sean Breckenridge 2021-05-16 16:28:26 -07:00 committed by GitHub
parent b64a11cc69
commit e8be20dcb5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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 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 enabled_modules : Optional[Sequence[str]] = None
''' '''
list of regexes/globs list of regexes/globs
@ -62,7 +70,20 @@ class Config(user_config):
from .cachew import _appdirs_cache_dir from .cachew import _appdirs_cache_dir
return _appdirs_cache_dir() return _appdirs_cache_dir()
else: 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]: def _is_module_active(self, module: str) -> Optional[bool]:
# None means the config doesn't specify anything # None means the config doesn't specify anything