Beispiel #1
0
def _process_json(response_body):
    """
    Returns a UwPassword objects
    """
    data = json.loads(response_body)
    uwpassword = UwPassword(uwnetid=data["uwNetID"],
                            kerb_status=data["kerbStatus"],
                            interval=None,
                            last_change=None,
                            last_change_med=None,
                            expires_med=None,
                            interval_med=None,
                            minimum_length=int(data["minimumLength"]),
                            time_stamp=parse(data["timeStamp"]),)
    if "lastChange" in data:
        uwpassword.last_change = parse(data["lastChange"])

    if "interval" in data:
        uwpassword.interval = timeparse(data["interval"])

    if "lastChangeMed" in data:
        uwpassword.last_change_med = parse(data["lastChangeMed"])

    if "expiresMed" in data:
        uwpassword.expires_med = parse(data["expiresMed"])

    if "intervalMed" in data:
        uwpassword.interval_med = timeparse(data["intervalMed"])

    if "netidStatus" in data:
        netid_status = []
        for status in data["netidStatus"]:
            netid_status.append(status)
        uwpassword.netid_status = netid_status
    return uwpassword
Beispiel #2
0
    def on_trigger(self, message):
        """
        @type message: hubbot.message.IRCMessage
        """
        flag = False
        if len(message.parameter_list) == 1:
            try:
                delay = int(message.parameter_list[0])
                flag = True
            except Exception:
                delay = timeparse(message.parameter_list[0])
        else:
            delay = timeparse(" ".join(message.parameter_list))
        if delay is None or delay <= 0:
            return IRCResponse(ResponseType.SAY, "I don't think I understand that...", message.reply_to)
        elif delay > (60 * 60 * 24 * 365):
            return IRCResponse(ResponseType.SAY, "Do you really need a timer that long?", message.reply_to)
        elif delay <= 1:
            return IRCResponse(ResponseType.SAY, "Your timer is up now, {}.".format(message.user.name), message.reply_to)

        else:
            reactor.callLater(delay, self.notify_user, flag, message)
            if flag:
                return IRCResponse(ResponseType.SAY, "{}: A {} second timer has been started!".format(message.user.name, message.parameter_list[0]), message.reply_to)
            else:
                return IRCResponse(ResponseType.SAY, "{}: A {} timer has been started!".format(message.user.name, " ".join(message.parameter_list)), message.reply_to)
Beispiel #3
0
 def test_timeparse_10(self):
     '''timeparse test case 10.'''
     self.assertAlmostEqual(timeparse.timeparse('2:04:13:02.266'),
                            187982.266)
     self.assertAlmostEqual(timeparse.timeparse('+2:04:13:02.266'),
                            187982.266)
     self.assertAlmostEqual(timeparse.timeparse('-2:04:13:02.266'),
                            -187982.266)
Beispiel #4
0
 def test_timeparse_14(self):
     '''timeparse test case 14.'''
     self.assertEqual(timeparse.timeparse('5 hours, 34 minutes, 56 seconds'),
                      20096)
     self.assertEqual(timeparse.timeparse('+5 hours, 34 minutes, 56 seconds'),
                      20096)
     self.assertEqual(timeparse.timeparse('-5 hours, 34 minutes, 56 seconds'),
                      -20096)
Beispiel #5
0
 def test_timeparse_12(self):
     '''timeparse test case 12.'''
     self.assertAlmostEqual(timeparse.timeparse('2 days,  4:13:02.266'),
                            187982.266)
     self.assertAlmostEqual(timeparse.timeparse('+2 days,  4:13:02.266'),
                            187982.266)
     self.assertAlmostEqual(timeparse.timeparse('-2 days,  4:13:02.266'),
                            -187982.266)
Beispiel #6
0
 def test_timeparse_16(self):
     '''timeparse test case 16.'''
     self.assertEqual(
         timeparse.timeparse('2 days, 5 hours, 34 minutes, 56 seconds'),
         192896)
     self.assertEqual(
         timeparse.timeparse('+2 days, 5 hours, 34 minutes, 56 seconds'),
         192896)
     self.assertEqual(
         timeparse.timeparse('-2 days, 5 hours, 34 minutes, 56 seconds'),
         -192896)
 def test_timeparse_multipliers(self):
     '''Test parsing time unit multipliers.'''
     self.assertEqual(timeparse.timeparse('32 min'),
                      1920)
     self.assertEqual(timeparse.timeparse('1 min'),
                      60)
     self.assertEqual(timeparse.timeparse('1 hours'),
                      3600)
     self.assertEqual(timeparse.timeparse('1 day'),
                      86400)
     self.assertEqual(timeparse.timeparse('1 sec'),
                      1)
Beispiel #8
0
def parse(executor, result, futures):
    for line in result.splitlines():
        words = line.split()

        if len(words) >= 5:
            status = words[4]
            namespace = words[0]
            name = words[1]
            idx = namespace + ":" + name
            if (status.startswith("Failed")) or (status == "Cancelled") or \
                    (status.startswith("Error")):
                if idx in global_build_status.keys():
                    logger.debug(idx + " FAILED")
                    if global_build_status[idx] < STATUS_NOT_COMPLETE:
                        global_build_status[idx] = STATUS_NOT_COMPLETE
                        stats_idx = idx[0:idx.rindex('-')]
                        global_build_stats[stats_idx]["failed"] += 1
            elif "Complete" == words[4]:
                if idx in global_build_status.keys():
                    if global_build_status[idx] < STATUS_COMPLETE:
                        logger.info(idx + " Complete")
                        global_build_status[idx] = STATUS_COMPLETE
                    duration_string = words[-1]
                    if global_build_status[idx] < STATUS_LOGGING:
                        futures.append(
                            executor.submit(do_post_actions, namespace,
                                            name, timeparse(duration_string)))
        else:
            logger.error("unexpected return "
                         "(oc get build --all-namespaces --no-headers): "
                         + result)
def timedelta_type(value):
    """Return the :class:`datetime.datetime.DateTime` for a time in the past.
    :param value: a string containing a time format supported by :mod:`pytimeparse`
    """
    if value is None:
        return None
    return datetime_seconds_ago(timeparse.timeparse(value))
