Coverage for graphqler / utils / config_handler.py: 72%

25 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-03-20 10:09 -0400

1from graphqler import config 

2from graphqler.utils.file_utils import get_graphqler_root 

3import tomllib 

4import os 

5 

6 

7def parse_config(config_obj: str | dict) -> dict: 

8 """ 

9 Parse the config, and return the dictionary. 

10 If it's a dictionary, just parses the config object directly 

11 Otherwise parses it out of a TOML file 

12 """ 

13 config = {} 

14 if isinstance(config_obj, dict): 

15 config = config_obj 

16 elif isinstance(config_obj, str): 

17 with open(config_obj, "rb") as f: 

18 config = tomllib.load(f) 

19 

20 if len(config.keys()) == 0: 

21 print(f"(!) No items in config {config_obj}") 

22 exit(1) 

23 

24 return config 

25 

26 

27def set_config(new_config: dict): 

28 """Sets config to the new file using reflection on the constants module 

29 

30 Args: 

31 new_config (dict): The configuration dictionary 

32 """ 

33 for k, v in new_config.items(): 

34 if hasattr(config, k): 

35 setattr(config, k, v) 

36 else: 

37 print(f"(!) Unknown configuration {k}, skipping it") 

38 

39 

40def generate_new_config(config_file_to_write: str) -> None: 

41 """Generates the new config file by copying from static/config.toml 

42 

43 Args: 

44 config_file_to_write (str): The config file to write 

45 """ 

46 # copy from the base path of the graphqler package 

47 project_root = get_graphqler_root() 

48 os.system(f"cp {project_root}/examples/config.toml {config_file_to_write}") 

49 

50 

51def does_config_file_exist_in_path(path: str) -> bool: 

52 """Checks if the config file exists in the path 

53 

54 Args: 

55 path (str): The path to check 

56 

57 Returns: 

58 bool: Whether the config file exists 

59 """ 

60 return os.path.exists(f"{path}/{config.CONFIG_FILE_NAME}")