Coverage for r11k/config.py: 91%

27 statements  

« prev     ^ index     » next       coverage.py v7.2.1, created at 2023-03-12 19:57 +0100

1""" 

2R11K configuration and global "singletons". 

3 

4Explicitly designed to be easy to substitute for another configuration 

5in tests. 

6""" 

7 

8import os.path 

9from typing import ( 

10 Optional 

11) 

12 

13from xdg.BaseDirectory import xdg_cache_home 

14 

15from r11k import util 

16from r11k.httpcache import HTTPCache 

17 

18 

19class Config: 

20 """ 

21 Global configuration manager. 

22 

23 :param cache_root: Where all cache directories should end up, 

24 Defaults to `$XDG_CACHE_HOME/r11k` 

25 :param clone_base: Where cloned repos store (non-deployed repos) 

26 should be stored, Defaults to `$cache_root/repos`. 

27 :param http_cache: Where misc. stuff downloaded ower HTTP should be 

28 placed. Defaults to `$cache_root/http.` 

29 :param tar_cache: Where downloaded tarballs should be kept. 

30 Defaults to `$cache_root/tar` 

31 :param http_ttl: How long until we retry downloading stuff over HTTP 

32 :param git_ttl: How long until we re-fetch git-repos. 

33 :param api_base: Base for all Puppet Forge calls. Defaults to 

34 `https://forge.puppet.com` 

35 """ 

36 

37 def __init__(self, 

38 cache_root: str = os.path.join(xdg_cache_home, 'r11k'), 

39 clone_base: Optional[str] = None, 

40 http_cache: Optional[str] = None, 

41 tar_cache: Optional[str] = None, 

42 http_ttl: int = 3600, 

43 git_ttl: int = 3600, 

44 api_base: str = 'https://forge.puppet.com'): 

45 

46 self.cache_root = cache_root 

47 self.clone_base: str = clone_base or os.path.join(cache_root, 'repos') 

48 self.http_cache: str = http_cache or os.path.join(cache_root, 'http') 

49 self.tar_cache: str = tar_cache or os.path.join(cache_root, 'tar') 

50 self._http_ttl = http_ttl 

51 self.git_ttl = git_ttl 

52 self.api_base = api_base 

53 

54 cache_dirs = [ 

55 self.cache_root, 

56 self.clone_base, 

57 self.http_cache, 

58 self.tar_cache, 

59 ] 

60 for dir in cache_dirs: 

61 util.ensure_directory(dir) 

62 

63 self.httpcache: HTTPCache = HTTPCache(self.http_cache, default_ttl=self._http_ttl) 

64 

65 @property 

66 def http_ttl(self) -> int: 

67 """Return http_ttl field.""" 

68 return self._http_ttl 

69 

70 @http_ttl.setter 

71 def http_ttl(self, value: int) -> None: 

72 """Save a new TTL value, and forwards it to `self.httpcache.default_ttl`.""" 

73 self._http_ttl = value 

74 self.httpcache.default_ttl = value 

75 

76 

77config = Config() 

78"""Default configuration instance when none has been defined."""