diff --git a/README.md b/README.md index 96dd3e8..a97d7ad 100644 --- a/README.md +++ b/README.md @@ -3,33 +3,44 @@ [![PyPI](https://img.shields.io/pypi/v/winregistry.svg)](https://pypi.python.org/pypi/winregistry) [![PyPI](https://img.shields.io/pypi/dm/winregistry.svg)](https://pypi.python.org/pypi/winregistry) -Python library aimed at working with Windows Registry +Python package aimed at working with Windows Registry ## Installation +Install via PyPI: + ```bash pip install winregistry ``` ## Usage -```py +```python import winreg + from winregistry import connect_registry _SUB_KEY_NAME = "SOFTWARE\_REMOVE_ME_" -if __name__ == "__main__": - with connect_registry(winreg.HKEY_LOCAL_MACHINE) as hklm: - hklm.create_key(_SUB_KEY_NAME) - with hklm.open_key(_SUB_KEY_NAME) as subkey: - subkey.set_value("remove_me", winreg.REG_SZ, "Don't forget remove me!") - value = subkey.read_value("remove_me") - print(f"{value=}: {value.data}") - value.data = "remove me again!" - print(f"{value=}: {value.data}") - print(f"{list(subkey.values)=}") - subkey.delete_value("remove_me") - print(f"{list(subkey.values)=}") - hklm.delete_key(_SUB_KEY_NAME) + +# connect to registry +with connect_registry(winreg.HKEY_LOCAL_MACHINE) as hklm: + + # create registry key + hklm.create_key(_SUB_KEY_NAME) + + # open registry subkey + with hklm.open_key(_SUB_KEY_NAME) as subkey: + + # set value to subkey + subkey.set_value("remove_me", winreg.REG_SZ, "Remove me!") + + # read value + value = subkey.read_value("remove_me") + + # change data of value + value.data = "Don't forget remove me!" + + # delete value in subkey + hklm.delete_key(_SUB_KEY_NAME) ``` diff --git a/winregistry.py b/winregistry.py index 9316bf2..7977c46 100644 --- a/winregistry.py +++ b/winregistry.py @@ -93,6 +93,9 @@ def name( def data( self, ) -> Any: + """ + The data of the registry value item + """ return self.raw_info.data @data.setter @@ -100,6 +103,9 @@ def data( self, value: Any, ) -> None: + """ + Stores data in the value field of an open registry key + """ winreg.SetValueEx( self._hkey, self._name, @@ -113,6 +119,9 @@ def data( def type( self, ) -> int: + """ + An integer giving the registry type for this value + """ return self.raw_info.type @@ -160,12 +169,18 @@ def from_index( def child_keys_count( self, ) -> int: + """ + Number of sub keys this key has + """ return self.raw_info.child_keys_count @property def values_count( self, ) -> int: + """ + Number of values this key has + """ return self.raw_info.values_count @property @@ -227,6 +242,9 @@ def delete_key( self, sub_key: str, ) -> None: + """ + Deletes the specified key + """ winreg.DeleteKey(self._hkey, sub_key) self._auto_refresh() @@ -234,6 +252,9 @@ def read_value( self, name: str, ) -> Value: + """ + Retrieves data for a specified value name + """ return Value( hkey=self._hkey, name=name, @@ -245,6 +266,9 @@ def set_value( type: int, value: Any, ) -> None: + """ + Associates a value with a specified key + """ winreg.SetValueEx(self._hkey, name, 0, type, value) self._auto_refresh() @@ -252,12 +276,18 @@ def delete_value( self, name: str, ) -> None: + """ + Removes a named value from a registry key + """ winreg.DeleteValue(self._hkey, name) self._auto_refresh() def close( self, ) -> None: + """ + Closes a previously opened registry key + """ self._hkey.Close() def __enter__( @@ -279,6 +309,9 @@ def connect_registry( computer_name: str | None = None, auto_refresh: bool = True, ) -> Key: + """ + Establishes a connection with registry + """ return Key( hkey=winreg.ConnectRegistry(computer_name, key), auto_refresh=auto_refresh,