From 0dfe625030f70dd0704877b8a2de765189ec3c18 Mon Sep 17 00:00:00 2001 From: Akbar Gumbira Date: Fri, 15 Aug 2014 15:20:34 +0700 Subject: [PATCH] Dont bother to continue the process if the sftp contains no shakemaps. --- realtime/exceptions.py | 5 +++++ realtime/make_map.py | 4 ++++ realtime/sftp_shake_data.py | 26 ++++++++++++++++---------- realtime/shake_event.py | 16 +++++++++++----- 4 files changed, 36 insertions(+), 15 deletions(-) diff --git a/realtime/exceptions.py b/realtime/exceptions.py index 898a9ace8b..7a6c7d85ab 100644 --- a/realtime/exceptions.py +++ b/realtime/exceptions.py @@ -103,3 +103,8 @@ class MapComposerError(Exception): class CopyError(Exception): """Raised if a problem occurs copying a file""" pass + + +class SFTPEmptyError(Exception): + """Raised if the SFTP remote directory does not contain any shakemaps.""" + pass diff --git a/realtime/make_map.py b/realtime/make_map.py index 200ddba386..2cfeef7e27 100644 --- a/realtime/make_map.py +++ b/realtime/make_map.py @@ -27,6 +27,7 @@ from realtime.sftp_client import SFtpClient from realtime.utilities import data_dir, is_event_id, realtime_logger_name from realtime.shake_event import ShakeEvent +from realtime.exceptions import SFTPEmptyError # Initialised in realtime.__init__ LOGGER = logging.getLogger(realtime_logger_name()) @@ -90,6 +91,9 @@ def process_event(event_id=None, locale='en'): event_id=event_id, locale=locale, force_flag=True) + except SFTPEmptyError as ex: + LOGGER.info(ex) + return except: LOGGER.exception('An error occurred setting up the shake event.') return diff --git a/realtime/sftp_shake_data.py b/realtime/sftp_shake_data.py index dfdfd13bb7..17ce2e73e6 100644 --- a/realtime/sftp_shake_data.py +++ b/realtime/sftp_shake_data.py @@ -34,7 +34,8 @@ EventIdError, NetworkError, EventValidationError, - CopyError) + CopyError, + SFTPEmptyError) from realtime.sftp_configuration.configuration import ( get_sftp_base_url, get_sftp_port, @@ -115,7 +116,7 @@ def __init__(self, if self.event_id is None: try: self.get_latest_event_id() - except NetworkError: + except (SFTPEmptyError, NetworkError, EventIdError): raise else: # If we fetched it above using get_latest_event_id we assume it is @@ -137,10 +138,12 @@ def reconnect_sftp(self): self.host, self.username, self.password, self.working_dir) def validate_event(self): - """Check that the event associated with this instance exists either - in the local event cache or on the remote ftp site. + """Check that the event associated with this instance exists. - :return: True if valid, False if not + This will check either in the local event cache or on the remote ftp + site. + + :return: True if valid, False if not. :rtype: bool :raises: NetworkError @@ -181,7 +184,7 @@ def cache_paths(self): def is_on_server(self): """Check the event associated with this instance exists on the server. - :return: True if valid, False if not + :return: True if valid, False if not. :raises: NetworkError """ @@ -190,16 +193,19 @@ def is_on_server(self): return self.sftp_client.path_exists(remote_xml_path) def get_list_event_ids(self): - """Get all event id indicated by folder in remote_path - """ + """Get all event id indicated by folder in remote_path.""" dirs = self.sftp_client.get_listing(function=is_event_id) if len(dirs) == 0: - raise Exception('List event is empty') + raise SFTPEmptyError( + 'The SFTP directory does not contain any shakemaps.') return dirs def get_latest_event_id(self): """Return latest event id.""" - event_ids = self.get_list_event_ids() + try: + event_ids = self.get_list_event_ids() + except SFTPEmptyError: + raise now = datetime.now() now = int( diff --git a/realtime/shake_event.py b/realtime/shake_event.py index 43e61ecf37..682e130183 100644 --- a/realtime/shake_event.py +++ b/realtime/shake_event.py @@ -99,7 +99,10 @@ ShapefileCreationError, CityMemoryLayerCreationError, FileNotFoundError, - MapComposerError) + MapComposerError, + SFTPEmptyError, + NetworkError, + EventIdError) LOGGER = logging.getLogger(realtime_logger_name()) QGIS_APP, CANVAS, IFACE, PARENT = get_qgis_app() @@ -153,7 +156,7 @@ def __init__(self, :return: Instance - :raises: EventXmlParseError + :raises: SFTPEmptyError, NetworkError, EventIdError, EventXmlParseError """ # We inherit from QObject for translation support QObject.__init__(self) @@ -165,9 +168,12 @@ def __init__(self, else: # fetch the data from (s)ftp #self.data = ShakeData(event_id, force_flag) - self.data = SftpShakeData( - event=event_id, - force_flag=force_flag) + try: + self.data = SftpShakeData( + event=event_id, + force_flag=force_flag) + except (SFTPEmptyError, NetworkError, EventIdError): + raise self.data.extract() self.event_id = self.data.event_id