예제 #1
0
    def __call__(self, path, parent, children):
        # The path to the object.
        lib_path = 'tensorflow.%s' % path if path else 'tensorflow'

        # A small helper method to construct members(children) protos.
        def _AddMember(member_name, member_obj, proto):
            """Add the child object to the object being constructed."""
            if member_name == '__init__' or not member_name.startswith('_'):
                if inspect.isroutine(member_obj):
                    new_method = proto.member_method.add()
                    new_method.name = member_name
                    # If member_obj is a python builtin, there is no way to get its
                    # argspec, because it is implemented on the C side. It also has no
                    # func_code.
                    if getattr(member_obj, 'func_code', None):
                        new_method.argspec = _SanitizedArgSpec(member_obj)
                else:
                    new_member = proto.member.add()
                    new_member.name = member_name
                    new_member.mtype = str(type(member_obj))

        parent_corner_cases = _CORNER_CASES.get(path, {})

        if path not in _CORNER_CASES or parent_corner_cases:
            # Decide if we have a module or a class.
            if inspect.ismodule(parent):
                # Create a module object.
                module_obj = api_objects_pb2.TFAPIModule()
                for name, child in children:
                    if name in parent_corner_cases:
                        # If we have an empty entry, skip this object.
                        if parent_corner_cases[name]:
                            module_obj.member.add(
                                **(parent_corner_cases[name]))
                    else:
                        _AddMember(name, child, module_obj)

                # Store the constructed module object.
                self._protos[lib_path] = api_objects_pb2.TFAPIObject(
                    path=lib_path, tf_module=module_obj)
            elif inspect.isclass(parent):
                # Construct a class.
                class_obj = api_objects_pb2.TFAPIClass()
                class_obj.is_instance.extend(_SanitizedMRO(parent))
                for name, child in children:
                    if name in parent_corner_cases:
                        # If we have an empty entry, skip this object.
                        if parent_corner_cases[name]:
                            module_obj.member.add(
                                **(parent_corner_cases[name]))
                    else:
                        _AddMember(name, child, class_obj)

                # Store the constructed class object.
                self._protos[lib_path] = api_objects_pb2.TFAPIObject(
                    path=lib_path, tf_class=class_obj)
            else:
                logging.error(
                    'Illegal call to ApiProtoDump::_py_obj_to_proto.'
                    'Object is neither a module nor a class: %s', path)
예제 #2
0
def _FilterGoldenProtoDict(golden_proto_dict, omit_golden_symbols_map):
    """Filter out golden proto dict symbols that should be omitted."""
    if not omit_golden_symbols_map:
        return golden_proto_dict
    filtered_proto_dict = dict(golden_proto_dict)
    for key, symbol_list in six.iteritems(omit_golden_symbols_map):
        api_object = api_objects_pb2.TFAPIObject()
        api_object.CopyFrom(filtered_proto_dict[key])
        filtered_proto_dict[key] = api_object
        module_or_class = None
        if api_object.HasField("tf_module"):
            module_or_class = api_object.tf_module
        elif api_object.HasField("tf_class"):
            module_or_class = api_object.tf_class
        if module_or_class is not None:
            for members in (
                    module_or_class.member,
                    module_or_class.member_method,
            ):
                filtered_members = [
                    m for m in members if m.name not in symbol_list
                ]
                # Two steps because protobuf repeated fields disallow slice
                # assignment.
                del members[:]
                members.extend(filtered_members)
    return filtered_proto_dict
예제 #3
0
 def _ReadFileToProto(filename):
     """Read a filename, create a protobuf from its contents."""
     ret_val = api_objects_pb2.TFAPIObject()
     text_format.Merge(file_io.read_file_to_string(filename), ret_val)
     return ret_val
예제 #4
0
    def __call__(self, path, parent, children):
        # The path to the object.
        lib_path = 'tensorflow.%s' % path if path else 'tensorflow'
        _, parent = tf_decorator.unwrap(parent)

        # A small helper method to construct members(children) protos.
        def _AddMember(member_name, member_obj, proto):
            """Add the child object to the object being constructed."""
            _, member_obj = tf_decorator.unwrap(member_obj)
            if (_SkipMember(parent, member_name) or isinstance(
                    member_obj, deprecation.HiddenTfApiAttribute)):
                return
            if member_name == '__init__' or not member_name.startswith('_'):
                if tf_inspect.isroutine(member_obj):
                    new_method = proto.member_method.add()
                    new_method.name = member_name
                    # If member_obj is a python builtin, there is no way to get its
                    # argspec, because it is implemented on the C side. It also has no
                    # func_code.
                    if hasattr(member_obj, '__code__'):
                        new_method.argspec = _SanitizedArgSpec(member_obj)
                else:
                    new_member = proto.member.add()
                    new_member.name = member_name
                    if tf_inspect.ismodule(member_obj):
                        new_member.mtype = "<type \'module\'>"
                    else:
                        new_member.mtype = _NormalizeType(str(
                            type(member_obj)))

        parent_corner_cases = _CORNER_CASES.get(path, {})

        if path not in _CORNER_CASES or parent_corner_cases:
            # Decide if we have a module or a class.
            if tf_inspect.ismodule(parent):
                # Create a module object.
                module_obj = api_objects_pb2.TFAPIModule()
                for name, child in children:
                    if name in parent_corner_cases:
                        # If we have an empty entry, skip this object.
                        if parent_corner_cases[name]:
                            module_obj.member.add(
                                **(parent_corner_cases[name]))
                    else:
                        _AddMember(name, child, module_obj)

                # Store the constructed module object.
                self._protos[lib_path] = api_objects_pb2.TFAPIObject(
                    path=lib_path, tf_module=module_obj)
            elif _IsProtoClass(parent):
                proto_obj = api_objects_pb2.TFAPIProto()
                parent.DESCRIPTOR.CopyToProto(proto_obj.descriptor)

                # Store the constructed proto object.
                self._protos[lib_path] = api_objects_pb2.TFAPIObject(
                    path=lib_path, tf_proto=proto_obj)
            elif tf_inspect.isclass(parent):
                # Construct a class.
                class_obj = api_objects_pb2.TFAPIClass()
                class_obj.is_instance.extend(
                    _NormalizeIsInstance(i) for i in _SanitizedMRO(parent))
                for name, child in children:
                    if name in parent_corner_cases:
                        # If we have an empty entry, skip this object.
                        if parent_corner_cases[name]:
                            class_obj.member.add(**(parent_corner_cases[name]))
                    else:
                        _AddMember(name, child, class_obj)

                # Store the constructed class object.
                self._protos[lib_path] = api_objects_pb2.TFAPIObject(
                    path=lib_path, tf_class=class_obj)
            else:
                logging.error(
                    'Illegal call to ApiProtoDump::_py_obj_to_proto.'
                    'Object is neither a module nor a class: %s', path)