|
| 1 | +# Copyright 2015 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 | +"""Visitor restricting traversal to only the public tensorflow API.""" |
| 16 | + |
| 17 | +from __future__ import absolute_import |
| 18 | +from __future__ import division |
| 19 | +from __future__ import print_function |
| 20 | + |
| 21 | +import inspect |
| 22 | + |
| 23 | + |
| 24 | +class PublicAPIVisitor(object): |
| 25 | + """Visitor to use with `traverse` to visit exactly the public TF API.""" |
| 26 | + |
| 27 | + def __init__(self, visitor): |
| 28 | + """Constructor. |
| 29 | +
|
| 30 | + `visitor` should be a callable suitable as a visitor for `traverse`. It will |
| 31 | + be called only for members of the public TensorFlow API. |
| 32 | +
|
| 33 | + Args: |
| 34 | + visitor: A visitor to call for the public API. |
| 35 | + """ |
| 36 | + self._visitor = visitor |
| 37 | + |
| 38 | + # Modules/classes we do not want to descend into if we hit them. Usually, |
| 39 | + # sytem modules exposed through platforms for compatibility reasons. |
| 40 | + # Each entry maps a module path to a name to ignore in traversal. |
| 41 | + _do_not_descend_map = { |
| 42 | + # TODO(drpng): This can be removed once sealed off. |
| 43 | + '': ['platform', 'pywrap_tensorflow'], |
| 44 | + |
| 45 | + # Some implementations have this internal module that we shouldn't expose. |
| 46 | + 'flags': ['cpp_flags'], |
| 47 | + |
| 48 | + # Everything below here is legitimate. |
| 49 | + 'app': 'flags', # It'll stay, but it's not officially part of the API |
| 50 | + 'test': ['mock'], # Imported for compatibility between py2/3. |
| 51 | + } |
| 52 | + |
| 53 | + def _isprivate(self, name): |
| 54 | + """Return whether a name is private.""" |
| 55 | + return name.startswith('_') |
| 56 | + |
| 57 | + def _do_not_descend(self, path, name): |
| 58 | + """Safely queries if a specific fully qualified name should be excluded.""" |
| 59 | + return (path in self._do_not_descend_map and |
| 60 | + name in self._do_not_descend_map[path]) |
| 61 | + |
| 62 | + def __call__(self, path, parent, children): |
| 63 | + """Visitor interface, see `traverse` for details.""" |
| 64 | + if inspect.ismodule(parent) and len(path.split('.')) > 10: |
| 65 | + raise RuntimeError('Modules nested too deep:\n%s\n\nThis is likely a ' |
| 66 | + 'problem with an accidental public import.' % path) |
| 67 | + |
| 68 | + # Remove things that are not visible. |
| 69 | + for name, child in list(children): |
| 70 | + if self._isprivate(name): |
| 71 | + children.remove((name, child)) |
| 72 | + |
| 73 | + self._visitor(path, parent, children) |
| 74 | + |
| 75 | + # Remove things that are visible, but which should not be descended into. |
| 76 | + for name, child in list(children): |
| 77 | + if self._do_not_descend(path, name): |
| 78 | + children.remove((name, child)) |
0 commit comments