Beispiel #10
0
def parse_time_interval(time_start, time_end):
    """created time values for time_start and time_end, while time_end
    will be replaced with time_start+ a duration if the duration is
    given in time_end. The format of the duration is intuitive through
    the timeparse module. YOu can specify values such as +1d, +1w10s.

    :param time_start: the start time, if the string 'current_time' is
                       passed it will be replaced by the current time

    :param time_end: either a time or a duration
    """
    t_end = time_end
    t_start = time_start

    if t_start is not None:
        if t_start in ["current_time", "now"]:
            t_start = str(datetime.now())

    if t_end is not None:
        if t_end.startswith("+"):
            duration = t_end[1:]
            delta = timeparse(duration)
            t_start = datetime.strptime(t_start, "%Y-%m-%d %H:%M:%S.%f")
            t_end = t_start + timedelta(seconds=delta)

    return (str(t_start), str(t_end))
Beispiel #11
0
 def validate_interval(self, field):
     try:
         int(field.data)
     except ValueError:
         if timeparse(field.data) is None:
             raise ValidationError("interval must either be a number "
                                   "(in seconds) or a human-readable "
                                   "string like '1h2m'  or '1d12h'")
Beispiel #12
0
def is_timedelta_expression(expr):
    """True IFF expr is of type timedelta or can be used to create a timedelta"""
    try:
        return isinstance(expr, timedelta) or timeparse(expr) is not None
    except:
        return False

    return False
Beispiel #13
0
 def query_data(self, query_expr):
     now = datetime.datetime.now()
     query = PrometheusQuery(
         query_expr,
         time.mktime((now - datetime.timedelta(
             seconds=timeparse(self.cfg.model["data_range"]))).timetuple()),
         time.mktime(now.timetuple()), "15s")
     return self.api.query(query)
Beispiel #14
0
    def parse_pattern(self, event: events.ChatAction):
        chat_id = int(event.pattern_match.group(1))
        ban_duration = event.pattern_match.group(2)
        ban_message = event.pattern_match.group(3)
        ban_end_date = datetime.utcnow() + timedelta(
            seconds=timeparse(ban_duration))

        return chat_id, ban_duration, ban_message, ban_end_date
Beispiel #15
0
    def process_schedule(self, schedule_list):
        application_run_list = []
        for application_schedule in schedule_list:
            if application_schedule['app_name'] not in self.application_list:
                continue
            # Don't use intervals less that minimum_task_interval
            interval_seconds = max(timeparse(application_schedule['interval']),
                                   timeparse(self.minimum_task_interval))

            # Unix timestamp in UTC, seconds
            # Python version awareness
            if six.PY2:
                timestamp_now = time.mktime(
                    datetime.datetime.now(pytz.utc).timetuple())
            else:
                timestamp_now = int(
                    datetime.datetime.now(pytz.utc).timestamp())

            timestamp_diff = timestamp_now - application_schedule['last_run']

            if (timestamp_diff >
                    interval_seconds) or application_schedule['run_now']:
                self.log.debug(
                    "Application %s time diff %s more than %s, run now flag is %s. Schedule it to run."
                    % (application_schedule['app_name'], timestamp_diff,
                       interval_seconds, application_schedule['run_now']))
                application_schedule['run_now'] = False
                application_run_list.append(
                    (application_schedule['app_name'],
                     application_schedule['run_parameters']))
                application_schedule['last_run'] = timestamp_now

        # Run tasks in different threads. Usually we don't need results of run_now.
        app_run_results = []
        with concurrent.futures.ThreadPoolExecutor(
                max_workers=self.max_threads) as executor:
            app_exec_pool = [
                executor.submit(
                    self.apps(app_name).run_app, app_run_parameters)
                for app_name, app_run_parameters in application_run_list
            ]
            for future in concurrent.futures.as_completed(app_exec_pool):
                app_run_results.append(future.result())

        return schedule_list
Beispiel #16
0
def generate_start_end_time(from_string="30m", to_string=None):
    """Parses the --from and --to command line arguments to create python
    datetime objects representing the start and end times for log retrieval

    :param from_string: The --from argument, defaults to 30 minutes
    :param to_string: The --to argument, defaults to the time right now
    :return: A tuple containing start_time, end_time, which specify the interval of log retrieval
    """
    if to_string is None:
        end_time = datetime.datetime.utcnow()
    else:
        # Try parsing as a a natural time duration first, if that fails move on to
        # parsing as an ISO-8601 timestamp
        to_duration = timeparse(to_string)

        if to_duration is not None:
            end_time = datetime.datetime.utcnow() - datetime.timedelta(
                seconds=to_duration)
        else:
            end_time = isodate.parse_datetime(to_string)
            if not end_time:
                raise ValueError(
                    "--to argument not in ISO8601 format and not a valid pytimeparse duration"
                )

    from_duration = timeparse(from_string)
    if from_duration is not None:
        start_time = datetime.datetime.utcnow() - datetime.timedelta(
            seconds=from_duration)
    else:
        start_time = isodate.parse_datetime(from_string)

        if not start_time:
            raise ValueError(
                "--from argument not in ISO8601 format and not a valid pytimeparse duration"
            )

    # Covert the timestamps to something timezone aware
    start_time = pytz.utc.localize(start_time)
    end_time = pytz.utc.localize(end_time)

    if start_time > end_time:
        raise ValueError("Start time bigger than end time")

    return start_time, end_time
Beispiel #17
0
 def check_window_valid(window):
     """Takes in the window parameter string, reformats as a float."""
     if window is None:
         msg = 'Moving aggregate must have window specified.'
         raise aggregates.CustomAggFailure(msg)
     try:
         return float(timeparse.timeparse(six.text_type(window)))
     except Exception:
         raise aggregates.CustomAggFailure('Invalid value for window')
Beispiel #18
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--log-level",
                        help="Logging level (default: INFO). \
                            (EKORRE_LOG_LEVEL)",
                        type=str,
                        default=os.environ.get('EKORRE_LOG_LEVEL', 'INFO'))
    parser.add_argument(
        "--address",
        help="Address the daemon will bind on (default: 0.0.0.0). \
                            (EKORRE_DAEMON_ADDRESS)",
        type=str,
        default=os.environ.get('EKORRE_DAEMON_ADDRESS', '0.0.0.0'))
    parser.add_argument("--port",
                        help="Port the daemon will bind on (default: 8582). \
                            (EKORRE_DAEMON_PORT)",
                        type=str,
                        default=os.environ.get('EKORRE_DAEMON_PORT', '8582'))
    parser.add_argument("--destination-bucket",
                        help="Bucket where the snapshot will be stored. \
                            (EKORRE_DESTINATION_BUCKET)",
                        type=str,
                        default=os.environ.get('EKORRE_DESTINATION_BUCKET'))
    parser.add_argument("--kms-key",
                        help="KMS key used to encrypt the backups. \
                            (EKORRE_KMS_KEY)",
                        type=str,
                        default=os.environ.get('EKORRE_KMS_KEY'))
    parser.add_argument(
        "--refresh-interval",
        help=
        "Interval between the refresh of the snapshots' list (default: 24h). \
                                (EKORRE_REFRESH_INTERVAL)",
        type=str,
        default=os.environ.get('EKORRE_REFRESH_INTERVAL', '24h'))

    args, unknown = parser.parse_known_args()
    _setup_logging(args.log_level)

    refresh_interval = timeparse(args.refresh_interval)

    start_http_server(int(args.port), addr=args.address)

    ekorre_role = _get_ekorre_role()

    while True:
        try:
            rds_snapshots = _list_rds_snapshots()
            s3_snapshots = _list_s3_snapshots(args.destination_bucket)
            snapshots_to_backup = _list_snapshots_to_backup(
                rds_snapshots, s3_snapshots)
            for snapshot in snapshots_to_backup:
                _backup_snapshot(args.destination_bucket, snapshot,
                                 ekorre_role, args.kms_key)
            time.sleep(refresh_interval)
        except (KeyboardInterrupt, SystemExit):
            raise
