import json from copy import deepcopy from pathlib import Path from os import makedirs from deezer import TrackFormats import deemix.utils.localpaths as localpaths class OverwriteOption(): """Should the lib overwrite files?""" OVERWRITE = 'y' # Yes, overwrite the file DONT_OVERWRITE = 'n' # No, don't overwrite the file DONT_CHECK_EXT = 'e' # No, and don't check for extensions KEEP_BOTH = 'b' # No, and keep both files ONLY_TAGS = 't' # Overwrite only the tags class FeaturesOption(): """What should I do with featured artists?""" NO_CHANGE = "0" # Do nothing REMOVE_TITLE = "1" # Remove from track title REMOVE_TITLE_ALBUM = "3" # Remove from track title and album title MOVE_TITLE = "2" # Move to track title DEFAULTS = { "albumNameTemplate": "%artist% - %album%", "albumTracknameTemplate": "%tracknumber% - %title%", "albumVariousArtists": True, "artistCasing": "nothing", "artistImageTemplate": "folder", "artistNameTemplate": "%artist%", "coverImageTemplate": "cover", "createAlbumFolder": True, "createArtistFolder": False, "createCDFolder": True, "createM3U8File": False, "createPlaylistFolder": True, "createSingleFolder": False, "createStructurePlaylist": False, "dateFormat": "Y-M-D", "downloadLocation": str(localpaths.getMusicFolder()), "embeddedArtworkPNG": False, "embeddedArtworkSize": 800, "executeCommand": "", "fallbackBitrate": False, "fallbackISRC": False, "fallbackSearch": False, "featuredToTitle": FeaturesOption.NO_CHANGE, "feelingLucky": False, "illegalCharacterReplacer": "_", "jpegImageQuality": 90, "localArtworkFormat": "jpg", "localArtworkSize": 1400, "logErrors": True, "logSearched": False, "maxBitrate": TrackFormats.MP3_320, "overwriteFile": OverwriteOption.DONT_OVERWRITE, "paddingSize": "0", "padTracks": True, "playlistFilenameTemplate": "playlist", "playlistNameTemplate": "%playlist%", "playlistTracknameTemplate": "%position% - %artist% - %title%", "queueConcurrency": 3, "removeAlbumVersion": False, "removeDuplicateArtists": True, "saveArtwork": True, "saveArtworkArtist": False, "syncedLyrics": False, "tags": { "album": True, "albumArtist": True, "artist": True, "artists": True, "barcode": True, "bpm": True, "composer": False, "copyright": False, "cover": True, "coverDescriptionUTF8": False, "date": True, "discNumber": True, "discTotal": False, "explicit": False, "genre": True, "involvedPeople": False, "isrc": True, "label": True, "length": True, "lyrics": False, "multiArtistSeparator": "default", "rating": False, "replayGain": False, "saveID3v1": True, "savePlaylistAsCompilation": False, "singleAlbumArtist": False, "source": False, "syncedLyrics": False, "title": True, "trackNumber": True, "trackTotal": False, "useNullSeparator": False, "year": True, }, "titleCasing": "nothing", "tracknameTemplate": "%artist% - %title%", } def save(settings, configFolder=None): configFolder = Path(configFolder or localpaths.getConfigFolder()) makedirs(configFolder, exist_ok=True) # Create config folder if it doesn't exsist with open(configFolder / 'config.json', 'w', encoding="utf-8") as configFile: json.dump(settings, configFile, indent=2) def load(configFolder=None): configFolder = Path(configFolder or localpaths.getConfigFolder()) makedirs(configFolder, exist_ok=True) # Create config folder if it doesn't exsist if not (configFolder / 'config.json').is_file(): save(DEFAULTS, configFolder) # Create config file if it doesn't exsist # Read config file with open(configFolder / 'config.json', 'r', encoding="utf-8") as configFile: try: settings = json.load(configFile) except json.decoder.JSONDecodeError: save(DEFAULTS, configFolder) settings = deepcopy(DEFAULTS) except Exception: settings = deepcopy(DEFAULTS) if check(settings) > 0: try: save(settings, configFolder) # Check the settings and save them if something changed except: print(f"Error saving config file {configFile.name}, continuing without saving.") return settings def check(settings): changes = 0 for i_set in DEFAULTS: if not i_set in settings or not type(settings[i_set] is type(DEFAULTS[i_set])): settings[i_set] = DEFAULTS[i_set] changes += 1 for i_set in DEFAULTS['tags']: if not i_set in settings['tags'] or not isinstance(settings['tags'][i_set], type(DEFAULTS['tags'][i_set])): settings['tags'][i_set] = DEFAULTS['tags'][i_set] changes += 1 if settings['downloadLocation'] == "": settings['downloadLocation'] = DEFAULTS['downloadLocation'] changes += 1 for template in ['tracknameTemplate', 'albumTracknameTemplate', 'playlistTracknameTemplate', 'playlistNameTemplate', 'artistNameTemplate', 'albumNameTemplate', 'playlistFilenameTemplate', 'coverImageTemplate', 'artistImageTemplate', 'paddingSize']: if settings[template] == "": settings[template] = DEFAULTS[template] changes += 1 return changes