Skip to content

Commit aa5aa14

Browse files
authored
Move _load_library to tensorflow_io.core.python.ops (tensorflow#419)
_load_library used to be defined in tensorflow_io/__init__.py which is not really desired as we actually want to use tensorflow_io/__init__.py to expose "top level APIs" Signed-off-by: Yong Tang <[email protected]>
1 parent 1e418f0 commit aa5aa14

File tree

18 files changed

+61
-57
lines changed

18 files changed

+61
-57
lines changed

tensorflow_io/__init__.py

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,4 @@
1717
from __future__ import division
1818
from __future__ import print_function
1919

20-
import os
21-
import sys
22-
import inspect
23-
2420
import tensorflow as tf
25-
from tensorflow import errors
26-
27-
def _load_library(filename, lib="op"):
28-
"""_load_library"""
29-
f = inspect.getfile(sys._getframe(1)) # pylint: disable=protected-access
30-
31-
# Construct filename
32-
f = os.path.join(os.path.dirname(f), filename)
33-
filenames = [f]
34-
35-
# Add datapath to load if en var is set, used for running tests where shared
36-
# libraries are built in a different path
37-
datapath = os.environ.get('TFIO_DATAPATH')
38-
if datapath is not None:
39-
# Build filename from `datapath` + `package_name` + `relpath_to_library`
40-
f = os.path.join(
41-
datapath, __name__, os.path.relpath(f, os.path.dirname(__file__)))
42-
filenames.append(f)
43-
44-
# Function to load the library, return True if file system library is loaded
45-
load_fn = tf.load_op_library if lib == "op" \
46-
else lambda f: tf.compat.v1.load_file_system_library(f) is None
47-
48-
# Try to load all paths for file, fail if none succeed
49-
errs = []
50-
for f in filenames:
51-
try:
52-
l = load_fn(f)
53-
if l is not None:
54-
return l
55-
except errors.NotFoundError as e:
56-
errs.append(str(e))
57-
raise NotImplementedError(
58-
"unable to open file: " +
59-
"{}, from paths: {}\ncaused by: {}".format(filename, filenames, errs))

tensorflow_io/bigquery/python/ops/bigquery_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from tensorflow.python.data.ops import dataset_ops
3232
from tensorflow.python.data.util import structure
3333
from tensorflow.python.framework import dtypes
34-
from tensorflow_io import _load_library
34+
from tensorflow_io.core.python.ops import _load_library
3535

3636

3737
_bigquery_so = _load_library("_bigquery.so")

tensorflow_io/bigtable/python/ops/bigtable_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
from tensorflow.python.data.util import structure
3737
from tensorflow.python.framework import dtypes
3838
from tensorflow.python.framework import tensor_shape
39-
from tensorflow_io import _load_library
39+
from tensorflow_io.core.python.ops import _load_library
4040

4141
_bigtable_so = _load_library("_bigtable.so")
4242

tensorflow_io/cloud/python/ops/bigquery_reader_ops.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
from tensorflow.python.framework import ops
2222
from tensorflow.python.ops import io_ops
23-
from tensorflow_io import _load_library
23+
from tensorflow_io.core.python.ops import _load_library
2424

2525
_bigquery_reader_so = _load_library("_bigquery_reader_ops.so")
2626

tensorflow_io/cloud/python/ops/gcs_config_ops.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from tensorflow.python.framework import ops
2626
from tensorflow.python.ops import array_ops
2727
from tensorflow.python.training import training
28-
from tensorflow_io import _load_library
28+
from tensorflow_io.core.python.ops import _load_library
2929

3030
_gcs_config_so = _load_library("_gcs_config_ops.so")
3131

tensorflow_io/core/python/ops/__init__.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,48 @@
1717
from __future__ import division
1818
from __future__ import print_function
1919

20-
from tensorflow_io import _load_library
20+
import os
21+
import sys
22+
import inspect
23+
24+
import tensorflow as tf
25+
26+
def _load_library(filename, lib="op"):
27+
"""_load_library"""
28+
f = inspect.getfile(sys._getframe(1)) # pylint: disable=protected-access
29+
30+
# Construct filename
31+
f = os.path.join(os.path.dirname(f), filename)
32+
filenames = [f]
33+
34+
# Add datapath to load if en var is set, used for running tests where shared
35+
# libraries are built in a different path
36+
datapath = os.environ.get('TFIO_DATAPATH')
37+
if datapath is not None:
38+
# Build filename from:
39+
# `datapath` + `tensorflow_io` + `package_name` + `relpath_to_library`
40+
rootpath = os.path.dirname(sys.modules['tensorflow_io'].__file__)
41+
filename = sys.modules[__name__].__file__
42+
f = os.path.join(
43+
datapath, "tensorflow_io",
44+
os.path.relpath(os.path.dirname(filename), rootpath),
45+
os.path.relpath(f, os.path.dirname(filename)))
46+
filenames.append(f)
47+
# Function to load the library, return True if file system library is loaded
48+
load_fn = tf.load_op_library if lib == "op" \
49+
else lambda f: tf.compat.v1.load_file_system_library(f) is None
50+
51+
# Try to load all paths for file, fail if none succeed
52+
errs = []
53+
for f in filenames:
54+
try:
55+
l = load_fn(f)
56+
if l is not None:
57+
return l
58+
except tf.errors.NotFoundError as e:
59+
errs.append(str(e))
60+
raise NotImplementedError(
61+
"unable to open file: " +
62+
"{}, from paths: {}\ncaused by: {}".format(filename, filenames, errs))
63+
2164
core_ops = _load_library('libtensorflow_io.so')

tensorflow_io/core/python/ops/ffmpeg_ops.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
import ctypes
2121

22-
from tensorflow_io import _load_library
22+
from tensorflow_io.core.python.ops import _load_library
2323

2424
import _ctypes
2525

tensorflow_io/gcs/python/ops/gcs_config_ops.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from tensorflow.python.framework import ops
2727
from tensorflow.python.ops import array_ops
2828
from tensorflow.python.training import training
29-
from tensorflow_io import _load_library
29+
from tensorflow_io.core.python.ops import _load_library
3030

3131
# Some GCS operations may be pre-defined and available via tf.contrib in
3232
# earlier TF versions. Because these ops are pre-registered, they will not be

tensorflow_io/grpc/python/ops/grpc_ops.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
import tensorflow as tf
2121
from tensorflow.compat.v1 import data
22-
from tensorflow_io import _load_library
22+
from tensorflow_io.core.python.ops import _load_library
2323
grpc_ops = _load_library('_grpc_ops.so')
2424

2525
class GRPCDataset(data.Dataset):

tensorflow_io/hadoop/python/ops/hadoop_dataset_ops.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import tensorflow as tf
2121
from tensorflow import dtypes
2222
from tensorflow.compat.v1 import data
23-
from tensorflow_io import _load_library
23+
from tensorflow_io.core.python.ops import _load_library
2424
hadoop_ops = _load_library('_hadoop_ops.so')
2525

2626

0 commit comments

Comments
 (0)