| OLD | NEW |
| (Empty) |
| 1 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 import optparse | |
| 5 import re | |
| 6 | |
| 7 class PageFilter(object): | |
| 8 """Filters pages in the page set based on command line flags.""" | |
| 9 | |
| 10 def __init__(self, options): | |
| 11 if options.page_filter: | |
| 12 try: | |
| 13 self._page_regex = re.compile(options.page_filter) | |
| 14 except re.error: | |
| 15 raise Exception('--page-filter: invalid regex') | |
| 16 else: | |
| 17 self._page_regex = None | |
| 18 | |
| 19 if options.page_filter_exclude: | |
| 20 try: | |
| 21 self._page_exclude_regex = re.compile(options.page_filter_exclude) | |
| 22 except re.error: | |
| 23 raise Exception('--page-filter-exclude: invalid regex') | |
| 24 else: | |
| 25 self._page_exclude_regex = None | |
| 26 | |
| 27 def IsSelected(self, page): | |
| 28 if self._page_exclude_regex and self._page_exclude_regex.search(page.url): | |
| 29 return False | |
| 30 if self._page_regex: | |
| 31 return self._page_regex.search(page.url) | |
| 32 return True | |
| 33 | |
| 34 @staticmethod | |
| 35 def AddCommandLineOptions(parser): | |
| 36 group = optparse.OptionGroup(parser, 'Page filtering options') | |
| 37 group.add_option('--page-filter', dest='page_filter', | |
| 38 help='Use only pages whose URLs match the given filter regexp.') | |
| 39 group.add_option('--page-filter-exclude', dest='page_filter_exclude', | |
| 40 help='Exclude pages whose URLs match the given filter regexp.') | |
| 41 parser.add_option_group(group) | |
| OLD | NEW |