def find_songs(cls, path, file_type=None): """ Scans the specified 'path'' and returns a list of Song-objects of supported types USAGE: pl = Playlist(pl_name) pl.add_songs(crawler.find_songs(path_to_songs))""" path = Path(path).absolute().resolve() found_songs = list() if path.exists() and path.is_dir(): supported_types = [".mp3"] # if "file_type" is not specified, # all supported types are checked: if file_type is None: file_type = supported_types elif file_type not in supported_types: logging.error( "Supplied file-type not supported by MML_client: {}". format(file_type)) # Get all the 'objects' in the specified 'path' # which are 'files' (not dirs) and iterate over them # it is NON-Recursive and with dept==1: for file in [obj for obj in path.iterdir() if obj.is_file()]: if file.suffix in file_type: temp_song = Song().load(file) if temp_song is not None: found_songs.append(temp_song) return found_songs
class TestSongAlbum(unittest.TestCase): def setUp(self) -> None: self.song = Song() def tearDown(self) -> None: del self.song def test_set_album_type(self): self.assertRaises(TypeError, self.song.set_album, album=3) self.assertRaises(TypeError, self.song.set_album, album=True) self.assertRaises(TypeError, self.song.set_album, album=None) def test_set_album_value(self): self.assertRaises(ValueError, self.song.set_album, album="") self.assertRaises(ValueError, self.song.set_album, album='') def test_set_album_correct(self): self.assertTrue(self.song.set_album("Name")) self.assertTrue(self.song.set_album("3")) self.assertTrue(self.song.set_album("None"))
class TestSongArtist(unittest.TestCase): def setUp(self) -> None: self.song = Song() def tearDown(self) -> None: del self.song def test_set_artist_type(self): self.assertRaises(TypeError, self.song.set_artist, artist=3) self.assertRaises(TypeError, self.song.set_artist, artist=True) self.assertRaises(TypeError, self.song.set_artist, artist=None) def test_set_artist_value(self): self.assertRaises(ValueError, self.song.set_artist, artist="") self.assertRaises(ValueError, self.song.set_artist, artist='') def test_set_artist_correct(self): self.assertTrue(self.song.set_artist("Name")) self.assertTrue(self.song.set_artist("3")) self.assertTrue(self.song.set_artist("None"))
class TestSongTitle(unittest.TestCase): def setUp(self) -> None: self.song = Song() def tearDown(self) -> None: del self.song def test_set_title_type(self): self.assertRaises(TypeError, self.song.set_title, title=3) self.assertRaises(TypeError, self.song.set_title, title=True) self.assertRaises(TypeError, self.song.set_title, title=None) def test_set_title_value(self): self.assertRaises(ValueError, self.song.set_title, title="") self.assertRaises(ValueError, self.song.set_title, title='') def test_set_title_correct(self): self.assertTrue(self.song.set_title("Name")) self.assertTrue(self.song.set_title("3")) self.assertTrue(self.song.set_title("None"))
class TestSongLength(unittest.TestCase): def setUp(self) -> None: self.song = Song() def tearDown(self) -> None: del self.song def test_set_length_type(self): self.assertRaises(TypeError, self.song.set_length, length=True) self.assertRaises(TypeError, self.song.set_length, length=object) def test_set_length_value(self): self.assertRaises(ValueError, self.song.set_length, length="-3") self.assertRaises(ValueError, self.song.set_length, length=-19.8) self.assertRaises(ValueError, self.song.set_length, length=-16) def test_set_length_correct(self): self.assertTrue(self.song.set_length("3")) self.assertTrue(self.song.set_length("5.14")) self.assertTrue(self.song.set_length(34)) self.assertTrue(self.song.set_length(3.14))
def load(path): """ Loads a PlayList from a specified JSON-Playlist file The file has to exists and should have an entry, indicating it's an MML-Playlist file (added by the Playlist.save() method) :param path: can be absolute or relative, but must point to a valid .json :type path: Path :type path: str :return a new Playlist or None :rtype Playlist object or Bool :raise TypeError """ # full OS-path to the Playlist file: path = Path(path).absolute().resolve().with_suffix(".json") new_playlist = Playlist("") if path.exists() and path.is_file(): try: with open(path, 'r') as json_file: data = json.load(json_file) if "meta" in data and \ "Is MyLibrary Playlist" in data["meta"] and \ data["meta"]["Is MyLibrary Playlist"] == "yes": logging.info("Loading MML-Playlist from: {}".format(path)) # to get rid of the PATH and from the '.json' part of the NAME: new_playlist.set_name(path.stem) new_playlist.path = path for tmp_song in data["songs"]: new_song = Song(tmp_song["title"], tmp_song["artist"], tmp_song["album"], tmp_song["length"], tmp_song["path"]) new_playlist.add_song(new_song) else: logging.warning("Not a valid MML-Playlist file: {}".format(path)) return None except Exception as e: logging.error("Could not read file: {}".format(path)) return None logging.info("Playlist loaded successfully!") return new_playlist else: raise TypeError("Playlist.path must be a valid OS Path!")
def setUp(self) -> None: self.song = Song() self.song.set_length(180)
class TestSongLengthPretty(unittest.TestCase): def setUp(self) -> None: self.song = Song() self.song.set_length(180) def tearDown(self) -> None: del self.song def test_length_pretty_180(self): self.song.set_length(180) self.assertEqual(self.song.length_pretty(hours=True, minutes=True, seconds=True), "0:03:00") self.assertEqual(self.song.length_pretty(minutes=True, seconds=True), "03:00") self.assertEqual(self.song.length_pretty(seconds=True), ":00") self.assertEqual(self.song.length_pretty(minutes=True), "03") self.assertEqual(self.song.length_pretty(hours=True), "0:") def test_length_pretty_221(self): self.song.set_length(221) self.assertEqual(self.song.length_pretty(hours=True, minutes=True, seconds=True), "0:03:41") self.assertEqual(self.song.length_pretty(minutes=True, seconds=True), "03:41") self.assertEqual(self.song.length_pretty(seconds=True), ":41") self.assertEqual(self.song.length_pretty(minutes=True), "03") self.assertEqual(self.song.length_pretty(hours=True), "0:") def test_length_pretty_4502(self): self.song.set_length(4502) self.assertEqual(self.song.length_pretty(hours=True, minutes=True, seconds=True), "1:15:02") self.assertEqual(self.song.length_pretty(minutes=True, seconds=True), "15:02") self.assertEqual(self.song.length_pretty(seconds=True), ":02") self.assertEqual(self.song.length_pretty(minutes=True), "15") self.assertEqual(self.song.length_pretty(hours=True), "1:")
def setUp(self) -> None: self.song = Song()