-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add substrait.proto convenience module and document it
- Loading branch information
Showing
2 changed files
with
155 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
def _load(): | ||
"""Import all substrait protobuf classes as human friendly. | ||
Instead of forcing users to deal with autogenerated protobuf | ||
modules, importing individual components of the protocol | ||
from submodules etc... this functions loads into the module | ||
all classes representing substrait expressions and loads | ||
the protocol modules with a friendly name making the protocol | ||
more convenient to use. | ||
substrait.gen.proto.extensions.extensions_pb2.SimpleExtensionDeclaration | ||
becomes substrait.proto.SimpleExtensionDeclaration | ||
""" | ||
import sys | ||
import inspect | ||
import pkgutil | ||
import importlib | ||
from substrait.gen import proto as _proto | ||
|
||
selfmodule = sys.modules[__name__] | ||
for submodule_info in pkgutil.iter_modules(_proto.__path__): | ||
submodule_name = submodule_info.name | ||
attr_name = submodule_name.replace("_pb2", "") | ||
if submodule_name == "extensions": | ||
# Extensions are in a submodule | ||
submodule_name = "extensions.extensions_pb2" | ||
attr_name = "extensions" | ||
|
||
submodule = importlib.import_module(f".{submodule_name}", _proto.__name__) | ||
setattr(selfmodule, attr_name, submodule) | ||
|
||
for membername, _ in inspect.getmembers(submodule): | ||
member = getattr(submodule, membername) | ||
if inspect.isclass(member): | ||
setattr(selfmodule, membername, member) | ||
|
||
|
||
_load() |