mirror of
https://github.com/yt-dlp/yt-dlp.git
synced 2024-10-03 15:51:25 +02:00
Compare commits
2 Commits
b6db885167
...
fccb22aed5
Author | SHA1 | Date | |
---|---|---|---|
|
fccb22aed5 | ||
|
abcd178e57 |
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import datetime
|
||||||
import json
|
import json
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
@ -16,7 +17,7 @@ _SUBTABLE = rf'(?P<subtable>^\[(?P<is_list>\[)?(?P<path>{KEY_RE.pattern})\]\]?)'
|
||||||
EXPRESSION_RE = re.compile(rf'^(?:{_SUBTABLE}|{KEY_RE.pattern}=)', re.MULTILINE)
|
EXPRESSION_RE = re.compile(rf'^(?:{_SUBTABLE}|{KEY_RE.pattern}=)', re.MULTILINE)
|
||||||
|
|
||||||
LIST_WS_RE = re.compile(rf'{WS}((#[^\n]*)?\n{WS})*')
|
LIST_WS_RE = re.compile(rf'{WS}((#[^\n]*)?\n{WS})*')
|
||||||
UNKNOWN_VALUE_RE = re.compile(r'[^,}\] \t\n#]+')
|
LEFTOVER_VALUE_RE = re.compile(r'[^,}\]\t\n#]+')
|
||||||
|
|
||||||
|
|
||||||
def parse_key(value: str):
|
def parse_key(value: str):
|
||||||
|
@ -36,22 +37,16 @@ def get_target(root: dict, paths: list[str], is_list=False):
|
||||||
use_list = is_list and index == len(paths)
|
use_list = is_list and index == len(paths)
|
||||||
result = target.get(key)
|
result = target.get(key)
|
||||||
if result is None:
|
if result is None:
|
||||||
if use_list:
|
result = [] if use_list else {}
|
||||||
target[key] = _temp = []
|
target[key] = result
|
||||||
target = {}
|
|
||||||
_temp.append(target)
|
|
||||||
else:
|
|
||||||
target[key] = target = {}
|
|
||||||
|
|
||||||
elif isinstance(result, list):
|
if isinstance(result, dict):
|
||||||
if use_list:
|
|
||||||
target = {}
|
|
||||||
result.append(target)
|
|
||||||
else:
|
|
||||||
target = result[-1]
|
|
||||||
|
|
||||||
else:
|
|
||||||
target = result
|
target = result
|
||||||
|
elif use_list:
|
||||||
|
target = {}
|
||||||
|
result.append(target)
|
||||||
|
else:
|
||||||
|
target = result[-1]
|
||||||
|
|
||||||
assert isinstance(target, dict)
|
assert isinstance(target, dict)
|
||||||
return target
|
return target
|
||||||
|
@ -60,9 +55,8 @@ def get_target(root: dict, paths: list[str], is_list=False):
|
||||||
def parse_enclosed(data: str, index: int, end: str, ws_re: re.Pattern):
|
def parse_enclosed(data: str, index: int, end: str, ws_re: re.Pattern):
|
||||||
index += 1
|
index += 1
|
||||||
|
|
||||||
match = ws_re.match(data, index)
|
if match := ws_re.match(data, index):
|
||||||
assert match
|
index = match.end()
|
||||||
index = match.end()
|
|
||||||
|
|
||||||
while data[index] != end:
|
while data[index] != end:
|
||||||
index = yield True, index
|
index = yield True, index
|
||||||
|
@ -73,10 +67,10 @@ def parse_enclosed(data: str, index: int, end: str, ws_re: re.Pattern):
|
||||||
if data[index] == ',':
|
if data[index] == ',':
|
||||||
index += 1
|
index += 1
|
||||||
|
|
||||||
match = ws_re.match(data, index)
|
if match := ws_re.match(data, index):
|
||||||
assert match
|
index = match.end()
|
||||||
index = match.end()
|
|
||||||
|
|
||||||
|
assert data[index] == end
|
||||||
yield False, index + 1
|
yield False, index + 1
|
||||||
|
|
||||||
|
|
||||||
|
@ -106,15 +100,22 @@ def parse_value(data: str, index: int):
|
||||||
if match := STRING_RE.match(data, index):
|
if match := STRING_RE.match(data, index):
|
||||||
return match.end(), json.loads(match[0]) if match[0][0] == '"' else match[0][1:-1]
|
return match.end(), json.loads(match[0]) if match[0][0] == '"' else match[0][1:-1]
|
||||||
|
|
||||||
# bool, int, float or date. Of these, only bool is used in pyproject.toml, so ignore others
|
match = LEFTOVER_VALUE_RE.match(data, index)
|
||||||
match = UNKNOWN_VALUE_RE.match(data, index)
|
|
||||||
assert match
|
assert match
|
||||||
if match[0] == 'true':
|
value = match[0].strip()
|
||||||
value = True
|
for func in [
|
||||||
elif match[0] == 'false':
|
int,
|
||||||
value = False
|
float,
|
||||||
else:
|
datetime.time.fromisoformat,
|
||||||
value = None
|
datetime.date.fromisoformat,
|
||||||
|
datetime.datetime.fromisoformat,
|
||||||
|
{'true': True, 'false': False}.get,
|
||||||
|
]:
|
||||||
|
try:
|
||||||
|
value = func(value)
|
||||||
|
break
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
return match.end(), value
|
return match.end(), value
|
||||||
|
|
||||||
|
@ -159,7 +160,6 @@ def parse_toml(data: str):
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
import argparse
|
import argparse
|
||||||
import json
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
|
@ -169,7 +169,11 @@ def main():
|
||||||
with args.infile.open('r', encoding='utf-8') as file:
|
with args.infile.open('r', encoding='utf-8') as file:
|
||||||
data = file.read()
|
data = file.read()
|
||||||
|
|
||||||
print(json.dumps(parse_toml(data)))
|
def default(obj):
|
||||||
|
if isinstance(obj, (datetime.date, datetime.time, datetime.datetime)):
|
||||||
|
return obj.isoformat()
|
||||||
|
|
||||||
|
print(json.dumps(parse_toml(data), default=default))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
@ -35,7 +35,7 @@ classifiers = [
|
||||||
"Programming Language :: Python :: Implementation",
|
"Programming Language :: Python :: Implementation",
|
||||||
"Programming Language :: Python :: Implementation :: CPython",
|
"Programming Language :: Python :: Implementation :: CPython",
|
||||||
"Programming Language :: Python :: Implementation :: PyPy",
|
"Programming Language :: Python :: Implementation :: PyPy",
|
||||||
"License :: Public Domain",
|
"License :: OSI Approved :: The Unlicense (Unlicense)",
|
||||||
"Operating System :: OS Independent",
|
"Operating System :: OS Independent",
|
||||||
]
|
]
|
||||||
dynamic = ["version"]
|
dynamic = ["version"]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user