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 

20from abc import ABC 

21from typing import Dict, Any 

22from toktokkie.exceptions import InvalidMetadata 

23from toktokkie.enums import IdType 

24from toktokkie.metadata.base.Validator import Validator 

25from toktokkie.metadata.music.MusicExtras import MusicExtras 

26 

27 

28class MusicValidator(Validator, MusicExtras, ABC): 

29 """ 

30 Implements the Validator functionality for music metadata 

31 """ 

32 

33 @classmethod 

34 def build_schema(cls) -> Dict[str, Any]: 

35 """ 

36 Generates the JSON schema 

37 :return: The JSON schema 

38 """ 

39 base = super().build_schema() 

40 

41 valid_album_ids = cls.valid_id_types() 

42 valid_album_ids.remove(IdType.MUSICBRAINZ_ARTIST) 

43 valid_album_ids.append(IdType.MUSICBRAINZ_RELEASE) 

44 valid_album_ids.append(IdType.YOUTUBE_VIDEO) 

45 

46 album_ids = cls._create_ids_schema(valid_album_ids) 

47 series_ids = cls._create_ids_schema(Validator.theme_song_id_types()) 

48 

49 base["properties"].update({ 

50 "albums": { 

51 "type": "array", 

52 "items": { 

53 "type": "object", 

54 "properties": { 

55 "name": {"type": "string"}, 

56 "genre": {"type": "string"}, 

57 "year": {"type": "number"}, 

58 "ids": album_ids 

59 }, 

60 "required": ["name", "genre", "year"], 

61 "additionalProperties": False 

62 } 

63 }, 

64 "theme_songs": { 

65 "type": "array", 

66 "items": { 

67 "type": "object", 

68 "properties": { 

69 "name": {"type": "string"}, 

70 "series_ids": series_ids, 

71 "theme_type": { 

72 "type": "string", 

73 "pattern": "(op|ed|insert|special|other){1}" 

74 } 

75 }, 

76 "required": ["name", "theme_type"], 

77 "additionalProperties": False 

78 } 

79 } 

80 }) 

81 base["required"].append("albums") 

82 return base 

83 

84 def validate(self): 

85 super().validate() 

86 album_names = [x.name for x in self.albums] 

87 for theme_song in self.json.get("theme_songs", []): 

88 name = theme_song["name"] 

89 if name not in album_names: 

90 raise InvalidMetadata(f"Missing album data for {name}")