From 36f5e827450967e1c3e0c45149d4537adce309d2 Mon Sep 17 00:00:00 2001 From: "levin@chromium.org" Date: Tue, 27 Sep 2011 15:52:14 +0000 Subject: [PATCH] watchlist: Change watchlistparser.py to be class based. https://bugs.webkit.org/show_bug.cgi?id=68869 Reviewed by Eric Seidel. * Scripts/webkitpy/common/watchlist/watchlistparser.py: * Scripts/webkitpy/common/watchlist/watchlistparser_unittest.py: git-svn-id: http://svn.webkit.org/repository/webkit/trunk@96115 268f45cc-cd09-0410-ab3c-d52691b4dbfc --- Tools/ChangeLog | 10 ++++ .../webkitpy/common/watchlist/watchlistparser.py | 69 +++++++++++----------- .../common/watchlist/watchlistparser_unittest.py | 12 ++-- 3 files changed, 52 insertions(+), 39 deletions(-) diff --git a/Tools/ChangeLog b/Tools/ChangeLog index a7f3aee..b4be654 100644 --- a/Tools/ChangeLog +++ b/Tools/ChangeLog @@ -1,5 +1,15 @@ 2011-09-27 David Levin + watchlist: Change watchlistparser.py to be class based. + https://bugs.webkit.org/show_bug.cgi?id=68869 + + Reviewed by Eric Seidel. + + * Scripts/webkitpy/common/watchlist/watchlistparser.py: + * Scripts/webkitpy/common/watchlist/watchlistparser_unittest.py: + +2011-09-27 David Levin + watchlist: Break out the diff boilerplate to allow for re-use. https://bugs.webkit.org/show_bug.cgi?id=68871 diff --git a/Tools/Scripts/webkitpy/common/watchlist/watchlistparser.py b/Tools/Scripts/webkitpy/common/watchlist/watchlistparser.py index 0031d2d..887438f 100644 --- a/Tools/Scripts/webkitpy/common/watchlist/watchlistparser.py +++ b/Tools/Scripts/webkitpy/common/watchlist/watchlistparser.py @@ -29,48 +29,47 @@ import re from webkitpy.common.watchlist.watchlist import WatchList -_DEFINITIONS = 'DEFINITIONS' -_INVALID_DEFINITION_NAME_REGEX = r'\|' +class WatchListParser(object): + _DEFINITIONS = 'DEFINITIONS' + _INVALID_DEFINITION_NAME_REGEX = r'\|' -def _eval_watch_list(watch_list_contents): - return eval(watch_list_contents, {'__builtins__': None}, None) + def __init__(self): + self._section_parsers = {self._DEFINITIONS: self._parse_definition_section, } + self._definition_pattern_parsers = {} -_DEFINITION_MATCH_PARSER = {} + def parse(self, watch_list_contents): + watch_list = WatchList() + # Change the watch list text into a dictionary. + dictionary = self._eval_watch_list(watch_list_contents) -def _parse_definition_section(definition_section, watch_list): - definitions = {} - for name in definition_section: - invalid_character = re.search(_INVALID_DEFINITION_NAME_REGEX, name) - if invalid_character: - raise Exception('Invalid character "%s" in definition "%s".' % (invalid_character.group(0), name)) + # Parse the top level sections in the watch list. + for section in dictionary: + parser = self._section_parsers.get(section) + if not parser: + raise Exception('Unknown section "%s" in watch list.' % section) + parser(dictionary[section], watch_list) - definition = definition_section[name] - definitions[name] = [] - for pattern_type in definition: - pattern_parser = _DEFINITION_MATCH_PARSER.get(pattern_type) - if not pattern_parser: - raise Exception('Invalid pattern type "%s" in definition "%s".' % (pattern_type, name)) + return watch_list - pattern = pattern_parser(definition[pattern_type]) - definitions[name].append(pattern) - watch_list.set_definitions(definitions) + def _eval_watch_list(self, watch_list_contents): + return eval(watch_list_contents, {'__builtins__': None}, None) -_SECTION_PARSERS = {_DEFINITIONS: _parse_definition_section, } + def _parse_definition_section(self, definition_section, watch_list): + definitions = {} + for name in definition_section: + invalid_character = re.search(self._INVALID_DEFINITION_NAME_REGEX, name) + if invalid_character: + raise Exception('Invalid character "%s" in definition "%s".' % (invalid_character.group(0), name)) + definition = definition_section[name] + definitions[name] = [] + for pattern_type in definition: + pattern_parser = self._definition_pattern_parsers.get(pattern_type) + if not pattern_parser: + raise Exception('Invalid pattern type "%s" in definition "%s".' % (pattern_type, name)) -def parse_watch_list(watch_list_contents): - watch_list = WatchList() - - # Change the watch list text into a dictionary. - dictionary = _eval_watch_list(watch_list_contents) - - # Parse the top level sections in the watch list. - for section in dictionary: - parser = _SECTION_PARSERS.get(section) - if not parser: - raise Exception('Unknown section "%s" in watch list.' % section) - parser(dictionary[section], watch_list) - - return watch_list + pattern = pattern_parser(definition[pattern_type]) + definitions[name].append(pattern) + watch_list.set_definitions(definitions) diff --git a/Tools/Scripts/webkitpy/common/watchlist/watchlistparser_unittest.py b/Tools/Scripts/webkitpy/common/watchlist/watchlistparser_unittest.py index 41d23fe..32c69c6 100644 --- a/Tools/Scripts/webkitpy/common/watchlist/watchlistparser_unittest.py +++ b/Tools/Scripts/webkitpy/common/watchlist/watchlistparser_unittest.py @@ -30,7 +30,7 @@ import re import unittest -from webkitpy.common.watchlist.watchlistparser import parse_watch_list +from webkitpy.common.watchlist.watchlistparser import WatchListParser class WatchListParserTest(unittest.TestCase): @@ -38,6 +38,7 @@ class WatchListParserTest(unittest.TestCase): # For versions of Python before 2.7. if not 'assertRaisesRegexp' in dir(self): self.assertRaisesRegexp = self._verifyException + self._watch_list_parser = WatchListParser() def _verifyException(self, regex_message, callable, *args): try: @@ -49,7 +50,8 @@ class WatchListParserTest(unittest.TestCase): def test_bad_section(self): watch_list_with_bad_section = ('{"FOO": {}}') - self.assertRaisesRegexp('Unknown section "FOO" in watch list.', parse_watch_list, watch_list_with_bad_section) + self.assertRaisesRegexp('Unknown section "FOO" in watch list.', + self._watch_list_parser.parse, watch_list_with_bad_section) def test_bad_definition(self): watch_list_with_bad_definition = ( @@ -61,7 +63,8 @@ class WatchListParserTest(unittest.TestCase): ' },' '}') - self._verifyException('Invalid character "|" in definition "WatchList1|A".', parse_watch_list, watch_list_with_bad_definition) + self._verifyException('Invalid character "|" in definition "WatchList1|A".', + self._watch_list_parser.parse, watch_list_with_bad_definition) def test_bad_match_type(self): watch_list_with_bad_match_type = ( @@ -73,4 +76,5 @@ class WatchListParserTest(unittest.TestCase): ' },' '}') - self._verifyException('Invalid pattern type "nothing_matches_this" in definition "WatchList1".', parse_watch_list, watch_list_with_bad_match_type) + self._verifyException('Invalid pattern type "nothing_matches_this" in definition "WatchList1".', + self._watch_list_parser.parse, watch_list_with_bad_match_type) -- 2.7.4