problem.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. """
  2. In this file we define a ProblemVault class where we store all the
  3. exceptions and all the problems we find with the code.
  4. The ProblemVault is capable of registering problems and also figuring out if a
  5. problem is worse than a registered exception so that it only warns when things
  6. get worse.
  7. """
  8. class ProblemVault(object):
  9. """
  10. Singleton where we store the various new problems we
  11. found in the code, and also the old problems we read from the exception
  12. file.
  13. """
  14. def __init__(self, exception_fname):
  15. # Exception dictionary: { problem.key() : Problem object }
  16. self.exceptions = {}
  17. try:
  18. with open(exception_fname, 'r') as exception_f:
  19. self.register_exceptions(exception_f)
  20. except IOError:
  21. print("No exception file provided")
  22. def register_exceptions(self, exception_file):
  23. # Register exceptions
  24. for line in exception_file:
  25. problem = get_old_problem_from_exception_str(line)
  26. if problem is None:
  27. continue
  28. self.exceptions[problem.key()] = problem
  29. #print "Registering exception: %s" % problem
  30. def register_problem(self, problem):
  31. """
  32. Register this problem to the problem value. Return True if it was a new
  33. problem or it worsens an already existing problem.
  34. """
  35. # This is a new problem, print it
  36. if problem.key() not in self.exceptions:
  37. print(problem)
  38. return True
  39. # If it's an old problem, we don't warn if the situation got better
  40. # (e.g. we went from 4k LoC to 3k LoC), but we do warn if the
  41. # situation worsened (e.g. we went from 60 includes to 80).
  42. if problem.is_worse_than(self.exceptions[problem.key()]):
  43. print(problem)
  44. return True
  45. return False
  46. class Problem(object):
  47. def __init__(self, problem_type, problem_location, metric_value):
  48. self.problem_location = problem_location
  49. self.metric_value = int(metric_value)
  50. self.problem_type = problem_type
  51. def is_worse_than(self, other_problem):
  52. """Return True if this is a worse problem than other_problem"""
  53. if self.metric_value > other_problem.metric_value:
  54. return True
  55. return False
  56. def key(self):
  57. """Generate a unique key that describes this problem that can be used as a dictionary key"""
  58. return "%s:%s" % (self.problem_location, self.problem_type)
  59. def __str__(self):
  60. return "problem %s %s %s" % (self.problem_type, self.problem_location, self.metric_value)
  61. class FileSizeProblem(Problem):
  62. def __init__(self, problem_location, metric_value):
  63. super(FileSizeProblem, self).__init__("file-size", problem_location, metric_value)
  64. class IncludeCountProblem(Problem):
  65. def __init__(self, problem_location, metric_value):
  66. super(IncludeCountProblem, self).__init__("include-count", problem_location, metric_value)
  67. class FunctionSizeProblem(Problem):
  68. def __init__(self, problem_location, metric_value):
  69. super(FunctionSizeProblem, self).__init__("function-size", problem_location, metric_value)
  70. def get_old_problem_from_exception_str(exception_str):
  71. try:
  72. _, problem_type, problem_location, metric_value = exception_str.split(" ")
  73. except ValueError:
  74. return None
  75. if problem_type == "file-size":
  76. return FileSizeProblem(problem_location, metric_value)
  77. elif problem_type == "include-count":
  78. return IncludeCountProblem(problem_location, metric_value)
  79. elif problem_type == "function-size":
  80. return FunctionSizeProblem(problem_location, metric_value)
  81. else:
  82. # print("Unknown exception line '{}'".format(exception_str))
  83. return None