Beispiel #19
0
def timedelta_type(value):
    """Return the :class:`datetime.datetime.DateTime` for a time in the past.

    :param value: a string containing a time format supported by
    mod:`pytimeparse`
    """
    if value is None:
        return None
    return datetime_seconds_ago(timeparse.timeparse(value))
Beispiel #20
0
 def check_window_valid(window):
     """Takes in the window parameter string, reformats as a float."""
     if window is None:
         msg = 'Moving aggregate must have window specified.'
         raise aggregates.CustomAggFailure(msg)
     try:
         return float(timeparse.timeparse(six.text_type(window)))
     except Exception:
         raise aggregates.CustomAggFailure('Invalid value for window')
Beispiel #21
0
def parse_duration_as_datetime(val: str):
    try:
        parsed = timeparse(val)
        if parsed is None:
            raise argparse.ArgumentTypeError(f'Not a valid duration: {val}.')
        delta = timedelta(seconds=parsed)
        return now_utc - delta
    except Exception:
        raise argparse.ArgumentTypeError(f'Not a valid duration: {val}.')
Beispiel #22
0
    def execute(self, message: IRCMessage):
        if len(message.parameterList) < 2:
            return IRCResponse(ResponseType.Say, self.help(None),
                               message.replyTo)

        command = message.parameterList[1].lower()
        delay = timeparse(message.parameterList[0])
        if not delay:
            delay = 0

        delayDelta = datetime.timedelta(seconds=delay)
        delayString = string.deltaTimeToString(delayDelta, 's')
        params = message.parameterList[2:]
        commandString = '{}{} {}'.format(self.bot.commandChar, command,
                                         ' '.join(params))
        commandString = commandString.replace('$delayString', delayString)
        commandString = commandString.replace('$delay', str(delay))

        newMessage = IRCMessage(message.type, message.user, message.channel,
                                commandString, self.bot)

        moduleHandler = self.bot.moduleHandler
        if command in moduleHandler.mappedTriggers:
            module = moduleHandler.mappedTriggers[command].execute
            d = task.deferLater(reactor, delay, module, newMessage)
            d.addCallback(self._activate)
            d.addErrback(self._deferredError)
            return IRCResponse(
                ResponseType.Say,
                "OK, I'll execute that in {}".format(delayString),
                message.replyTo, {
                    'delay': delay,
                    'delayString': delayString
                })
        else:
            if 'Alias' not in moduleHandler.commands:
                return IRCResponse(
                    ResponseType.Say,
                    "'{}' is not a recognized command".format(command),
                    message.replyTo)

            if command not in moduleHandler.commands['Alias'].aliases:
                return IRCResponse(
                    ResponseType.Say,
                    "'{}' is not a recognized command or alias".format(
                        command), message.replyTo)

            d = task.deferLater(reactor, delay,
                                moduleHandler.commands['Alias'].execute,
                                newMessage)
            d.addCallback(self._activate)
            d.addErrback(self._deferredError)
            return IRCResponse(
                ResponseType.Say,
                "OK, I'll execute that in {}".format(delayString),
                message.replyTo)
Beispiel #23
0
 def __init__(self, md_instance, store, model, data_source, slack_channel,
              timeout):
     super(Rules, self).__init__(
         RULES_MODEL_NAME, md_instance, store,
         RulesConfig(store.get_model_template(RULES_MODEL_NAME),
                     model.get("config", None)))
     self.api = PrometheusAPI(data_source)
     self.slack_channel = slack_channel
     self.timer = Timer(timeparse(timeout), self.timeout_action)
     self.metrics = model.get("metrics")
Beispiel #24
0
def parse_snapshot_settings(snapshot_settings):
    """convert JSON snapshot settings to timedeltas"""

    # validate keys are present
    expected_keys = ['retention', 'minimum', 'frequency']
    for k in expected_keys:
        if k not in snapshot_settings['snapshot']:
            raise Exception('missing required snapshot setting {}'.format(k))

    retention_seconds = timeparse(snapshot_settings['snapshot']['retention'])
    retention = timedelta(seconds=retention_seconds)

    f_expr = snapshot_settings['snapshot']['frequency']
    if is_timedelta_expression(f_expr):
        frequency_seconds = timeparse(f_expr)
        return retention, timedelta(seconds=frequency_seconds)
    elif is_crontab_expression(f_expr):
        return retention, CronTab(f_expr)
    else:
        raise Exception('Could not identify expression', f_expr)
Beispiel #25
0
    def get_times_matrix(self) -> np.array:
        """
        (nº iterations)
        :return:
        """
        times_matrix = np.zeros(len(self.iteration_list))
        for i, it in enumerate(self.iteration_list):
            duration_str = it['execution_info']['duration']
            times_matrix[i] = timeparse(duration_str)

        return times_matrix
