Creating Channel.info_json_path property to avoid redefining channel's info json path.

This commit is contained in:
Roland 2024-12-24 14:41:51 -07:00
parent 808f46be5b
commit e49ffc8a6c
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
3 changed files with 27 additions and 1 deletions

View file

@ -101,3 +101,20 @@ async def test_channel_download_relationships(test_session, download_channel):
# Delete the Channel, and any relationships. # Delete the Channel, and any relationships.
download_channel.delete_with_videos() download_channel.delete_with_videos()
assert test_session.query(Download).count() == 0 assert test_session.query(Download).count() == 0
def test_channel_info_json(test_session, test_directory):
channel = Channel(name='new channel')
test_session.add(channel)
with pytest.raises(FileNotFoundError) as e:
# Cannot get info json because directory is not defined.
channel.info_json_path
assert 'directory' in str(e)
# Channel's directory is created if it does not exist.
channel.directory = test_directory / 'new channel'
assert channel.info_json_path == test_directory / 'new channel/new channel.info.json'
assert channel.directory.is_dir()
# info json file is not yet created.
assert not channel.info_json_path.exists()

View file

@ -768,7 +768,7 @@ def update_channel_catalog(channel: Channel, info: dict):
# Write the Channel's info to a JSON file. # Write the Channel's info to a JSON file.
if channel.directory: if channel.directory:
info_json_path = channel.directory / f'{channel.name}.info.json' info_json_path = channel.info_json_path
with info_json_path.open('wt') as fh: with info_json_path.open('wt') as fh:
json.dump(info, fh, indent=2, sort_keys=True) json.dump(info, fh, indent=2, sort_keys=True)
logger.debug(f'Wrote channel info json to {info_json_path}') logger.debug(f'Wrote channel info json to {info_json_path}')

View file

@ -556,6 +556,15 @@ class Channel(ModelHelper, Base):
def location(self) -> str: def location(self) -> str:
return f'/videos/channel/{self.id}/video' return f'/videos/channel/{self.id}/video'
@property
def info_json_path(self) -> pathlib.Path:
if self.directory:
if not self.directory.exists():
self.directory.mkdir(parents=True)
return self.directory / f'{self.name}.info.json'
raise FileNotFoundError(f'Cannot create Channel info json because directory is not defined: {self}')
def delete_with_videos(self): def delete_with_videos(self):
"""Delete all Video records (but not video files) related to this Channel. Then delete the Channel.""" """Delete all Video records (but not video files) related to this Channel. Then delete the Channel."""
session = Session.object_session(self) session = Session.object_session(self)