Module wslpy.win

Provides easy access to the Windows information

Expand source code
"""
Provides easy access to the Windows information
"""
from .__core__.access import distro_info, registry
from wslpy.exec import winps


def get_current_executable():
    """
    Get current exes of the current WSL (for ones that have)

    Returns
    -------
    A string of the executables
    """
    import os
    from wslpy.convert import to_wsl
    from wslpy.exec import winps

    _distro_info = distro_info()
    if "PackageFamilyName" in _distro_info:
        pname = _distro_info["PackageFamilyName"]['value']
        p = winps("[Environment]::GetFolderPath('LocalApplicationData')")
        if p.returncode:
            raise Exception("Failed to get LocalApplicationData")
        raw_win_path = p.stdout.replace("\r\n", "")
        win_path = raw_win_path + "\\Microsoft\\WindowsApps\\" + pname
        exe_real_loc = to_wsl(win_path)
        return os.listdir(exe_real_loc)[0]
    else:
        return None


def get_display_scaling():
    """
    Get Windows Display Scaling

    Returns
    -------
    A number of the current display scaling
    """
    command = """
Add-Type @'
using System;
using System.Runtime.InteropServices;
using System.Drawing;

public class DPI {
    [DllImport("gdi32.dll")]
    static extern int GetDeviceCaps(IntPtr hdc, int nIndex);

    public enum DeviceCap {
    VERTRES = 10,
    DESKTOPVERTRES = 117
    }

    public static float scaling() {
    Graphics g = Graphics.FromHwnd(IntPtr.Zero);
    IntPtr desktop = g.GetHdc();
    int LogicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.VERTRES);
    int PhysicalScreenHeight = GetDeviceCaps(desktop,
        (int)DeviceCap.DESKTOPVERTRES);

    return (float)PhysicalScreenHeight / (float)LogicalScreenHeight;
    }
}
'@ -ReferencedAssemblies 'System.Drawing.dll'

[Math]::round([DPI]::scaling(), 2) * 100
"""

    p = winps(command)
    if p.returncode:
        raise Exception("Failed to get display scaling: ", p.stderr)
    dscale = int(p.stdout.rstrip()) / 100
    return dscale


def get_windows_locale():
    """
    Get Windows Locale

    Returns
    -------
    a string of Windows Locale
    """
    p = winps("(Get-Culture).Name")
    if p.returncode:
        raise Exception("Failed to get Windows Locale: ", p.stderr)
    win_locale = p.stdout.rstrip().replace("-", "_")
    return win_locale


def get_windows_theme():
    """
    Get Windows Theme

    Returns
    -------
    a string of either "light" or "dark"
    """
    raw_theme = registry("HKCU\\SOFTWARE\\Microsoft\\Windows\\Current"
                         "Version\\Themes\\Personalize", "AppsUseLightTheme")
    return "dark" if int(raw_theme, 0) else "light"


def get_windows_install_date(friendly_output=False):
    """
    Get the install date of the current installed build of Windows

    Parameters
    ----------
    friendly_output: bool
        whether the value returned is readable or not, `false` by default

    Returns
    -------
    A integer of unix timestamp or a string of readable time
    """
    raw_time = registry("HKLM\\Software\\Microsoft"
                        "\\Windows NT\\CurrentVersion", "InstallDate")
    dec_time = int(raw_time, 0)

    if friendly_output:
        from datetime import datetime
        f_time = \
            datetime.utcfromtimestamp(dec_time).strftime('%Y-%m-%d %H:%M:%S')
        return f_time
    else:
        return dec_time


def get_windows_branch():
    """
    Get the current Windows branch

    Returns
    -------
    a string of Windows Branch
    """
    raw_branch = registry("HKLM\\Software\\Microsoft"
                          "\\Windows NT\\CurrentVersion", "BuildBranch")
    return raw_branch


def get_windows_build(is_full_build=False):
    """
    Get Windows build information

    Parameters
    ----------
    is_full_build: bool
        if it should print full build, `False` by default

    Returns
    -------
    a string of windows build
    """
    if is_full_build:
        raw_build = registry("HKLM\\Software\\Microsoft"
                             "\\Windows NT\\CurrentVersion",
                             "BuildLabEx")
    else:
        raw_build = registry("HKLM\\Software\\Microsoft"
                             "\\Windows NT\\CurrentVersion",
                             "CurrentBuild")
    return raw_build


def get_windows_uptime():
    """
    Get the current uptime in Windows

    Returns
    -------
    a array in the format of [days, hours, minutes]
    """
    p = winps("[int64]((get-date) - (gcim Win32_Operating"
              "System).LastBootUpTime).TotalSeconds")
    if p.returncode:
        return -1
    raw_time = int(p.stdout.rstrip())
    raw_days = raw_time // 86400
    raw_hours = raw_time // 3600 % 24
    raw_minutes = raw_time // 60 % 60
    return [raw_days, raw_hours, raw_minutes]


