Python winreg.REG_EXPAND_SZ Examples

The following are 17 code examples of winreg.REG_EXPAND_SZ(). 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: win_add2path.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 7 votes vote down vote up
def modify():
    pythonpath = os.path.dirname(os.path.normpath(sys.executable))
    scripts = os.path.join(pythonpath, "Scripts")
    appdata = os.environ["APPDATA"]
    if hasattr(site, "USER_SITE"):
        usersite = site.USER_SITE.replace(appdata, "%APPDATA%")
        userpath = os.path.dirname(usersite)
        userscripts = os.path.join(userpath, "Scripts")
    else:
        userscripts = None

    with winreg.CreateKey(HKCU, ENV) as key:
        try:
            envpath = winreg.QueryValueEx(key, PATH)[0]
        except OSError:
            envpath = DEFAULT

        paths = [envpath]
        for path in (pythonpath, scripts, userscripts):
            if path and path not in envpath and os.path.isdir(path):
                paths.append(path)

        envpath = os.pathsep.join(paths)
        winreg.SetValueEx(key, PATH, 0, winreg.REG_EXPAND_SZ, envpath)
        return paths, envpath 
Example #2
Source File: __init__.py    From browser_cookie3 with GNU Lesser General Public License v3.0 7 votes vote down vote up
def windows_group_policy_path():
    # we know that we're running under windows at this point so it's safe to do these imports
    from winreg import ConnectRegistry, HKEY_LOCAL_MACHINE, OpenKeyEx, QueryValueEx, REG_EXPAND_SZ, REG_SZ
    try:
        root = ConnectRegistry(None, HKEY_LOCAL_MACHINE)
        policy_key = OpenKeyEx(root, r"SOFTWARE\Policies\Google\Chrome")
        user_data_dir, type_ = QueryValueEx(policy_key, "UserDataDir")
        if type_ == REG_EXPAND_SZ:
            user_data_dir = os.path.expandvars(user_data_dir)
        elif type_ != REG_SZ:
            return None
    except OSError:
        return None
    return os.path.join(user_data_dir, "Default", "Cookies")


# Code adapted slightly from https://github.com/Arnie97/chrome-cookies 
Example #3
Source File: installation.py    From blogger-cli with MIT License 6 votes vote down vote up
def set_windows_path_var(self, value):
        import ctypes

        with winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER) as root:
            with winreg.OpenKey(root, "Environment", 0, winreg.KEY_ALL_ACCESS) as key:
                winreg.SetValueEx(key, "PATH", 0, winreg.REG_EXPAND_SZ, value)

        # Tell other processes to update their environment
        HWND_BROADCAST = 0xFFFF
        WM_SETTINGCHANGE = 0x1A

        SMTO_ABORTIFHUNG = 0x0002

        result = ctypes.c_long()
        SendMessageTimeoutW = ctypes.windll.user32.SendMessageTimeoutW
        SendMessageTimeoutW(
            HWND_BROADCAST,
            WM_SETTINGCHANGE,
            0,
            u"Environment",
            SMTO_ABORTIFHUNG,
            5000,
            ctypes.byref(result),
        ) 
Example #4
Source File: get_blogger.py    From blogger-cli with MIT License 6 votes vote down vote up
def set_windows_path_var(self, value):
        import ctypes

        with winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER) as root:
            with winreg.OpenKey(root, "Environment", 0, winreg.KEY_ALL_ACCESS) as key:
                winreg.SetValueEx(key, "PATH", 0, winreg.REG_EXPAND_SZ, value)

        # Tell other processes to update their environment
        HWND_BROADCAST = 0xFFFF
        WM_SETTINGCHANGE = 0x1A

        SMTO_ABORTIFHUNG = 0x0002

        result = ctypes.c_long()
        SendMessageTimeoutW = ctypes.windll.user32.SendMessageTimeoutW
        SendMessageTimeoutW(
            HWND_BROADCAST,
            WM_SETTINGCHANGE,
            0,
            u"Environment",
            SMTO_ABORTIFHUNG,
            5000,
            ctypes.byref(result),
        ) 
Example #5
Source File: win_add2path.py    From android_universal with MIT License 6 votes vote down vote up
def modify():
    pythonpath = os.path.dirname(os.path.normpath(sys.executable))
    scripts = os.path.join(pythonpath, "Scripts")
    appdata = os.environ["APPDATA"]
    if hasattr(site, "USER_SITE"):
        usersite = site.USER_SITE.replace(appdata, "%APPDATA%")
        userpath = os.path.dirname(usersite)
        userscripts = os.path.join(userpath, "Scripts")
    else:
        userscripts = None

    with winreg.CreateKey(HKCU, ENV) as key:
        try:
            envpath = winreg.QueryValueEx(key, PATH)[0]
        except OSError:
            envpath = DEFAULT

        paths = [envpath]
        for path in (pythonpath, scripts, userscripts):
            if path and path not in envpath and os.path.isdir(path):
                paths.append(path)

        envpath = os.pathsep.join(paths)
        winreg.SetValueEx(key, PATH, 0, winreg.REG_EXPAND_SZ, envpath)
        return paths, envpath 
