Example #1
0
def attributes(obj, ignore=None):
    if ignore is None:
        attrs = tuple(map(to_str, obj))
    else:
        ignored = ignore if isiterable(ignore) else (ignore,)
        attrs = (to_str(x) for x in obj if x not in ignored)
    return attrs
Example #2
0
def is_running_process(pid=None):
    ctime = None
    if isiterable(pid):
        pid, ctime = pid

    psp = _get_psutil_process(pid, ctime)
    return psp.is_running() and psp.create_time() == ctime
Example #3
0
def renice(pid=None, niceness=None):
    """Unix notation process nicener."""
    ctime = None
    value = None

    if os.name == 'nt':
        MIN_NICENESS = -20
        MAX_NICENESS = 19

        normval = min(MAX_NICENESS, niceness) if niceness else max(
            MIN_NICENESS, niceness)
        priocls = [psutil.IDLE_PRIORITY_CLASS,
                   psutil.BELOW_NORMAL_PRIORITY_CLASS,
                   psutil.NORMAL_PRIORITY_CLASS,
                   psutil.ABOVE_NORMAL_PRIORITY_CLASS,
                   psutil.HIGH_PRIORITY_CLASS,
                   psutil.REALTIME_PRIORITY_CLASS]
        prioval = (normval - MAX_NICENESS) * \
            (len(priocls) - 1) // (MIN_NICENESS - MAX_NICENESS)
        value = priocls[prioval]

    if isiterable(pid):
        pid, ctime = pid

    psp = _get_psutil_process(pid, ctime)
    psp.nice(value)
Example #4
0
def safeurlencode(data):
    if isiterable(data):
        res = urlencode(
            dict((to_bytes(x), to_bytes(y)) for x, y in dict(data).items()))
    else:
        res = urlencode(to_bytes(data))
    return res
Example #5
0
def attributes(obj, ignore=None):
    if ignore is None:
        attrs = tuple(map(to_str, obj))
    else:
        ignored = ignore if isiterable(ignore) else (ignore, )
        attrs = (to_str(x) for x in obj if x not in ignored)
    return attrs
Example #6
0
def items(obj, ignore=None):
    if ignore is None:
        res = (f"{k}={v}" for k, v in obj.items())
    else:
        ignored = ignore if isiterable(ignore) else (ignore, )
        res = (f"{k}={v}" for k, v in obj.items() if k not in ignored)
    return res
Example #7
0
def is_running_process(pid=None):
    ctime = None
    if isiterable(pid):
        pid, ctime = pid

    psp = _get_psutil_process(pid, ctime)
    return psp.is_running() and psp.create_time() == ctime
Example #8
0
def safeurlencode(data):
    if isiterable(data):
        res = urlencode(
            dict((to_bytes(x), to_bytes(y)) for x, y in dict(data).items()))
    else:
        res = urlencode(to_bytes(data))
    return res
Example #9
0
def renice(pid=None, niceness=None):
    """Unix notation process nicener."""
    ctime = None
    value = None

    if os.name == "nt":
        MIN_NICENESS = -20
        MAX_NICENESS = 19

        normval = (min(MAX_NICENESS, niceness) if niceness else max(
            MIN_NICENESS, niceness))
        priocls = [
            psutil.IDLE_PRIORITY_CLASS,
            psutil.BELOW_NORMAL_PRIORITY_CLASS,
            psutil.NORMAL_PRIORITY_CLASS,
            psutil.ABOVE_NORMAL_PRIORITY_CLASS,
            psutil.HIGH_PRIORITY_CLASS,
            psutil.REALTIME_PRIORITY_CLASS,
        ]
        prioval = ((normval - MAX_NICENESS) * (len(priocls) - 1) //
                   (MIN_NICENESS - MAX_NICENESS))
        value = priocls[prioval]

    if isiterable(pid):
        pid, ctime = pid

    psp = _get_psutil_process(pid, ctime)
    psp.nice(value)
Example #10
0
def items(obj, ignore=None):
    if ignore is None:
        res = ('{0}={1}'.format(k, v) for k, v in obj.items())
    else:
        ignored = ignore if isiterable(ignore) else (ignore, )
        res = ('{0}={1}'.format(k, v) for k, v in obj.items()
               if k not in ignored)
    return res
