2013-05-16 00:14:38 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#-*- coding: utf-8 -*-
|
2013-05-06 02:16:30 +00:00
|
|
|
# psr/sys.py
|
|
|
|
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
2013-04-20 11:36:49 +00:00
|
|
|
#
|
2013-05-06 02:16:30 +00:00
|
|
|
# software : Kirmah <http://kirmah.sourceforge.net/>
|
2013-05-15 18:51:08 +00:00
|
|
|
# version : 2.18
|
2014-07-17 22:19:51 +00:00
|
|
|
# date : 2014
|
2013-05-06 02:16:30 +00:00
|
|
|
# licence : GPLv3.0 <http://www.gnu.org/licenses/>
|
|
|
|
# author : a-Sansara <[a-sansara]at[clochardprod]dot[net]>
|
|
|
|
# copyright : pluie.org <http://www.pluie.org/>
|
2013-04-20 11:36:49 +00:00
|
|
|
#
|
2013-05-06 02:16:30 +00:00
|
|
|
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
2013-04-20 11:36:49 +00:00
|
|
|
#
|
2013-05-06 02:16:30 +00:00
|
|
|
# This file is part of Kirmah.
|
|
|
|
#
|
|
|
|
# Kirmah is free software (free as in speech) : you can redistribute it
|
|
|
|
# and/or modify it under the terms of the GNU General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the License,
|
|
|
|
# or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Kirmah is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
# more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Kirmah. If not, see <http://www.gnu.org/licenses/>.
|
2013-04-20 11:36:49 +00:00
|
|
|
#
|
|
|
|
|
2013-05-06 02:16:30 +00:00
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
# ~~ module sys ~~
|
2013-04-20 11:36:49 +00:00
|
|
|
|
2013-05-06 02:16:30 +00:00
|
|
|
from psr.io import Io
|
|
|
|
from psr.const import Const
|
2014-07-16 23:55:55 +00:00
|
|
|
from threading import RLock, current_thread
|
2013-05-07 00:43:02 +00:00
|
|
|
from multiprocessing import Event
|
2013-05-06 02:16:30 +00:00
|
|
|
from queue import Queue
|
|
|
|
|
2014-07-16 23:55:55 +00:00
|
|
|
def init(name, debug, remote=False, color=True, loglvl=Const.LOG_NEVER):
|
2013-05-06 02:16:30 +00:00
|
|
|
Sys.g_init(name, debug, remote, color, loglvl)
|
|
|
|
Sys.g_set_main_proc(remote)
|
2013-04-20 11:36:49 +00:00
|
|
|
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
# ~~ class Sys ~~
|
|
|
|
|
|
|
|
class Sys:
|
|
|
|
""""""
|
|
|
|
|
2013-05-06 02:16:30 +00:00
|
|
|
from platform import system as getSysName
|
2014-07-16 23:32:38 +00:00
|
|
|
from os import system as sysCall, remove as removeFile, makedirs, sep, getpid, listdir
|
2013-05-06 02:16:30 +00:00
|
|
|
from getpass import getuser as getUserLogin
|
|
|
|
from time import strftime, mktime, time, localtime, sleep
|
2014-07-16 23:32:38 +00:00
|
|
|
from datetime import datetime, timedelta
|
2014-07-16 23:55:55 +00:00
|
|
|
from sys import exit, stdout, executable
|
2013-05-06 02:16:30 +00:00
|
|
|
from os.path import abspath, dirname, join, realpath, basename, getsize, isdir, splitext
|
|
|
|
from math import log, floor, ceil
|
|
|
|
from _thread import exit as thread_exit
|
|
|
|
|
2013-04-20 11:36:49 +00:00
|
|
|
import builtins as g
|
2013-05-06 02:16:30 +00:00
|
|
|
|
|
|
|
g.DEBUG = False
|
|
|
|
g.LOG_LEVEL = Const.LOG_DEFAULT
|
|
|
|
g.LOG_TIME = False
|
|
|
|
g.LOG_LIM_ARG_LENGTH = Const.LOG_LIM_ARG_LENGTH
|
|
|
|
g.QUIET = False
|
2013-05-07 00:43:02 +00:00
|
|
|
g.COLOR_MODE = True
|
|
|
|
g.RLOCK = None
|
|
|
|
g.MPRLOCK = None
|
2013-05-06 02:16:30 +00:00
|
|
|
g.WPIPE = None
|
|
|
|
g.THREAD_CLI = None
|
|
|
|
g.UI_AUTO_SCROLL = True
|
|
|
|
g.CPID = None
|
|
|
|
g.SIGNAL_STOP = 0
|
|
|
|
g.SIGNAL_START = 1
|
|
|
|
g.SIGNAL_RUN = 2
|
2014-07-16 23:55:55 +00:00
|
|
|
g.SIGNAL_CLEAR = 3
|
2013-05-06 02:16:30 +00:00
|
|
|
g.GUI = False
|
|
|
|
g.GUI_PRINT_STDOUT = True
|
2013-05-07 00:43:02 +00:00
|
|
|
g.MPEVENT = Event()
|
2013-05-15 18:47:12 +00:00
|
|
|
g.LOG_QUEUE = None
|
2013-05-06 02:16:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def g_init(prjName, debug=True, remote=False, color=True, loglvl=Const.LOG_DEFAULT):
|
2013-04-20 11:36:49 +00:00
|
|
|
""""""
|
2013-05-06 02:16:30 +00:00
|
|
|
Sys.g.PRJ_NAME = prjName
|
|
|
|
Sys.g.DEBUG = debug
|
|
|
|
Sys.g.COLOR_MODE = color
|
|
|
|
Sys.g.LOG_LEVEL = loglvl
|
|
|
|
Sys.g.LOG_TIME = True
|
|
|
|
Sys.g.MAIN_PROC = None
|
|
|
|
Sys.g.RLOCK = RLock()
|
|
|
|
Sys.g.LOG_QUEUE = Queue() if Sys.g.GUI else None
|
|
|
|
|
2013-04-20 11:36:49 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2013-05-06 02:16:30 +00:00
|
|
|
def sendMainProcMsg(type, data):
|
|
|
|
""""""
|
|
|
|
if not Sys.g_is_main_proc() and Sys.g.WPIPE is not None and Sys.g.CPID is not None and type in range(4) :
|
|
|
|
Sys.g.WPIPE.send((Sys.g.CPID, type, data))
|
|
|
|
|
|
|
|
|
2013-04-20 11:36:49 +00:00
|
|
|
@staticmethod
|
2013-05-06 02:16:30 +00:00
|
|
|
def g_set_main_proc(ppid=None):
|
|
|
|
""""""
|
|
|
|
Sys.g.MAIN_PROC = Sys.getpid() if ppid is None or ppid is False else ppid
|
|
|
|
|
|
|
|
|
2013-04-20 11:36:49 +00:00
|
|
|
@staticmethod
|
|
|
|
def g_is_main_proc():
|
2013-05-06 02:16:30 +00:00
|
|
|
""""""
|
2013-04-20 11:36:49 +00:00
|
|
|
try :
|
2013-05-06 02:16:30 +00:00
|
|
|
return Sys.g.MAIN_PROC == Sys.getpid()
|
2013-04-20 11:36:49 +00:00
|
|
|
except :
|
2013-05-06 02:16:30 +00:00
|
|
|
return False
|
|
|
|
|
2013-04-20 11:36:49 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2013-05-06 02:16:30 +00:00
|
|
|
def g_has_ui_trace():
|
|
|
|
""""""
|
2013-04-20 11:36:49 +00:00
|
|
|
try:
|
2013-05-06 02:16:30 +00:00
|
|
|
return Sys.g.GUI and Sys.g.DEBUG
|
2013-04-20 11:36:49 +00:00
|
|
|
except Exception as e:
|
|
|
|
return False
|
|
|
|
|
2013-05-06 02:16:30 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def cli_emit_progress(value=0):
|
|
|
|
""""""
|
|
|
|
if Sys.g.THREAD_CLI is not None : Sys.g.THREAD_CLI.progress(value)
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
2013-05-07 00:43:02 +00:00
|
|
|
def is_cli_cancel(event=None):
|
2013-05-06 02:16:30 +00:00
|
|
|
""""""
|
2013-05-07 00:43:02 +00:00
|
|
|
c = Sys.g.THREAD_CLI is not None and Sys.g.THREAD_CLI.cancelled
|
|
|
|
return c or (event is not None and event.is_set())
|
|
|
|
|
2013-05-06 02:16:30 +00:00
|
|
|
|
2013-04-20 11:36:49 +00:00
|
|
|
@staticmethod
|
|
|
|
def isUnix():
|
|
|
|
""""""
|
|
|
|
return not Sys.getSysName() == 'Windows'
|
|
|
|
|
2013-05-06 02:16:30 +00:00
|
|
|
|
2013-04-20 11:36:49 +00:00
|
|
|
@staticmethod
|
|
|
|
def clear():
|
|
|
|
return Sys.sysCall('cls' if not Sys.isUnix() else 'clear')
|
|
|
|
|
2013-05-06 02:16:30 +00:00
|
|
|
|
2013-04-20 11:36:49 +00:00
|
|
|
@staticmethod
|
|
|
|
def mkdir_p(path):
|
|
|
|
""""""
|
|
|
|
try:
|
|
|
|
Sys.makedirs(path)
|
2013-05-06 02:16:30 +00:00
|
|
|
except OSError as e:
|
2013-04-20 11:36:49 +00:00
|
|
|
if e.errno == Io.EEXIST:
|
|
|
|
pass
|
|
|
|
else: raise
|
|
|
|
|
2013-05-06 02:16:30 +00:00
|
|
|
|
2013-04-20 11:36:49 +00:00
|
|
|
@staticmethod
|
|
|
|
def readableBytes(b, p=2):
|
|
|
|
"""Give a human representation of bytes size `b`
|
|
|
|
:Returns: `str`
|
|
|
|
"""
|
2014-07-16 23:32:38 +00:00
|
|
|
if b is None or b=='': return '0'
|
|
|
|
else :b = int(b)
|
2013-05-06 02:16:30 +00:00
|
|
|
units = [Const.UNIT_SHORT_B, Const.UNIT_SHORT_KIB, Const.UNIT_SHORT_MIB, Const.UNIT_SHORT_GIB, Const.UNIT_SHORT_TIB];
|
2013-04-20 11:36:49 +00:00
|
|
|
b = max(b,0);
|
|
|
|
if b == 0 : lb= 0
|
2013-05-06 02:16:30 +00:00
|
|
|
else : lb = Sys.log(b)
|
2013-04-20 11:36:49 +00:00
|
|
|
p = Sys.floor(lb/Sys.log(1024))
|
2013-05-06 02:16:30 +00:00
|
|
|
p = min(p, len(units)- 1)
|
2013-04-20 11:36:49 +00:00
|
|
|
#Uncomment one of the following alternatives
|
|
|
|
b /= pow(1024,p)
|
2013-05-06 02:16:30 +00:00
|
|
|
#b /= (1 << (10 * p))
|
|
|
|
return str(round(b, 1))+' '+units[p]
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def getFileExt(fromPath):
|
|
|
|
""""""
|
|
|
|
return Sys.splitext(fromPath)
|
|
|
|
|
|
|
|
|
2013-04-20 11:36:49 +00:00
|
|
|
@staticmethod
|
|
|
|
def getFileSize(path):
|
|
|
|
""""""
|
|
|
|
return Sys.readableBytes(Sys.getsize(path))
|
|
|
|
|
2013-05-06 02:16:30 +00:00
|
|
|
|
2013-04-20 11:36:49 +00:00
|
|
|
@staticmethod
|
|
|
|
def getPrintableBytes(bdata):
|
|
|
|
""""""
|
|
|
|
data = ''
|
|
|
|
if isinstance(bdata,bytes) :
|
|
|
|
try:
|
|
|
|
data = str(bdata, 'utf-8')
|
|
|
|
except Exception as e:
|
|
|
|
hexv = []
|
|
|
|
for i in bdata[1:] :
|
|
|
|
hexv.append(hex(i)[2:].rjust(2,'0'))
|
|
|
|
data = ' '.join(hexv)
|
|
|
|
pass
|
|
|
|
else :
|
|
|
|
data = bdata
|
|
|
|
return data
|
|
|
|
|
2013-05-06 02:16:30 +00:00
|
|
|
|
2013-04-20 11:36:49 +00:00
|
|
|
@staticmethod
|
|
|
|
def getHexaBytes(bdata):
|
|
|
|
""""""
|
|
|
|
data = ''
|
|
|
|
if isinstance(bdata,bytes) :
|
|
|
|
hexv = []
|
|
|
|
for i in bdata[1:] :
|
|
|
|
hexv.append(hex(i)[2:].rjust(2,'0'))
|
|
|
|
data = ' '.join(hexv)
|
|
|
|
else :
|
|
|
|
data = bdata
|
|
|
|
return data
|
|
|
|
|
2013-05-06 02:16:30 +00:00
|
|
|
|
2013-04-20 11:36:49 +00:00
|
|
|
@staticmethod
|
2013-05-06 02:16:30 +00:00
|
|
|
# never log this func -> maximum recursion
|
2013-05-07 00:43:02 +00:00
|
|
|
def wlog(data=[('','default')]):
|
2013-04-20 11:36:49 +00:00
|
|
|
""""""
|
2013-05-06 02:16:30 +00:00
|
|
|
if not Sys.is_cli_cancel():
|
|
|
|
if Sys.g.LOG_QUEUE is not None :
|
|
|
|
try :
|
2014-07-16 23:55:55 +00:00
|
|
|
Sys.g.LOG_QUEUE.put((current_thread().name,data))
|
2013-05-06 02:16:30 +00:00
|
|
|
Sys.cli_emit_progress()
|
|
|
|
except Exception as e:
|
|
|
|
Sys.pwarn((('wlog exception ',(str(e),Sys.CLZ_ERROR_PARAM), ' !'),), True)
|
2013-05-07 00:43:02 +00:00
|
|
|
|
2013-05-06 02:16:30 +00:00
|
|
|
else :
|
|
|
|
Sys.g.THREAD_CLI.stop()
|
|
|
|
|
|
|
|
|
2013-04-20 11:36:49 +00:00
|
|
|
@staticmethod
|
2014-07-16 23:55:55 +00:00
|
|
|
def pwlog(data, guiClear=False):
|
|
|
|
""" data=[('text', keycolor, newline)]"""
|
|
|
|
if guiClear : Sys.wlog(Sys.g.SIGNAL_CLEAR)
|
|
|
|
wd = []
|
|
|
|
for item in data :
|
|
|
|
nl = False if len(item)< 3 else (item[2]==1 or item[2]==True)
|
|
|
|
c = Const.CLZ_0 if (len(item)< 2 or item[1] not in Sys.clzdic) else item[1]
|
|
|
|
Sys.echo(item[0], Sys.clzdic[c], nl)
|
|
|
|
wd += [(item[0], c)]
|
|
|
|
if nl and Sys.g.GUI :
|
|
|
|
Sys.wlog(wd)
|
|
|
|
wd = []
|
|
|
|
if len(wd) > 0 and Sys.g.GUI :
|
|
|
|
Sys.wlog(wd)
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def echo(data, colors, endLF=True, endClz=True):
|
2013-04-20 11:36:49 +00:00
|
|
|
""""""
|
|
|
|
if isinstance(data,bytes) :
|
2013-05-06 02:16:30 +00:00
|
|
|
data = Sys.getPrintableBytes(data)
|
2013-04-20 11:36:49 +00:00
|
|
|
|
|
|
|
ev = '' if not endLF else Sys.Clz._LF
|
|
|
|
tokens = [c.lstrip(Sys.Clz._MARKER[0]).rstrip(Sys.Clz._SEP) for c in colors.split(Sys.Clz._MARKER) if c is not '']
|
|
|
|
if Sys.isUnix() :
|
|
|
|
if data is None: data = ''
|
|
|
|
if endClz : data += Sys.Clz._uOFF
|
|
|
|
if Sys.g.COLOR_MODE :
|
2013-05-06 02:16:30 +00:00
|
|
|
Sys.dprint(eval('Sys.Clz._u'+'+Sys.Clz._u'.join(tokens))+data,end=ev, dbcall=False)
|
2013-04-20 11:36:49 +00:00
|
|
|
else :
|
2013-05-06 02:16:30 +00:00
|
|
|
Sys.dprint(data,end=ev, dbcall=False)
|
2013-04-20 11:36:49 +00:00
|
|
|
else :
|
|
|
|
if Sys.g.COLOR_MODE : Sys.Clz.setColor(eval('Sys.Clz._w'+'|Sys.Clz._w'.join(tokens)))
|
2013-05-06 02:16:30 +00:00
|
|
|
Sys.dprint(data,end=ev, dbcall=False)
|
2014-07-16 23:55:55 +00:00
|
|
|
Sys.stdout.flush()
|
2013-04-20 11:36:49 +00:00
|
|
|
if endClz and Sys.g.COLOR_MODE : Sys.Clz.setColor(Sys.Clz._wOFF)
|
2013-05-06 02:16:30 +00:00
|
|
|
|
2013-04-20 11:36:49 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2013-05-06 02:16:30 +00:00
|
|
|
def dprint(d='',end=Const.LF, dbcall=False):
|
2013-04-20 11:36:49 +00:00
|
|
|
""""""
|
2014-07-16 23:32:38 +00:00
|
|
|
dbcall = Sys.g.QUIET
|
2013-05-06 02:16:30 +00:00
|
|
|
if not dbcall :
|
|
|
|
if not Sys.g.GUI or Sys.g.GUI_PRINT_STDOUT :
|
2013-05-07 00:43:02 +00:00
|
|
|
if Sys.g.RLOCK is not None :
|
|
|
|
with Sys.g.RLOCK :
|
2014-07-16 23:55:55 +00:00
|
|
|
if not Sys.g.QUIET :
|
|
|
|
print(d,end=end)
|
2013-05-07 00:43:02 +00:00
|
|
|
else :
|
|
|
|
if not Sys.g.QUIET :
|
|
|
|
print(d,end=end)
|
2013-05-06 02:16:30 +00:00
|
|
|
|
|
|
|
bdata = [(d,Const.CLZ_DEFAULT)]
|
|
|
|
return bdata
|
|
|
|
|
2013-04-20 11:36:49 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2013-05-06 02:16:30 +00:00
|
|
|
def eprint(d='', label=Const.WARN, dbcall=False):
|
2013-04-20 11:36:49 +00:00
|
|
|
""""""
|
2013-05-06 02:16:30 +00:00
|
|
|
c = Sys.CLZ_ERROR if label is Const.ERROR else Sys.CLZ_WARN
|
2014-07-16 23:55:55 +00:00
|
|
|
Sys.echo(' '+label+' : ', c, False, False)
|
|
|
|
Sys.echo(str(d)+' ', c, True, True)
|
2013-05-06 02:16:30 +00:00
|
|
|
|
|
|
|
bdata = [(label+' : ' , label),(str(d)+' ', label)]
|
|
|
|
return bdata
|
|
|
|
|
2013-04-20 11:36:49 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def pdate(t, dbcall = False):
|
|
|
|
""""""
|
|
|
|
t, s = Sys.strftime('%H:%M',t), Sys.strftime(':%S ',t)
|
2013-05-06 02:16:30 +00:00
|
|
|
if not dbcall :
|
2014-07-16 23:55:55 +00:00
|
|
|
Sys.echo(t , Sys.CLZ_TIME, False)
|
|
|
|
Sys.echo(s , Sys.CLZ_SEC , False)
|
2013-05-06 02:16:30 +00:00
|
|
|
|
|
|
|
bdata = [(t , Const.CLZ_TIME),(s , Const.CLZ_SEC)]
|
|
|
|
return bdata
|
|
|
|
|
2013-04-20 11:36:49 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def pkval(label, value, pad=40, dbcall= False):
|
|
|
|
""""""
|
2013-05-06 02:16:30 +00:00
|
|
|
l, v = label.rjust(pad,' '), ' '+str(value)
|
|
|
|
if not dbcall :
|
2014-07-16 23:55:55 +00:00
|
|
|
Sys.echo(l, Sys.CLZ_SEC , False)
|
|
|
|
Sys.echo(v, Sys.CLZ_TIME , True)
|
2013-05-06 02:16:30 +00:00
|
|
|
|
|
|
|
bdata = [(l, Const.CLZ_SEC),(v, Const.CLZ_TIME)]
|
|
|
|
return bdata
|
|
|
|
|
2013-04-20 11:36:49 +00:00
|
|
|
|
2014-07-16 23:32:38 +00:00
|
|
|
@staticmethod
|
|
|
|
def getDelta(t):
|
|
|
|
v = ''.join(['{:.5f}'.format(Sys.time()-(Sys.mktime(t.timetuple())+1e-6*t.microsecond)),' s'])
|
|
|
|
return v
|
|
|
|
|
2013-04-20 11:36:49 +00:00
|
|
|
@staticmethod
|
|
|
|
def pdelta(t, label='', dbcall= False):
|
|
|
|
""""""
|
2014-07-16 23:55:55 +00:00
|
|
|
if len(label)>0 and not dbcall : Sys.echo(label+' ', Sys.CLZ_IO, False)
|
2014-07-16 23:32:38 +00:00
|
|
|
v = Sys.getDelta(t)
|
2013-05-06 02:16:30 +00:00
|
|
|
if not dbcall :
|
2014-07-16 23:55:55 +00:00
|
|
|
Sys.echo(v, Sys.CLZ_DELTA)
|
2013-05-06 02:16:30 +00:00
|
|
|
|
|
|
|
bdata = []
|
|
|
|
if len(label)>0 :
|
|
|
|
bdata.append((label+' ', Const.CLZ_IO))
|
|
|
|
bdata.append((v, Const.CLZ_DELTA))
|
|
|
|
return bdata
|
|
|
|
|
2013-04-20 11:36:49 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2013-05-06 02:16:30 +00:00
|
|
|
def pcontent(content, color=None, bcolor=Const.CLZ_DEFAULT, dbcall= False):
|
2013-04-20 11:36:49 +00:00
|
|
|
""""""
|
2014-07-16 23:55:55 +00:00
|
|
|
if not dbcall : Sys.echo(content, Sys.CLZ_SEC if color is None else color)
|
2013-05-06 02:16:30 +00:00
|
|
|
|
|
|
|
bdata = [(content, bcolor)]
|
|
|
|
return bdata
|
|
|
|
|
2013-04-20 11:36:49 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2013-05-06 02:16:30 +00:00
|
|
|
def pwarn(data, isError=False, length=Const.LINE_SEP_LEN, dbcall=False):
|
|
|
|
""" data struct :
|
|
|
|
( # line0
|
2013-04-20 11:36:49 +00:00
|
|
|
'simple line', # LF
|
|
|
|
# line1
|
|
|
|
# p0 p1 p2
|
2013-05-06 02:16:30 +00:00
|
|
|
('complex line with ',('paramValue',fgcolor), ' suit complex line'), # LF
|
2013-04-20 11:36:49 +00:00
|
|
|
# line2
|
|
|
|
'other simple line '
|
|
|
|
)
|
|
|
|
"""
|
2013-05-06 02:16:30 +00:00
|
|
|
w = ' '+(Const.WARN if not isError else Const.ERROR)+' : '
|
|
|
|
clz = Sys.CLZ_WARN if not isError else Sys.CLZ_ERROR
|
|
|
|
clzp = Sys.CLZ_WARN_PARAM if not isError else Sys.CLZ_ERROR_PARAM
|
|
|
|
uiclz = Const.CLZ_WARN if not isError else Const.CLZ_ERROR
|
|
|
|
uiclzp = Const.CLZ_WARN_PARAM if not isError else Const.CLZ_ERROR_PARAM
|
|
|
|
|
2014-07-16 23:55:55 +00:00
|
|
|
if not dbcall : Sys.echo(w, clzp, False, False)
|
2013-05-06 02:16:30 +00:00
|
|
|
bdata = []
|
2013-05-15 18:51:08 +00:00
|
|
|
if not Sys.g.QUIET :
|
2013-05-06 02:16:30 +00:00
|
|
|
bdata.append((w, uiclzp))
|
2013-04-20 11:36:49 +00:00
|
|
|
for i, line in enumerate(data) :
|
2013-05-06 02:16:30 +00:00
|
|
|
if i > 0 :
|
2014-07-16 23:55:55 +00:00
|
|
|
if not dbcall : Sys.echo(' '*len(w), clz, False, False)
|
2013-05-15 18:51:08 +00:00
|
|
|
if not Sys.g.QUIET :
|
2013-05-06 02:16:30 +00:00
|
|
|
bdata.append((' '*len(w), uiclz))
|
2013-04-20 11:36:49 +00:00
|
|
|
if isinstance(line,str) :
|
2013-05-06 02:16:30 +00:00
|
|
|
s = line.ljust(length-len(w),' ')
|
2014-07-16 23:55:55 +00:00
|
|
|
if not dbcall : Sys.echo(s, clz, True, True)
|
2013-05-06 02:16:30 +00:00
|
|
|
|
2013-05-15 18:51:08 +00:00
|
|
|
if not Sys.g.QUIET :
|
2013-05-06 02:16:30 +00:00
|
|
|
bdata.append((s, uiclz))
|
|
|
|
Sys.wlog(bdata)
|
|
|
|
bdata = []
|
2013-04-20 11:36:49 +00:00
|
|
|
else :
|
|
|
|
sl = 0
|
2013-05-06 02:16:30 +00:00
|
|
|
for p in line :
|
2013-04-20 11:36:49 +00:00
|
|
|
if isinstance(p,str) :
|
2014-07-16 23:55:55 +00:00
|
|
|
Sys.echo(p, clz, False, False)
|
2013-05-06 02:16:30 +00:00
|
|
|
bdata.append((p, uiclz))
|
2013-04-20 11:36:49 +00:00
|
|
|
sl += len(p)
|
|
|
|
else :
|
2014-07-16 23:55:55 +00:00
|
|
|
Sys.echo(p[0], clzp+p[1], False, False)
|
2013-05-06 02:16:30 +00:00
|
|
|
bdata.append((p[0], uiclzp))
|
2013-04-20 11:36:49 +00:00
|
|
|
sl += len(p[0])
|
2013-05-06 02:16:30 +00:00
|
|
|
s = ' '.ljust(length-sl-len(w),' ')
|
2014-07-16 23:55:55 +00:00
|
|
|
if not dbcall : Sys.echo(s, clz, True, True)
|
2013-05-15 18:51:08 +00:00
|
|
|
if not Sys.g.QUIET :
|
2013-05-06 02:16:30 +00:00
|
|
|
bdata.append((s, uiclz))
|
|
|
|
Sys.wlog(bdata)
|
|
|
|
bdata = []
|
|
|
|
|
|
|
|
if not dbcall : Sys.dprint()
|
|
|
|
if Sys.g.DEBUG : Sys.wlog([('',Const.CLZ_DEFAULT)])
|
|
|
|
|
|
|
|
|
2013-04-20 11:36:49 +00:00
|
|
|
@staticmethod
|
2013-05-06 02:16:30 +00:00
|
|
|
def _psymbol(ch, done=True):
|
2013-04-20 11:36:49 +00:00
|
|
|
""""""
|
2014-07-16 23:55:55 +00:00
|
|
|
Sys.echo(' ', Sys.CLZ_DEFAULT, False, False)
|
|
|
|
Sys.echo(' '+ch+' ', Sys.CLZ_HEAD_APP if done else Sys.CLZ_SYMBOL, False, True)
|
|
|
|
Sys.echo(' ', Sys.CLZ_DEFAULT, False, True)
|
2013-05-06 02:16:30 +00:00
|
|
|
bdata = [(' ', Const.CLZ_DEFAULT),(' '+ch+' ', Const.CLZ_HEAD_APP if done else Sys.CLZ_SYMBOL),(' ', Const.CLZ_DEFAULT)]
|
|
|
|
return bdata
|
|
|
|
|
|
|
|
|
2013-04-20 11:36:49 +00:00
|
|
|
@staticmethod
|
|
|
|
def pask(ask, yesValue='yes', noValue='no'):
|
|
|
|
""""""
|
|
|
|
Sys._psymbol('?')
|
2014-07-16 23:55:55 +00:00
|
|
|
Sys.echo('', Sys.Clz.fgb3, False, False)
|
2013-04-20 11:36:49 +00:00
|
|
|
ask = ask + ' ('+yesValue+'/'+noValue+') ? '
|
|
|
|
answer = input(ask)
|
|
|
|
while answer.lower()!=yesValue.lower() and answer.lower()!=noValue.lower() :
|
|
|
|
s = 'Please enter either '+yesValue+' or '+noValue+' : '
|
|
|
|
answer = input(' '.ljust(5,' ')+s.ljust(len(ask),' '))
|
|
|
|
Sys.dprint()
|
|
|
|
return answer.lower()==yesValue.lower()
|
2013-05-06 02:16:30 +00:00
|
|
|
|
|
|
|
|
2013-04-20 11:36:49 +00:00
|
|
|
@staticmethod
|
2013-05-06 02:16:30 +00:00
|
|
|
def pstep(title, stime, done, noelf=False, exitOnFailed=True, length=100):
|
2013-04-20 11:36:49 +00:00
|
|
|
""""""
|
|
|
|
if stime is not None :
|
2013-05-06 02:16:30 +00:00
|
|
|
v = ' ('+''.join(['{:.5f}'.format(Sys.time()-(Sys.mktime(stime.timetuple())+1e-6*stime.microsecond)),' s'])+')'
|
2013-04-20 11:36:49 +00:00
|
|
|
else : v = ''
|
2014-07-16 23:55:55 +00:00
|
|
|
bdata = Sys._psymbol('*')
|
|
|
|
Sys.echo(title, Sys.CLZ_TITLE, False, False)
|
|
|
|
Sys.echo(v+' '.ljust(length-len(title)-20-len(v), ' '),Sys.CLZ_DELTA, False, True)
|
2013-05-06 02:16:30 +00:00
|
|
|
if done :
|
2014-07-16 23:55:55 +00:00
|
|
|
Sys.echo(' == '+Const.OK+' == ', Sys.CLZ_OK)
|
2013-04-20 11:36:49 +00:00
|
|
|
else :
|
2014-07-16 23:55:55 +00:00
|
|
|
Sys.echo(' == '+Const.KO+' == ', Sys.CLZ_KO)
|
2013-05-06 02:16:30 +00:00
|
|
|
|
|
|
|
bdata = bdata + [(title, Const.CLZ_TITLE),(v+' '.ljust(length-len(title)-20-len(v)), Const.CLZ_DELTA),(' == '+(Const.OK if done else Const.KO)+' == ', (Const.CLZ_OK if done else Const.CLZ_KO))]
|
|
|
|
|
|
|
|
Sys.wlog(bdata)
|
|
|
|
if not noelf :
|
|
|
|
Sys.wlog(Sys.dprint())
|
2013-05-07 00:43:02 +00:00
|
|
|
|
2013-04-20 11:36:49 +00:00
|
|
|
if exitOnFailed and not done:
|
2013-05-06 02:16:30 +00:00
|
|
|
Sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def ptask(title='Processing, please wait'):
|
|
|
|
if not Sys.g.QUIET :
|
|
|
|
s = ' '+title+'...'
|
2014-07-16 23:55:55 +00:00
|
|
|
Sys.echo(s, Sys.CLZ_TASK )
|
2013-05-06 02:16:30 +00:00
|
|
|
Sys.wlog([(s, Const.CLZ_TASK)])
|
|
|
|
Sys.wlog(Sys.dprint())
|
|
|
|
|
2013-04-20 11:36:49 +00:00
|
|
|
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
# ~~ class Coloriz ~~
|
|
|
|
|
|
|
|
class Coloriz:
|
|
|
|
|
|
|
|
_MARKER = '!§'
|
|
|
|
""""""
|
|
|
|
_SEP = ';'
|
|
|
|
""""""
|
|
|
|
_PATTERN_COLOR = '^'+_MARKER[0]+'[nfNFbB][0-7]'+_SEP+'$'
|
|
|
|
""""""
|
|
|
|
_wFH = 0x0008
|
|
|
|
""""""
|
|
|
|
_wBH = 0x0080
|
|
|
|
""""""
|
|
|
|
_uOFF = '\033[1;m'
|
|
|
|
""""""
|
|
|
|
_wOFF = None
|
|
|
|
""""""
|
2013-05-06 02:16:30 +00:00
|
|
|
_LF = Const.LF
|
2013-04-20 11:36:49 +00:00
|
|
|
""""""
|
|
|
|
OFF = _MARKER+_MARKER[0]+'OFF'+_SEP+_MARKER
|
2013-05-06 02:16:30 +00:00
|
|
|
""""""
|
2013-04-20 11:36:49 +00:00
|
|
|
def __init__(self):
|
2013-05-06 02:16:30 +00:00
|
|
|
"""Colors for both plateform are : 0: black - 1: red - 2:green - 3: yellow - 4: blue - 5: purple - 6: cyan - 7: white
|
2013-04-20 11:36:49 +00:00
|
|
|
available class members :
|
2013-05-06 02:16:30 +00:00
|
|
|
foreground normal (same as bold for w32):
|
2013-04-20 11:36:49 +00:00
|
|
|
self.fgn0 -> self.fgn7
|
|
|
|
foreground bold :
|
|
|
|
self.fgb0 -> self.fgb7
|
|
|
|
foreground high intensity (same as bold high intensity for w35):
|
|
|
|
self.fgN0 -> self.fgN7
|
|
|
|
foreground bold high intensity :
|
|
|
|
self.fgB0 -> self.fgB7
|
|
|
|
background
|
|
|
|
self.bg0 -> self.bg7
|
|
|
|
background high intensity
|
|
|
|
self.BG0 -> self.BG7
|
|
|
|
default colors :
|
|
|
|
self.OFF
|
|
|
|
"""
|
|
|
|
if not Sys.isUnix():
|
|
|
|
j = 0
|
|
|
|
for i in (0,4,2,6,1,5,3,7):
|
2013-05-06 02:16:30 +00:00
|
|
|
exec('self._wf%i = 0x000%i' % (i,j) + Const.LF+'self._wb%i = 0x00%i0' % (i,j) + Const.LF+'self._wF%i = 0x000%i | self._wFH' % (i,j) + Const.LF+'self._wB%i = 0x00%i0 | self._wBH' % (i,j))
|
2013-04-20 11:36:49 +00:00
|
|
|
# normal eq bold
|
|
|
|
exec('self._wn%i = self._wf%i' % (i,i))
|
|
|
|
# normal high intensity eq bold high intensity
|
|
|
|
exec('self._wN%i = self._wF%i' % (i,i))
|
|
|
|
j += 1
|
2013-05-06 02:16:30 +00:00
|
|
|
import psr.w32color as w32cons
|
2013-04-20 11:36:49 +00:00
|
|
|
self._wOFF = w32cons.get_text_attr()
|
|
|
|
self._wOFFbg = self._wOFF & 0x0070
|
|
|
|
self._wOFFfg = self._wOFF & 0x0007
|
|
|
|
self.setColor = w32cons.set_text_attr
|
|
|
|
|
2013-05-06 02:16:30 +00:00
|
|
|
for i in range(0,8):
|
2013-04-20 11:36:49 +00:00
|
|
|
# foreground normal
|
|
|
|
exec('self.fgn%i = self._MARKER + self._MARKER[0] + "n%i" + self._SEP + self._MARKER' % (i,i))
|
2014-07-16 23:55:55 +00:00
|
|
|
if Sys.isUnix() : exec('self._un%i = "\\033[0;3%im"' % (i,i))
|
2013-04-20 11:36:49 +00:00
|
|
|
# foreground bold
|
|
|
|
exec('self.fgb%i = self._MARKER + self._MARKER[0] + "f%i" + self._SEP + self._MARKER' % (i,i))
|
2014-07-16 23:55:55 +00:00
|
|
|
if Sys.isUnix() : exec('self._uf%i = "\\033[1;3%im"' % (i,i))
|
2013-04-20 11:36:49 +00:00
|
|
|
# foreground high intensity
|
|
|
|
exec('self.fgN%i = self._MARKER + self._MARKER[0] + "N%i" + self._SEP + self._MARKER' % (i,i))
|
2014-07-16 23:55:55 +00:00
|
|
|
if Sys.isUnix() : exec('self._uN%i = "\\033[0;9%im"' % (i,i))
|
2013-04-20 11:36:49 +00:00
|
|
|
# foreground bold high intensity
|
|
|
|
exec('self.fgB%i = self._MARKER + self._MARKER[0] + "F%i" + self._SEP + self._MARKER' % (i,i))
|
2014-07-16 23:55:55 +00:00
|
|
|
if Sys.isUnix() : exec('self._uF%i = "\\033[1;9%im"' % (i,i))
|
2013-05-06 02:16:30 +00:00
|
|
|
# background
|
2013-04-20 11:36:49 +00:00
|
|
|
exec('self.bg%i = self._MARKER + self._MARKER[0] + "b%i" + self._SEP + self._MARKER' % (i,i))
|
2014-07-16 23:55:55 +00:00
|
|
|
if Sys.isUnix() : exec('self._ub%i = "\\033[4%im"' % (i,i))
|
2013-04-20 11:36:49 +00:00
|
|
|
# background high intensity
|
|
|
|
exec('self.BG%i = self._MARKER + self._MARKER[0] + "B%i" + self._SEP + self._MARKER' % (i,i))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LZ_2 : Sys.CLZ_2,
|
|
|
|
Const.CLZ_3 : Sys.CLZ_3 , Const.CLZ_4 : Sys.CLZ_4 , Const.CLZ_5 : Sys.CLZ_5,
|
|
|
|
Const.CLZ_6 : Sys.CLZ_6 , Const.CLZ_7 : Sys.CLZ_7 ,
|
|
|
|
Const.CLZ_TASK : Sys.CLZ_TASK , Const.CLZ_SYMBOL: Sys.CLZ_SYMBOL,
|
|
|
|
Const.CLZ_TIME : Sys.CLZ_TIME , Const.CLZ_SEC : Sys.CLZ_SEC ,
|
|
|
|
Const.CLZ_IO : Sys.CLZ_IO , Const.CLZ_CPID : Sys.CLZ_CPID , Const.CLZ_PID : Sys.CLZ_PID,
|
|
|
|
Const.CLZ_CFUNC : Sys.CLZ_CFUNC , Const.CLZ_FUNC : Sys.CLZ_FUNC , Const.CLZ_ARGS : Sys.CLZ_ARGS,
|
|
|
|
Const.CLZ_DELTA : Sys.CLZ_DELTA ,
|
|
|
|
Const.CLZ_ERROR : Sys.CLZ_ERROR , Const.CLZ_WARN : Sys.CLZ_WARN , Const.CLZ_ERROR_PARAM : Sys.CLZ_ERROR_PARAM, Const.CLZ_WARN_PARAM : Sys.CLZ_WARN_PARAM,
|
|
|
|
Const.CLZ_DEFAULT : Sys.CLZ_DEFAULT,
|
|
|
|
Const.CLZ_ACTION : Sys.CLZ_ACTION ,
|
|
|
|
Const.CLZ_INIT : Sys.CLZ_INIT
|
|
|
|
}
|