Beispiel #26
0
def execute(targets, args):
    execute_argparser = argparse.ArgumentParser(
        'execute a command on target of machines')
    execute_argparser.add_argument('-t',
                                   '--timeout',
                                   help='timeout to execute commands. ',
                                   default='2m')
    execute_argparser.add_argument('command')
    execute_argparser.add_argument('args', nargs=argparse.REMAINDER)
    args = execute_argparser.parse_args(args)
    timeout = timeparse(args.timeout)
    if args.args:
        cmd = convert_list_command_to_str([args.command, *args.args])
    else:
        cmd = args.command

    l = Lock()
    for target in targets:
        print(f'Start executing on {target}')

    def exec(i):
        target = targets[i]
        ret = None
        try:
            log_path = os.path.join(config.logs_dir, str(target))
            log = open(log_path, 'w')
            proc = target.run(cmd, timeout=timeout, stdout=log, stderr=log)
            if proc.returncode == 0:
                output = f'{term.green}SUCCESS{term.off} on {target}'
            else:
                output = f'{term.red}FAIL{term.off} on {target}. Exit code: {proc.returncode}'
            ret = proc
        except RunException as e:
            output = f'{term.red}FAIL{term.off} on {target}. Timeout'
            ret = e
        output += f'. Log: file://{log_path}'
        with l:
            term.saveCursor()
            term.up(len(targets) - i)
            term.clearLine()
            term.writeLine(output)
            term.restoreCursor()
        return ret

    results = pmap(exec, range(len(targets)))
    if all(
            map(lambda r: isinstance(r, RunResult) and r.returncode == 0,
                results)):
        term.writeLine('All execution succeeded', term.green)
        exit(0)
    else:
        term.writeLine('Some execution failed', term.red)
        exit(1)
 def from_dict(cls, raw_data: OrderedDict) -> CallMetadata:
     """
     Create an instance of the class from a given dict.
     """
     date = _parse_date(raw_data[FieldNames.DATE],
                        raw_data[FieldNames.TIME])
     return cls(name=raw_data[FieldNames.NAME],
                number=_parse_number(raw_data[FieldNames.NUMBER]),
                duration=timeparse(raw_data[FieldNames.DURATION]),
                call_type=raw_data[FieldNames.TYPE],
                date=date.datetime,
                week_day=date.weekday())
Beispiel #28
0
 def __init__(self, md_instance, store, model, data_source, slack_channel,
              timeout):
     super(IForest, self).__init__(
         IFOREST_MODEL_NAME, md_instance, store,
         IForestConfig(store.get_model_template(IFOREST_MODEL_NAME),
                       model.get("config", None)))
     self.api = PrometheusAPI(data_source)
     self.slack_channel = slack_channel
     self.df = dict()
     self.ilf = dict()
     self.timer = Timer(timeparse(timeout), self.timeout_action)
     self.metrics = model.get("metrics")
Beispiel #29
0
    def process_misfire_grace_time(misfire_grace_time):
        """Process the 'misfire_grace_time' parameter."""
        if misfire_grace_time:
            seconds = timeparse(misfire_grace_time)

            if seconds is None:
                raise ScheduleJobError(
                    "Unable to parse format for 'misfire_grace_time': '%s'" %
                    misfire_grace_time)

            misfire_grace_time = seconds
        return misfire_grace_time
Beispiel #30
0
def parse_timedelta(value):
    """Return the delta in nanoseconds.
    :param value: a string containing a time format supported by :mod:`pytimeparse`
    :returns: an integer (or float) representing the specified delta in nanoseconds
    """
    error_msg = "'%s' is not a valid time expression" % value
    try:
        seconds = timeparse.timeparse(value)
    except TypeError:
        raise argparse.ArgumentTypeError(error_msg)
    if not seconds:
        raise argparse.ArgumentTypeError(error_msg)
    return seconds_to_nanoseconds(seconds)
Beispiel #31
0
def parse_timedelta(value):
    """Return the delta in nanoseconds.
    :param value: a string containing a time format supported by :mod:`pytimeparse`
    :returns: an integer (or float) representing the specified delta in nanoseconds
    """
    error_msg = "'%s' is not a valid time expression" % value
    try:
        seconds = timeparse.timeparse(value)
    except TypeError:
        raise argparse.ArgumentTypeError(error_msg)
    if not seconds:
        raise argparse.ArgumentTypeError(error_msg)
    return seconds_to_nanoseconds(seconds)
Beispiel #32
0
async def reply(message):
    """Reply to use that their value had been accepted."""
    try:
        time_string = message.content.removeprefix('/remindme').strip()
        time_seconds = timeparse(time_string)
        Reminder.queue.put(
            [time.time() + time_seconds, message.id, message.channel.id])
        await Reminder.save()
        await message.reply(f'I will remind you in {time_string}')
    except Exception as e:  # pylint: disable=W0703,C0103
        logging.debug(
            f"Couldn't parse remindme message: {message.content} e: {e}")
        await message.reply(help_string())
Beispiel #33
0
def generate_start_end_time(from_string="30m", to_string=None):
    """Parses the --from and --to command line arguments to create python
    datetime objects representing the start and end times for log retrieval

    :param from_string: The --from argument, defaults to 30 minutes
    :param to_string: The --to argument, defaults to the time right now
    :return: A tuple containing start_time, end_time, which specify the interval of log retrieval
    """
    if to_string is None:
        end_time = datetime.datetime.utcnow()
    else:
        # Try parsing as a a natural time duration first, if that fails move on to
        # parsing as an ISO-8601 timestamp
        to_duration = timeparse(to_string)

        if to_duration is not None:
            end_time = datetime.datetime.utcnow() - datetime.timedelta(seconds=to_duration)
        else:
            end_time = isodate.parse_datetime(to_string)
            if not end_time:
                raise ValueError("--to argument not in ISO8601 format and not a valid pytimeparse duration")

    from_duration = timeparse(from_string)
    if from_duration is not None:
        start_time = datetime.datetime.utcnow() - datetime.timedelta(seconds=from_duration)
    else:
        start_time = isodate.parse_datetime(from_string)

        if not start_time:
            raise ValueError("--from argument not in ISO8601 format and not a valid pytimeparse duration")

    # Covert the timestamps to something timezone aware
    start_time = pytz.utc.localize(start_time)
    end_time = pytz.utc.localize(end_time)

    if start_time > end_time:
        raise ValueError("Start time bigger than end time")

    return start_time, end_time
Beispiel #34
0
 def test_timeparse_signs(self):
     '''Test parsing time signs.'''
     self.assertEqual(timeparse.timeparse('+32 m 1 s'), 1921)
     self.assertEqual(timeparse.timeparse('+ 32 m 1 s'), 1921)
     self.assertEqual(timeparse.timeparse('-32 m 1 s'), -1921)
     self.assertEqual(timeparse.timeparse('- 32 m 1 s'), -1921)
     self.assertIsNone(timeparse.timeparse('32 m - 1 s'))
     self.assertIsNone(timeparse.timeparse('32 m + 1 s'))
