# Path: tensorflow_transform/tf_metadata/dataset_metadata.py
# class DatasetMetadata:
#   def __init__(self, schema: schema_pb2.Schema):
#   def from_feature_spec(
#       cls: Type[_DatasetMetadataType],
#       feature_spec: Mapping[str, common_types.FeatureSpecType],
#       domains: Optional[Mapping[str, common_types.DomainType]] = None
#   ) -> _DatasetMetadataType:
#   def schema(self) -> schema_pb2.Schema:
#   def __eq__(self, other):
#   def __ne__(self, other):
#   def __repr__(self):
# 
# Path: tensorflow_transform/tf_metadata/schema_utils.py
# TENSOR_REPRESENTATION_GROUP = ''
# RAGGED_TENSOR_TAG = 'ragged_tensor'
# _DEPRECATED_LIFECYCLE_STAGES = [
#     schema_pb2.DEPRECATED, schema_pb2.DISABLED, schema_pb2.PLANNED,
#     schema_pb2.ALPHA, schema_pb2.DEBUG_ONLY
# ]
# _LEGACY_DEFAULT_VALUE_FOR_DTYPE = {tf.string: '', tf.int64: -1, tf.float32: -1}
# def schema_from_feature_spec(
#     feature_spec: Mapping[str, common_types.FeatureSpecType],
#     domains: Optional[Mapping[str, common_types.DomainType]] = None
# ) -> schema_pb2.Schema:
# def _ragged_tensor_representation_from_feature_spec(
#     spec: common_types.RaggedFeature, name: str,
#     domains: Dict[str, common_types.DomainType]
# ) -> Tuple[schema_pb2.Feature, List[schema_pb2.Feature],
# def _sparse_feature_from_feature_spec(spec, name, domains):
# def _feature_from_feature_spec(spec, name, domains):
# def _set_type(name, feature, dtype):
# def _set_domain(name, feature, domain):
# def schema_as_feature_spec(
#     schema_proto: schema_pb2.Schema) -> SchemaAsFeatureSpecResult:
# def _get_string_domains(schema):
# def _get_domain(feature, string_domains):
# def pop_ragged_source_columns(
#     name: str, tensor_representation: schema_pb2.TensorRepresentation,
#     feature_by_name: Dict[str, schema_pb2.Feature]) -> schema_pb2.Feature:
# def _ragged_tensor_representation_as_feature_spec(
#     name: str, tensor_representation: schema_pb2.TensorRepresentation,
#     feature_by_name: Dict[str, schema_pb2.Feature],
#     string_domains: Dict[str, common_types.DomainType]
# ) -> Tuple[common_types.RaggedFeature, Optional[common_types.DomainType]]:
# def _sparse_feature_as_feature_spec(feature, feature_by_name, string_domains):
# def _feature_as_feature_spec(feature, string_domains):
# def _feature_dtype(feature):
# def _fixed_shape_as_tf_shape(fixed_shape):
# def _include_in_parsing_spec(feature):
# def _legacy_schema_from_feature_spec(feature_spec, domains=None):
# def _legacy_schema_as_feature_spec(schema_proto):
# def _legacy_feature_as_feature_spec(feature):
# def _legacy_infer_default_value(feature_proto, dtype):


import json
import os
import tensorflow as tf
from tensorflow_transform.tf_metadata import dataset_metadata
from tensorflow_transform.tf_metadata import schema_utils
from google.protobuf import text_format
from tensorflow.python.lib.io import file_io  # pylint: disable=g-direct-tensorflow-import
from tensorflow_metadata.proto.v0 import schema_pb2
#
#     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.
"""Utilities to read and write metadata in standardized versioned formats."""






def read_metadata(path):
  """Load metadata in JSON format from a path into a new DatasetMetadata."""
  schema_file = os.path.join(path, 'schema.pbtxt')
  legacy_schema_file = os.path.join(path, 'v1-json', 'schema.json')
  if file_io.file_exists(schema_file):
    text_proto = file_io.FileIO(schema_file, 'r').read()
    schema_proto = text_format.Parse(text_proto, schema_pb2.Schema(),
                                     allow_unknown_extension=True)
  elif file_io.file_exists(legacy_schema_file):
    schema_json = file_io.FileIO(legacy_schema_file, 'r').read()
    schema_proto = _parse_schema_json(schema_json)
  else:
    raise IOError(
        'Schema file {} does not exist and neither did legacy format file '
        '{}'.format(schema_file, legacy_schema_file))

