Mini Shell

Direktori : /opt/imh-python/lib/python3.9/site-packages/fpmstatus/
Upload File :
Current File : //opt/imh-python/lib/python3.9/site-packages/fpmstatus/util.py

# vim: set ts=4 sw=4 expandtab syntax=python:
"""

fpmstatus.util
Utility Functions

@author J. Hipps <jacobh@inmotionhosting.com>

"""

import re
import json
import logging

logger = logging.getLogger('fpmstatus')


def format_uptime(usec):
    """
    Format uptime @usec as duration string
    """
    oday = int(usec / 86400)
    ohour = int((usec - (oday * 86400)) / 3600)
    omin = int((usec - (oday * 86400) - (ohour * 3600)) / 60)
    osec = float(usec) - float(oday * 86400) - float(ohour * 3600) - float(omin * 60)
    ostr = "{:01d}d {:02d}:{:02d}:{:02.02f}".format(oday, ohour, omin, osec)
    return ostr

def format_size(insize, rate=False, bits=False):
    """
    format human-readable file size and xfer rates
    """
    onx = float(abs(insize))
    for u in ['B', 'K', 'M', 'G', 'T', 'P']:
        if onx < 1024.0:
            tunit = u
            break
        onx /= 1024.0
    suffix = ""
    if tunit != 'B': suffix = "iB"
    if rate:
        if bits:
            suffix = "bps"
            onx *= 8.0
        else:
            suffix += "/sec"
    if tunit == 'B':
        ostr = "%3d %s%s" % (onx, tunit, suffix)
    else:
        ostr = "%3.01f %s%s" % (onx, tunit, suffix)
    return ostr

def mkpct(ival, tot):
    """
    Make value/percent string
    """
    try:
        po = "{:d} ({:3.01f}%)".format(ival, (float(ival) / float(tot)) * 100.0)
    except:
        po = "0 (-.-%)"
    return po

def strcolor(color, instr):
    """
    ANSI colorify a string @instr with @color (foreground)
    """
    cdex = {
            '': '\x1b[0m',
            'black': '\x1b[30m',
            'red': '\x1b[31m',
            'green': '\x1b[32m',
            'yellow': '\x1b[33m',
            'blue': '\x1b[34m',
            'magenta': '\x1b[35m',
            'cyan': '\x1b[36m',
            'white': '\x1b[37m',
           }
    return "{}{}{}".format(cdex.get(color.lower(), ''), instr, cdex[''])

def strip_colors(instr):
    """
    Strip ANSI color codes from input @str
    """
    return re.sub(r'\x1b\[[0-9;]*m', '', instr)

Zerion Mini Shell 1.0