extract_reference_link.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Copyright (C) 2011 Google Inc. All rights reserved.
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions are
  5. # met:
  6. #
  7. # * Redistributions of source code must retain the above copyright
  8. # notice, this list of conditions and the following disclaimer.
  9. # * Redistributions in binary form must reproduce the above
  10. # copyright notice, this list of conditions and the following disclaimer
  11. # in the documentation and/or other materials provided with the
  12. # distribution.
  13. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  14. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  15. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  16. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  17. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  18. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  19. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. """Utility module for reftests."""
  25. from HTMLParser import HTMLParser
  26. class ExtractReferenceLinkParser(HTMLParser):
  27. def __init__(self):
  28. HTMLParser.__init__(self)
  29. self.matches = []
  30. self.mismatches = []
  31. def handle_starttag(self, tag, attrs):
  32. if tag != "link":
  33. return
  34. attrs = dict(attrs)
  35. if not "rel" in attrs:
  36. return
  37. if not "href" in attrs:
  38. return
  39. if attrs["rel"] == "match":
  40. self.matches.append(attrs["href"])
  41. if attrs["rel"] == "mismatch":
  42. self.mismatches.append(attrs["href"])
  43. def get_reference_link(html_string):
  44. """Returns reference links in the given html_string.
  45. Returns:
  46. a tuple of two URL lists, (matches, mismatches).
  47. """
  48. parser = ExtractReferenceLinkParser()
  49. parser.feed(html_string)
  50. parser.close()
  51. return parser.matches, parser.mismatches