feat(transports): pass get obj None checks to user

also adds copy obj and children method
This commit is contained in:
izzy lyseggen
2020-12-01 15:11:02 +00:00
parent ee22740a93
commit 5d059b6770
3 changed files with 28 additions and 6 deletions
+14 -2
View File
@@ -63,8 +63,8 @@ class AbstractTransport(Transport):
pass
@abstractmethod
def get_object(self, id: str) -> str:
"""Gets an object
def get_object(self, id: str) -> str or None:
"""Gets an object. Returns `None` if the object is not found.
Arguments:
id {str} -- the hash of the object
@@ -73,3 +73,15 @@ class AbstractTransport(Transport):
str -- the full string representation of the object (or null of no object is found)
"""
pass
@abstractmethod
def copy_object_and_children(self, id: str, target_transport: Transport) -> str:
"""Copies the parent object and all its children to the provided transport.
Arguments:
id {str} -- the id of the object you want to copy
target_transport {AbstractTransport} -- the transport you want to copy the object to
Returns:
str -- the string representation of the root object
"""
pass
+7 -2
View File
@@ -25,14 +25,19 @@ class MemoryTransport(AbstractTransport):
) -> None:
raise NotImplementedError
def get_object(self, id: str) -> str:
def get_object(self, id: str) -> str or None:
if id in self.objects:
return self.objects[id]
else:
raise SpeckleException("No object found in this memory transport")
return None
def begin_write(self) -> None:
self.saved_object_count = 0
def end_write(self) -> None:
pass
def copy_object_and_children(
self, id: str, target_transport: AbstractTransport
) -> str:
raise NotImplementedError
+7 -2
View File
@@ -105,13 +105,13 @@ class SQLiteTransport(AbstractTransport):
print(e)
raise e
def get_object(self, id: str) -> tuple:
def get_object(self, id: str) -> str or None:
self.__check_connection()
with closing(self.__connection.cursor()) as c:
row = c.execute(
"SELECT * FROM objects WHERE hash = ? LIMIT 1", (id,)
).fetchone()
return row
return row[1] if row else None
def begin_write(self):
self.saved_obj_count = 0
@@ -119,6 +119,11 @@ class SQLiteTransport(AbstractTransport):
def end_write(self):
pass
def copy_object_and_children(
self, id: str, target_transport: AbstractTransport
) -> str:
raise NotImplementedError
def close(self):
"""Close the connection to the database"""
if self.__connection: