Skip to content

Commit

Permalink
better support for track file types
Browse files Browse the repository at this point in the history
  • Loading branch information
DerGoogler committed Feb 16, 2025
1 parent 7f015b1 commit 6d494dc
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 52 deletions.
8 changes: 2 additions & 6 deletions sync/cli/Main.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,7 @@ def track(cls) -> int:
elif cls._args.stdin:
track = TrackJson(json.load(fp=sys.stdin))
module_folder = modules_folder.joinpath(track.id)

has_yaml_track = module_folder.joinpath("track.yaml").exists()
json_file = module_folder.joinpath(TrackJson.filename(has_yaml_track))
json_file = module_folder.joinpath(TrackJson.filename(module_folder))

track.write(json_file)

Expand All @@ -184,9 +182,7 @@ def track(cls) -> int:

elif cls._args.modify_module_id is not None:
module_folder = modules_folder.joinpath(cls._args.modify_module_id)

has_yaml_track = module_folder.joinpath("track.yaml").exists()
json_file = module_folder.joinpath(TrackJson.filename(has_yaml_track))
json_file = module_folder.joinpath(TrackJson.filename(module_folder))

if not json_file.exists():
print_error(f"There is no track for this id ({cls._args.modify_module_id})")
Expand Down
3 changes: 1 addition & 2 deletions sync/core/Check.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ def ids(self, module_ids=None, new=False):
self._log.i(f"ids: [{old_id}] -> track has been migrated to {track.id}")
module_folder = self._modules_folder.joinpath(track.id)

has_yaml_track = module_folder.joinpath("track.yaml").exists()
track_json_file = module_folder.joinpath(TrackJson.filename(has_yaml_track))
track_json_file = module_folder.joinpath(TrackJson.filename(module_folder))

track.write(track_json_file)

Expand Down
5 changes: 2 additions & 3 deletions sync/core/Sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,15 @@ def _update_jsons(self, track, force):
return None

update_json_file = module_folder.joinpath(UpdateJson.filename())
has_yaml_track = module_folder.joinpath("track.yaml").exists()
track_json_file = module_folder.joinpath(TrackJson.filename(has_yaml_track))
track_json_file = module_folder.joinpath(TrackJson.filename(module_folder))
zipfile = module_folder.joinpath(online_module.zipfile_name)



if force:
for file in module_folder.glob("*"):
if file.name not in [
TrackJson.filename(has_yaml_track),
TrackJson.filename(module_folder),
online_module.zipfile_name,
online_module.changelog_filename
]:
Expand Down
10 changes: 9 additions & 1 deletion sync/model/ConfigJson.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re, os

from .AttrDict import AttrDict
from .JsonIO import JsonIO

Expand Down Expand Up @@ -34,8 +36,14 @@ def default(cls):
)

@classmethod
def filename(cls):
def filename(cls, json_folder=None):
return "config.json"
# pattern = r"^config\.(json|y(a)?ml)$"
# files = os.listdir(json_folder)
# for file in files:
# if re.match(pattern, file):
# return file
# raise FileNotFoundError("No matching file found in the folder. Supported file types are config.json, config.yml or config.yaml")

@classmethod
def expected_fields(cls, __type=True):
Expand Down
2 changes: 1 addition & 1 deletion sync/model/ConfigJson.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ class ConfigJson(AttrDict, JsonIO):
@classmethod
def default(cls) -> ConfigJson: ...
@classmethod
def filename(cls) -> str: ...
def filename(cls, json_folder: Path) -> str: ...
@classmethod
def expected_fields(cls, __type: bool = ...) -> Dict[str, Union[str, Union[str, Type]]]: ...
47 changes: 23 additions & 24 deletions sync/model/JsonIO.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ def write(self, file):

file.parent.mkdir(parents=True, exist_ok=True)

if ext.lower() == '.yaml':
with open(file, "w") as f:

yaml.dump(self, f, indent=2, default_flow_style=False)

