#!/usr/bin/python import time import copy OPT_DEBUG_LEVEL=0 def LOG_LVL_TO_STRING(n): return ["MIDIDEBUG", "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"][n] def SET_LOG_LEVEL(n): getOptions().log.setLogLevel(n) import collections def dicUpdate(d, u): for k, v in u.items(): if isinstance(v, collections.Mapping): d[k] = dicUpdate(d.get(k, {}), v) else: d[k] = v return d def dictAssign(a, *mods): a=copy.deepcopy(a) for i in mods: dicUpdate(a, i) return a def initParams( default, *params): if params==None: raise Exception("Parametre non passés") return dictAssign(default, *params) class Log: def __init__(self, lvl=1): self.level=lvl Log.CRITICAL=5 Log.ERROR=4 Log.WARNING=3 Log.INFO=2 Log.DEBUG=1 Log.MIDIDEBUG=0 def setLogLevel(self, lvl): self.level=lvl def log(self, level, *args): if self.level>level: return x="" for i in args: x+=str(i) for i in x.split('\n'): print("%.3f" %TIME(),":",LOG_LVL_TO_STRING(level),":",i) def midiDebug(self, *args): self.log(Log.MIDIDEBUG, *args) def debug(self, *args): self.log(Log.DEBUG, *args) def info(self, *args): self.log(Log.INFO, *args) def warn(self, *args): self.log(Log.WARNING, *args) def err(self, *args): self.log(Log.ERROR, *args) def crit(self, *args): self.log(Log.CRITICAL, *args) def CRIT(*args): getOptions().log.crit(*args) def ERR(*args): getOptions().log.err(*args) def WARN(*args): getOptions().log.warn(*args) def INFO(*args): getOptions().log.info(*args) def DEBUG(*args): getOptions().log.debug(*args) def MIDIDEBUG(*args): getOptions().log.midiDebug(*args) def TIME(): return time.time()-getOptions().startTime class SimpleMidiOptions: def __init__(self): self.startTime=time.time() self.log=Log() def _initOption(): global ___SIMPLE_MIDI_OPTION_OBJ__ # add this line! try: x=___SIMPLE_MIDI_OPTION_OBJ__ except: ___SIMPLE_MIDI_OPTION_OBJ__ = SimpleMidiOptions() _initOption() def getOptions(): global ___SIMPLE_MIDI_OPTION_OBJ__ return ___SIMPLE_MIDI_OPTION_OBJ__