Coverage for src\gibr\config.py: 94%

51 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-20 09:51 +0300

1"""Configuration handling for gibr.""" 

2 

3import logging 

4from configparser import BasicInterpolation, ConfigParser 

5from os import path 

6from pathlib import Path 

7 

8from gibr.notify import error 

9 

10 

11class EnvInterpolation(BasicInterpolation): 

12 """Expand environment variables inside .gibrconfig.""" 

13 

14 def before_get(self, parser, section, option, value, defaults): 

15 """Overload before_get method.""" 

16 value = super().before_get(parser, section, option, value, defaults) 

17 return path.expandvars(value) 

18 

19 

20class GibrConfig: 

21 """Handle loading and storing gibr configurations.""" 

22 

23 CONFIG_FILENAME = ".gibrconfig" 

24 

25 def __init__(self): 

26 """Construct GibrConfig object.""" 

27 self.config_file = None 

28 self.config = {} 

29 

30 def _find_config_file(self): 

31 """Search for config file. 

32 

33 Search the current directory and all parent directories until config file 

34 is found then return the path to the file or None if not found 

35 """ 

36 d = Path.cwd() 

37 root = Path(d.root) 

38 while d != root: 

39 logging.debug(f"Looking for .gibrconfig in {d}") 

40 attempt = d / self.CONFIG_FILENAME 

41 if attempt.exists(): 

42 logging.debug(f"Found config file: {attempt}") 

43 return attempt 

44 if d == d.parent: 

45 return None 

46 d = d.parent 

47 return None 

48 

49 def _get_tracker_details_str(self): 

50 """Get tracker details string for __str__.""" 

51 tracker_type = self.config.get("issue-tracker", {}).get("name") 

52 if tracker_type == "github": 

53 return f"""Github: 

54 Repo : {self.config.get("github", {}).get("repo")} 

55 Token : {self.config.get("github", {}).get("token")}""" 

56 elif tracker_type == "jira": 

57 return f"""Jira: 

58 URL : {self.config.get("jira", {}).get("url")} 

59 Project Key : {self.config.get("jira", {}).get("project_key")} 

60 User : {self.config.get("jira", {}).get("user")} 

61 Token : {self.config.get("jira", {}).get("token")}""" 

62 else: 

63 return "" 

64 

65 def __str__(self): 

66 """Stringify.""" 

67 return f"""Gibr Configuration: 

68 Default: 

69 Branch Name Format : {self.config.get("DEFAULT", {}).get("branch_name_format")} 

70 Issue Tracker: 

71 Name : {self.config.get("issue-tracker", {}).get("name")} 

72 {self._get_tracker_details_str()}""" 

73 

74 def load(self): 

75 """Load .gibrconfig into a simple dictionary.""" 

76 config_file = self._find_config_file() 

77 if not config_file: 

78 error(f"{self.CONFIG_FILENAME} not found in this or any parent directory") 

79 

80 parser = ConfigParser(interpolation=EnvInterpolation()) 

81 parser.read(config_file) 

82 self.config_file = config_file 

83 

84 config = {} 

85 for section in parser.sections(): 

86 config[section] = dict(parser.items(section)) 

87 # DEFAULT section is special 

88 if parser.defaults(): 

89 config["DEFAULT"] = dict(parser.defaults()) 

90 

91 self.config = config 

92 logging.debug(str(self)) 

93 return self