warn.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #
  2. # Metrix++, Copyright 2009-2013, Metrix++ Project
  3. # Link: http://swi.sourceforge.net
  4. #
  5. # This file is a part of Metrix++ Tool.
  6. #
  7. # Metrix++ is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, version 3 of the License.
  10. #
  11. # Metrix++ is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Metrix++. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import re
  20. import core.api
  21. class Plugin(core.api.Plugin, core.api.IConfigurable):
  22. MODE_NEW = 0x01
  23. MODE_TREND = 0x03
  24. MODE_TOUCHED = 0x07
  25. MODE_ALL = 0x15
  26. def declare_configuration(self, parser):
  27. self.parser = parser
  28. parser.add_option("--general.warn", default='all', choices=['new', 'trend', 'touched', 'all'],
  29. help="Defines the warnings mode. "
  30. "'off' - no warnings, 'new' - warnings for new regions only, "
  31. "'trend' - warnings for new regions and for bad trend of modified regions, "
  32. "'touched' - warnings for new regions and modified regions, "
  33. "'all' - all warnings active"
  34. "[default: %default]")
  35. parser.add_option("--general.min-limit", action="multiopt",
  36. help='TBD')
  37. parser.add_option("--general.max-limit", action="multiopt",
  38. help='TBD')
  39. def configure(self, options):
  40. if options.__dict__['general.warn'] == 'new':
  41. self.mode = self.MODE_NEW
  42. elif options.__dict__['general.warn'] == 'trend':
  43. self.mode = self.MODE_TREND
  44. elif options.__dict__['general.warn'] == 'touched':
  45. self.mode = self.MODE_TOUCHED
  46. elif options.__dict__['general.warn'] == 'all':
  47. self.mode = self.MODE_ALL
  48. class Limit(object):
  49. def __init__(self, limit_type, limit, namespace, field, db_filter):
  50. self.type = limit_type
  51. self.limit = limit
  52. self.namespace = namespace
  53. self.field = field
  54. self.filter = db_filter
  55. def __repr__(self):
  56. return "namespace '" + self.namespace + "', filter '" + str(self.filter) + "'"
  57. self.limits = []
  58. pattern = re.compile(r'''([^:]+)[:]([^:]+)[:]([-+]?[0-9]+(?:[.][0-9]+)?)''')
  59. if options.__dict__['general.max_limit'] != None:
  60. for each in options.__dict__['general.max_limit']:
  61. match = re.match(pattern, each)
  62. if match == None:
  63. self.parser.error("Invalid format of the 'general.max-limit' option: " + each)
  64. limit = Limit("max", match.group(3), match.group(1), match.group(2), (match.group(2), '>', float(match.group(3))))
  65. self.limits.append(limit)
  66. if options.__dict__['general.min_limit'] != None:
  67. for each in options.__dict__['general.min_limit']:
  68. match = re.match(pattern, each)
  69. if match == None:
  70. self.parser.error("Invalid format of the 'general.min-limit' option: " + each)
  71. limit = Limit("min", match.group(3), match.group(1), match.group(2), (match.group(2), '<', float(match.group(3))))
  72. self.limits.append(limit)
  73. def verify_namespaces(self, valid_namespaces):
  74. valid = []
  75. for each in valid_namespaces:
  76. valid.append(each)
  77. for each in self.limits:
  78. if each.namespace not in valid:
  79. self.parser.error("Invalid limit option (namespace does not exist): " + each.namespace)
  80. def verify_fields(self, namespace, valid_fields):
  81. valid = []
  82. for each in valid_fields:
  83. valid.append(each)
  84. for each in self.limits:
  85. if each.namespace == namespace:
  86. if each.field not in valid:
  87. self.parser.error("Invalid limit option (field does not exist): " + each.namespace + ":" + each.field)
  88. def iterate_limits(self):
  89. for each in self.limits:
  90. yield each
  91. def is_mode_matched(self, limit, value, diff, is_modified):
  92. if is_modified == None:
  93. return True
  94. if self.mode == self.MODE_ALL:
  95. return True
  96. if self.mode == self.MODE_TOUCHED and is_modified == True:
  97. return True
  98. if self.mode == self.MODE_TREND and is_modified == True:
  99. if limit < value and diff > 0:
  100. return True
  101. if limit > value and diff < 0:
  102. return True
  103. return False