summaryrefslogtreecommitdiff
path: root/definate/filter_factory.py
blob: 1ad1d3f3a5311dcc7ab26f3e00072a0027fc5778 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/python
#
# Copyright 2012 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Functionality to allow easily retrieving certain filter objects."""

__author__ = 'msu@google.com (Martin Suess)'


import definition_filter
import file_filter

DEFINITION_FILTER = 1
FILE_FILTER = 2
GLOBAL_FILTER = 3

PRE_FILTERS = 'PreFilters'
POST_FILTERS = 'PostFilters'


class Error(Exception):
  """Base error class."""


class FilterIdentificationError(Error):
  """Exception to use when FilterFactory fails to identify the Filter."""


class FilterFactory(object):
  """Functionality to get a filter object easily based on its name.

  This class can be initialized and the GetFilter() method allows retrieving a
  specific filter based on the name of the filter and the scope (global, file
  and definition).
  """

  def __init__(self):
    """Initializer."""
    self._filters = {
        DEFINITION_FILTER: {
            'PostFilters': {
                'SortFilter': definition_filter.SortFilter,
                'AlignFilter': definition_filter.AlignFilter,
                },
            },
        FILE_FILTER: {
            'PostFilters': {
                'PrintFilter': file_filter.PrintFilter,
                'WriteFileFilter': file_filter.WriteFileFilter,
                },
            },
        GLOBAL_FILTER: {
            'PreFilters': {
                },
            'PostFilters': {
                },
            },
        }

  def GetFilter(self, scope, identifier, sequence):
    """Returns a specific filter instance based on the identifier.

    Args:
      scope: Type of filter to be returned. Valid types are listed as globals
        in the beginning of this module.
      identifier: String identifier for the filter to get.
        sequence: String identifier for the sequence information to determine
        when the filter should be applied. Valid values:
          - 'PreFilters': Filters that are applied before processing the data
              (e.g. before the definition is created).
          - 'PostFilters': Filters that are applied after processing the data
              (e.g. after the definition has been created).

    Raises:
      FilterIdentificationError: If the filter cannot be identified.

    Returns:
      Filter instance based on the identifier passed in.
    """
    if scope not in self._filters:
      raise FilterIdentificationError(
          'Filter scope \'%d\' could not be found in filters.' % scope)
    if sequence not in self._filters[scope]:
      raise FilterIdentificationError(
          'Filter sequence \'%s\' is not applicable to scope \'%d\'.' % (
              sequence, scope))
    filters = self._filters[scope][sequence]
    if identifier not in filters:
      raise FilterIdentificationError(
          'Filter \'%s\' could not be identified. Wrong scope (%d) or sequence'
          ' (%s)?' % (identifier, scope, sequence))
    return filters[identifier]()