Coverage for scripts/merge_tags.py: 0%

49 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-07-22 06:43 -0400

1import json 

2import subprocess 

3import difflib 

4from colorama import Fore, Style 

5 

6 

7def str_list_to_dict(str_list: list[str]) -> dict[str, str]: 

8 """ 

9 Convert a list of strings in the format "key=value" into a dictionary. 

10 

11 Args: 

12 str_list (list): A list of strings in the format "key=value". 

13 

14 Returns: 

15 dict: A dictionary with the keys and values extracted from the input list. 

16 """ 

17 result = {} 

18 for item in str_list: 

19 key, value = item.split("=") 

20 result[key] = value 

21 return result 

22 

23 

24with open("scripts/tags.json", "r+", encoding="utf-8") as f: 

25 tags = json.load(f) 

26 

27with open("scripts/tags_ext.json", "r+", encoding="utf-8") as f: 

28 other = json.load(f) 

29 

30new = {key: str_list_to_dict(value.get("osm_tags", [])) for key, value in other.items()} 

31 

32 

33def ask_add(key: str, val: str, match: dict[str, str]): 

34 user_input = input("Enter '/' to add: ") 

35 if user_input == "/": 

36 tags[key] = match 

37 print("Added ✅") 

38 else: 

39 print("Skipped ❌") 

40 

41 

42def diff(before: str, after: str) -> str: 

43 seqm = difflib.SequenceMatcher(None, before, after) 

44 

45 output = [] 

46 for opcode, a0, a1, b0, b1 in seqm.get_opcodes(): 

47 if opcode == "equal": 

48 output.append(before[a0:a1]) 

49 if opcode in ["delete", "replace"]: 

50 output.append(f"{Fore.RED}{before[a0:a1]}{Style.RESET_ALL}") 

51 if opcode in ["insert", "replace"]: 

52 output.append(f"{Fore.GREEN}{after[b0:b1]}{Style.RESET_ALL}") 

53 return "".join(output) 

54 

55 

56try: 

57 for key, val in tags.items(): 

58 try: 

59 match = new.get(key) 

60 key_print = f"{Fore.BLUE}{key}{Style.RESET_ALL}" 

61 if match and val and match != val: 

62 print("mismatch:", key_print, diff(str(val), str(match))) 

63 ask_add(key, val, match) 

64 if match and not val: 

65 print("unadded:", key_print, match) 

66 ask_add(key, val, match) 

67 except KeyError: 

68 pass 

69 

70except KeyboardInterrupt: 

71 with open("scripts/tags.json", "r+", encoding="utf-8") as f: 

72 f.write(json.dumps(tags, indent=4)) 

73 subprocess.run(["prettier", "scripts/tags.json", "-w"])