refactor: simplify nested try-except blocks

This commit is contained in:
2025-12-20 22:35:01 +01:00
parent 93a124be2a
commit 54c3353667

View File

@@ -12,15 +12,19 @@ from .logger import get_logger, ProgressTracker
class OperationContext: class OperationContext:
def __init__(self, client=None, config_data=None): def __init__(self, client=None, config_data=None):
self.logger = get_logger() self.logger = get_logger()
self.client, self.did = self._initialize_client(client)
self.config_data = self._initialize_config(config_data)
self.batch_size = self.config_data.get("batch_size", 10)
self.delay = self.config_data.get("delay", 1)
def _initialize_client(self, client):
if client is not None: if client is not None:
self.client = client return client, client.me.did
self.did = client.me.did
else:
try: try:
self.auth = Auth() auth = Auth()
self.client = self.auth.login() client = auth.login()
self.did = self.client.me.did return client, client.me.did
except (ValueError, FileNotFoundError) as e: except (ValueError, FileNotFoundError) as e:
self.logger.error(f"Configuration error: {e}") self.logger.error(f"Configuration error: {e}")
raise raise
@@ -30,12 +34,13 @@ class OperationContext:
raise ValueError( raise ValueError(
f"Failed to initialize operation context: {e}") from e f"Failed to initialize operation context: {e}") from e
def _initialize_config(self, config_data):
if config_data is not None: if config_data is not None:
self.config_data = config_data return config_data
else:
try: try:
self.config = Configuration() config = Configuration()
self.config_data = self.config.load() return config.load()
except (ValueError, FileNotFoundError) as e: except (ValueError, FileNotFoundError) as e:
self.logger.error(f"Configuration error: {e}") self.logger.error(f"Configuration error: {e}")
raise raise
@@ -45,9 +50,6 @@ class OperationContext:
raise ValueError( raise ValueError(
f"Failed to load configuration: {e}") from e f"Failed to load configuration: {e}") from e
self.batch_size = self.config_data.get("batch_size", 10)
self.delay = self.config_data.get("delay", 1)
class BaseStrategy: class BaseStrategy:
def get_cursor(self, response): def get_cursor(self, response):