Example #6
Source File: win_add2path.py    From odoo13-x64 with GNU General Public License v3.0 6 votes vote down vote up
def modify():
    pythonpath = os.path.dirname(os.path.normpath(sys.executable))
    scripts = os.path.join(pythonpath, "Scripts")
    appdata = os.environ["APPDATA"]
    if hasattr(site, "USER_SITE"):
        usersite = site.USER_SITE.replace(appdata, "%APPDATA%")
        userpath = os.path.dirname(usersite)
        userscripts = os.path.join(userpath, "Scripts")
    else:
        userscripts = None

    with winreg.CreateKey(HKCU, ENV) as key:
        try:
            envpath = winreg.QueryValueEx(key, PATH)[0]
        except OSError:
            envpath = DEFAULT

        paths = [envpath]
        for path in (pythonpath, scripts, userscripts):
            if path and path not in envpath and os.path.isdir(path):
                paths.append(path)

        envpath = os.pathsep.join(paths)
        winreg.SetValueEx(key, PATH, 0, winreg.REG_EXPAND_SZ, envpath)
        return paths, envpath 
Example #7
Source File: get-poetry.py    From poetry with MIT License 6 votes vote down vote up
def set_windows_path_var(self, value):
        import ctypes

        with winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER) as root:
            with winreg.OpenKey(root, "Environment", 0, winreg.KEY_ALL_ACCESS) as key:
                winreg.SetValueEx(key, "PATH", 0, winreg.REG_EXPAND_SZ, value)

        # Tell other processes to update their environment
        HWND_BROADCAST = 0xFFFF
        WM_SETTINGCHANGE = 0x1A

        SMTO_ABORTIFHUNG = 0x0002

        result = ctypes.c_long()
        SendMessageTimeoutW = ctypes.windll.user32.SendMessageTimeoutW
        SendMessageTimeoutW(
            HWND_BROADCAST,
            WM_SETTINGCHANGE,
            0,
            u"Environment",
            SMTO_ABORTIFHUNG,
            5000,
            ctypes.byref(result),
        ) 
Example #8
Source File: store_env_in_registry.py    From coala-quickstart with GNU Affero General Public License v3.0 6 votes vote down vote up
def set_envvar_in_registry(envvar, value):
    try:
        import winreg
    except ImportError:
        import _winreg as winreg

    reg = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)
    with winreg.OpenKey(reg, KEY, 0, winreg.KEY_ALL_ACCESS) as regkey:
        winreg.SetValueEx(regkey, envvar, 0, winreg.REG_EXPAND_SZ, value) 
Example #9
Source File: px.py    From px with MIT License 5 votes vote down vote up
def install():
    if check_installed() is False:
        runkey = winreg.OpenKey(winreg.HKEY_CURRENT_USER,
            r"Software\Microsoft\Windows\CurrentVersion\Run", 0,
            winreg.KEY_WRITE)
        winreg.SetValueEx(runkey, "Px", 0, winreg.REG_EXPAND_SZ,
            get_script_cmd())
        winreg.CloseKey(runkey)
        pprint("Px installed successfully")
    else:
        pprint("Px already installed")

    sys.exit() 
Example #10
Source File: _winconsole.py    From pipenv with MIT License 5 votes vote down vote up
def get_value_from_tuple(value, value_type):
    try:
        import winreg
    except ImportError:
        import _winreg as winreg
    if value_type in (winreg.REG_SZ, winreg.REG_EXPAND_SZ):
        if "\0" in value:
            return value[: value.index("\0")]
        return value
    return None 
Example #11
Source File: recipe-577621.py    From code with MIT License 5 votes vote down vote up
def setenv(self, name, value):
        # Note: for 'system' scope, you must run this as Administrator
        key = winreg.OpenKey(self.root, self.subkey, 0, winreg.KEY_ALL_ACCESS)
        winreg.SetValueEx(key, name, 0, winreg.REG_EXPAND_SZ, value)
        winreg.CloseKey(key)
        # For some strange reason, calling SendMessage from the current process
        # doesn't propagate environment changes at all.
        # TODO: handle CalledProcessError (for assert)
        check_call('''\
"%s" -c "import win32api, win32con; assert win32api.SendMessage(win32con.HWND_BROADCAST, win32con.WM_SETTINGCHANGE, 0, 'Environment')"''' % sys.executable) 
Example #12
Source File: _winconsole.py    From vistir with ISC License 5 votes vote down vote up
def get_value_from_tuple(value, value_type):
    try:
        import winreg
    except ImportError:
        import _winreg as winreg
    if value_type in (winreg.REG_SZ, winreg.REG_EXPAND_SZ):
        if "\0" in value:
            return value[: value.index("\0")]
        return value
    return None 
Example #13
Source File: store_env_in_registry.py    From coala with GNU Affero General Public License v3.0 5 votes vote down vote up
def set_envvar_in_registry(envvar, value):
    try:
        import winreg
    except ImportError:
        import _winreg as winreg

    reg = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)
    with winreg.OpenKey(reg, KEY, 0, winreg.KEY_ALL_ACCESS) as regkey:
        winreg.SetValueEx(regkey, envvar, 0, winreg.REG_EXPAND_SZ, value) 
