File size: 1,269 Bytes
5b76e0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import re

_match_duration = re.compile(r"^(-?\d+\.?\d*)(s|ms)$").match


class DurationError(Exception):
    """
    Exception indicating a general issue with a CSS duration.
    """


class DurationParseError(DurationError):
    """
    Indicates a malformed duration string that could not be parsed.
    """


def _duration_as_seconds(duration: str) -> float:
    """
    Args:
        duration (str): A string of the form ``"2s"`` or ``"300ms"``, representing 2 seconds and
            300 milliseconds respectively. If no unit is supplied, e.g. ``"2"``, then the duration is
            assumed to be in seconds.
    Raises:
        DurationParseError: If the argument ``duration`` is not a valid duration string.
    Returns:
        float: The duration in seconds.

    """
    match = _match_duration(duration)

    if match:
        value, unit_name = match.groups()
        value = float(value)
        if unit_name == "ms":
            duration_secs = value / 1000
        else:
            duration_secs = value
    else:
        try:
            duration_secs = float(duration)
        except ValueError:
            raise DurationParseError(
                f"{duration!r} is not a valid duration."
            ) from ValueError

    return duration_secs