def get_latest_event_id(self): """Query the ftp server and determine the latest event id. :return: A string containing a valid event id. :raises: NetworkError """ ftp_client = FtpClient() try: ftp_client_list = ftp_client.get_listing() ftp_client_list.sort(key=lambda x: x.lower()) except NetworkError: raise now = datetime.now() now = int( '%04d%02d%02d%02d%02d%02d' % (now.year, now.month, now.day, now.hour, now.minute, now.second)) event_id = now + 1 while int(event_id) > now: if len(ftp_client_list) < 1: raise EventIdError('Latest Event Id could not be obtained') event_id = ftp_client_list.pop().split('/')[-1].split('.')[0] if event_id is None: raise EventIdError('Latest Event Id could not be obtained') self.event_id = event_id
def getLatestEventId(self): """Return latest event id """ event_ids = self.get_list_event_ids() latest_event_id = None if event_ids is not None: event_ids.sort() latest_event_id = event_ids[-1] if latest_event_id is None: raise EventIdError('Latest Event Id could not be obtained') self.eventId = latest_event_id return self.eventId
def __init__(self, theEvent=None, theHost=defaultHost, theUserName=defUserName, thePassword=defPassword, theWorkingDir=defWorkDir, theForceFlag=False): """Constructor for the SftpShakeData class Args: * theEvent - (Optional) a string representing the event id that this raster is associated with. e.g. 20110413170148. **If no event id is supplied, a query will be made to the ftp server, and the latest event id assigned.** * theHost - (Optional) a string representing the ip address or host name of the server from which the data should be retrieved. It assumes that the data is in the root directory. Returns: None Raises: None """ self.eventId = theEvent self.host = theHost self.username = theUserName self.password = thePassword self.workdir = theWorkingDir self.forceFlag = theForceFlag self.sftpclient = SFtpClient(self.host, self.username, self.password, self.workdir) if self.eventId is None: try: self.getLatestEventId() except NetworkError: raise else: # If we fetched it above using getLatestEventId we assume it is # already validated. try: self.validateEvent() except EventValidationError: raise # If eventId is still None after all the above, moan.... if self.eventId is None: myMessage = ('No id was passed to the constructor and the ' 'latest id could not be retrieved from the' 'server.') LOGGER.exception('ShakeData initialisation failed') raise EventIdError(myMessage)
def getLatestEventId(self): """Query the ftp server and determine the latest event id. Args: None Returns: A string containing a valid event id. Raises: NetworkError """ myFtpClient = FtpClient() try: myList = myFtpClient.getListing() except NetworkError: raise myEventId = myList[-1].split('/')[-1].split('.')[0] if myEventId is None: raise EventIdError('Latest Event Id could not be obtained') self.eventId = myEventId
def __init__(self, event=None, host=default_host, user_name=def_user_name, password=def_password, working_dir=def_work_dir, force_flag=False): """Constructor for the SftpShakeData class :param event: (Optional) a string representing the event id that this raster is associated with. e.g. 20110413170148. **If no event id is supplied, a query will be made to the ftp server, and the latest event id assigned.** :param host: (Optional) a string representing the ip address or host name of the server from which the data should be retrieved. It assumes that the data is in the root directory. """ self.event_id = event self.host = host self.username = user_name self.password = password self.workdir = working_dir self.force_flag = force_flag self.sftpclient = SFtpClient(self.host, self.username, self.password, self.workdir) if self.event_id is None: try: self.get_latest_event_id() except NetworkError: raise else: # If we fetched it above using get_latest_event_id we assume it is # already validated. try: self.validate_event() except EventValidationError: raise # If event_id is still None after all the above, moan.... if self.event_id is None: message = ('No id was passed to the constructor and the ' 'latest id could not be retrieved from the' 'server.') LOGGER.exception('ShakeData initialisation failed') raise EventIdError(message)
def __init__(self, theEvent=None, theHost='118.97.83.243'): """Constructor for the ShakeData class Args: * theEvent - (Optional) a string representing the event id that this raster is associated with. e.g. 20110413170148. **If no event id is supplied, a query will be made to the ftp server, and the latest event id assigned.** * theHost - (Optional) a string representing the ip address or host name of the server from which the data should be retrieved. It assumes that the data is in the root directory. Defaults to 118.97.83.243 Returns: None Raises: None """ self.eventId = theEvent self.host = theHost # private Shake event instance associated with this shake dataset self._shakeEvent = None if self.eventId is None: try: self.getLatestEventId() except NetworkError: raise else: # If we fetched it above using getLatestEventId we assume it is # already validated. try: self.validateEvent() except EventValidationError: raise # If eventId is still None after all the above, moan.... if self.eventId is None: myMessage = ('No id was passed to the constructor and the ' 'latest id could not be retrieved from the server.') LOGGER.exception('ShakeData initialisation failed') raise EventIdError(myMessage)
def get_latest_event_id(self): """Return latest event id. """ event_ids = self.get_list_event_ids() now = datetime.now() now = int( '%04d%02d%02d%02d%02d%02d' % (now.year, now.month, now.day, now.hour, now.minute, now.second)) if event_ids is not None: event_ids.sort() latest_event_id = now + 1 while int(latest_event_id) > now: if len(event_ids) < 1: raise EventIdError('Latest Event Id could not be obtained') latest_event_id = event_ids.pop() self.event_id = latest_event_id return self.event_id