Module wslpy.system

Provides users with the access for Windows system information like system vatiables and registry keys.

Expand source code
"""
Provides users with the access for Windows system
information like system vatiables and registry keys.
"""
import re
from .__core__.access import __exec_command__
from wslpy.exec import winps
from .__core__.check import wsl_version, detect_distro


def list_shellenv():
    """
    List avaiable shell environment variables to use and
    its corresponding path.

    Returns
    -------
    A Dictionary of registry keys and its corresbonding values.
    """
    cmd = ["reg.exe", "query", "HKCU\\Software\\Microsoft\\Windows"
           "\\CurrentVersion\\Explorer\\User Shell Folders", "/s"]
    p = __exec_command__(cmd)
    if p.returncode != 0:
        raise RuntimeError("Failed to get shell environment variables: ",
                           p.stderr)
    routput = p.stdout
    # Clean output first to toutput
    toutput = re.sub((r"\r\nHKEY_CURRENT_USER\\Software\\Microsoft\\"
                      r"Windows\\CurrentVersion\\Explorer\\User Shell"
                      r" Folders\r\n"), '', routput)
    toutput = re.sub(r'(REG_EXPAND_SZ|\r|\n)', '', toutput)
    toutput = re.sub(r'(REG_SZ|\r|\n)', '', toutput)
    # split toutput into list with aoutput
    aoutput = (re.split(r'\s\s+', toutput))[1:]
    # convert aoutput to dictionary
    output = dict(zip(aoutput[::2], aoutput[1::2]))
    return output


def get_shellenv(input):
    """
    Get the value from shell environment variable.

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

    Returns
    -------
    A string of the corresbonding value from the input.

    Raises
    ------
    KeyError
        An error occured when you input a empty value or the registry key
        cannot be found in registry.
    """
    try:
        shl_list = list_shellenv()
        if input in shl_list.keys():
            return shl_list[input]
        else:
            raise KeyError("Key does not exist.")
    except KeyError as err:
        print(err)


def list_sysenv():
    """
    List available system environment variables to use and
    its corresponding path.

    Returns
    -------
    A Dictionary of system environment variables keys and its corresponding
    values.
    """
    p = winps("Get-ChildItem env:")
    if p.returncode != 0:
        raise RuntimeError("Failed to get system environment variables: ",
                           p.stderr)
    routput = p.stdout
    toutput = re.sub(r'(Name|Value|----|-|-----|\r|\n)',
                     '', routput)
    aoutput = (re.split(r'\s\s+', toutput))[1:]
    # convert aoutput to dictionary
    output = dict(zip(aoutput[::2], aoutput[1::2]))
    return output


def get_sysenv(input):
    """
    Get the value from shell environment variable.

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

    Returns
    -------
    A string of the corresbonding value from the input.

    Raises
    ------
    KeyError
        An error occured when you input a empty value or the registry key
        cannot be found in registry.
    """
    try:
        sys_var_list = list_sysenv()
        if input in sys_var_list.keys():
            return sys_var_list[input]
        else:
            raise KeyError("Key does not exist.")
    except KeyError as err:
        print(err)


def get_ip():
    """
    Get the current IPv4 address from your WSL2 instance

    Returns
    -------
    A string of IPv4 address
    """
    import netifaces as ni
    return ni.ifaddresses("eth0")[ni.AF_INET][0]['addr']


__all__ = [
    "list_shellenv",
    "get_shellenv",
    "list_sysenv",
    "get_sysenv",
    "wsl_version",
    "detect_distro",
    "get_ip",
    ]

Functions

def detect_distro()

Reads the /etc/os-release file nad tries to infer the OS form available attributes.

Returns

Name of distribution.

Expand source code
def detect_distro():
    """
    Reads the /etc/os-release file nad tries to infer
    the OS form available attributes.

    Returns
    -------
    Name of distribution.
    """
    file = '/etc/os-release'
    distro = __read_attribute__(file, 'NAME=')
    if distro == 'Arch':
        return 'archlinux'
    elif distro == 'Scientific':
        return 'scilinux'
    elif distro == 'Fedora Remix for WSL':
        return 'fedoraremix'
    elif distro == 'Generic':
        os_id = __read_attribute__(file, 'ID_LIKE=')
        if os_id == 'fedora':
            return 'oldfedora'
        else:
            return 'unknown'

    return distro.lower()
def get_ip()

Get the current IPv4 address from your WSL2 instance

Returns

A string of IPv4 address
 
Expand source code
def get_ip():
    """
    Get the current IPv4 address from your WSL2 instance

    Returns
    -------
    A string of IPv4 address
    """
    import netifaces as ni
    return ni.ifaddresses("eth0")[ni.AF_INET][0]['addr']
def get_shellenv(input)

Get the value from shell environment variable.

Parameters

input : str
string of a shell environment variable key.

Returns

A string of the corresbonding value from the input.

Raises

KeyError
An error occured when you input a empty value or the registry key cannot be found in registry.
Expand source code
def get_shellenv(input):
    """
    Get the value from shell environment variable.

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

    Returns
    -------
    A string of the corresbonding value from the input.

    Raises
    ------
    KeyError
        An error occured when you input a empty value or the registry key
        cannot be found in registry.
    """
    try:
        shl_list = list_shellenv()
        if input in shl_list.keys():
            return shl_list[input]
        else:
            raise KeyError("Key does not exist.")
    except KeyError as err:
        print(err)
def get_sysenv(input)

Get the value from shell environment variable.

Parameters

input : str
string of a shell environment variable key.

Returns

A string of the corresbonding value from the input.

Raises

KeyError
An error occured when you input a empty value or the registry key cannot be found in registry.
Expand source code
def get_sysenv(input):
    """
    Get the value from shell environment variable.

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

    Returns
    -------
    A string of the corresbonding value from the input.

    Raises
    ------
    KeyError
        An error occured when you input a empty value or the registry key
        cannot be found in registry.
    """
    try:
        sys_var_list = list_sysenv()
        if input in sys_var_list.keys():
            return sys_var_list[input]
        else:
            raise KeyError("Key does not exist.")
    except KeyError as err:
        print(err)
def list_shellenv()

List avaiable shell environment variables to use and its corresponding path.

Returns

A Dictionary of registry keys and its corresbonding values.

Expand source code
def list_shellenv():
    """
    List avaiable shell environment variables to use and
    its corresponding path.

    Returns
    -------
    A Dictionary of registry keys and its corresbonding values.
    """
    cmd = ["reg.exe", "query", "HKCU\\Software\\Microsoft\\Windows"
           "\\CurrentVersion\\Explorer\\User Shell Folders", "/s"]
    p = __exec_command__(cmd)
    if p.returncode != 0:
        raise RuntimeError("Failed to get shell environment variables: ",
                           p.stderr)
    routput = p.stdout
    # Clean output first to toutput
    toutput = re.sub((r"\r\nHKEY_CURRENT_USER\\Software\\Microsoft\\"
                      r"Windows\\CurrentVersion\\Explorer\\User Shell"
                      r" Folders\r\n"), '', routput)
    toutput = re.sub(r'(REG_EXPAND_SZ|\r|\n)', '', toutput)
    toutput = re.sub(r'(REG_SZ|\r|\n)', '', toutput)
    # split toutput into list with aoutput
    aoutput = (re.split(r'\s\s+', toutput))[1:]
    # convert aoutput to dictionary
    output = dict(zip(aoutput[::2], aoutput[1::2]))
    return output
def list_sysenv()

List available system environment variables to use and its corresponding path.

Returns

A Dictionary of system environment variables keys and its corresponding
 

values.

Expand source code
def list_sysenv():
    """
    List available system environment variables to use and
    its corresponding path.

    Returns
    -------
    A Dictionary of system environment variables keys and its corresponding
    values.
    """
    p = winps("Get-ChildItem env:")
    if p.returncode != 0:
        raise RuntimeError("Failed to get system environment variables: ",
                           p.stderr)
    routput = p.stdout
    toutput = re.sub(r'(Name|Value|----|-|-----|\r|\n)',
                     '', routput)
    aoutput = (re.split(r'\s\s+', toutput))[1:]
    # convert aoutput to dictionary
    output = dict(zip(aoutput[::2], aoutput[1::2]))
    return output
def wsl_version()

This function returns the version of WSL.

Returns

The version of WSL.

Expand source code
def wsl_version():
    """
    This function returns the version of WSL.

    Returns
    -------
    The version of WSL.
    """
    if is_interop_enabled():
        info = distro_info()
        flag = int(info['Flags']['value'], 0)
        if flag // 8 == 1:
            return 2
        else:
            return 1
    else:
        import re
        from platform import release
        rel = release()
        if re.match(r'^4\.\d\.\d-\d{5}-Microsoft', rel) is not None:
            return 1
        else:
            return 2