__all__ = ["get_current_executable", "get_display_scaling",
           "get_windows_locale", "get_windows_theme",
           "get_windows_install_date", "get_windows_branch",
           "get_windows_build", "get_windows_uptime",
           "registry", "distro_info"]

Functions

def distro_info(distro_name=None)

Returns the distro information as a string.

Returns

The distro information as a dictionary.

Raises

Returns the error from the command
 
Expand source code
def distro_info(distro_name=None):
    """
    Returns the distro information as a string.

    Returns
    -------
    The distro information as a dictionary.

    Raises
    ------
    Returns the error from the command
    """
    if distro_name is None:
        if environ.get("WSL_DISTRO_NAME"):
            distro_name = environ.get("WSL_DISTRO_NAME")
        else:
            raise RuntimeError("WSL_DISTRO_NAME is not set")
    p = __exec_command__(["reg.exe", "query",
                          "HKCU\\SOFTWARE\\Microsoft\\"
                          "Windows\\CurrentVersion\\Lxss",
                          "/s", "/f", "DistributionName"])
    if p.returncode != 0:
        raise RuntimeError("failed to retrive distro information: {}"
                           .format(p.stderr))
    raw_data = p.stdout[2:]
    raw_list = raw_data.split("\r\n\r\n")[:-1]
    for raw_item in raw_list:
        if raw_item.endswith(distro_name):
            distro_loc = raw_item.split("\r\n")[0]
            p2 = __exec_command__(["reg.exe", "query", distro_loc, "/s"])
            if p2.returncode != 0:
                raise RuntimeError("failed to retrieve distro information: {}"
                                   .format(p2.stderr))
            import re
            raw_distro_info = p2.stdout
            # Clean output first to toutput
            out = re.sub((r"\r\nHKEY_CURRENT_USER.*\r\n"), '', raw_distro_info)
            # split toutput into list with aoutput
            out_p = (re.split(r'\s\s+', out))[1:][:-1]
            # convert aoutput to dictionary
            output = {item: {'type': type, 'value': value}
                      for item, type, value in zip(out_p[::3], out_p[1::3],
                                                   out_p[2::3])}
            return output
def get_current_executable()

Get current exes of the current WSL (for ones that have)

Returns

A string of the executables
 
Expand source code
def get_current_executable():
    """
    Get current exes of the current WSL (for ones that have)

    Returns
    -------
    A string of the executables
    """
    import os
    from wslpy.convert import to_wsl
    from wslpy.exec import winps

    _distro_info = distro_info()
    if "PackageFamilyName" in _distro_info:
        pname = _distro_info["PackageFamilyName"]['value']
        p = winps("[Environment]::GetFolderPath('LocalApplicationData')")
        if p.returncode:
            raise Exception("Failed to get LocalApplicationData")
        raw_win_path = p.stdout.replace("\r\n", "")
        win_path = raw_win_path + "\\Microsoft\\WindowsApps\\" + pname
        exe_real_loc = to_wsl(win_path)
        return os.listdir(exe_real_loc)[0]
    else:
        return None
def get_display_scaling()

Get Windows Display Scaling

Returns

A number of the current display scaling
 
Expand source code
def get_display_scaling():
    """
    Get Windows Display Scaling

    Returns
    -------
    A number of the current display scaling
    """
    command = """
Add-Type @'
using System;
using System.Runtime.InteropServices;
using System.Drawing;

public class DPI {
    [DllImport("gdi32.dll")]
    static extern int GetDeviceCaps(IntPtr hdc, int nIndex);

    public enum DeviceCap {
    VERTRES = 10,
    DESKTOPVERTRES = 117
    }

    public static float scaling() {
    Graphics g = Graphics.FromHwnd(IntPtr.Zero);
    IntPtr desktop = g.GetHdc();
    int LogicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.VERTRES);
    int PhysicalScreenHeight = GetDeviceCaps(desktop,
        (int)DeviceCap.DESKTOPVERTRES);

    return (float)PhysicalScreenHeight / (float)LogicalScreenHeight;
    }
}
'@ -ReferencedAssemblies 'System.Drawing.dll'

[Math]::round([DPI]::scaling(), 2) * 100
"""

    p = winps(command)
    if p.returncode:
        raise Exception("Failed to get display scaling: ", p.stderr)
    dscale = int(p.stdout.rstrip()) / 100
    return dscale
def get_windows_branch()

Get the current Windows branch

Returns

a string of Windows Branch
 
