Skip to content

Commit

Permalink
Merge pull request #13 from bitranox/development
Browse files Browse the repository at this point in the history
v2.0.7
  • Loading branch information
bitranox authored Oct 10, 2020
2 parents 848289a + 3cb1971 commit cffcdd6
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .docs/README_template.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ lib_registry
============


Version v2.0.6 as of 2020-10-09 see `Changelog`_
Version v2.0.7 as of 2020-10-10 see `Changelog`_


.. include:: ./badges.rst
Expand Down
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ tasks:
- pathlib-like Interface
- jupyter notebook update

v2.0.7
--------
2020-10-10: fix minor bugs

v2.0.6
--------
2020-10-09: service release
Expand Down
6 changes: 5 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ lib_registry
============


Version v2.0.6 as of 2020-10-09 see `Changelog`_
Version v2.0.7 as of 2020-10-10 see `Changelog`_

|travis_build| |license| |jupyter| |pypi|

Expand Down Expand Up @@ -437,6 +437,10 @@ tasks:
- pathlib-like Interface
- jupyter notebook update

v2.0.7
--------
2020-10-10: fix minor bugs

v2.0.6
--------
2020-10-09: service release
Expand Down
4 changes: 2 additions & 2 deletions lib_registry/__init__conf__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = 'lib_registry'
title = 'a more pythonic way to access the windows registry as winreg'
version = 'v2.0.6'
version = 'v2.0.7'
url = 'https://github.com/bitranox/lib_registry'
author = 'Robert Nowotny'
author_email = 'bitranox@gmail.com'
Expand All @@ -14,7 +14,7 @@ def print_info() -> None:
a more pythonic way to access the windows registry as winreg
Version : v2.0.6
Version : v2.0.7
Url : https://github.com/bitranox/lib_registry
Author : Robert Nowotny
Email : bitranox@gmail.com""")
47 changes: 27 additions & 20 deletions lib_registry/lib_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
else:
import fake_winreg as winreg # type: ignore
# an empty Registry at the Moment
fake_registry = winreg.fake_reg_tools.get_minimal_windows_testregistry()
winreg.load_fake_registry(fake_registry)
fake_registry = winreg.fake_reg_tools.get_minimal_windows_testregistry() # type: ignore
winreg.load_fake_registry(fake_registry) # type: ignore


main_key_hashed_by_name: Dict[str, int] = \
Expand Down Expand Up @@ -311,17 +311,24 @@ def _reg_connect(self, key: Union[str, int], computer_name: Optional[str] = None
self.reg_hive_connections[hive_key] = hive_handle
self._is_computer_name_set = True
return hive_handle

except FileNotFoundError as e_fnf:
if hasattr(e_fnf, 'winerror') and e_fnf.winerror == 53: # type: ignore
raise RegistryNetworkConnectionError('The network path to "{}" was not found'.format(computer_name))
raise RegistryNetworkConnectionError(f'The network path to "{computer_name}" was not found')
else:
raise RegistryConnectionError('unknown error connecting to registry')

except OSError as e_os:
if hasattr(e_os, 'winerror'):
if e_os.winerror == 1707: # type: ignore
# OSError: [WinError 1707] The network address is invalid
raise RegistryNetworkConnectionError('The network address "{}" is invalid'.format(computer_name))
raise RegistryNetworkConnectionError(f'The network address "{computer_name}" is invalid')
elif e_os.winerror == 6: # type: ignore
raise RegistryHKeyError('invalid KEY: "{}"'.format(key))
raise RegistryHKeyError(f'invalid KEY: "{key}"')
else:
raise RegistryConnectionError('unknown error connecting to registry')

assert False

def _open_key(self, key: Union[str, int], sub_key: str = '', access: int = winreg.KEY_READ) -> winreg.HKEYType:
"""
Expand Down Expand Up @@ -367,7 +374,7 @@ def _open_key(self, key: Union[str, int], sub_key: str = '', access: int = winre
self.reg_key_handles[(hive_key, hive_sub_key, access)] = key_handle
except FileNotFoundError:
key_str = get_key_as_string(key, sub_key)
raise RegistryKeyNotFoundError('registry key "{}" not found'.format(key_str))
raise RegistryKeyNotFoundError(f'registry key "{key_str}" not found')
return key_handle

# create_key{{{
Expand Down Expand Up @@ -435,7 +442,7 @@ def create_key(self, key: Union[str, int], sub_key: str = '', exist_ok: bool = T
_key_exists = self.key_exist(hive_key, hive_sub_key)
if (not exist_ok) and _key_exists:
key_string = get_key_as_string(hive_key, hive_sub_key)
raise RegistryKeyCreateError('can not create key, it already exists: {key_string}'.format(key_string=key_string))
raise RegistryKeyCreateError(f'can not create key, it already exists: {key_string}')
elif _key_exists:
key_handle = self._open_key(hive_key, hive_sub_key, winreg.KEY_ALL_ACCESS)
return key_handle
Expand All @@ -444,7 +451,7 @@ def create_key(self, key: Union[str, int], sub_key: str = '', exist_ok: bool = T
hive_sub_key_parent = '\\'.join(hive_sub_key.split('\\')[:-1])
if not self.key_exist(hive_key, hive_sub_key_parent):
key_str = get_key_as_string(hive_key, hive_sub_key)
raise RegistryKeyCreateError('can not create key, the parent key to "{key_str}" does not exist'.format(key_str=key_str))
raise RegistryKeyCreateError(f'can not create key, the parent key to "{key_str}" does not exist')

new_key_handle = winreg.CreateKey(hive_key, hive_sub_key)
self.reg_key_handles[(hive_key, hive_sub_key, winreg.KEY_ALL_ACCESS)] = new_key_handle
Expand Down Expand Up @@ -522,12 +529,12 @@ def delete_key(self, key: Union[str, int], sub_key: str = '', missing_ok: bool =
return
else:
key_str = get_key_as_string(key, sub_key)
raise RegistryKeyDeleteError('can not delete key none existing key "{key_str}"'.format(key_str=key_str))
raise RegistryKeyDeleteError(f'can not delete key none existing key "{key_str}"')

if self.has_subkeys(hive_key, hive_sub_key):
if not delete_subkeys:
key_str = get_key_as_string(key, sub_key)
raise RegistryKeyDeleteError('can not delete none empty key "{key_str}"'.format(key_str=key_str))
raise RegistryKeyDeleteError(f'can not delete none empty key "{key_str}"')
else:
for sub_key in self.subkeys(hive_key, hive_sub_key):
hive_subkey_child = '\\'.join([hive_sub_key, sub_key])
Expand Down Expand Up @@ -904,7 +911,7 @@ def get_value_ex(self, key: Union[str, int], value_name: Optional[str]) -> Tuple
return reg_value, reg_type
except FileNotFoundError:
key_str = get_key_as_string(key)
raise RegistryValueNotFoundError('value "{}" not found in key "{}"'.format(value_name, key_str))
raise RegistryValueNotFoundError(f'value "{value_name}" not found in key "{key_str}"')

def username_from_sid(self, sid: str) -> str:
"""
Expand Down Expand Up @@ -933,9 +940,9 @@ def username_from_sid(self, sid: str) -> str:
try:
username = self._get_username_from_profile_list(sid)
except RegistryError:
raise RegistryError('can not determine User for SID "{}"'.format(sid))
raise RegistryError(f'can not determine User for SID "{sid}"')
if not username:
raise RegistryError('can not determine User for SID "{}"'.format(sid))
raise RegistryError(f'can not determine User for SID "{sid}"')
return username

def _get_username_from_profile_list(self, sid: str) -> str:
Expand All @@ -959,7 +966,7 @@ def _get_username_from_profile_list(self, sid: str) -> str:
lib_registry.RegistryKeyNotFoundError: registry key "...unknown" not found
"""
value, value_type = self.get_value_ex(r'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\{}'.format(sid), 'ProfileImagePath')
value, value_type = self.get_value_ex(fr'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\{sid}', 'ProfileImagePath')
assert isinstance(value, str)
username = pathlib.PureWindowsPath(value).name
return username
Expand All @@ -983,7 +990,7 @@ def _get_username_from_volatile_environment(self, sid: str) -> str:
'...'
"""
value, value_type = self.get_value_ex(r'HKEY_USERS\{}\Volatile Environment'.format(sid), 'USERNAME')
value, value_type = self.get_value_ex(fr'HKEY_USERS\{sid}\Volatile Environment', 'USERNAME')
assert isinstance(value, str)
return str(value)

Expand Down Expand Up @@ -1078,14 +1085,14 @@ def set_value(self, key: Union[str, int], value_name: Optional[str], value: RegD

key_handle = self._open_key(key, access=winreg.KEY_ALL_ACCESS)
try:
winreg.SetValueEx(key_handle, value_name, 0, value_type, value)
winreg.SetValueEx(key_handle, value_name, 0, value_type, value) # type: ignore
except Exception: # ToDo: narrow down
# different Errors can occur here :
# TypeError: Objects of type 'str' can not be used as binary registry values (if try to write string to REG_NONE type)
# others to explore ...
key_str = get_key_as_string(key)
value_type_str = get_value_type_as_string(value_type)
raise RegistryValueWriteError('can not write data to key "{}", value_name "{}", value_type "{}"'.format(key_str, value_name, value_type_str))
raise RegistryValueWriteError(f'can not write data to key "{key_str}", value_name "{value_name}", value_type "{value_type_str}"')

def delete_value(self, key: Union[str, int], value_name: Optional[str]) -> None:
"""
Expand Down Expand Up @@ -1135,7 +1142,7 @@ def delete_value(self, key: Union[str, int], value_name: Optional[str]) -> None:
except FileNotFoundError as e:
if hasattr(e, 'winerror') and e.winerror == 2: # type: ignore
key_str = get_key_as_string(key)
raise RegistryValueDeleteError('can not delete value "{}" from key "{}"'.format(value_name, key_str))
raise RegistryValueDeleteError(f'can not delete value "{value_name}" from key "{key_str}"')
else:
raise e

Expand Down Expand Up @@ -1207,7 +1214,7 @@ def resolve_key(key: Union[str, int], sub_key: str = '') -> Tuple[int, str]:
else:
hive_key = key
if hive_key not in hive_names_hashed_by_int:
raise RegistryHKeyError('invalid HIVE KEY: "{hive_key}"'.format(hive_key=hive_key))
raise RegistryHKeyError(f'invalid HIVE KEY: "{hive_key}"')
return hive_key, sub_key


Expand All @@ -1232,7 +1239,7 @@ def get_hkey_int(key_name: str) -> int:
main_key = int(main_key_hashed_by_name[main_key_name_lower])
return main_key
else:
raise RegistryHKeyError('invalid KEY: "{main_key_name}"'.format(main_key_name=main_key_name))
raise RegistryHKeyError(f'invalid KEY: "{main_key_name}"')


def remove_hive_from_key_str_if_present(key_name: str) -> str:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def get_line_data(line: str) -> str:

setup_kwargs: Dict[str, Any] = dict()
setup_kwargs["name"] = "lib_registry"
setup_kwargs["version"] = "v2.0.6"
setup_kwargs["version"] = "v2.0.7"
setup_kwargs["url"] = "https://github.com/bitranox/lib_registry"
setup_kwargs["packages"] = find_packages()
setup_kwargs["package_data"] = {"lib_registry": ["py.typed", "*.pyi", "__init__.pyi"]}
Expand Down

0 comments on commit cffcdd6

Please sign in to comment.