| 1050 | |
| 1051 | |
| 1052 | class MockWinreg: |
| 1053 | HKEY_LOCAL_MACHINE = "HKLM" |
| 1054 | HKEY_CURRENT_USER = "HKCU" |
| 1055 | |
| 1056 | def __init__(self, keys): |
| 1057 | self.keys = {k.casefold(): v for k, v in keys.items()} |
| 1058 | self.open = {} |
| 1059 | |
| 1060 | def __repr__(self): |
| 1061 | return "<MockWinreg>" |
| 1062 | |
| 1063 | def __eq__(self, other): |
| 1064 | return isinstance(other, type(self)) |
| 1065 | |
| 1066 | def open_keys(self): |
| 1067 | return list(self.open) |
| 1068 | |
| 1069 | def OpenKeyEx(self, hkey, subkey): |
| 1070 | if verbose: |
| 1071 | print(f"OpenKeyEx({hkey}, {subkey})") |
| 1072 | key = f"{hkey}\\{subkey}".casefold() |
| 1073 | if key in self.keys: |
| 1074 | self.open[key] = self.open.get(key, 0) + 1 |
| 1075 | return key |
| 1076 | raise FileNotFoundError() |
| 1077 | |
| 1078 | def CloseKey(self, hkey): |
| 1079 | if verbose: |
| 1080 | print(f"CloseKey({hkey})") |
| 1081 | hkey = hkey.casefold() |
| 1082 | if hkey not in self.open: |
| 1083 | raise RuntimeError("key is not open") |
| 1084 | self.open[hkey] -= 1 |
| 1085 | if not self.open[hkey]: |
| 1086 | del self.open[hkey] |
| 1087 | |
| 1088 | def EnumKey(self, hkey, i): |
| 1089 | if verbose: |
| 1090 | print(f"EnumKey({hkey}, {i})") |
| 1091 | hkey = hkey.casefold() |
| 1092 | if hkey not in self.open: |
| 1093 | raise RuntimeError("key is not open") |
| 1094 | prefix = f'{hkey}\\' |
| 1095 | subkeys = [k[len(prefix):] for k in sorted(self.keys) if k.startswith(prefix)] |
| 1096 | subkeys[:] = [k for k in subkeys if '\\' not in k] |
| 1097 | for j, n in enumerate(subkeys): |
| 1098 | if j == i: |
| 1099 | return n.removeprefix(prefix) |
| 1100 | raise OSError("end of enumeration") |
| 1101 | |
| 1102 | def QueryValue(self, hkey, subkey): |
| 1103 | if verbose: |
| 1104 | print(f"QueryValue({hkey}, {subkey})") |
| 1105 | hkey = hkey.casefold() |
| 1106 | if hkey not in self.open: |
| 1107 | raise RuntimeError("key is not open") |
| 1108 | if subkey: |
| 1109 | subkey = subkey.casefold() |
no outgoing calls
searching dependent graphs…