Example #14
Source File: registry.py    From rekall with GNU General Public License v2.0 5 votes vote down vote up
def Reg2Py(data, size, data_type):
    if data_type == winreg.REG_DWORD:
        if size == 0:
            return 0
        return ctypes.cast(data, ctypes.POINTER(ctypes.c_int)).contents.value
    elif data_type == winreg.REG_SZ or data_type == winreg.REG_EXPAND_SZ:
        return ctypes.wstring_at(data, size // 2).rstrip(u"\x00")
    elif data_type == winreg.REG_MULTI_SZ:
        return ctypes.wstring_at(data, size // 2).rstrip(u"\x00").split(u"\x00")
    else:
        if size == 0:
            return None
        return ctypes.string_at(data, size) 
Example #15
Source File: store_env_in_registry.py    From coala-bears with GNU Affero General Public License v3.0 5 votes vote down vote up
def set_envvar_in_registry(envvar, value):
    try:
        import winreg
    except ImportError:
        import _winreg as winreg

    reg = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)
    with winreg.OpenKey(reg, KEY, 0, winreg.KEY_ALL_ACCESS) as regkey:
        winreg.SetValueEx(regkey, envvar, 0, winreg.REG_EXPAND_SZ, value) 
Example #16
Source File: postinstall_simnibs.py    From simnibs with GNU General Public License v3.0 5 votes vote down vote up
def path_cleanup():
    ''' Removes SIMNIBS from PATH '''
    if sys.platform in ['linux', 'darwin']:
        bashrc, backup_file = _get_bashrc()

        if not os.path.isfile(bashrc):
            print('Could not find bashrc file')
            return

        print('Removing SimNIBS install from PATH')
        print(f'Backing up the bashrc file at {backup_file}')
        _copy_and_log(bashrc, backup_file)
        with open(backup_file, 'r') as fin:
            with open(bashrc, 'w') as fout:
                for line in fin:
                    if not re.search('simnibs', line, re.IGNORECASE):
                        fout.write(line)
    else:
        simnibs_env_vars = _get_win_simnibs_env_vars()
        path = _get_win_path()
        path = path.split(';')
        path = [p for p in path if len(p) > 0]
        for key, value in simnibs_env_vars.items():
            # If the directory is in the PATH variable, remove it
            path = [
                os.path.normpath(p) for p in path if not (
                os.path.normpath(value) in os.path.normpath(p))]
            # Remove environment variable
            with winreg.OpenKey(winreg.HKEY_CURRENT_USER, 'Environment', access=winreg.KEY_WRITE) as reg:
                winreg.DeleteValue(reg, key)

        # write out the PATH with SimNIBS removed
        with winreg.OpenKey(winreg.HKEY_CURRENT_USER, 'Environment', access=winreg.KEY_WRITE) as reg:
            path = ';'.join(path) + ';'
            winreg.SetValueEx(reg,'Path', 0, winreg.REG_EXPAND_SZ, path) 
Example #17
Source File: postinstall_simnibs.py    From simnibs with GNU General Public License v3.0 4 votes vote down vote up
def path_setup(scripts_dir, force=False, silent=False):
    ''' Modifies the bash startup path and postpends SimNIBS to the PATH '''
    scripts_dir = os.path.abspath(scripts_dir)
    silent = silent and GUI
    if sys.platform in ['linux', 'darwin']:
        bashrc, _ = _get_bashrc()
        if os.path.exists(bashrc):
            has_simnibs = (
                re.search('simnibs', open(bashrc, 'r').read(), re.IGNORECASE)
                is not None)
        else:
            has_simnibs = False

    if sys.platform == 'win32':
        simnibs_env_vars = _get_win_simnibs_env_vars()
        has_simnibs = len(simnibs_env_vars) != 0

    if has_simnibs:
        if force:
             overwrite=True
        else:
            overwrite = _get_input(
                'Found another SimNIBS install, overwite it from the PATH?',
                silent)

        if not overwrite:
            print('Not Adding the current SimNIBS install to the PATH')
            return

        path_cleanup()

    print(f'Postpending {scripts_dir} to the PATH')
    if sys.platform in ['linux', 'darwin']:
        with open(bashrc, 'a') as f:
            f.write('\n')
            f.write('## Added by SimNIBS\n')
            f.write(f'SIMNIBS_BIN="{scripts_dir}"\n')
            f.write('export PATH=${PATH}:${SIMNIBS_BIN}')

    else:
        with winreg.OpenKey(winreg.HKEY_CURRENT_USER, 'Environment', access=winreg.KEY_WRITE) as reg:
            winreg.SetValueEx(reg,'SIMNIBS_BIN', 0, winreg.REG_SZ, scripts_dir)
            path = scripts_dir + ';' + _get_win_path()
            winreg.SetValueEx(reg,'Path', 0, winreg.REG_EXPAND_SZ, path)