Beispiel #35
0
    def action_fin(self, ago):
        current = self.ensure_working()

        now = pendulum.now()
        if ago:
            seconds_ago = timeparse(ago)
            now = now.subtract(seconds=seconds_ago)

        self.store.finish_tracking(current, now)
        print(
            'You were working on {}{} for {} and task is stopped now.'.format(
                green(current['task']), ' of project {}'.format(
                    red(current['project'])) if current['project'] else '',
                now.diff_for_humans(current['start'], absolute=True)))
Beispiel #36
0
    def __init__(self,
                 min_ratio=MIN_RATIO,
                 min_recrawl_delta: str = None,
                 **kwargs):
        """
        :param min_ratio: the min ratio
        :param min_recrawl_delta: a string that can be parsed to a time difference using ``pytimeparse.timeparse``
        """
        self.min_ratio = min_ratio  #: Child URLs are added only if sentence_count / sg_count > min_ratio
        delta_sec = None
        if min_recrawl_delta is not None:
            try:
                delta_sec = timeparse(min_recrawl_delta)
            except:
                logger.warning(
                    f'Could not parse min_recrawl_delta ({min_recrawl_delta}). Using default.'
                )
        if delta_sec is None:
            delta_sec = timeparse(MIN_RECRAWL_DELTA)

        self.min_recrawl_delta = timedelta(
            seconds=delta_sec
        )  #: a timedelta. URLs are revisited if now() - last visit > min_recrawl_delta (UTC)
    def __init__(self, dict):
        self.fromName = dict["Name"]
        self.type = dict["Type"]
        self.toNumber = parseNumber(dict["To Number"])
        self.toCarrier = carrier.name_for_number(self.toNumber, "hu")

        f = "{} {}".format(dict["Date"], dict["Time"])
        self.start = datetime.datetime.strptime(f, "%Y-%m-%d %I:%M %p")
        self.yearMonth = int(self.start.strftime("%Y%m"))
        self.end = self.start + datetime.timedelta(
            0, timeparse(str(dict["Duration"])))
        self.duration = self.end - self.start
        self.hossz_perc = math.ceil(self.duration.seconds / 60)
        self.szamolt_dij = None
Beispiel #38
0
class Admin(commands.Cog):
    """Commands for managing Discord servers."""
    def __init__(self,bot):
        self.bot = bot

    @checks.can_kick()
    @commands.command()
    async def kick(self, ctx, user : discord.Member):
        """Kicks a user from the server."""
        if ctx.author == user:
            return await ctx.send("You cannot kick yourself.")
        await user.kick()
        embed = discord.Embed(title=f'User {user.name} has been kicked.', color=0x00ff00)
        embed.add_field(name="Goodbye!", value=":boot:")
        embed.set_thumbnail(url=user.avatar_url)
        await ctx.send(embed=embed)

    @checks.can_ban()
    @commands.command()
    async def ban(self, ctx, user : discord.Member):
        """Bans a user from the server."""
        if ctx.author == user:
            return await ctx.send("You cannot ban yourself.")
        await user.ban()
        embed = discord.Embed(title=f'User {user.name} has been banned.', color=0x00ff00)
        embed.add_field(name="Goodbye!", value=":hammer:")
        embed.set_thumbnail(url=user.avatar_url)
        await ctx.send(embed=embed)

    @checks.can_mute()
    @commands.command()
    async def mute(self, ctx, user : discord.Member, time: str):
        """Prevents a user from speaking for a specified amount of time."""
        if ctx.author == user:
            return await ctx.send("You cannot mute yourself.")
        if (rolem := discord.utils.get(ctx.guild.roles, name='Muted')) is None:
            return ctx.send("You have not set up the 'Muted' role!")
        if rolem in user.roles:
            return await ctx.send(f'User {user.mention} is already muted.')
        await user.add_roles(rolem)
        if time.isnumeric():
            time += "s"
        duration = datetime.timedelta(seconds=timeparse(time))
        ends = datetime.datetime.utcnow() + duration
        await self.bot.mongoIO.muteUser(user, ctx.guild, ends)
        embed = discord.Embed(title=f'User {user.name} has been successfully muted for {duration}', color=0x00ff00)
        embed.add_field(name="Shhh!", value=":zipper_mouth:")
        embed.set_thumbnail(url=user.avatar_url)
        await ctx.send(embed=embed)
def to_timespan(value):
    if value is None:
        raise ValueError("Invalid timespan")
    try:
        seconds = int(value)
    except Exception:
        try:
            seconds = timeparse.timeparse(six.text_type(value))
        except Exception:
            raise ValueError("Unable to parse timespan")
    if seconds is None:
        raise ValueError("Unable to parse timespan")
    if seconds <= 0:
        raise ValueError("Timespan must be positive")
    return datetime.timedelta(seconds=seconds)
Beispiel #40
0
    def __init__(self, config, database_manager: DatabaseManager):
        action_map = {
            'kick': lambda x, y: kick_user_and_raise_max_tries(x),
            'ban': ban_user_and_raise_max_tries,
            'none': lambda x, y: None
        }

        self._config = config
        self._db_man = database_manager
        self.delay = timedelta(seconds=timeparse(self._config["Captcha"]
                                                 ["TimeDelayBetweenAttempts"]))

        self.action_for_failure = action_map[self._config["Captcha"]
                                             ["ActionOnFailedCaptcha"].lower()]

        self.ban_delay = timedelta(seconds=timeparse(
            self._config["Captcha"]["FailedCaptchaBanDuration"]))

        self.captcha_expiration_delay = timedelta(
            seconds=timeparse(self._config["Captcha"]["ExpirationTime"]))

        self.max_tries = int(self._config["Captcha"]["MaxCaptchaTries"])

        self._last_attempt_dict = {}
Beispiel #41
0
    async def _check_dbclean(self):
        last_clean_time = await self._get_last_dbclean_time()
        config_delta = timedelta(
            seconds=timeparse(self.config['cleaning_interval']))
        utcnow_dt = datetime.utcnow()
        if not last_clean_time:
            actual_delta = config_delta + timedelta(seconds=1)
        else:
            actual_delta = utcnow_dt - filetime_to_dt(last_clean_time)

        if actual_delta > config_delta:
            await self._clean_database(utcnow_dt)
            return config_delta + timedelta(seconds=1)
        else:
            return config_delta - actual_delta + timedelta(seconds=1)
Beispiel #42
0
    def __init__(self, job, callback):
        self.job = job
        self.callback = callback
        self.api = PrometheusAPI(job.data_source)
        self.df = dict()
        self.ilf = dict()
        # TODO: make them configurable.
        self.train_count = 120
        self.train_interval = 60
        self.predict_interval = 300

        self.event = Event()
        self.lock = Lock()
        self.__exit = False
        self.timer = Timer(timeparse(self.job.timeout), self.timeout_action)