Expand source code
def get_windows_branch():
    """
    Get the current Windows branch

    Returns
    -------
    a string of Windows Branch
    """
    raw_branch = registry("HKLM\\Software\\Microsoft"
                          "\\Windows NT\\CurrentVersion", "BuildBranch")
    return raw_branch
def get_windows_build(is_full_build=False)

Get Windows build information

Parameters

is_full_build : bool
if it should print full build, False by default

Returns

a string of windows build
 
Expand source code
def get_windows_build(is_full_build=False):
    """
    Get Windows build information

    Parameters
    ----------
    is_full_build: bool
        if it should print full build, `False` by default

    Returns
    -------
    a string of windows build
    """
    if is_full_build:
        raw_build = registry("HKLM\\Software\\Microsoft"
                             "\\Windows NT\\CurrentVersion",
                             "BuildLabEx")
    else:
        raw_build = registry("HKLM\\Software\\Microsoft"
                             "\\Windows NT\\CurrentVersion",
                             "CurrentBuild")
    return raw_build
def get_windows_install_date(friendly_output=False)

Get the install date of the current installed build of Windows

Parameters

friendly_output : bool
whether the value returned is readable or not, false by default

Returns

A integer of unix timestamp or a string of readable time
 
Expand source code
def get_windows_install_date(friendly_output=False):
    """
    Get the install date of the current installed build of Windows

    Parameters
    ----------
    friendly_output: bool
        whether the value returned is readable or not, `false` by default

    Returns
    -------
    A integer of unix timestamp or a string of readable time
    """
    raw_time = registry("HKLM\\Software\\Microsoft"
                        "\\Windows NT\\CurrentVersion", "InstallDate")
    dec_time = int(raw_time, 0)

    if friendly_output:
        from datetime import datetime
        f_time = \
            datetime.utcfromtimestamp(dec_time).strftime('%Y-%m-%d %H:%M:%S')
        return f_time
    else:
        return dec_time
def get_windows_locale()

Get Windows Locale

Returns

a string of Windows Locale
 
Expand source code
def get_windows_locale():
    """
    Get Windows Locale

    Returns
    -------
    a string of Windows Locale
    """
    p = winps("(Get-Culture).Name")
    if p.returncode:
        raise Exception("Failed to get Windows Locale: ", p.stderr)
    win_locale = p.stdout.rstrip().replace("-", "_")
    return win_locale
def get_windows_theme()

Get Windows Theme

Returns

a string of either "light" or "dark"
 
Expand source code
def get_windows_theme():
    """
    Get Windows Theme

    Returns
    -------
    a string of either "light" or "dark"
    """
    raw_theme = registry("HKCU\\SOFTWARE\\Microsoft\\Windows\\Current"
                         "Version\\Themes\\Personalize", "AppsUseLightTheme")
    return "dark" if int(raw_theme, 0) else "light"
def get_windows_uptime()

Get the current uptime in Windows

Returns

a array in the format of [days, hours, minutes]
 
Expand source code
def get_windows_uptime():
    """
    Get the current uptime in Windows

    Returns
    -------
    a array in the format of [days, hours, minutes]
    """
    p = winps("[int64]((get-date) - (gcim Win32_Operating"
              "System).LastBootUpTime).TotalSeconds")
    if p.returncode:
        return -1
    raw_time = int(p.stdout.rstrip())
    raw_days = raw_time // 86400
    raw_hours = raw_time // 3600 % 24
    raw_minutes = raw_time // 60 % 60
    return [raw_days, raw_hours, raw_minutes]
def registry(input, key)

Given a valid registry path, retrieves the value of an entry in the registry.

Parameters

input : str
string of a shell environment variable key.
key : str
the name of the registry's key in string

Returns

The corresponding value as a string
 

Raises

Returns the error from Reg.exe
 
Expand source code
def registry(input, key):
    """
    Given a valid registry path, retrieves the value of an entry in the
    registry.

    Parameters
    ----------
    input : str
        string of a shell environment variable key.

    key : str
        the name of the registry's key in string

    Returns
    -------
    The corresponding value as a string

    Raises
    ------
    Returns the error from Reg.exe
    """
    cmd = ["reg.exe", "query", input, "/v", key]
    p = __exec_command__(cmd)
    if p.returncode != 0:
        raise RuntimeError("The following error propagated from Registry: {}"
                           .format(p.stderr))

    query = p.stdout.rstrip()
    import re
    query = re.sub(r"^\r\n.+\r\n", "", query).lstrip()
    query = re.sub(key, "", query).lstrip()
    query = re.sub(r"REG_(SZ|MULTI_SZ|EXPAND_SZ|DWORD|BINARY|NONE)",
                   "", query).lstrip()
    return query