# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + pycharm={"name": "#%%\n"}
# update pip and setuptools
import sys
# !{sys.executable} -m pip install --upgrade pip
# !{sys.executable} -m pip install --upgrade setuptools

# install lib_registry from pypi
# !{sys.executable} -m pip install --upgrade lib_registry

# install lib_registry from github
# !{sys.executable} -m pip install --upgrade git+https://github.com/bitranox/lib_registry.git

# + pycharm={"name": "#%%\n"}
# please be noted that the lib_registry interface will change (a lot) in the future, to offer pathlib-like
# behaviour, as well as context managers, etc.

# this version is fully functional however and allows to read / write / query the windows registry
# here a few examples !

# lib_registry is using fake_winreg to simulate a windows registry on linux, so this is possible here on jupyter.
from lib_registry import *
registry=Registry()

# + pycharm={"name": "#%%\n"}
# query values - there are only a few values set in the fake registry You can query here.
# on Your windows machine of course You can query all values You have permission for.
registry.get_value(key='HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion', value_name='CurrentBuild')

# + pycharm={"name": "#%%\n"}
# all subkeys, values, etc use generator objects
registry.subkeys(winreg.HKEY_USERS)

# + pycharm={"name": "#%%\n"}
# so lets make a list of all subkeys of HKEY_USERS
list(registry.subkeys(winreg.HKEY_USERS))

# + pycharm={"name": "#%%\n"}
# create a key - pathlib like options (in preparation to pathlib-like behaviour)
reg_handle = registry.create_key('HKCU\\Software\\lib_registry_test', exist_ok=True, parents=True)

# + pycharm={"name": "#%%\n"}
# set a value, REG_TYPE can be given or is automatically set
binary_test_value=(chr(128512) * 10).encode('utf-8')
registry.set_value(reg_handle, value_name='test_string', value='HAM')
registry.set_value(reg_handle, value_name='test_multi_string', value=['HAM', 'SPAM'])
registry.set_value(reg_handle, value_name='test_int', value=42)
registry.set_value(reg_handle, value_name='test_binary', value=binary_test_value)
registry.set_value(reg_handle, value_name='test_none', value=None)

# + pycharm={"name": "#%%\n"}
# get a value from the Registry
registry.get_value(reg_handle, value_name='test_string')
registry.get_value(reg_handle, value_name='test_multi_string')
registry.get_value(reg_handle, value_name='test_int')
registry.get_value(reg_handle, value_name='test_binary')
# or get it by name of the key
registry.get_value(key='HKCU\\Software\\lib_registry_test', value_name='test_none')

# + pycharm={"name": "#%%\n"}
# delete a value from the registry
registry.delete_value(key='HKCU\\Software\\lib_registry_test', value_name='test_string')


# + pycharm={"name": "#%%\n"}
# delete a key from the registry
registry.delete_key(key='HKCU\\Software\\lib_registry_test')

# + pycharm={"name": "#%%\n"}
# some tools like SID's (Secure ID)
list(registry.sids())


# + pycharm={"name": "#%%\n"}
# or get the username from a SID
for sid in registry.sids():
    print(registry.username_from_sid(sid))


StarcoderdataJupyterScriptsDedupFiltered