utils.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. from compiler_exceptions import CodeBlockException
  2. from sys import stdout
  3. class Logger(object):
  4. verbose = 0 #-1= no output
  5. #0 = only errors
  6. #1 = only warnings and errors
  7. #2 = all output
  8. @staticmethod
  9. def showError(error):
  10. if(Logger.verbose > -1) :
  11. print "ERROR : " + error
  12. @staticmethod
  13. def showWarning(warning):
  14. if(Logger.verbose > 0) :
  15. print "WARNING : " + warning
  16. @staticmethod
  17. def showInfo(info):
  18. if(Logger.verbose > 1) :
  19. print "INFO : " + info
  20. #######################
  21. class Enum():
  22. def __init__(self, *entries):
  23. self._keys = entries
  24. self._map = {}
  25. for v,k in enumerate(self._keys) :
  26. self._map[k] = v
  27. def __getattr__(self, name):
  28. return self._map[name]
  29. def name_of(self, index):
  30. return self._keys[index]
  31. #######################
  32. NOT_SET = 0
  33. SPACES_USED = 1
  34. TABS_USED = 2
  35. class FormattedWriter:
  36. def __init__(self, out = stdout):
  37. self.out = out
  38. self.indentLevel = 0
  39. self.indentSpace = "\t"
  40. self.first_write = True
  41. def write(self, text = ""):
  42. if self.first_write :
  43. self.first_write = False
  44. if text == "":
  45. self.out.write(self.indentLevel*self.indentSpace)
  46. else:
  47. self.out.write(self.indentLevel*self.indentSpace + text)
  48. else:
  49. if text == "":
  50. self.out.write("\n" + self.indentLevel*self.indentSpace)
  51. else:
  52. self.out.write("\n" + self.indentLevel*self.indentSpace + text)
  53. def extendWrite(self, text = ""):
  54. self.out.write(text)
  55. def indent(self):
  56. self.indentLevel+=1
  57. def dedent(self):
  58. self.indentLevel-=1
  59. def writeCodeCorrectIndent(self, body):
  60. lines = body.split('\n')
  61. while( len(lines) > 0 and lines[-1].strip() == "") :
  62. del(lines[-1])
  63. index = 0;
  64. while( len(lines) > index and lines[index].strip() == "") :
  65. index += 1
  66. if index >= len(lines) :
  67. return
  68. #first index where valid code is present
  69. to_strip_index = len(lines[index].rstrip()) - len(lines[index].strip())
  70. indent_type = NOT_SET;
  71. while index < len(lines):
  72. strip_part = lines[index][:to_strip_index]
  73. if( ('\t' in strip_part and ' ' in strip_part) or
  74. (indent_type == SPACES_USED and '\t' in strip_part) or
  75. (indent_type == TABS_USED and ' ' in strip_part)
  76. ) :
  77. raise CodeBlockException("Mixed tab and space indentation!")
  78. if indent_type == NOT_SET :
  79. if ' ' in strip_part :
  80. indent_type = SPACES_USED
  81. elif '\t' in strip_part :
  82. indent_type = TABS_USED
  83. self.write(lines[index][to_strip_index:])
  84. index += 1
  85. class FileWriter(FormattedWriter):
  86. def __init__(self, filename):
  87. FormattedWriter.__init__(self, open(filename, 'w'))
  88. def close(self):
  89. self.out.close()
  90. #######################