-
-
Notifications
You must be signed in to change notification settings - Fork 91
/
parser.py
47 lines (37 loc) · 1.37 KB
/
parser.py
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
from __future__ import annotations
import re
import urllib
import urllib.request
from pathlib import Path
from yutto.utils.console.logger import Logger
def path_from_cli(path: str) -> Path:
"""从命令行参数获取路径,支持 ~,以便配置中使用 ~"""
return Path(path).expanduser()
def is_comment(line: str) -> bool:
"""判断文件某行是否为注释"""
if line.startswith("#"):
return True
return False
def alias_parser(file_path: str) -> dict[str, str]:
result: dict[str, str] = {}
re_alias_splitter = re.compile(r"[\s=]")
with path_from_cli(file_path).open("r") as f_alias:
for line in f_alias:
line = line.strip()
if not line or is_comment(line):
continue
alias, url = re_alias_splitter.split(line, maxsplit=1)
result[alias] = url
return result
def file_scheme_parser(url: str) -> list[str]:
file_url: str = urllib.parse.urlparse(url).path # type: ignore
file_path = path_from_cli(urllib.request.url2pathname(file_url)) # type: ignore
Logger.info(f"解析下载列表 {file_path} 中...")
result: list[str] = []
with file_path.open("r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if not line or is_comment(line):
continue
result.append(line)
return result