Beispiel #43
0
def to_timespan(value):
    if value is None:
        raise ValueError("Invalid timespan")
    try:
        seconds = int(value)
    except Exception:
        try:
            seconds = timeparse.timeparse(six.text_type(value))
        except Exception:
            raise ValueError("Unable to parse timespan")
    if seconds is None:
        raise ValueError("Unable to parse timespan")
    if seconds <= 0:
        raise ValueError("Timespan must be positive")
    return datetime.timedelta(seconds=seconds)
Beispiel #44
0
def to_timestamp(v):
    if isinstance(v, datetime.datetime):
        return v
    try:
        v = float(v)
    except (ValueError, TypeError):
        v = six.text_type(v)
        try:
            return timeutils.normalize_time(timeutils.parse_isotime(v))
        except ValueError:
            delta = timeparse.timeparse(v)
            if delta is None:
                raise ValueError("Unable to parse timestamp %s" % v)
            return timeutils.utcnow() + datetime.timedelta(seconds=delta)
    return datetime.datetime.utcfromtimestamp(v)
Beispiel #45
0
 def __init__(self, obj):
     self.start = getdate(obj['start'])
     try:
         self.interval = int(obj['interval'])
     except ValueError:
         self.interval = timeparse(obj['interval'])
     assert type(self.interval) is int
     self.end = getdate(obj['end']) if 'end' in obj else None
     self.weekdays = [int(x) for x in obj['weekdays']] if \
         'weekdays' in obj else None
     if self.weekdays is not None:
         for weekday in self.weekdays:
             if not 1 <= weekday <= 7:
                 raise ValueError('Not a valid weekday: {}'
                                  .format(weekday))
Beispiel #46
0
    def forward_simulate(self, duration, current_time=None):
        """
        Forward simulates the fleet for a given duration.

        Args:
            duration (string): A duration to forward simulate for, e.g. "5m."
            current_time (datetime.datetime or str, optional): The current time, from when the simulation will begin. Can be provided as a datetime.datetime object or ISO string. Set to datetime.datetime.now() if not provided. Defaults to None.

        Returns:
            VehicleAssignments: The final state of all vehicles and requests, after the simulation.

        Raises:
            StatusError: If unsuccessful.
        """
        if current_time is None:
            current_time = datetime.datetime.now()

        if isinstance(current_time, str):
            current_time = isoparse(current_time)

        duration_td = datetime.timedelta(seconds=timeparse(duration))

        if current_time + duration_td > self.end_time:
            self.end_time = current_time + duration_td

        url = self.build_url(Endpoints.FORWARD_SIMULATE)
        payload = {
            'user_key': self.user_key.todict(),
            'sim_duration': duration,
            'current_time': to_rfc3339(current_time)
        }

        r = requests.post(url, data=json.dumps(payload))
        resp = r.json()

        if r.status_code == 200:
            return VehicleAssignments(
                vehs=[Vehicle.fromdict(self, veh) for veh in resp.get('vehs')],
                requests=[
                    Request.fromdict(self, req) for req in resp.get('reqs')
                ],
                notifications=[
                    Notification.fromdict(notif)
                    for notif in resp.get('notifications')
                ],
            )
        else:
            raise StatusError(resp=resp)
Beispiel #47
0
def create_boxcar_handler(email=None,
                          password=None,
                          record_limit=None,
                          record_delta=None,
                          level=0,
                          filter=None,
                          bubble=True):
    if None in [email, password]:
        raise ValueError("Email and password are required!")

    if isinstance(record_delta, basestring):
        record_delta = timedelta(seconds=timeparse(record_delta))

    return ThreadedWrapperHandler(
        BoxcarHandler(email, password, record_limit, record_delta, level,
                      filter, bubble))
Beispiel #48
0
  def run(self, events, *args, **kwargs):
    event = kwargs.pop('event')
    event_id = kwargs.pop('event_id')
    duration = kwargs.pop('duration')

    parsed_duration_in_seconds = timeparse(duration, granularity='minutes')
    event['duration'] = parsed_duration_in_seconds
    body = strings.MSG_DURATION_SET % datetime.timedelta(seconds=parsed_duration_in_seconds)
    calendar_event_id = event.get('calendar_event') and event['calendar_event']['id']
    if calendar_event_id:
      try:
        calendar_events.update_gcal_event(event, event_id)
      except calendar_events.KeyfilePathNotSpecifiedError:
        pass

    return RSVPCommandResponse(events, RSVPMessage('stream', body))
def to_timestamp(v):
    if isinstance(v, datetime.datetime):
        return v
    try:
        v = float(v)
    except (ValueError, TypeError):
        v = six.text_type(v)
        try:
            return timeutils.parse_isotime(v)
        except ValueError:
            delta = timeparse.timeparse(v)
            if delta is None:
                raise ValueError("Unable to parse timestamp %s" % v)
            return utcnow() + datetime.timedelta(seconds=delta)
    return datetime.datetime.utcfromtimestamp(v).replace(
        tzinfo=iso8601.iso8601.UTC)
Beispiel #50
0
def parse_time_interval(time_start, time_end):
    t_end = time_end
    t_start = time_start

    if t_start is not None:
        if t_start in ["current_time", "now"]:
            t_start = str(datetime.now())

    if t_end is not None:
        if t_end.startswith("+"):
            duration = t_end[1:]
            delta = timeparse(duration)
            t_start = datetime.strptime(t_start, "%Y-%m-%d %H:%M:%S.%f")
            t_end = t_start + timedelta(seconds=delta)

    return (str(t_start), str(t_end))
Beispiel #51
0
def go():
  if not ("create" in action) and hit is None:
    sys.exit("You haven't created the hit on Turk yet (mode: %s)" % mode)

  print(bold(underline("%s mode" % string.capwords(mode))))

  if action == "create":
    create_hit(settings)
  elif action == "update":
    update_hit(settings)
  elif action == "download":
    get_results(HOST, mode, hit["id"])
  ## add time, assignments, or both
  elif re.match("^add ", action):
    action_ = re.sub("add ","", action)
    num_assignments = 0
    td = None
    # extract assignments
    assignments_search = re.search("([0-9]+) *assignments", action_)
    if (assignments_search):
      num_assignments = int(assignments_search.group(1))
      print("Adding %d assignments" % num_assignments)
      action_ = re.sub(assignments_search.group(0), "", action_)
      action_ = re.sub("and", "", action_)
      add_assignments(hit, num_assignments)
      print("-> Done")

    # time parse the rest
    seconds = timeparse(action_)

    if (seconds is not None):
      print("Adding %s" % humane_timedelta(timedelta(seconds = seconds)))
      add_time(hit, seconds)
      print("-> Done" )
  elif action == "status":
    show_status(hit)
  elif action == "expire":
    expire_hit(hit)
  elif action == "history":
    sys.exit("Not yet implemented")
  elif action == "manage":
    sys.exit("Not yet implemented")
  elif action == "view":
    sys.exit("Not yet implemented")
  else:
    usage()
