From b49e0bfbbd50294a5754380b7c07667dc032b27c Mon Sep 17 00:00:00 2001 From: BernardWez Date: Thu, 30 Nov 2023 13:57:42 +0100 Subject: [PATCH 1/3] Add method `has_existing_state` to StateClient --- elx/state.py | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/elx/state.py b/elx/state.py index e1007b6..d648d77 100644 --- a/elx/state.py +++ b/elx/state.py @@ -61,6 +61,32 @@ def client(self): os.environ["AZURE_STORAGE_CONNECTION_STRING"] ) + @property + def container_name(self) -> str: + """ + Gives the container name where state files are stored in Azure Blob Storage. + + Returns: + str: Name of the container. + """ + return self.base_path.replace("azure://", "") + + def has_existing_state(self, state_file_name: str) -> bool: + """ + Checks for a pre-existing state file. + + Args: + state_file_name (str): The name of the state file to load. + + Returns: + bool: Boolean flag to indicate whether there is a pre-existing state file. + """ + # Get container client where the state file would be located + container = self.client.get_container_client(container=self.container_name) + + # Check if state file exists + return container.get_blob_client(blob=state_file_name).exists() + class GCSStateClient(StateClient): """ @@ -113,6 +139,18 @@ def __init__(self, base_path: str = ".") -> None: self.base_path = base_path self.state_client = state_client_factory(base_path) + def has_existing_state(self, state_file_name: str) -> bool: + """ + Checks for a pre-existing state file. + + Args: + state_file_name (str): The name of the state file to load. + + Returns: + bool: Boolean flag to indicate whether there is a pre-existing state file. + """ + return Path(f"{self.base_path}/{state_file_name}").exists() + def load(self, state_file_name: str) -> dict: """ Load a state file. @@ -123,7 +161,7 @@ def load(self, state_file_name: str) -> dict: Returns: dict: The contents of the state file. """ - if not Path(f"{self.base_path}/{state_file_name}").exists(): + if not self.has_existing_state(state_file_name): return {} with open( From d9ded41bdc1e71dc8264f2bd5a8539608b3821c8 Mon Sep 17 00:00:00 2001 From: BernardWez Date: Thu, 30 Nov 2023 14:09:24 +0100 Subject: [PATCH 2/3] Move `has_existing_state` to StateClient --- elx/state.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/elx/state.py b/elx/state.py index d648d77..b8342c3 100644 --- a/elx/state.py +++ b/elx/state.py @@ -118,6 +118,18 @@ class LocalStateClient(StateClient): def params(self) -> dict: return {} + def has_existing_state(self, state_file_name: str) -> bool: + """ + Checks for a pre-existing state file. + + Args: + state_file_name (str): The name of the state file to load. + + Returns: + bool: Boolean flag to indicate whether there is a pre-existing state file. + """ + return Path(f"{self.base_path}/{state_file_name}").exists() + def state_client_factory(base_path: str) -> StateClient: if base_path.startswith("s3://"): @@ -139,18 +151,6 @@ def __init__(self, base_path: str = ".") -> None: self.base_path = base_path self.state_client = state_client_factory(base_path) - def has_existing_state(self, state_file_name: str) -> bool: - """ - Checks for a pre-existing state file. - - Args: - state_file_name (str): The name of the state file to load. - - Returns: - bool: Boolean flag to indicate whether there is a pre-existing state file. - """ - return Path(f"{self.base_path}/{state_file_name}").exists() - def load(self, state_file_name: str) -> dict: """ Load a state file. @@ -161,7 +161,7 @@ def load(self, state_file_name: str) -> dict: Returns: dict: The contents of the state file. """ - if not self.has_existing_state(state_file_name): + if not self.state_client.has_existing_state(state_file_name): return {} with open( From 17a51bee8753d4f24a51db8e77b11e64de098927 Mon Sep 17 00:00:00 2001 From: BernardWez Date: Fri, 1 Dec 2023 12:42:49 +0100 Subject: [PATCH 3/3] Add default `has_existing_state` method to StateClient --- elx/state.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/elx/state.py b/elx/state.py index b8342c3..07c62ec 100644 --- a/elx/state.py +++ b/elx/state.py @@ -31,6 +31,18 @@ def params(self) -> Dict[str, Any]: "client": self.client, } + def has_existing_state(self, state_file_name: str) -> bool: + """ + Checks for a pre-existing state file. + + Args: + state_file_name (str): The name of the state file to load. + + Returns: + bool: Boolean flag to indicate whether there is a pre-existing state file. + """ + raise NotImplementedError + class S3StateClient(StateClient): """