Module libwine.wineprocess

Expand source code
from typing import NewType

from .exceptions import ProtectedProcess

Wine = NewType('Wine', object)


class WineProcess:
    '''
    Create a new object of type WineProcess with all the methods for its management.

    Parameters
    ----------
    pid : str
        the process id
    name: str
        the process name (command)
    parent_pid: str (optional)
        the parent process id
    wine: Wine
        the Wine object
    '''

    pid = int
    name = str
    parent_pid = str
    wine = Wine

    _protected = [
        "explorer.exe",
        "services.exe",
        "rpcss.exe",
        "svchost.exe",
        "winedevice.exe",
        "plugplay.exe"
        "winedbg.exe",
        "conhost.exe"
    ]

    def __init__(self, pid: str, name: str, wine: Wine, parent_pid: str = None):
        self.pid = self._pid(pid)
        self.name = name
        self.parent_pid = self._pid(parent_pid)
        self.wine = wine

    '''
    Data check and assignment
    '''

    def _pid(self, pid: str):
        '''
        Validate the process ID.

        Parameters
        ----------
        pid : str
            the process ID as string

        Return
        ----------
        str:
            a valid process ID as hex
        '''
        if pid is not None:
            return f"0x{pid}"

    def _cpu_usage(self, cpu: str):
        '''
        Get CPU usage as percentage.

        Parameters
        ----------
        cpu : str
            the CPU percentage used by the process as string

        Return
        ----------
        int:
            a valid CPU percentage usage as integer (100 = 1)
        '''
        # TODO: calculate cpu percentage
        return int(cpu)

    def _memory_usage(self, memory: str):
        '''
        Get memory usage as percentage.

        Parameters
        ----------
        memory : str
            the memory percentage used by the process as string

        Return
        ----------
        int:
            a valid memory percentage usage as integer (100 = 1)
        '''
        # TODO: calculate memory percentage
        return int(memory)

    '''
    Process management
    '''

    def kill(self):
        '''
        Kill the process.
        '''
        if self.name not in self._protected:
            command = f"winedbg << END_OF_INPUTS\n\
                attach {self.pid}\n\
                kill\n\
                quit\n\
                END_OF_INPUTS"
            self.wine.execute(command=command, comunicate=True)
        else:
            raise ProtectedProcess(self.name)

    def update(self):
        '''
        Update process status/data.
        '''
        # TODO: update process info by pid
        return

Classes

class WineProcess (pid: str, name: str, wine: Wine, parent_pid: str = None)

Create a new object of type WineProcess with all the methods for its management.

Parameters

pid : str
the process id
name : str
the process name (command)
parent_pid : str (optional)
the parent process id
wine : Wine
the Wine object
Expand source code
class WineProcess:
    '''
    Create a new object of type WineProcess with all the methods for its management.

    Parameters
    ----------
    pid : str
        the process id
    name: str
        the process name (command)
    parent_pid: str (optional)
        the parent process id
    wine: Wine
        the Wine object
    '''

    pid = int
    name = str
    parent_pid = str
    wine = Wine

    _protected = [
        "explorer.exe",
        "services.exe",
        "rpcss.exe",
        "svchost.exe",
        "winedevice.exe",
        "plugplay.exe"
        "winedbg.exe",
        "conhost.exe"
    ]

    def __init__(self, pid: str, name: str, wine: Wine, parent_pid: str = None):
        self.pid = self._pid(pid)
        self.name = name
        self.parent_pid = self._pid(parent_pid)
        self.wine = wine

    '''
    Data check and assignment
    '''

    def _pid(self, pid: str):
        '''
        Validate the process ID.

        Parameters
        ----------
        pid : str
            the process ID as string

        Return
        ----------
        str:
            a valid process ID as hex
        '''
        if pid is not None:
            return f"0x{pid}"

    def _cpu_usage(self, cpu: str):
        '''
        Get CPU usage as percentage.

        Parameters
        ----------
        cpu : str
            the CPU percentage used by the process as string

        Return
        ----------
        int:
            a valid CPU percentage usage as integer (100 = 1)
        '''
        # TODO: calculate cpu percentage
        return int(cpu)

    def _memory_usage(self, memory: str):
        '''
        Get memory usage as percentage.

        Parameters
        ----------
        memory : str
            the memory percentage used by the process as string

        Return
        ----------
        int:
            a valid memory percentage usage as integer (100 = 1)
        '''
        # TODO: calculate memory percentage
        return int(memory)

    '''
    Process management
    '''

    def kill(self):
        '''
        Kill the process.
        '''
        if self.name not in self._protected:
            command = f"winedbg << END_OF_INPUTS\n\
                attach {self.pid}\n\
                kill\n\
                quit\n\
                END_OF_INPUTS"
            self.wine.execute(command=command, comunicate=True)
        else:
            raise ProtectedProcess(self.name)

    def update(self):
        '''
        Update process status/data.
        '''
        # TODO: update process info by pid
        return

Class variables

var name

str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.

var parent_pid

str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.

var pid

int([x]) -> integer int(x, base=10) -> integer

Convert a number or string to an integer, or return 0 if no arguments are given. If x is a number, return x.int(). For floating point numbers, this truncates towards zero.

If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in the given base. The literal can be preceded by '+' or '-' and be surrounded by whitespace. The base defaults to 10. Valid bases are 0 and 2-36. Base 0 means to interpret the base from the string as an integer literal.

>>> int('0b100', base=0)
4

Methods

def kill(self)

Kill the process.

Expand source code
def kill(self):
    '''
    Kill the process.
    '''
    if self.name not in self._protected:
        command = f"winedbg << END_OF_INPUTS\n\
            attach {self.pid}\n\
            kill\n\
            quit\n\
            END_OF_INPUTS"
        self.wine.execute(command=command, comunicate=True)
    else:
        raise ProtectedProcess(self.name)
def update(self)

Update process status/data.

Expand source code
def update(self):
    '''
    Update process status/data.
    '''
    # TODO: update process info by pid
    return
def wine(x)
Expand source code
def new_type(x):
    return x