From 5d059b67701064e8c81a60042e2d51c8f9ffbba2 Mon Sep 17 00:00:00 2001 From: izzy lyseggen Date: Tue, 1 Dec 2020 15:11:02 +0000 Subject: [PATCH] feat(transports): pass get obj None checks to user also adds copy obj and children method --- speckle/transports/abstract_transport.py | 16 ++++++++++++++-- speckle/transports/memory.py | 9 +++++++-- speckle/transports/sqlite.py | 9 +++++++-- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/speckle/transports/abstract_transport.py b/speckle/transports/abstract_transport.py index bb0dd74..a88b9e3 100644 --- a/speckle/transports/abstract_transport.py +++ b/speckle/transports/abstract_transport.py @@ -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 diff --git a/speckle/transports/memory.py b/speckle/transports/memory.py index d0a05b3..2139445 100644 --- a/speckle/transports/memory.py +++ b/speckle/transports/memory.py @@ -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 diff --git a/speckle/transports/sqlite.py b/speckle/transports/sqlite.py index 4c6b1bd..b79ccc0 100644 --- a/speckle/transports/sqlite.py +++ b/speckle/transports/sqlite.py @@ -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: