rule.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/env python
  2. # Copyright 2015 Google Inc. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. class Rule(object):
  16. """An optional base class for rule implementations.
  17. The rule_parser looks for the 'IsType' and 'ApplyRule' methods by name, so
  18. rules are not strictly required to extend this class.
  19. """
  20. def IsType(self, rule_type_name):
  21. """Returns True if the name matches this rule."""
  22. raise NotImplementedError
  23. def ApplyRule(self, return_value, request, response):
  24. """Invokes this rule with the given args.
  25. Args:
  26. return_value: the prior rule's return_value (if any).
  27. request: the httparchive ArchivedHttpRequest.
  28. response: the httparchive ArchivedHttpResponse, which may be None.
  29. Returns:
  30. A (should_stop, return_value) tuple. Typically the request and response
  31. are treated as immutable, so it's the caller's job to apply the
  32. return_value (e.g., set response fields).
  33. """
  34. raise NotImplementedError