# Path: tensorflow_transform/tf_metadata/dataset_metadata.py
# class DatasetMetadata:
#   def __init__(self, schema: schema_pb2.Schema):
#   def from_feature_spec(
#       cls: Type[_DatasetMetadataType],
#       feature_spec: Mapping[str, common_types.FeatureSpecType],
#       domains: Optional[Mapping[str, common_types.DomainType]] = None
#   ) -> _DatasetMetadataType:
#   def schema(self) -> schema_pb2.Schema:
#   def __eq__(self, other):
#   def __ne__(self, other):
#   def __repr__(self):
# 
# Path: tensorflow_transform/tf_metadata/schema_utils.py
# TENSOR_REPRESENTATION_GROUP = ''
# RAGGED_TENSOR_TAG = 'ragged_tensor'
# _DEPRECATED_LIFECYCLE_STAGES = [
#     schema_pb2.DEPRECATED, schema_pb2.DISABLED, schema_pb2.PLANNED,
#     schema_pb2.ALPHA, schema_pb2.DEBUG_ONLY
# ]
# _LEGACY_DEFAULT_VALUE_FOR_DTYPE = {tf.string: '', tf.int64: -1, tf.float32: -1}
# def schema_from_feature_spec(
#     feature_spec: Mapping[str, common_types.FeatureSpecType],
#     domains: Optional[Mapping[str, common_types.DomainType]] = None
# ) -> schema_pb2.Schema:
# def _ragged_tensor_representation_from_feature_spec(
#     spec: common_types.RaggedFeature, name: str,
#     domains: Dict[str, common_types.DomainType]
# ) -> Tuple[schema_pb2.Feature, List[schema_pb2.Feature],
# def _sparse_feature_from_feature_spec(spec, name, domains):
# def _feature_from_feature_spec(spec, name, domains):
# def _set_type(name, feature, dtype):
# def _set_domain(name, feature, domain):
# def schema_as_feature_spec(
#     schema_proto: schema_pb2.Schema) -> SchemaAsFeatureSpecResult:
# def _get_string_domains(schema):
# def _get_domain(feature, string_domains):
# def pop_ragged_source_columns(
#     name: str, tensor_representation: schema_pb2.TensorRepresentation,
#     feature_by_name: Dict[str, schema_pb2.Feature]) -> schema_pb2.Feature:
# def _ragged_tensor_representation_as_feature_spec(
#     name: str, tensor_representation: schema_pb2.TensorRepresentation,
#     feature_by_name: Dict[str, schema_pb2.Feature],
#     string_domains: Dict[str, common_types.DomainType]
# ) -> Tuple[common_types.RaggedFeature, Optional[common_types.DomainType]]:
# def _sparse_feature_as_feature_spec(feature, feature_by_name, string_domains):
# def _feature_as_feature_spec(feature, string_domains):
# def _feature_dtype(feature):
# def _fixed_shape_as_tf_shape(fixed_shape):
# def _include_in_parsing_spec(feature):
# def _legacy_schema_from_feature_spec(feature_spec, domains=None):
# def _legacy_schema_as_feature_spec(schema_proto):
# def _legacy_feature_as_feature_spec(feature):
# def _legacy_infer_default_value(feature_proto, dtype):


# Path: tensorflow_transform/tf_metadata/metadata_io.py
import json
import os
import tensorflow as tf
from tensorflow_transform.tf_metadata import dataset_metadata
from tensorflow_transform.tf_metadata import schema_utils
from google.protobuf import text_format
from tensorflow.python.lib.io import file_io  # pylint: disable=g-direct-tensorflow-import
from tensorflow_metadata.proto.v0 import schema_pb2

#
#     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.
"""Utilities to read and write metadata in standardized versioned formats."""






def read_metadata(path):
  """Load metadata in JSON format from a path into a new DatasetMetadata."""
  schema_file = os.path.join(path, 'schema.pbtxt')
  legacy_schema_file = os.path.join(path, 'v1-json', 'schema.json')
  if file_io.file_exists(schema_file):
    text_proto = file_io.FileIO(schema_file, 'r').read()
    schema_proto = text_format.Parse(text_proto, schema_pb2.Schema(),
                                     allow_unknown_extension=True)
  elif file_io.file_exists(legacy_schema_file):
    schema_json = file_io.FileIO(legacy_schema_file, 'r').read()
    schema_proto = _parse_schema_json(schema_json)
  else:
    raise IOError(
        'Schema file {} does not exist and neither did legacy format file '
        '{}'.format(schema_file, legacy_schema_file))

  return dataset_metadata.DatasetMetadata(schema_proto)