Example #11
0
def items(obj, ignore=None):
    if ignore is None:
        res = ('{0}={1}'.format(k, v) for k, v in obj.items())
    else:
        ignored = ignore if isiterable(ignore) else (ignore,)
        res = ('{0}={1}'.format(k, v) for k, v in obj.items()
               if k not in ignored)
    return res
Example #12
0
def is_zombie_process(pid=None):
    ctime = None
    if isiterable(pid):
        pid, ctime = pid

    try:
        psp = _get_psutil_process(pid, ctime)
        flag = psp.status() is psutil.STATUS_ZOMBIE
    except psutil.ZombieProcess:
        flag = True
    return flag
Example #13
0
def ionice(pid=None, ioclass=None, niceness=None):
    """Unix notation process I/O nicener."""
    if os.name == "nt":
        ioclass = {0: 2, 1: 2, 2: 2, 3: 0}[ioclass]

    ctime = None
    if isiterable(pid):
        pid, ctime = pid

    psp = _get_psutil_process(pid, ctime)
    psp.ionice(ioclass, niceness)
Example #14
0
def kill_process(pid=None, timeout=None):
    ctime = None
    if isiterable(pid):
        pid, ctime = pid

    try:
        psp = _get_psutil_process(pid, ctime)
        psp.terminate()
        psp.wait(timeout)
    except (psutil.TimeoutExpired, psutil.ZombieProcess):
        psp.kill()
Example #15
0
def kill_process(pid=None, timeout=None):
    ctime = None
    if isiterable(pid):
        pid, ctime = pid

    try:
        psp = _get_psutil_process(pid, ctime)
        psp.terminate()
        psp.wait(timeout)
    except (psutil.TimeoutExpired, psutil.ZombieProcess):
        psp.kill()
Example #16
0
def is_zombie_process(pid=None):
    ctime = None
    if isiterable(pid):
        pid, ctime = pid

    try:
        psp = _get_psutil_process(pid, ctime)
        flag = psp.status() is psutil.STATUS_ZOMBIE
    except psutil.ZombieProcess:
        flag = True
    return flag
Example #17
0
def ionice(pid=None, ioclass=None, niceness=None):
    """Unix notation process I/O nicener."""
    if os.name == 'nt':
        ioclass = {0: 2, 1: 2, 2: 2, 3: 0}[ioclass]

    ctime = None
    if isiterable(pid):
        pid, ctime = pid

    psp = _get_psutil_process(pid, ctime)
    psp.ionice(ioclass, niceness)
Example #18
0
def to_list(obj):
    """Convert value to a list with value inside."""
    if isinstance(obj, list):
        pass
    elif ismapping(obj):
        return list(obj.items())
    elif isiterable(obj, strict=False):
        return list(obj)
    elif obj is not None:
        return [obj]
    else:
        return list(obj)
Example #19
0
def to_list(obj):
    """Convert value to a list with value inside."""
    if isinstance(obj, list):
        pass
    elif is_mapping(obj):
        return list(obj.items())
    elif isiterable(obj, strict=False):
        return list(obj)
    elif obj is not None:
        return [obj]
    else:
        return list(obj)
Example #20
0
def _same_inputs(taginputs, inputs):
    for key, value in inputs.items():
        if key not in taginputs:
            return False
        tagvalue = taginputs[key]
        if hasattr(value, 'search') and re.match(value, tagvalue):
            continue
        elif isiterable(value) and tagvalue in value:
            continue
        elif tagvalue == value:
            continue
        return False
    return True
Example #21
0
def _same_inputs(taginputs, inputs):
    for key, value in inputs.items():
        if key not in taginputs:
            return False
        tagvalue = taginputs[key]
        if hasattr(value, 'search') and re.match(value, tagvalue):
            continue
        elif isiterable(value) and tagvalue in value:
            continue
        elif tagvalue == value:
            continue
        return False
    return True
Example #22
0
def convert(obj, rule, func, args=(), kwargs=None, fallback=None):
    if kwargs is None:
        kwargs = {}
    res = None
    cvargs = (rule, func, args, kwargs, fallback)
    try:
        if rule(obj):
            res = func(obj, *args, **kwargs)
        elif ismapping(obj):
            res = dict((convert(k, *cvargs), convert(v, *cvargs))
                       for k, v in obj.items())
        elif isiterable(obj):
            res = type(obj)(convert(i, *cvargs) for i in obj)
        else:
            res = obj
    except Exception as exc:
        if callable(fallback):
            fbargs = cvargs[:-1] + (exc,)
            return fallback(obj, *fbargs)
        raise
    return res
