Class that encapsulates a registry (path) as a configuration store. section = registry key option = registry valuename value = registry value Storage model = dictionary
| 5 | import os.path as path |
| 6 | |
| 7 | class RegConfig: |
| 8 | """ Class that encapsulates a registry (path) as a configuration store. |
| 9 | |
| 10 | section = registry key |
| 11 | option = registry valuename |
| 12 | value = registry value |
| 13 | |
| 14 | Storage model = dictionary |
| 15 | |
| 16 | """ |
| 17 | |
| 18 | def __init__(self, reg_path=None, autowrite=False): |
| 19 | |
| 20 | self._regpath = reg_path |
| 21 | self._autowrite = autowrite |
| 22 | self._store = {} |
| 23 | |
| 24 | # Autowrite is available only if a registry path is provided. |
| 25 | if not self._regpath: self._autowrite = False |
| 26 | |
| 27 | # read the registry and store values if path given. |
| 28 | if self._regpath: self.read(self._regpath) |
| 29 | |
| 30 | |
| 31 | def add_section(self, section): |
| 32 | """ Add a section named section to the instance. If a section by the |
| 33 | given name already exists, ValueError is raised. """ |
| 34 | if not self.has_section(section): |
| 35 | self._store[section] = {} |
| 36 | else: |
| 37 | raise ValueError("Section already exists") |
| 38 | |
| 39 | def get(self, section, option): |
| 40 | """ Get an option value for the named section. """ |
| 41 | if self.has_option(section, option): |
| 42 | return self._store[section][option][0] |
| 43 | |
| 44 | |
| 45 | def _get_default_regkey(self, regpath=None, forwriting=False): |
| 46 | """ Get the registry key handle for registry operations. provided the |
| 47 | registry path. """ |
| 48 | if regpath: |
| 49 | key = regpath |
| 50 | subkey = '' |
| 51 | while path.split(key)[0]: |
| 52 | key, tmp = path.split(key) |
| 53 | subkey = '\\'.join([tmp, subkey]) |
| 54 | if key == 'HKEY_CLASSES_ROOT': |
| 55 | key = _winreg.HKEY_CLASSES_ROOT |
| 56 | elif key == 'HKEY_CURRENT_CONFIG': |
| 57 | key = _winreg.HKEY_CURRENT_CONFIG |
| 58 | elif key == 'HKEY_CURRENT_USER': |
| 59 | key = _winreg.HKEY_CURRENT_USER |
| 60 | elif key == 'HKEY_DYN_DATA': |
| 61 | key = _winreg.HKEY_DYN_DATA |
| 62 | elif key == 'HKEY_LOCAL_MACHINE': |
| 63 | key = _winreg.HKEY_LOCAL_MACHINE |
| 64 | elif key == 'HKEY_PERFORMANCE_DATA': |