Skip to content
This repository has been archived by the owner on May 3, 2021. It is now read-only.

Commit

Permalink
Scan image files.
Browse files Browse the repository at this point in the history
This just scans for the image mime types; it doesn't parse the images
themselves yet.

Addresses #61.
  • Loading branch information
dseomn committed Sep 7, 2020
1 parent 4e08e52 commit f229840
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
32 changes: 32 additions & 0 deletions pepper_music_player/library/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ class AudioFile(File):
track: entity.Track


@dataclasses.dataclass(frozen=True)
class ImageFile(File):
"""An image file.
Attributes:
image: The image in the file.
"""
image: entity.Image


def _read_audio_tags(dirname: str, basename: str, filename: str) -> tag.Tags:
"""Returns tags read from an audio file."""
file_info = mutagen.File(filename, easy=True)
Expand All @@ -61,6 +71,17 @@ def _read_audio_tags(dirname: str, basename: str, filename: str) -> tag.Tags:
}).derive()


def _read_image_tags(dirname: str, basename: str, filename: str) -> tag.Tags:
"""Returns tags read from an image file."""
# TODO(#61): Actually read more tags (e.g., width and height) from the file
# itself.
return tag.Tags({
tag.BASENAME: (basename,),
tag.DIRNAME: (dirname,),
tag.FILENAME: (filename,),
}).derive()


def scan(root_dirname: str) -> Iterable[File]:
"""Scans a directory."""
# TODO: Keep track of errors with os.walk(onerror=...)
Expand All @@ -81,6 +102,17 @@ def scan(root_dirname: str) -> Iterable[File]:
basename=basename,
filename=str(filepath))),
)
elif mime_major == 'image':
yield ImageFile(
filename=str(filepath),
dirname=dirname,
basename=basename,
image=entity.Image(tags=_read_image_tags(
dirname=dirname,
basename=basename,
filename=str(filepath),
)),
)
else:
yield File(filename=str(filepath),
dirname=dirname,
Expand Down
18 changes: 18 additions & 0 deletions pepper_music_player/library/scan_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,24 @@ def test_parses_flac(self):
scan.scan(str(self._root_dirpath)),
)

def test_parses_image(self):
# TODO(#61): Write a real image, and test tags parsed from that.
self._root_dirpath.joinpath('foo.png').write_bytes(b'')
self.assertCountEqual(
(scan.ImageFile(
filename=str(self._root_dirpath.joinpath('foo.png')),
dirname=str(self._root_dirpath),
basename='foo.png',
image=entity.Image(tags=tag.Tags({
tag.BASENAME: ('foo.png',),
tag.DIRNAME: (str(self._root_dirpath),),
tag.FILENAME:
(str(self._root_dirpath.joinpath('foo.png')),),
}).derive()),
),),
scan.scan(str(self._root_dirpath)),
)


if __name__ == '__main__':
unittest.main()

0 comments on commit f229840

Please sign in to comment.