Example #23
0
def convert(obj, rule, func, args=(), kwargs=None, fallback=None):
    if kwargs is None:
        kwargs = {}
    res = None
    cvargs = (rule, func, args, kwargs, fallback)
    try:
        if rule(obj):
            res = func(obj, *args, **kwargs)
        elif ismapping(obj):
            res = dict((convert(k, *cvargs), convert(v, *cvargs))
                       for k, v in obj.items())
        elif isiterable(obj):
            res = type(obj)(convert(i, *cvargs) for i in obj)
        else:
            res = obj
    except Exception as exc:
        if callable(fallback):
            fbargs = cvargs[:-1] + (exc, )
            return fallback(obj, *fbargs)
        raise
    return res
Example #24
0
 def _to_filevalue(self, value):
     return ','.join(map(to_str, value)) if isiterable(value) else value
Example #25
0
 def _to_filevalue(self, value):
     return ','.join(map(to_str, value)) if isiterable(value) else value
Example #26
0
 def _to_filevalue(self, value):
     return ','.join([str(v)
                      for v in value]) if isiterable(value) else value
Example #27
0
class ConfigOption(object):

    __slots__ = [
        'allowed_values', 'default', 'desc', 'label', 'parser', 'type', 'value'
    ]

    DEFAULT_TYPE = InputType.Str

    _convert_map = {
        InputType.NA:
        lambda x: x,
        InputType.Str:
        lambda x: "" if x is None else str(x),
        InputType.Int:
        lambda x: 0 if x is None else int(x),
        InputType.File:
        lambda x: "" if x is None else fullpath(x),
        InputType.Folder:
        lambda x: "" if x is None else os.path.dirname(fullpath(x)),
        InputType.Password:
        lambda x: "" if x is None else str(x),
        InputType.Bool:
        lambda x: boolean(x) if isinstance(x, str) else bool(x),
        InputType.Float:
        lambda x: 0.0 if x is None else float(x),
        InputType.Tristate:
        lambda x: x if x is None else bool(x),
        InputType.Octal:
        lambda x: 0 if x is None else oct(x),
        InputType.Size:
        lambda x: 0 if x is None else (-1 if str(x) == '-1' else bytesize(x)),
        InputType.Address:
        lambda x: (None, None)
        if x is None else (endpoint if isendpoint(x) else socket)(x),
        InputType.Bytes:
        lambda x: b"" if x is None else bytes(x),
        InputType.StrList:
        lambda l: [str(x) for x in l] if isiterable(l) else entries(l)
    }

    def __init__(self,
                 parser,
                 value,
                 label=None,
                 desc=None,
                 allowed_values=None,
                 input_type=None):
        self.parser = parser

        self.type = None
        self.value = None
        self.default = None
        self.label = None
        self.desc = None
        self.allowed_values = ()

        self._set_type(input_type)
        self._set_value(value)
        self._set_allowed(allowed_values)
        self._set_info(label, desc)

    def _set_info(self, label, desc):
        self.label = "" if label is None else str(label)
        self.desc = "" if desc is None else str(desc)

    def _set_type(self, input_type):
        if not input_type:
            input_type = self.DEFAULT_TYPE
        if input_type not in InputType:
            raise InvalidValueError(input_type)
        self.type = input_type

    def _set_value(self, value):
        self.value = self.default = self._normalize_value(value)

    def _set_allowed(self, allowed):
        if not allowed:
            self.allowed_values = ()
            return

        self.allowed_values = tuple(self._normalize_value(v) for v in allowed)

    def _normalize_value(self, value):
        return self._convert_map[self.type](value)

    def reset(self):
        self.value = self.default

    def get(self):
        return self.value

    def get_default(self):
        return self.default

    def set(self, value, store=True):
        norm_value = self._normalize_value(value)
        if self.allowed_values and norm_value not in self.allowed_values:
            raise InvalidValueError(value)
        if self.value == norm_value:
            return None
        self.value = norm_value
        if store:
            self.parser.store()