]>
crepu.dev Git - config.git/blob - djavu-asus/emacs/elpy/rpc-venv/lib/python3.11/site-packages/tomli/_re.py
994bb7493fd92865e6ab87c277ba5741b44c31a9
1 # SPDX-License-Identifier: MIT
2 # SPDX-FileCopyrightText: 2021 Taneli Hukkinen
3 # Licensed to PSF under a Contributor Agreement.
5 from __future__
import annotations
7 from datetime
import date
, datetime
, time
, timedelta
, timezone
, tzinfo
8 from functools
import lru_cache
10 from typing
import Any
12 from ._types
import ParseFloat
17 _TIME_RE_STR
= r
"([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])(?:\.([0-9]{1,6})[0-9]*)?"
19 RE_NUMBER
= re
.compile(
23 x[0-9A-Fa-f](?:_?[0-9A-Fa-f])* # hex
25 b[01](?:_?[01])* # bin
27 o[0-7](?:_?[0-7])* # oct
30 [+-]?(?:0|[1-9](?:_?[0-9])*) # dec, integer part
32 (?:\.[0-9](?:_?[0-9])*)? # optional fractional part
33 (?:[eE][+-]?[0-9](?:_?[0-9])*)? # optional exponent part
38 RE_LOCALTIME
= re
.compile(_TIME_RE_STR
)
39 RE_DATETIME
= re
.compile(
41 ([0-9]{{4}})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01]) # date, e.g. 1988-10-27
45 (?:([Zz])|([+-])([01][0-9]|2[0-3]):([0-5][0-9]))? # optional time offset
52 def match_to_datetime(match
: re
.Match
) -> datetime | date
:
53 """Convert a `RE_DATETIME` match to `datetime.datetime` or `datetime.date`.
55 Raises ValueError if the match does not correspond to a valid date
71 year
, month
, day
= int(year_str
), int(month_str
), int(day_str
)
73 return date(year
, month
, day
)
74 hour
, minute
, sec
= int(hour_str
), int(minute_str
), int(sec_str
)
75 micros
= int(micros_str
.ljust(6, "0")) if micros_str
else 0
77 tz
: tzinfo |
None = cached_tz(
78 offset_hour_str
, offset_minute_str
, offset_sign_str
82 else: # local date-time
84 return datetime(year
, month
, day
, hour
, minute
, sec
, micros
, tzinfo
=tz
)
87 @lru_cache(maxsize
=None)
88 def cached_tz(hour_str
: str, minute_str
: str, sign_str
: str) -> timezone
:
89 sign
= 1 if sign_str
== "+" else -1
92 hours
=sign
* int(hour_str
),
93 minutes
=sign
* int(minute_str
),
98 def match_to_localtime(match
: re
.Match
) -> time
:
99 hour_str
, minute_str
, sec_str
, micros_str
= match
.groups()
100 micros
= int(micros_str
.ljust(6, "0")) if micros_str
else 0
101 return time(int(hour_str
), int(minute_str
), int(sec_str
), micros
)
104 def match_to_number(match
: re
.Match
, parse_float
: ParseFloat
) -> Any
:
105 if match
.group("floatpart"):
106 return parse_float(match
.group())
107 return int(match
.group(), 0)