else:
with open(file, "w") as f:
json.dump(self, f, indent=2)
match ext.lower():
case ".json":
with open(file, "w") as f:
json.dump(self, f, indent=2)
case ".yaml" | ".yml":
with open(file, "w") as f:
yaml.dump(self, f, indent=2, default_flow_style=False)
case _:
raise ValueError(f"Invalid file extension: {file}")

@classmethod
def filter(cls, text):
Expand All @@ -40,19 +41,17 @@ def load(cls, file):

_, ext = os.path.splitext(file)

if ext.lower() == '.yaml':
with open(file, encoding="utf-8", mode="r") as f:
text = cls.filter(f.read())
obj = yaml.load(text, Loader=yaml.FullLoader)



assert isinstance(obj, dict)
else:
with open(file, encoding="utf-8", mode="r") as f:
text = cls.filter(f.read())
obj = json.loads(text)

assert isinstance(obj, dict)

return obj
match ext.lower():
case ".json":
with open(file, encoding="utf-8", mode="r") as f:
text = cls.filter(f.read())
obj = json.loads(text)
case ".yaml" | ".yml":
with open(file, encoding="utf-8", mode="r") as f:
text = cls.filter(f.read())
obj = yaml.load(text, Loader=yaml.FullLoader)
case _:
raise ValueError(f"Invalid file extension: {file}")

assert isinstance(obj, dict)
return obj
18 changes: 9 additions & 9 deletions sync/model/TrackJson.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re, os

from enum import Enum

from .AttrDict import AttrDict
Expand Down Expand Up @@ -114,15 +116,13 @@ def load(cls, file):
return TrackJson(obj)

@classmethod
def filename(cls, yaml):
if yaml:
return "track.yaml"
else:
return "track.json"

@classmethod
def filenameYaml(cls):
return "track.yaml"
def filename(cls, module_folder):
pattern = r"^track\.(json|y(a)?ml)$"
files = os.listdir(module_folder)
for file in files:
if re.match(pattern, file):
return file
raise FileNotFoundError("No matching file found in the folder. Supported file types are track.json, track.yml or track.yaml")

@classmethod
def expected_fields(cls, __type=True):
Expand Down
3 changes: 1 addition & 2 deletions sync/model/TrackJson.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ class TrackJson(AttrDict, JsonIO):
@classmethod
def load(cls, file: Path) -> TrackJson: ...
@classmethod
def filename(cls, yaml: bool) -> str: ...
@classmethod
def filename(cls, module_folder: Path) -> str: ...
@classmethod
def expected_fields(cls, __type: bool = ...) -> Dict[str, Type]: ...

Expand Down
7 changes: 3 additions & 4 deletions sync/track/LocalTracks.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def _get_from_file(self, file):
def get_track(self, module_id):
module_folder = self._modules_folder.joinpath(module_id)
has_yaml_track = module_folder.joinpath("track.yaml").exists()
json_file = module_folder.joinpath(TrackJson.filename(has_yaml_track))
json_file = module_folder.joinpath(TrackJson.filename(module_folder))

result = self._get_from_file(json_file)
if result.is_failure:
Expand Down Expand Up @@ -87,7 +87,7 @@ def tracks(self):
def add_track(cls, track, modules_folder, cover=True):
module_folder = modules_folder.joinpath(track.id)
has_yaml_track = module_folder.joinpath("track.yaml").exists()
json_file = module_folder.joinpath(TrackJson.filename(has_yaml_track))
json_file = module_folder.joinpath(TrackJson.filename(module_folder))

if not json_file.exists():
track.added = datetime.now().timestamp()
Expand All @@ -111,8 +111,7 @@ def del_track(cls, module_id, modules_folder):
@classmethod
def update_track(cls, track, modules_folder):
module_folder = modules_folder.joinpath(track.id)
has_yaml_track = module_folder.joinpath("track.yaml").exists()
json_file = module_folder.joinpath(TrackJson.filename(has_yaml_track))
json_file = module_folder.joinpath(TrackJson.filename(module_folder))

if not json_file.exists():
return
Expand Down

0 comments on commit 6d494dc

Please sign in to comment.