1# Copyright 2016 The TensorFlow Authors. All Rights Reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================== 15"""Global configuration support.""" 16 17import enum 18 19 20# TODO(mdan): For better performance, allow each rule to take a set names. 21 22 23class Rule(object): 24 """Base class for conversion rules.""" 25 26 def __init__(self, module_prefix): 27 self._prefix = module_prefix 28 29 def matches(self, module_name): 30 return (module_name.startswith(self._prefix + '.') or 31 module_name == self._prefix) 32 33 34class Action(enum.Enum): 35 NONE = 0 36 CONVERT = 1 37 DO_NOT_CONVERT = 2 38 39 40class DoNotConvert(Rule): 41 """Indicates that this module should be not converted.""" 42 43 def __str__(self): 44 return 'DoNotConvert rule for {}'.format(self._prefix) 45 46 def get_action(self, module): 47 if self.matches(module.__name__): 48 return Action.DO_NOT_CONVERT 49 return Action.NONE 50 51 52class Convert(Rule): 53 """Indicates that this module should be converted.""" 54 55 def __str__(self): 56 return 'Convert rule for {}'.format(self._prefix) 57 58 def get_action(self, module): 59 if self.matches(module.__name__): 60 return Action.CONVERT 61 return Action.NONE 62