Beispiel #52
0
    def execute(self, message: IRCMessage):
        if len(message.parameterList) < 2:
            return IRCResponse(ResponseType.Say, self.help(None), message.replyTo)

        command = message.parameterList[1].lower()
        delay = timeparse(message.parameterList[0])
        delayDelta = datetime.timedelta(seconds=delay)
        delayString = string.deltaTimeToString(delayDelta, 's')
        params = message.parameterList[2:]
        commandString = '{}{} {}'.format(self.bot.commandChar, command, ' '.join(params))
        commandString = commandString.replace('$delayString', delayString)
        commandString = commandString.replace('$delay', str(delay))

        newMessage = IRCMessage(message.type, message.user, message.channel,
                                commandString, self.bot)

        moduleHandler = self.bot.moduleHandler
        if command in moduleHandler.mappedTriggers:
            module = moduleHandler.mappedTriggers[command].execute
            d = task.deferLater(reactor, delay, module, newMessage)
            d.addCallback(self._activate)
            d.addErrback(self._deferredError)
            return IRCResponse(ResponseType.Say,
                               "OK, I'll execute that in {}".format(delayString),
                               message.replyTo,
                               {'delay': delay, 'delayString': delayString})
        else:
            if 'Alias' not in moduleHandler.commands:
                return IRCResponse(ResponseType.Say,
                                   "'{}' is not a recognized command".format(command),
                                   message.replyTo)

            if command not in moduleHandler.commands['Alias'].aliases:
                return IRCResponse(ResponseType.Say,
                                   "'{}' is not a recognized command or alias".format(command),
                                   message.replyTo)

            d = task.deferLater(reactor, delay, moduleHandler.commands['Alias'].execute, newMessage)
            d.addCallback(self._activate)
            d.addErrback(self._deferredError)
            return IRCResponse(ResponseType.Say,
                               "OK, I'll execute that in {}".format(delayString),
                               message.replyTo)
Beispiel #53
0
def main():
    """Calculate phases of the moon."""

    def mktime(str, _cal = Calendar()):
        return DateTime.mktime(_cal.parse(str)[0])

    def mkdelta(str):
        return DateTime.DateTimeDeltaFromSeconds(timeparse(str))

    parser = argparse.ArgumentParser(description=main.__doc__)
    parser.add_argument('start', nargs='?', type=mktime,
                        default=DateTime.now())
    parser.add_argument('stop', nargs='?', type=mktime,
                        default=None)
    parser.add_argument('--step', nargs='?', type=mkdelta,
                        default=timeparse('1d'))
    parser.add_argument('--jitter', action='store_true')
    args_obj = parser.parse_args(['one year ago', 'now', '--step', '4w'])
    if args_obj.stop is None:
        args_obj.stop = args_obj.start
    data = FakeFile(args_obj)

    headers = {
        "Content-type": "application/x-www-form-urlencoded",
        "Accept": "text/plain",
        }
    responses = set()
    datablock = data.read(1024)
    while datablock:
        conn = httplib.HTTPConnection("192.168.32.131", 8086)
        conn.request("POST", "/write?db=mydb", datablock, headers)
        response = conn.getresponse()
        if (response.status, response.reason) not in responses:
            print response.status, response.reason
            responses.add((response.status, response.reason))
        datablock = data.read(1024)
Beispiel #54
0
def my_timeparse(_s):
  s = re.sub("and","",_s)
  return timeparse(s)
Beispiel #55
0
def generate(arguments):

    if arguments["clean"]:

        db = reservation_connect()
        reservations = Reservation.objects({})
        print "deleting:"
        for reservation in reservations:
            print reservation.label,
            reservation.delete()
        print

    elif arguments["--rst"]:

        print "generate"
        print 70 * "="
        print "\n::\n"
        lines = __doc__.split("\n")
        for line in lines:
            print "  ", line

    else:

        reservations = int(arguments["RESERVATIONS"])
        duration = int(arguments["DURATION"])
        server_string = arguments["SERVERS"]
        servers = hostlist.expand_hostlist(server_string)
        if arguments["START"] in ["now"]:
            first_time = datetime.datetime.now()
        else:
            first_time = timeparse(arguments("START"))

        print 70 * "="
        print "Servers:     ", servers
        print "Reservations:", reservations
        print "Duration:    ", duration
        print "First date:  ", first_time
        print 70 * "="

        db = reservation_connect()

        def random_delta(duration):
            r = randint(0, duration)
            return datetime.timedelta(seconds=timeparse("{0} h".format(r)))

        t_start = {}
        t_end = {}
        for s in xrange(0, len(servers)):
            t_start[s] = []
            t_end[s] = []

            t_start[s].append(first_time + random_delta(duration))
            t_end[s].append(t_start[s][0] + random_delta(duration))

        for s in range(0, len(servers)):
            for n in range(1, reservations):
                t_start[s].append(t_end[s][n - 1] + random_delta(duration))
                t_end[s].append(t_start[s][n] + random_delta(duration))

        for s in range(0, len(servers)):
            for n in range(0, reservations):
                print s, n, t_start[s][n], t_end[s][n]

        pprint("start: " + str(t_start))
        pprint("end  : " + str(t_end))

        for s in range(0, len(servers)):
            for n in range(0, reservations):
                entry = {
                    "cm_id": "cm_reservation-{0}-{1}".format(s, n),
                    "label": "exp-{0}-{1}".format(s, n),
                    "summary": "task-{0}-{1}".format(s, n),
                    "host": servers[s],
                    "user": "******",
                    "project": "fg82",
                    "start_time": str(t_start[s][n]),
                    "end_time": str(t_end[s][n]),
                }

                print entry
                r = Reservation(
                    cm_id=entry["cm_id"],
                    host=entry["host"],
                    label=entry["label"],
                    user=entry["user"],
                    summary=entry["summary"],
                    project=entry["project"],
                    start_time=entry["start_time"],
                    end_time=entry["end_time"],
                )
                r.save()

        print 70 * "A"
        reservations = Reservation.objects()
        print len(reservations)
        for reservation in reservations:
            pprint(reservation)
        print 70 * "B"

        for reservation in reservations:
            print reservation
