Example #1
0
def awscli_region(profile_name):
    """
    Summary:
        Sets default AWS region
    Args:
        profile_name:  a username in local awscli profile
    Returns:
        region (str): AWS region code | None
    Raises:
        Exception if profile_name not found in config
    """
    awscli = 'aws'

    if not which(awscli):
        print('Unable to locate awscli')
        return None
    else:
        cmd = awscli + ' configure get ' + profile_name + '.region'

    try:
        region = subprocess.getoutput(cmd)
    except Exception:
        logger.exception(
            '%s: Failed to identify AccessKeyId used in %s profile.' %
            (inspect.stack()[0][3], profile_name))
        return None
    return region
Example #2
0
    def end(self):
        """
        Summary:
            Mark point in time where timer ends duration

        Returns:
            elapsed time in seconds with precision, TYPE: int
            Mark point datetime representation stored in self.end_time, TYPE: datetime
        """
        try:
            end_time = time.time()
            duration = end_time - self.start_time
            self.end_time = time.strftime('%H:%M:%S', time.localtime(end_time))
        except Exception as e:
            logger.exception(
                '%s: Unknown error when calculating end time (Code: %s)' %
                (inspect.stack()[0][3], str(e)))
            return 0.00
        return round(duration, self.precision)
Example #3
0
def convert_timedelta(duration):
    """
    Summary:
        Convert duration into component time units
    Args:
        :duration (datetime.timedelta): time duration to convert
    Returns:
        days, hours, minutes, seconds | TYPE: tuple (integers)
    """
    try:
        days, seconds = duration.days, duration.seconds
        hours = seconds // 3600
        minutes = (seconds % 3600) // 60
        seconds = (seconds % 60)
    except Exception:
        logger.exception(
            f'{inspect.stack()[0][3]}: Input must be datetime.timedelta object'
        )
        return 0, 0, 0, 0
    return days, hours, minutes, seconds
Example #4
0
def convert_dt_human(duration, return_iter=False):
    """
    Summary:
        convert timedelta objects to human readable output
    Args:
        :duration (datetime.timedelta): time duration to convert
        :return_iter (tuple):  tuple containing time sequence
    Returns:
        days, hours, minutes, seconds | TYPE: tuple (integers), OR
        human readable, notated units | TYPE: string
    """
    try:
        days, hours, minutes, seconds = convert_timedelta(duration)
        if return_iter:
            return days, hours, minutes, seconds
        # string format conversions
        if days > 0:
            format_string = ('{} day{}, {} hour{}'.format(
                days, 's' if days != 1 else '', hours,
                's' if hours != 1 else ''))
        elif hours > 1:
            format_string = ('{} hour{}, {} minute{}'.format(
                hours, 's' if hours != 1 else '', minutes,
                's' if minutes != 1 else ''))
        else:
            format_string = ('{} minute{}, {} sec{}'.format(
                minutes, 's' if minutes != 1 else '', seconds,
                's' if seconds != 1 else ''))
    except AttributeError as e:
        logger.exception(
            '%s: Type mismatch when converting timedelta objects (Code: %s)' %
            (inspect.stack()[0][3], str(e)))
        raise e
    except Exception as e:
        logger.exception(
            '%s: Unknown error when converting datetime objects (Code: %s)' %
            (inspect.stack()[0][3], str(e)))
        raise e
    return format_string