warn.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. '''
  2. Created on 26/06/2012
  3. @author: konstaa
  4. '''
  5. import re
  6. import core.api
  7. class Plugin(core.api.Plugin, core.api.IConfigurable):
  8. MODE_NEW = 0x01
  9. MODE_TREND = 0x03
  10. MODE_TOUCHED = 0x07
  11. MODE_ALL = 0x15
  12. def declare_configuration(self, parser):
  13. self.parser = parser
  14. parser.add_option("--general.warn", default='all', choices=['new', 'trend', 'touched', 'all'],
  15. help="Defines the warnings mode. "
  16. "'off' - no warnings, 'new' - warnings for new regions only, "
  17. "'trend' - warnings for new regions and for bad trend of modified regions, "
  18. "'touched' - warnings for new regions and modified regions, "
  19. "'all' - all warnings active"
  20. "[default: %default]")
  21. parser.add_option("--general.min-limit", action="multiopt",
  22. help='TBD')
  23. parser.add_option("--general.max-limit", action="multiopt",
  24. help='TBD')
  25. def configure(self, options):
  26. if options.__dict__['general.warn'] == 'new':
  27. self.mode = self.MODE_NEW
  28. elif options.__dict__['general.warn'] == 'trend':
  29. self.mode = self.MODE_TREND
  30. elif options.__dict__['general.warn'] == 'touched':
  31. self.mode = self.MODE_TOUCHED
  32. elif options.__dict__['general.warn'] == 'all':
  33. self.mode = self.MODE_ALL
  34. class Limit(object):
  35. def __init__(self, limit_type, limit, namespace, field, db_filter):
  36. self.type = limit_type
  37. self.limit = limit
  38. self.namespace = namespace
  39. self.field = field
  40. self.filter = db_filter
  41. def __repr__(self):
  42. return "namespace '" + self.namespace + "', filter '" + str(self.filter) + "'"
  43. self.limits = []
  44. pattern = re.compile(r'''([^:]+)[:]([^:]+)[:]([-+]?[0-9]+(?:[.][0-9]+)?)''')
  45. if options.__dict__['general.max_limit'] != None:
  46. for each in options.__dict__['general.max_limit']:
  47. match = re.match(pattern, each)
  48. if match == None:
  49. self.parser.error("Invalid format of the 'general.max-limit' option: " + each)
  50. limit = Limit("max", match.group(3), match.group(1), match.group(2), (match.group(2), '>', float(match.group(3))))
  51. self.limits.append(limit)
  52. if options.__dict__['general.min_limit'] != None:
  53. for each in options.__dict__['general.min_limit']:
  54. match = re.match(pattern, each)
  55. if match == None:
  56. self.parser.error("Invalid format of the 'general.min-limit' option: " + each)
  57. limit = Limit("min", match.group(3), match.group(1), match.group(2), (match.group(2), '<', float(match.group(3))))
  58. self.limits.append(limit)
  59. def verify_namespaces(self, valid_namespaces):
  60. valid = []
  61. for each in valid_namespaces:
  62. valid.append(each)
  63. for each in self.limits:
  64. if each.namespace not in valid:
  65. self.parser.error("Invalid limit option (namespace does not exist): " + each.namespace)
  66. def verify_fields(self, namespace, valid_fields):
  67. valid = []
  68. for each in valid_fields:
  69. valid.append(each)
  70. for each in self.limits:
  71. if each.namespace == namespace:
  72. if each.field not in valid:
  73. self.parser.error("Invalid limit option (field does not exist): " + each.namespace + ":" + each.field)
  74. def iterate_limits(self):
  75. for each in self.limits:
  76. yield each
  77. def is_mode_matched(self, limit, value, diff, is_modified):
  78. if is_modified == None:
  79. return True
  80. if self.mode == self.MODE_ALL:
  81. return True
  82. if self.mode == self.MODE_TOUCHED and is_modified == True:
  83. return True
  84. if self.mode == self.MODE_TREND and is_modified == True:
  85. if limit < value and diff > 0:
  86. return True
  87. if limit > value and diff < 0:
  88. return True
  89. return False