Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1"""LICENSE 

2Copyright 2015 Hermann Krumrey <hermann@krumreyh.com> 

3 

4This file is part of toktokkie. 

5 

6toktokkie is free software: you can redistribute it and/or modify 

7it under the terms of the GNU General Public License as published by 

8the Free Software Foundation, either version 3 of the License, or 

9(at your option) any later version. 

10 

11toktokkie is distributed in the hope that it will be useful, 

12but WITHOUT ANY WARRANTY; without even the implied warranty of 

13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

14GNU General Public License for more details. 

15 

16You should have received a copy of the GNU General Public License 

17along with toktokkie. If not, see <http://www.gnu.org/licenses/>. 

18LICENSE""" 

19 

20import os 

21import argparse 

22from puffotter.os import listdir, get_ext 

23from toktokkie.commands.Command import Command 

24from toktokkie.Directory import Directory 

25 

26 

27class PlaylistCreateCommand(Command): 

28 """ 

29 Class that encapsulates behaviour of the playlist-create command 

30 """ 

31 

32 @classmethod 

33 def name(cls) -> str: 

34 """ 

35 :return: The command name 

36 """ 

37 return "playlist-create" 

38 

39 @classmethod 

40 def help(cls) -> str: 

41 """ 

42 :return: The help message for the command 

43 """ 

44 return "Creates a playlist file containing" \ 

45 "all songs in the directories" 

46 

47 @classmethod 

48 def prepare_parser(cls, parser: argparse.ArgumentParser): 

49 """ 

50 Prepares an argumentparser for this command 

51 :param parser: The parser to prepare 

52 :return: None 

53 """ 

54 cls.add_directories_arg(parser) 

55 parser.add_argument("playlist_file", 

56 help="The destination playlist file") 

57 parser.add_argument("--format", choices={"m3u"}, default="m3u", 

58 help="The playlist format") 

59 parser.add_argument("--prefix", help="Prefix for the generated paths") 

60 

61 def execute(self): 

62 """ 

63 Executes the commands 

64 :return: None 

65 """ 

66 music_exts = ["mp3", "flac", "wav", "aac"] 

67 

68 playlist_files = [] 

69 

70 for directory in Directory.load_directories(self.args.directories): 

71 for album, album_path in listdir(directory.path, no_files=True): 

72 for song, song_path in listdir(album_path, no_dirs=True): 

73 if get_ext(song) in music_exts: 

74 

75 if self.args.prefix is None: 

76 playlist_files.append(song_path) 

77 else: 

78 playlist_files.append( 

79 os.path.join(self.args.prefix, song_path) 

80 ) 

81 

82 if self.args.format == "m3u": 

83 playlist = "\n".join(playlist_files) 

84 else: 

85 playlist = "" 

86 

87 with open(self.args.playlist_file, "w") as f: 

88 f.write(playlist)