Beispiel #56
0
 def random_delta(duration):
     r = randint(0, duration)
     return datetime.timedelta(seconds=timeparse("{0} h".format(r)))
Beispiel #57
0
def generate(arguments):
    """
    ::
    
        Usage:
            generate -h | --help | --rst
            generate clean
            generate SERVERS RESERVATIONS DURATION START
            generate list [--json|--table|--calendar]

        Arguments:
            SERVERS       Number of servers for which we generate 
                          reservations

            RESERVATIONS  Number of reservations per server

            DURATION      The maximum duration of a reservation 
                          (determined randomly)

            START         The start date. if now is specified, the current
                          time is used, otherwise an offset is used in the
                          form of 1m, or 1h, or 1w[default: now]

        Description:
            This program generates a number of reservations so they can be
            used to test the reservation package.

            generate clean
                deletes all reservations from the reservation database

            generate SERVERS RESERVATIONS DURATION
                generates a number of reservations where the servers are
                specified as hostlist (e.g. i[001-003]. The reservations
                specifies how many reservations there will be for each
                server. The duration is a random number between [0,duration]
                that specified not only a duration, but also the time delta
                between two reservations on the same host.

            generate list
                retiurns the list of reservations in the specified
                format. Thoe format can be list, table, or calendar

        Bugs:
            Not implemented:

            * clean 
            * list
            * the generation function does not yet have a start date

        See Also:
            * https://pypi.python.org/pypi/pytimeparse/1.1.0

    """

    if arguments['clean']:

        db = reservation_connect()
        reservations = Reservation.objects({})
        print "deleting:"
        for reservation in reservations:
            print reservation.label,
            reservation.delete()
        print
        
    elif arguments["--rst"]:

        print "generate"
        print 70 * "="
        print "\n::\n"
        lines = generate.__doc__.split("\n")
        for line in lines:
            print "  ", line

    else:

        reservations = int(arguments["RESERVATIONS"])
        duration = int(arguments["DURATION"])
        server_string = arguments["SERVERS"]
        servers = hostlist.expand_hostlist(server_string)
        if arguments["START"] in ["now"]:
            first_time = datetime.datetime.now()
        else:
            first_time = timeparse(arguments("START"))

        print 70 * "="
        print "Servers:     ", servers
        print "Reservations:", reservations
        print "Duration:    ", duration
        print "First date:  ", first_time
        print 70 * "="

        db = reservation_connect()        

        def random_delta(duration):
            r = randint(0, duration)
            return datetime.timedelta(seconds=timeparse("{0} h".format(r)))
        
        t_start = {}
        t_end = {}
        for s in xrange(0, len(servers)):
            t_start[s] = []
            t_end[s] = []
            
            t_start[s].append(first_time + random_delta(duration))
            t_end[s].append(t_start[s][0] + random_delta(duration))

        for s in range(0, len(servers)):        	
            for n in range(1, reservations):
                t_start[s].append(t_end[s][n - 1] + random_delta(duration))
                t_end[s].append(t_start[s][n] +  random_delta(duration))

        for s in range(0, len(servers)):
            for n in range(0, reservations):
                print s, n, t_start[s][n], t_end[s][n]
                

        pprint("start: " + str(t_start))
        pprint("end  : " + str(t_end))

        for s in range(0, len(servers)):
            for n in range(0, reservations):
                entry = {
                    "cm_id": "cm_reservation-{0}-{1}".format(s,n),
                    "label": "exp-{0}-{1}".format(s,n),
                    "summary": "task-{0}-{1}".format(s,n),
                    "host": servers[s],
                    "user": "******",
                    "project" : "fg82",
                    "start_time": str(t_start[s][n]),
                    "end_time": str(t_end[s][n]),
                    }

                print entry
                r = Reservation(
                    cm_id=entry["cm_id"],
                    host=entry["host"],
                    label=entry["label"],
                    user=entry["user"],
                    summary=entry["summary"],                    
                    project=entry["project"],
                    start_time=entry["start_time"],
                    end_time=entry["end_time"]
                    )
                r.save()

        print 70 * "A"
        reservations = Reservation.objects()
        print len(reservations)
        for reservation in reservations:
           pprint(reservation)
        print 70 * "B"

        for reservation in reservations:
            print reservation
Beispiel #58
0
def build_duration_logs(path_to_file, pattern=None):
    r"""
    INPUT:

    - ``path_to_file`` -- string, file to parse
    - ``pattern`` -- string or ``None``, string to parse. If ``None``, it
      returns 0 seconds.

    OUTPUT:

        list of timedelta objects

    EXAMPLES::

        sage: L = [ ('/../ptestlong.log', 'Total time for all tests: '),
        ....:       ('/../dochtml.log', 'Elapsed time: '),
        ....:       ('/../start.log', None),
        ....:       ('/sagelib-7.5.beta6.log', 'real\t'),
        ....:       ('/sqlite.log', 'real\t')]

    ::

        sage: from slabbe.analyze_sage_build import build_duration_logs
        sage: from sage.env import SAGE_LOGS
        sage: import os
        sage: for (file, pattern) in L:                  # random
        ....:     print(file)
        ....:     if not os.path.exists(SAGE_LOGS+file):
        ....:         continue
        ....:     build_duration_logs(SAGE_LOGS+file, pattern)
        /../ptestlong.log
        /../dochtml.log
        [datetime.timedelta(0, 471, 700000)]
        /../start.log
        []
        /sagelib-7.5.beta6.log
        /sqlite.log
        []

    """
    if pattern is None:
        return []

    with open(path_to_file, 'r') as f:
        s = f.read()

    result = re.findall('^'+pattern+'.*', s, flags=re.MULTILINE)

    # line = "real\t0m41.027s"            # in logs/*.log
    # line = "Total time for all tests: 14922.7 seconds" # in ptestlong.log
    # line = "Elapsed time: 4156.8 seconds." # in dochtml.log
    L = []
    for line in result:
        time_string = line[len(pattern):].strip('.')
        seconds = timeparse(time_string)
        if seconds is None:
            raise ValueError('Unable to parse time delta in string '
                    '"{}" of file {}'.format(time_string, path_to_file))
        L.append(datetime.timedelta(seconds=seconds))

    return L
 def test_timeparse_6(self):
     '''timeparse test case 6.'''
     self.assertEqual(timeparse.timeparse('1 w 3 d 2 h 32 m'), 873120)
Beispiel #60
0
def duration_from_string(duration_string):
    return timeparse(duration_string) * 1000000000