gpkgstatus.utils.config
A Config module that creates, reads and checks ~/.gpkgconfig JSON config file.
1"""A Config module that creates, reads and checks ~/.gpkgconfig JSON \ 2 config file. 3""" 4 5import logging 6import sys 7 8from pathlib import Path 9 10import json 11import pyinputplus as pyinp 12 13from termcolor import colored 14 15 16class Config: 17 """A Config class that contains information about fields \ 18 or keys of the config file. 19 20 It contains getters for `cache_time` and `verbose`. In 21 case if `cache_time` or `verbose` is not determined, the default 22 values (`52 min`) and (`False`) are set respectively. 23 """ 24 25 _cache_time: int 26 _path: Path 27 _verbose: bool 28 29 # _cache_time = 52 min (default) 30 # _verbose = False (default) 31 def __init__(self, _cache_time: int = 52 * 60, _verbose: bool = False): 32 self._cache_time = _cache_time 33 logging.info("Set Cache Time: %d min", _cache_time // 60) 34 35 self._path = Path.expanduser(Path("~/.gpkgconfig")) 36 self._verbose = _verbose 37 38 def user_input(self): 39 """Sets the fields based on user input. 40 41 Using `PyInputPlus` library for safe input, the input for 42 `cache_time` and `verbose` are set accordingly. 43 """ 44 45 print(colored("Creating a new config file...", "green")) 46 47 _cache_time = pyinp.inputNum( 48 colored("Cache Time (min/[default]): ", "yellow"), blank=True 49 ) 50 51 if _cache_time: 52 self._cache_time = _cache_time 53 else: 54 logging.info("Default Cache Time: %d min", self._cache_time // 60) 55 56 _verbose = pyinp.inputYesNo( 57 colored("Do you want verbose info (y/n[default]): ", "yellow"), blank=True 58 ) 59 if _verbose: 60 self._verbose = _verbose == "yes" 61 else: 62 logging.info("Default Verbose Value: %r", self._verbose) 63 64 def _check(self) -> bool: 65 """Checks whether config file exists, and also checks if \ 66 it is a valid JSON file. 67 68 In case if file doesn't exist, it automatically creates 69 a config file based on user input. 70 71 Returns: 72 bool: Whether config file exists or not 73 """ 74 _path = self._path 75 if not (_path.exists() and _path.is_file): 76 print(colored("Config file doesn't exist.", "light_red")) 77 self.user_input() 78 self.create() 79 80 try: 81 with open(_path, encoding="utf-8") as file: 82 json.load(file) 83 except json.JSONDecodeError: 84 print(colored("File is not a valid JSON file.", "light_red")) 85 return False 86 87 logging.info("Config file exists") 88 return True 89 90 def create(self): 91 """Creates the JSON config (~/.gpkgconfig) file. 92 93 Based on the given fields, the JSON config file is created. 94 If user home directory is permissive, the method will ask to 95 check home directory permissions. 96 """ 97 98 config = {"cache_time": self._cache_time, "verbose": self._verbose} 99 try: 100 with open(self._path, "w", encoding="utf-8") as file: 101 json.dump(config, file, skipkeys=True, indent=4) 102 103 logging.info("Config File written to %s", str(self._path)) 104 105 except PermissionError: 106 print( 107 colored( 108 "Error: Permission denied. Please check home directory permissions.", 109 "red", 110 ) 111 ) 112 sys.exit(1) 113 114 def read(self): 115 """Reads the JSON config file 116 117 It also checks if (`cache_time` and `verbose`) are keys in 118 JSON file. In case if one of them doesn't exist, it states 119 that file is not a valid config file and asks the user 120 for overwriting the file. 121 """ 122 123 with open(self._path, encoding="utf-8") as file: 124 data = json.load(file) 125 126 if all(key not in data.keys() for key in ("cache_time", "verbose")): 127 print(colored("File is not a valid Config file.", "light_red")) 128 self.overwrite() 129 logging.info("File overwritten.") 130 131 self._cache_time = data["cache_time"] 132 self._verbose = data["verbose"] 133 134 def overwrite(self): 135 """Asks the user for overwriting config file.""" 136 137 _overwrite = pyinp.inputYesNo( 138 colored("Do you want to overwrite the file (y/n[default]): ", "yellow"), 139 blank=True, 140 ) 141 if not _overwrite or _overwrite == "no": 142 sys.exit(1) 143 else: 144 self.user_input() 145 self.create() 146 logging.info("File overwritten.") 147 148 def set_info(self): 149 """The main program that checks the file using `_check()` 150 function. 151 152 If output of checking is False, it starts to overwrite 153 the invalid file, or else, it reads the file. 154 """ 155 156 if not self._check(): 157 self.overwrite() 158 159 self.read() 160 161 def get_cache_time(self) -> int: 162 """Getter for field `_cache_time` 163 164 Returns: 165 int: Value of `_cache_time` 166 """ 167 168 return self._cache_time 169 170 def get_verbose_status(self) -> bool: 171 """Getter for field `_verbose` 172 173 Returns: 174 bool: Value of `_verbose` 175 """ 176 return self._verbose
17class Config: 18 """A Config class that contains information about fields \ 19 or keys of the config file. 20 21 It contains getters for `cache_time` and `verbose`. In 22 case if `cache_time` or `verbose` is not determined, the default 23 values (`52 min`) and (`False`) are set respectively. 24 """ 25 26 _cache_time: int 27 _path: Path 28 _verbose: bool 29 30 # _cache_time = 52 min (default) 31 # _verbose = False (default) 32 def __init__(self, _cache_time: int = 52 * 60, _verbose: bool = False): 33 self._cache_time = _cache_time 34 logging.info("Set Cache Time: %d min", _cache_time // 60) 35 36 self._path = Path.expanduser(Path("~/.gpkgconfig")) 37 self._verbose = _verbose 38 39 def user_input(self): 40 """Sets the fields based on user input. 41 42 Using `PyInputPlus` library for safe input, the input for 43 `cache_time` and `verbose` are set accordingly. 44 """ 45 46 print(colored("Creating a new config file...", "green")) 47 48 _cache_time = pyinp.inputNum( 49 colored("Cache Time (min/[default]): ", "yellow"), blank=True 50 ) 51 52 if _cache_time: 53 self._cache_time = _cache_time 54 else: 55 logging.info("Default Cache Time: %d min", self._cache_time // 60) 56 57 _verbose = pyinp.inputYesNo( 58 colored("Do you want verbose info (y/n[default]): ", "yellow"), blank=True 59 ) 60 if _verbose: 61 self._verbose = _verbose == "yes" 62 else: 63 logging.info("Default Verbose Value: %r", self._verbose) 64 65 def _check(self) -> bool: 66 """Checks whether config file exists, and also checks if \ 67 it is a valid JSON file. 68 69 In case if file doesn't exist, it automatically creates 70 a config file based on user input. 71 72 Returns: 73 bool: Whether config file exists or not 74 """ 75 _path = self._path 76 if not (_path.exists() and _path.is_file): 77 print(colored("Config file doesn't exist.", "light_red")) 78 self.user_input() 79 self.create() 80 81 try: 82 with open(_path, encoding="utf-8") as file: 83 json.load(file) 84 except json.JSONDecodeError: 85 print(colored("File is not a valid JSON file.", "light_red")) 86 return False 87 88 logging.info("Config file exists") 89 return True 90 91 def create(self): 92 """Creates the JSON config (~/.gpkgconfig) file. 93 94 Based on the given fields, the JSON config file is created. 95 If user home directory is permissive, the method will ask to 96 check home directory permissions. 97 """ 98 99 config = {"cache_time": self._cache_time, "verbose": self._verbose} 100 try: 101 with open(self._path, "w", encoding="utf-8") as file: 102 json.dump(config, file, skipkeys=True, indent=4) 103 104 logging.info("Config File written to %s", str(self._path)) 105 106 except PermissionError: 107 print( 108 colored( 109 "Error: Permission denied. Please check home directory permissions.", 110 "red", 111 ) 112 ) 113 sys.exit(1) 114 115 def read(self): 116 """Reads the JSON config file 117 118 It also checks if (`cache_time` and `verbose`) are keys in 119 JSON file. In case if one of them doesn't exist, it states 120 that file is not a valid config file and asks the user 121 for overwriting the file. 122 """ 123 124 with open(self._path, encoding="utf-8") as file: 125 data = json.load(file) 126 127 if all(key not in data.keys() for key in ("cache_time", "verbose")): 128 print(colored("File is not a valid Config file.", "light_red")) 129 self.overwrite() 130 logging.info("File overwritten.") 131 132 self._cache_time = data["cache_time"] 133 self._verbose = data["verbose"] 134 135 def overwrite(self): 136 """Asks the user for overwriting config file.""" 137 138 _overwrite = pyinp.inputYesNo( 139 colored("Do you want to overwrite the file (y/n[default]): ", "yellow"), 140 blank=True, 141 ) 142 if not _overwrite or _overwrite == "no": 143 sys.exit(1) 144 else: 145 self.user_input() 146 self.create() 147 logging.info("File overwritten.") 148 149 def set_info(self): 150 """The main program that checks the file using `_check()` 151 function. 152 153 If output of checking is False, it starts to overwrite 154 the invalid file, or else, it reads the file. 155 """ 156 157 if not self._check(): 158 self.overwrite() 159 160 self.read() 161 162 def get_cache_time(self) -> int: 163 """Getter for field `_cache_time` 164 165 Returns: 166 int: Value of `_cache_time` 167 """ 168 169 return self._cache_time 170 171 def get_verbose_status(self) -> bool: 172 """Getter for field `_verbose` 173 174 Returns: 175 bool: Value of `_verbose` 176 """ 177 return self._verbose
A Config class that contains information about fields or keys of the config file.
It contains getters for cache_time
and verbose
. In
case if cache_time
or verbose
is not determined, the default
values (52 min
) and (False
) are set respectively.
39 def user_input(self): 40 """Sets the fields based on user input. 41 42 Using `PyInputPlus` library for safe input, the input for 43 `cache_time` and `verbose` are set accordingly. 44 """ 45 46 print(colored("Creating a new config file...", "green")) 47 48 _cache_time = pyinp.inputNum( 49 colored("Cache Time (min/[default]): ", "yellow"), blank=True 50 ) 51 52 if _cache_time: 53 self._cache_time = _cache_time 54 else: 55 logging.info("Default Cache Time: %d min", self._cache_time // 60) 56 57 _verbose = pyinp.inputYesNo( 58 colored("Do you want verbose info (y/n[default]): ", "yellow"), blank=True 59 ) 60 if _verbose: 61 self._verbose = _verbose == "yes" 62 else: 63 logging.info("Default Verbose Value: %r", self._verbose)
Sets the fields based on user input.
Using PyInputPlus
library for safe input, the input for
cache_time
and verbose
are set accordingly.
91 def create(self): 92 """Creates the JSON config (~/.gpkgconfig) file. 93 94 Based on the given fields, the JSON config file is created. 95 If user home directory is permissive, the method will ask to 96 check home directory permissions. 97 """ 98 99 config = {"cache_time": self._cache_time, "verbose": self._verbose} 100 try: 101 with open(self._path, "w", encoding="utf-8") as file: 102 json.dump(config, file, skipkeys=True, indent=4) 103 104 logging.info("Config File written to %s", str(self._path)) 105 106 except PermissionError: 107 print( 108 colored( 109 "Error: Permission denied. Please check home directory permissions.", 110 "red", 111 ) 112 ) 113 sys.exit(1)
Creates the JSON config (~/.gpkgconfig) file.
Based on the given fields, the JSON config file is created. If user home directory is permissive, the method will ask to check home directory permissions.
115 def read(self): 116 """Reads the JSON config file 117 118 It also checks if (`cache_time` and `verbose`) are keys in 119 JSON file. In case if one of them doesn't exist, it states 120 that file is not a valid config file and asks the user 121 for overwriting the file. 122 """ 123 124 with open(self._path, encoding="utf-8") as file: 125 data = json.load(file) 126 127 if all(key not in data.keys() for key in ("cache_time", "verbose")): 128 print(colored("File is not a valid Config file.", "light_red")) 129 self.overwrite() 130 logging.info("File overwritten.") 131 132 self._cache_time = data["cache_time"] 133 self._verbose = data["verbose"]
Reads the JSON config file
It also checks if (cache_time
and verbose
) are keys in
JSON file. In case if one of them doesn't exist, it states
that file is not a valid config file and asks the user
for overwriting the file.
135 def overwrite(self): 136 """Asks the user for overwriting config file.""" 137 138 _overwrite = pyinp.inputYesNo( 139 colored("Do you want to overwrite the file (y/n[default]): ", "yellow"), 140 blank=True, 141 ) 142 if not _overwrite or _overwrite == "no": 143 sys.exit(1) 144 else: 145 self.user_input() 146 self.create() 147 logging.info("File overwritten.")
Asks the user for overwriting config file.
149 def set_info(self): 150 """The main program that checks the file using `_check()` 151 function. 152 153 If output of checking is False, it starts to overwrite 154 the invalid file, or else, it reads the file. 155 """ 156 157 if not self._check(): 158 self.overwrite() 159 160 self.read()
The main program that checks the file using _check()
function.
If output of checking is False, it starts to overwrite the invalid file, or else, it reads the file.