mirror of
https://github.com/283375/arcaea-offline.git
synced 2025-04-19 06:00:18 +00:00
impr(external): ArcaeaParser
file reading
This commit is contained in:
parent
6258a55ba4
commit
5796d9a80f
20
src/arcaea_offline/external/arcaea/common.py
vendored
20
src/arcaea_offline/external/arcaea/common.py
vendored
@ -1,3 +1,4 @@
|
|||||||
|
import contextlib
|
||||||
import json
|
import json
|
||||||
from os import PathLike
|
from os import PathLike
|
||||||
from typing import Any, List, Optional, Union
|
from typing import Any, List, Optional, Union
|
||||||
@ -43,6 +44,25 @@ class ArcaeaParser:
|
|||||||
def __init__(self, filepath: Union[str, bytes, PathLike]):
|
def __init__(self, filepath: Union[str, bytes, PathLike]):
|
||||||
self.filepath = filepath
|
self.filepath = filepath
|
||||||
|
|
||||||
|
def read_file_text(self):
|
||||||
|
file_handle = None
|
||||||
|
|
||||||
|
with contextlib.suppress(TypeError):
|
||||||
|
# original open
|
||||||
|
file_handle = open(self.filepath, "r", encoding="utf-8")
|
||||||
|
|
||||||
|
if file_handle is None:
|
||||||
|
try:
|
||||||
|
# or maybe a `pathlib.Path` subset
|
||||||
|
# or an `importlib.resources.abc.Traversable` like object
|
||||||
|
# e.g. `zipfile.Path`
|
||||||
|
file_handle = self.filepath.open(mode="r", encoding="utf-8")
|
||||||
|
except Exception as e:
|
||||||
|
raise ValueError("Invalid `filepath`.") from e
|
||||||
|
|
||||||
|
with file_handle:
|
||||||
|
return file_handle.read()
|
||||||
|
|
||||||
def parse(self) -> List[DeclarativeBase]:
|
def parse(self) -> List[DeclarativeBase]:
|
||||||
...
|
...
|
||||||
|
|
||||||
|
@ -10,8 +10,7 @@ class PacklistParser(ArcaeaParser):
|
|||||||
super().__init__(filepath)
|
super().__init__(filepath)
|
||||||
|
|
||||||
def parse(self) -> List[Union[Pack, PackLocalized]]:
|
def parse(self) -> List[Union[Pack, PackLocalized]]:
|
||||||
with open(self.filepath, "r", encoding="utf-8") as pl_f:
|
packlist_json_root = json.loads(self.read_file_text())
|
||||||
packlist_json_root = json.loads(pl_f.read())
|
|
||||||
|
|
||||||
packlist_json = packlist_json_root["packs"]
|
packlist_json = packlist_json_root["packs"]
|
||||||
results: List[Union[Pack, PackLocalized]] = [
|
results: List[Union[Pack, PackLocalized]] = [
|
||||||
|
@ -12,8 +12,7 @@ class SonglistParser(ArcaeaParser):
|
|||||||
def parse(
|
def parse(
|
||||||
self,
|
self,
|
||||||
) -> List[Union[Song, SongLocalized, Difficulty, DifficultyLocalized]]:
|
) -> List[Union[Song, SongLocalized, Difficulty, DifficultyLocalized]]:
|
||||||
with open(self.filepath, "r", encoding="utf-8") as sl_f:
|
songlist_json_root = json.loads(self.read_file_text())
|
||||||
songlist_json_root = json.loads(sl_f.read())
|
|
||||||
|
|
||||||
songlist_json = songlist_json_root["songs"]
|
songlist_json = songlist_json_root["songs"]
|
||||||
results = []
|
results = []
|
||||||
|
Loading…
x
Reference in New Issue
Block a user