Python _winreg.CreateKeyEx() Examples

The following are 6 code examples of _winreg.CreateKeyEx(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module _winreg , or try the search function .
Example #1
Source File: _registry.py    From pythonfinder with MIT License 5 votes vote down vote up
def set_value(self, value_name, value):
        with winreg.CreateKeyEx(self._root, self.subkey, 0, winreg.KEY_WRITE | self._flags) as key:
            if value is None:
                winreg.DeleteValue(key, value_name)
            elif isinstance(value, str):
                winreg.SetValueEx(key, value_name, 0, winreg.REG_SZ, value)
            else:
                raise TypeError('cannot write {} to registry'.format(type(value))) 
Example #2
Source File: _registry.py    From pythonfinder with MIT License 5 votes vote down vote up
def _set_all_values(self, rootkey, name, info, errors):
        with winreg.CreateKeyEx(rootkey, name, 0, winreg.KEY_WRITE | self._flags) as key:
            for k, v in info:
                if isinstance(v, PythonWrappedDict):
                    self._set_all_values(key, k, v._items(), errors)
                elif isinstance(v, dict):
                    self._set_all_values(key, k, v.items(), errors)
                elif v is None:
                    winreg.DeleteValue(key, k)
                elif isinstance(v, str):
                    winreg.SetValueEx(key, k, 0, winreg.REG_SZ, v)
                else:
                    errors.append('cannot write {} to registry'.format(type(v))) 
Example #3
Source File: _registry.py    From pipenv with MIT License 5 votes vote down vote up
def set_value(self, value_name, value):
        with winreg.CreateKeyEx(self._root, self.subkey, 0, winreg.KEY_WRITE | self._flags) as key:
            if value is None:
                winreg.DeleteValue(key, value_name)
            elif isinstance(value, str):
                winreg.SetValueEx(key, value_name, 0, winreg.REG_SZ, value)
            else:
                raise TypeError('cannot write {} to registry'.format(type(value))) 
Example #4
Source File: _registry.py    From pipenv with MIT License 5 votes vote down vote up
def _set_all_values(self, rootkey, name, info, errors):
        with winreg.CreateKeyEx(rootkey, name, 0, winreg.KEY_WRITE | self._flags) as key:
            for k, v in info:
                if isinstance(v, PythonWrappedDict):
                    self._set_all_values(key, k, v._items(), errors)
                elif isinstance(v, dict):
                    self._set_all_values(key, k, v.items(), errors)
                elif v is None:
                    winreg.DeleteValue(key, k)
                elif isinstance(v, str):
                    winreg.SetValueEx(key, k, 0, winreg.REG_SZ, v)
                else:
                    errors.append('cannot write {} to registry'.format(type(v))) 
Example #5
Source File: __init__.py    From butterflow with MIT License 5 votes vote down vote up
def add_registry_keys():
    for key in get_local_machine_registry_subkeys(KHRONOS_REG_PATH):
        if key is not None:
            key = RegistryKey(key[0], key[2], key[1])
            if key in KEYS_NEEDED:
                KEYS_NEEDED.remove(key)
            else:
                # print("?: "+str(key))
                pass

    # print("No keys left\nKeys to add: "+str(KEYS_NEEDED))

    for key_needed in KEYS_NEEDED:
        try:
            try:
                subkey = winreg.CreateKeyEx(REG_HIVE, KHRONOS_REG_PATH, 0, winreg.KEY_CREATE_SUB_KEY)
            except WindowsError as error: 
                print("Couldn't create subkeys at: %s\tReason: %s" % (KHRONOS_REG_PATH, error))
                exit(1)
            finally:
                subkey.Close()
            with winreg.OpenKey(REG_HIVE, KHRONOS_REG_PATH, 0, winreg.KEY_WRITE) as key: 
                winreg.SetValueEx(key, key_needed.name, 0, key_needed.data_type, key_needed.value)
                # print("+"+str(key_needed))
        except WindowsError as error:
            print("Couldn't create (%s)\tReason: %s" % (key_needed, error))
            exit(1)

# TODO: How to Enumerate Vendor ICDs on Windows: https://github.com/KhronosGroup/OpenCL-Docs/blob/master/ext/cl_khr_icd.txt#L68
# SEARCH_HIVE=winreg.HKEY_LOCAL_MACHINE 
Example #6
Source File: rpath.py    From r-bridge-install with Apache License 2.0 4 votes vote down vote up
def r_reg_write_value(r_key=None, r_value=None):
    """Write R registry values."""
    # keys to write
    r_write_keys = ('InstallPath', 'Current Version')
    if r_key not in r_write_keys:
        log.warn("asked to write an invalid key, {}".format(r_key))
        return None

    root_keys = OrderedDict((
        # try HKLM, then HKCU
        ('HKLM', winreg.HKEY_LOCAL_MACHINE),
        ('HKCU', winreg.HKEY_CURRENT_USER)
    ))
    # only work with the R and R64 hives, ArcGIS doesn't examine R32
    r_reg_paths = ["SOFTWARE\\R-core\\R",
                   "SOFTWARE\\R-core\\R64",
                   "SOFTWARE\\Wow6432Node\\R-Core\\R",
                   "SOFTWARE\\Wow6432Node\\R-Core\\R64"]

    for (key_name, root_key) in list(root_keys.items()):
        wrote = False
        for r_path in r_reg_paths:
            r_reg = None

            try:
                log.info("CreateKeyEx on {}\\{}, with write".format(
                    key_name, r_path))
                r_reg = winreg.CreateKeyEx(root_key, r_path, 0, FULL_ACCESS)
            except WindowsError as error:
                if error.errno == errno.ENOENT:
                    pass
                # permission denied, skip
                if error.errno == errno.EACCES:
                    log.debug("permission denied.")
                    continue

            if r_reg:
                try:
                    log.info('setting "{}" to "{}"'.format(r_key, r_value))
                    winreg.SetValueEx(r_reg, r_key, 0,
                                      winreg.REG_SZ, r_value)
                    wrote = True
                except fnf_exception as error:
                    handle_fnf(error)
        # only enter the keys into one hive
        if wrote:
            break