options.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/python
  2. import time
  3. import copy
  4. OPT_DEBUG_LEVEL=0
  5. def LOG_LVL_TO_STRING(n):
  6. return ["MIDIDEBUG", "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"][n]
  7. def SET_LOG_LEVEL(n):
  8. getOptions().log.setLogLevel(n)
  9. import collections
  10. def dicUpdate(d, u):
  11. for k, v in u.items():
  12. if isinstance(v, collections.Mapping):
  13. d[k] = dicUpdate(d.get(k, {}), v)
  14. else:
  15. d[k] = v
  16. return d
  17. def dictAssign(a, *mods):
  18. a=copy.deepcopy(a)
  19. for i in mods:
  20. dicUpdate(a, i)
  21. return a
  22. def initParams( default, *params):
  23. if params==None:
  24. raise Exception("Parametre non passés")
  25. return dictAssign(default, *params)
  26. class Log:
  27. def __init__(self, lvl=1):
  28. self.level=lvl
  29. Log.CRITICAL=5
  30. Log.ERROR=4
  31. Log.WARNING=3
  32. Log.INFO=2
  33. Log.DEBUG=1
  34. Log.MIDIDEBUG=0
  35. def setLogLevel(self, lvl):
  36. self.level=lvl
  37. def log(self, level, *args):
  38. if self.level>level: return
  39. x=""
  40. for i in args:
  41. x+=str(i)
  42. for i in x.split('\n'):
  43. print("%.3f" %TIME(),":",LOG_LVL_TO_STRING(level),":",i)
  44. def midiDebug(self, *args): self.log(Log.MIDIDEBUG, *args)
  45. def debug(self, *args): self.log(Log.DEBUG, *args)
  46. def info(self, *args): self.log(Log.INFO, *args)
  47. def warn(self, *args): self.log(Log.WARNING, *args)
  48. def err(self, *args): self.log(Log.ERROR, *args)
  49. def crit(self, *args): self.log(Log.CRITICAL, *args)
  50. def CRIT(*args): getOptions().log.crit(*args)
  51. def ERR(*args): getOptions().log.err(*args)
  52. def WARN(*args): getOptions().log.warn(*args)
  53. def INFO(*args): getOptions().log.info(*args)
  54. def DEBUG(*args): getOptions().log.debug(*args)
  55. def MIDIDEBUG(*args): getOptions().log.midiDebug(*args)
  56. def TIME(): return time.time()-getOptions().startTime
  57. class SimpleMidiOptions:
  58. def __init__(self):
  59. self.startTime=time.time()
  60. self.log=Log()
  61. def _initOption():
  62. global ___SIMPLE_MIDI_OPTION_OBJ__ # add this line!
  63. try:
  64. x=___SIMPLE_MIDI_OPTION_OBJ__
  65. except:
  66. ___SIMPLE_MIDI_OPTION_OBJ__ = SimpleMidiOptions()
  67. _initOption()
  68. def getOptions():
  69. global ___SIMPLE_MIDI_OPTION_OBJ__
  70. return ___SIMPLE_MIDI_OPTION_OBJ__