Skip to content

Commit

Permalink
Publish metadata-compact.json
Browse files Browse the repository at this point in the history
  • Loading branch information
mackorone committed Sep 26, 2023
1 parent d0e41a2 commit 4713847
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 63 deletions.
20 changes: 11 additions & 9 deletions src/file_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@


class Formatter:

TRACK_NO = "No."
TITLE = "Title"
ARTISTS = "Artist(s)"
Expand Down Expand Up @@ -42,17 +41,20 @@ def readme(cls, prev_content: str, playlists: Mapping[PlaylistID, Playlist]) ->
return "\n".join(new_lines) + "\n"

@classmethod
def metadata_json(cls, playlists: Mapping[PlaylistID, Playlist]) -> str:
metadata_dict = {}
def metadata_full_json(cls, playlists: Mapping[PlaylistID, Playlist]) -> str:
data = {}
for playlist_id, playlist in playlists.items():
playlist_dict = dataclasses.asdict(playlist)
del playlist_dict["tracks"]
metadata_dict[playlist_id] = playlist_dict
return json.dumps(
metadata_dict,
indent=2,
sort_keys=True,
)
data[playlist_id] = playlist_dict
return json.dumps(data, indent=2, sort_keys=True)

@classmethod
def metadata_compact_json(cls, playlists: Mapping[PlaylistID, Playlist]) -> str:
data = {}
for playlist_id, playlist in playlists.items():
data[playlist_id] = playlist.unique_name
return json.dumps(data, separators=(",", ":"), sort_keys=True)

@classmethod
def plain(cls, playlist_id: PlaylistID, playlist: Playlist) -> str:
Expand Down
12 changes: 11 additions & 1 deletion src/file_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def ensure_subdirs_exist(self) -> None:
self._get_plain_dir(),
self._get_pretty_dir(),
self._get_cumulative_dir(),
self._get_metadata_dir(),
]:
directory.mkdir(parents=True, exist_ok=True)

Expand Down Expand Up @@ -101,9 +102,15 @@ def get_cumulative_json_path(self, playlist_id: PlaylistID) -> pathlib.Path:
def get_cumulative_markdown_path(self, playlist_id: PlaylistID) -> pathlib.Path:
return self._get_cumulative_dir() / f"{playlist_id}.md"

def get_metadata_json_path(self) -> pathlib.Path:
def get_old_metadata_json_path(self) -> pathlib.Path:
return self._playlists_dir / "metadata.json"

def get_metadata_full_json_path(self) -> pathlib.Path:
return self._get_metadata_dir() / "metadata-full.json"

def get_metadata_compact_json_path(self) -> pathlib.Path:
return self._get_metadata_dir() / "metadata-compact.json"

def get_readme_path(self) -> pathlib.Path:
return self._playlists_dir.parent / "README.md"

Expand All @@ -119,6 +126,9 @@ def _get_pretty_dir(self) -> pathlib.Path:
def _get_cumulative_dir(self) -> pathlib.Path:
return self._playlists_dir / "cumulative"

def _get_metadata_dir(self) -> pathlib.Path:
return self._playlists_dir / "metadata"

@classmethod
def _remove_suffix(cls, string: str, suffix: str) -> str:
if not suffix:
Expand Down
30 changes: 23 additions & 7 deletions src/file_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,20 @@ async def _update_files_impl(
# Check for unexpected files in playlist directories
file_manager.ensure_no_unexpected_files()

# Write metadata.json
metadata_json_path = file_manager.get_metadata_json_path()
prev_content = cls._get_file_content_or_empty_string(metadata_json_path)
cls._write_to_file_if_content_changed(
prev_content=prev_content,
content=Formatter.metadata_json(playlists) + "\n",
path=metadata_json_path,
# Update all metadata files
metadata_full_content = Formatter.metadata_full_json(playlists) + "\n"
metadata_compact_content = Formatter.metadata_compact_json(playlists) + "\n"
cls._maybe_update_file(
path=file_manager.get_old_metadata_json_path(),
content=metadata_full_content,
)
cls._maybe_update_file(
path=file_manager.get_metadata_full_json_path(),
content=metadata_full_content,
)
cls._maybe_update_file(
path=file_manager.get_metadata_compact_json_path(),
content=metadata_compact_content,
)

# Lastly, update README.md
Expand Down Expand Up @@ -292,3 +299,12 @@ def _write_to_file_if_content_changed(
logger.info(f" Writing updates to file: {path}")
with open(path, "w") as f:
f.write(content)

@classmethod
def _maybe_update_file(cls, path: pathlib.Path, content: str) -> None:
prev_content = cls._get_file_content_or_empty_string(path)
cls._write_to_file_if_content_changed(
prev_content=prev_content,
content=content,
path=path,
)
102 changes: 56 additions & 46 deletions src/tests/test_file_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,9 @@ async def test_readme_and_metadata_json(self, mock_logger: Mock) -> None:
for playlist_id in "abcd":
(registry_dir / playlist_id).touch()

metadata_dir = self.playlists_dir / "metadata"
metadata_dir.mkdir(parents=True)

pretty_dir = self.playlists_dir / "pretty"
pretty_dir.mkdir(parents=True)
for playlist_id in "ac":
Expand Down Expand Up @@ -350,53 +353,60 @@ async def test_readme_and_metadata_json(self, mock_logger: Mock) -> None:
"""
),
)
with open(self.playlists_dir / "metadata.json", "r") as f:
for path in [
self.playlists_dir / "metadata.json",
metadata_dir / "metadata-full.json",
]:
with open(path, "r") as f:
content = f.read()
self.assertEqual(
content,
textwrap.dedent(
"""\
{
"a": {
"description": "description",
"num_followers": 0,
"original_name": "name_a",
"owner": {
"name": "owner_name",
"url": "owner_url"
},
"snapshot_id": "snapshot_id",
"unique_name": "name_a",
"url": "url_a"
},
"b": {
"description": "description",
"num_followers": 0,
"original_name": "name_b",
"owner": {
"name": "owner_name",
"url": "owner_url"
},
"snapshot_id": "snapshot_id",
"unique_name": "name_b",
"url": "url_b"
},
"c": {
"description": "description",
"num_followers": 0,
"original_name": " name_c ",
"owner": {
"name": "owner_name",
"url": "owner_url"
},
"snapshot_id": "snapshot_id",
"unique_name": " name_c ",
"url": "url_c"
}
}
"""
),
)
with open(metadata_dir / "metadata-compact.json", "r") as f:
content = f.read()
self.assertEqual(
content,
textwrap.dedent(
"""\
{
"a": {
"description": "description",
"num_followers": 0,
"original_name": "name_a",
"owner": {
"name": "owner_name",
"url": "owner_url"
},
"snapshot_id": "snapshot_id",
"unique_name": "name_a",
"url": "url_a"
},
"b": {
"description": "description",
"num_followers": 0,
"original_name": "name_b",
"owner": {
"name": "owner_name",
"url": "owner_url"
},
"snapshot_id": "snapshot_id",
"unique_name": "name_b",
"url": "url_b"
},
"c": {
"description": "description",
"num_followers": 0,
"original_name": " name_c ",
"owner": {
"name": "owner_name",
"url": "owner_url"
},
"snapshot_id": "snapshot_id",
"unique_name": " name_c ",
"url": "url_c"
}
}
"""
),
)
self.assertEqual(content, '{"a":"name_a","b":"name_b","c":" name_c "}\n')

async def test_success(self) -> None:
# TODO
Expand Down

0 comments on commit 4713847

Please sign in to comment.