Switch reflect usage to yagrc's new lazy importer

This makes the normal imports a bit more readable.

Lazy import requires yagrc v1.1.0, so bumped requirements.txt entry for that.
This commit is contained in:
sparky8512 2021-02-14 17:22:52 -08:00
parent 30e4b27516
commit 1659133168
2 changed files with 25 additions and 32 deletions

View file

@ -1,6 +1,6 @@
grpcio>=1.12.0 grpcio>=1.12.0
grpcio-tools>=1.20.0 grpcio-tools>=1.20.0
protobuf>=3.6.0 protobuf>=3.6.0
yagrc>=1.0.1 yagrc>=1.1.0
paho-mqtt>=1.5.1 paho-mqtt>=1.5.1
influxdb>=5.3.1 influxdb>=5.3.1

View file

@ -308,28 +308,21 @@ import statistics
import grpc import grpc
try: try:
from spacex.api.device import device_pb2
from spacex.api.device import device_pb2_grpc
from spacex.api.device import dish_pb2
import_ok = True
except ImportError:
from yagrc import importer from yagrc import importer
import_ok = False importer.add_lazy_packages(["spacex.api.device"])
imports_pending = True
except (ImportError, AttributeError):
imports_pending = False
from spacex.api.device import device_pb2
from spacex.api.device import device_pb2_grpc
from spacex.api.device import dish_pb2
def import_protocols(channel): def resolve_imports(channel):
grpc_importer = importer.GrpcImporter() importer.resolve_lazy_imports(channel)
grpc_importer.configure( global imports_pending
channel, filenames=["spacex/api/device/device.proto", "spacex/api/device/dish.proto"]) imports_pending = False
global device_pb2
global device_pb2_grpc
global dish_pb2
from spacex.api.device import device_pb2
from spacex.api.device import device_pb2_grpc
from spacex.api.device import dish_pb2
global import_ok
import_ok = True
class GrpcError(Exception): class GrpcError(Exception):
@ -379,9 +372,9 @@ def status_field_names():
A tuple with 3 lists, with status data field names, alert detail field A tuple with 3 lists, with status data field names, alert detail field
names, and obstruction detail field names, in that order. names, and obstruction detail field names, in that order.
""" """
if not import_ok: if imports_pending:
with grpc.insecure_channel("192.168.100.1:9200") as channel: with grpc.insecure_channel("192.168.100.1:9200") as channel:
import_protocols(channel) resolve_imports(channel)
alert_names = [] alert_names = []
for field in dish_pb2.DishAlerts.DESCRIPTOR.fields: for field in dish_pb2.DishAlerts.DESCRIPTOR.fields:
alert_names.append("alert_" + field.name) alert_names.append("alert_" + field.name)
@ -419,9 +412,9 @@ def status_field_types():
A tuple with 3 lists, with status data field types, alert detail field A tuple with 3 lists, with status data field types, alert detail field
types, and obstruction detail field types, in that order. types, and obstruction detail field types, in that order.
""" """
if not import_ok: if imports_pending:
with grpc.insecure_channel("192.168.100.1:9200") as channel: with grpc.insecure_channel("192.168.100.1:9200") as channel:
import_protocols(channel) resolve_imports(channel)
return [ return [
str, # id str, # id
str, # hardware_version str, # hardware_version
@ -459,8 +452,8 @@ def get_status(context=None):
""" """
if context is None: if context is None:
with grpc.insecure_channel("192.168.100.1:9200") as channel: with grpc.insecure_channel("192.168.100.1:9200") as channel:
if not import_ok: if imports_pending:
import_protocols(channel) resolve_imports(channel)
stub = device_pb2_grpc.DeviceStub(channel) stub = device_pb2_grpc.DeviceStub(channel)
response = stub.Handle(device_pb2.Request(get_status={})) response = stub.Handle(device_pb2.Request(get_status={}))
return response.dish_get_status return response.dish_get_status
@ -468,8 +461,8 @@ def get_status(context=None):
while True: while True:
channel, reused = context.get_channel() channel, reused = context.get_channel()
try: try:
if not import_ok: if imports_pending:
import_protocols(channel) resolve_imports(channel)
stub = device_pb2_grpc.DeviceStub(channel) stub = device_pb2_grpc.DeviceStub(channel)
response = stub.Handle(device_pb2.Request(get_status={})) response = stub.Handle(device_pb2.Request(get_status={}))
return response.dish_get_status return response.dish_get_status
@ -721,8 +714,8 @@ def get_history(context=None):
""" """
if context is None: if context is None:
with grpc.insecure_channel("192.168.100.1:9200") as channel: with grpc.insecure_channel("192.168.100.1:9200") as channel:
if not import_ok: if imports_pending:
import_protocols(channel) resolve_imports(channel)
stub = device_pb2_grpc.DeviceStub(channel) stub = device_pb2_grpc.DeviceStub(channel)
response = stub.Handle(device_pb2.Request(get_history={})) response = stub.Handle(device_pb2.Request(get_history={}))
return response.dish_get_history return response.dish_get_history
@ -730,8 +723,8 @@ def get_history(context=None):
while True: while True:
channel, reused = context.get_channel() channel, reused = context.get_channel()
try: try:
if not import_ok: if imports_pending:
import_protocols(channel) resolve_imports(channel)
stub = device_pb2_grpc.DeviceStub(channel) stub = device_pb2_grpc.DeviceStub(channel)
response = stub.Handle(device_pb2.Request(get_history={})) response = stub.Handle(device_pb2.Request(get_history={}))
return response.dish_get_history return response.dish_get_history