mercurial/thirdparty/tomli/_re.py
author Pierre-Yves David <pierre-yves.david@octobus.net>
Fri, 03 Feb 2023 04:19:06 +0100
changeset 50778 ba3add904ab4
parent 50761 2c34c9b61a4f
permissions -rw-r--r--
wrapfunction: use sysstr instead of bytes as argument in "fsmonitor" This is as valid and simpler, it will help us to eventually get ride of `safehasattr`.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50761
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     1
from datetime import date, datetime, time, timedelta, timezone, tzinfo
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     2
from functools import lru_cache
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     3
import re
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     4
from typing import Any, Optional, Union
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     5
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     6
from ._types import ParseFloat
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     7
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     8
# E.g.
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     9
# - 00:32:00.999999
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    10
# - 00:32:00
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    11
_TIME_RE_STR = r"([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])(?:\.([0-9]{1,6})[0-9]*)?"
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    12
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    13
RE_NUMBER = re.compile(
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    14
    r"""
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    15
0
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    16
(?:
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    17
    x[0-9A-Fa-f](?:_?[0-9A-Fa-f])*   # hex
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    18
    |
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    19
    b[01](?:_?[01])*                 # bin
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    20
    |
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    21
    o[0-7](?:_?[0-7])*               # oct
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    22
)
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    23
|
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    24
[+-]?(?:0|[1-9](?:_?[0-9])*)         # dec, integer part
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    25
(?P<floatpart>
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    26
    (?:\.[0-9](?:_?[0-9])*)?         # optional fractional part
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    27
    (?:[eE][+-]?[0-9](?:_?[0-9])*)?  # optional exponent part
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    28
)
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    29
""",
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    30
    flags=re.VERBOSE,
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    31
)
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    32
RE_LOCALTIME = re.compile(_TIME_RE_STR)
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    33
RE_DATETIME = re.compile(
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    34
    fr"""
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    35
([0-9]{{4}})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])  # date, e.g. 1988-10-27
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    36
(?:
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    37
    [Tt ]
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    38
    {_TIME_RE_STR}
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    39
    (?:([Zz])|([+-])([01][0-9]|2[0-3]):([0-5][0-9]))?  # optional time offset
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    40
)?
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    41
""",
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    42
    flags=re.VERBOSE,
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    43
)
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    44
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    45
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    46
def match_to_datetime(match: "re.Match") -> Union[datetime, date]:
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    47
    """Convert a `RE_DATETIME` match to `datetime.datetime` or `datetime.date`.
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    48
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    49
    Raises ValueError if the match does not correspond to a valid date
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    50
    or datetime.
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    51
    """
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    52
    (
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    53
        year_str,
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    54
        month_str,
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    55
        day_str,
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    56
        hour_str,
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    57
        minute_str,
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    58
        sec_str,
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    59
        micros_str,
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    60
        zulu_time,
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    61
        offset_sign_str,
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    62
        offset_hour_str,
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    63
        offset_minute_str,
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    64
    ) = match.groups()
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    65
    year, month, day = int(year_str), int(month_str), int(day_str)
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    66
    if hour_str is None:
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    67
        return date(year, month, day)
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    68
    hour, minute, sec = int(hour_str), int(minute_str), int(sec_str)
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    69
    micros = int(micros_str.ljust(6, "0")) if micros_str else 0
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    70
    if offset_sign_str:
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    71
        tz: Optional[tzinfo] = cached_tz(
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    72
            offset_hour_str, offset_minute_str, offset_sign_str
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    73
        )
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    74
    elif zulu_time:
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    75
        tz = timezone.utc
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    76
    else:  # local date-time
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    77
        tz = None
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    78
    return datetime(year, month, day, hour, minute, sec, micros, tzinfo=tz)
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    79
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    80
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    81
@lru_cache(maxsize=None)
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    82
def cached_tz(hour_str: str, minute_str: str, sign_str: str) -> timezone:
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    83
    sign = 1 if sign_str == "+" else -1
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    84
    return timezone(
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    85
        timedelta(
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    86
            hours=sign * int(hour_str),
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    87
            minutes=sign * int(minute_str),
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    88
        )
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    89
    )
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    90
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    91
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    92
def match_to_localtime(match: "re.Match") -> time:
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    93
    hour_str, minute_str, sec_str, micros_str = match.groups()
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    94
    micros = int(micros_str.ljust(6, "0")) if micros_str else 0
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    95
    return time(int(hour_str), int(minute_str), int(sec_str), micros)
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    96
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    97
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    98
def match_to_number(match: "re.Match", parse_float: "ParseFloat") -> Any:
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    99
    if match.group("floatpart"):
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   100
        return parse_float(match.group())
2c34c9b61a4f thirdparty: vendor tomli
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   101
    return int(match.group(), 0)