CORD-762 eliminate need for orm to directly import protos
Change-Id: I7668793805a72ff15e3d4c7907628aded5192d4d
diff --git a/xos/grpc/orm.py b/xos/grpc/orm.py
index 79d43e9..538a278 100644
--- a/xos/grpc/orm.py
+++ b/xos/grpc/orm.py
@@ -21,8 +21,6 @@
import functools
from google.protobuf.empty_pb2 import Empty
-from protos.common_pb2 import ID
-from protos.xosoptions_pb2 import foreignKey, reverseForeignKey
from google.protobuf import symbol_database as _symbol_database
_sym_db = _symbol_database.Default()
@@ -46,6 +44,7 @@
for (name, field) in self._wrapped_class.DESCRIPTOR.fields_by_name.items():
if name.endswith("_id"):
+ foreignKey = field.GetOptions().Extensions._FindExtensionByName("xos.foreignKey")
fk = field.GetOptions().Extensions[foreignKey]
if fk:
fkmap[name[:-3]] = {"src_fieldName": name, "modelName": fk.modelName}
@@ -57,6 +56,7 @@
for (name, field) in self._wrapped_class.DESCRIPTOR.fields_by_name.items():
if name.endswith("_ids"):
+ reverseForeignKey = field.GetOptions().Extensions._FindExtensionByName("xos.reverseForeignKey")
fk = field.GetOptions().Extensions[reverseForeignKey]
if fk:
reverse_fkmap[name[:-4]] = {"src_fieldName": name, "modelName": fk.modelName}
@@ -68,9 +68,8 @@
return ORMWrapper(self.cache[name], self.stub)
fk_entry = self._fkmap[name]
- get_method = getattr(self.stub, "Get%s" % fk_entry["modelName"])
- id=ID(id=getattr(self, fk_entry["src_fieldName"]))
- dest_model = get_method(id)
+ id=self.stub.make_ID(id=getattr(self, fk_entry["src_fieldName"]))
+ dest_model = self.stub.invoke("Get%s" % fk_entry["modelName"], id)
self.cache[name] = dest_model
@@ -106,18 +105,15 @@
def save(self):
if self.is_new:
- create_method = getattr(self.stub,"Create%s" % self._wrapped_class.__class__.__name__)
- new_class = create_method(self._wrapped_class)
+ new_class = self.stub.invoke("Create%s" % self._wrapped_class.__class__.__name__, self._wrapped_class)
self._wrapped_class = new_class
self.is_new = False
else:
- update_method = getattr(self.stub,"Update%s" % self._wrapped_class.__class__.__name__)
- update_method(self._wrapped_class)
+ self.stub.invoke("Update%s" % self._wrapped_class.__class__.__name__, self._wrapped_class)
def delete(self):
- delete_method = getattr(self.stub,"Delete%s" % self._wrapped_class.__class__.__name__)
- id = ID(id=self._wrapped_class.id)
- delete_method(id)
+ id = self.stub.make_ID(id=self._wrapped_class.id)
+ self.stub.invoke("Delete%s" % self._wrapped_class.__class__.__name__, id)
class ORMLocalObjectManager(object):
""" Manages a local list of objects """
@@ -133,9 +129,8 @@
return self._cache
models = []
- get_method = getattr(self._stub, "Get%s" % self._modelName)
for id in self._idList:
- models.append(get_method(ID(id=id)))
+ models.append(self._stub.invoke("Get%s" % self._modelName, self._stub.make_ID(id=id)))
self._cache = models
@@ -163,12 +158,10 @@
return result
def all(self):
- list_method = getattr(self._stub, "List%s" % self._modelName)
- return self.wrap_list(list_method(Empty()))
+ return self.wrap_list(self._stub.invoke("List%s" % self._modelName, Empty()))
def get(self, id):
- get_method = getattr(self._stub, "Get%s" % self._modelName)
- return self.wrap_single(get_method(ID(id=id)))
+ return self.wrap_single(self._stub.invoke("Get%s" % self._modelName, self._stub.make_ID(id=id)))
def new(self, **kwargs):
full_model_name = "%s.%s" % (self._packageName, self._modelName)
@@ -181,10 +174,20 @@
class ORMStub(object):
def __init__(self, stub, package_name):
+ self.grpc_stub = stub
+
for name in dir(stub):
if name.startswith("Get"):
model_name = name[3:]
- setattr(self,model_name, ORMModelClass(stub, model_name, package_name))
+ setattr(self,model_name, ORMModelClass(self, model_name, package_name))
+
+ def invoke(self, name, request):
+ method = getattr(self.grpc_stub, name)
+ return method(request)
+
+ def make_ID(self, id):
+ return _sym_db._classes["xos.ID"](id=id)
+
#def wrap_get(*args, **kwargs):
# stub=kwargs.pop("stub")