# HG changeset patch # User gregor # Date 1713695684 -7200 # Sun Apr 21 12:34:44 2024 +0200 # Node ID 20be26148513357d4349f87c132ddd94a91884f4 # Parent 19bcd62f558fb1ce069e6e89f0d86e7f8e4ea5af python3 migration. Check for directory. diff --git a/music_collection_sync.py b/music_collection_sync.py --- a/music_collection_sync.py +++ b/music_collection_sync.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 """ This script synchronizes a music collection recursivly one way from a losless format source directory to a lossy file format target directory. @@ -44,7 +44,7 @@ (options, args) = parser.parse_args() -if len(args) <> 2: +if len(args) != 2: parser.error("incorrect number of arguments") try: level = int(getattr(logging, options.loglevel)) @@ -54,10 +54,12 @@ for path in args: if not os.path.exists(path): parser.error("path does not exist: %s" % path) + if not os.path.isdir(path): + parser.error("path is not a directory: %s" % path) if args[0] == args[1]: parser.error("source and target path must not be the same") - + if options.win: illegal_characters = '?[],\=+<>:;"*|^' else: @@ -105,7 +107,7 @@ else: try: reference_tag = [reference_key for (reference_key, reference_values) - in flac_tags_synonyms.items() if tag.lower() in reference_values][0] + in list(flac_tags_synonyms.items()) if tag.lower() in reference_values][0] if (reference_tag in ["composer", "artist"] and id3_tags_dict[reference_tag] != ""): #tag value is a list, mp3 id3 v2 separator is a / id3_tags_dict[reference_tag] = id3_tags_dict[reference_tag] + "/" + value @@ -113,7 +115,7 @@ id3_tags_dict[reference_tag] = value except IndexError: logging.info("unsupported id3 tag '%s' ignored" % id3) - for key in id3_tags_dict.keys(): + for key in list(id3_tags_dict.keys()): id3_tags_dict[key] = shellquote(id3_tags_dict[key]) return id3_tags_dict @@ -177,8 +179,8 @@ ".wav":[".ogg", x_to_ogg]}) elif target_format == 'mp3': convert_map.update({".flac":[".mp3", flac_to_mp3], - ".wav":[".mp3", wav_to_mp3]}) - + ".wav":[".mp3", wav_to_mp3]}) + def shellquote(s): if sys.platform.startswith("win"): @@ -189,9 +191,9 @@ def convert(source_fname): target_fname = source_fname.replace(source_dir, target_dir) cmd = None - + if os.path.isdir(source_fname): - cmd = convert_map["dir"][1] + cmd = convert_map["dir"][1] elif os.path.isfile(source_fname) or os.path.islink(source_fname): try: ext = os.path.splitext(source_fname)[1].lower() @@ -200,21 +202,21 @@ logging.warning("File extension '%s' not supported." % (ext)) else: target_fname = os.path.splitext(target_fname)[0] + "%s" % conv[0] - cmd = conv[1] - - else: + cmd = conv[1] + + else: logging.error("File type not supported.") for c in illegal_characters: target_fname = target_fname.replace(c, "-") - + if not os.path.exists(target_fname): if cmd: logging.debug("cmd: %s, source: %s, target: %s" % (cmd, source_fname, target_fname)) cmd(source_fname, target_fname) else: logging.debug("File '%s' ignored." % source_fname) - + else: logging.debug("Target '%s' already exists." % target_fname) @@ -281,5 +283,5 @@ log_elapsed_time(start, end) if not options.nodonation: - print "\nIf you find the script useful and would like to donate: \n" \ - "UniCredit Bank Austria AG, Gregor Horvath, IBAN: AT47 1100 0014 1436 2200, BIC: BKAUATWW" + print("\nIf you find the script useful and would like to donate: \n" \ + "UniCredit Bank Austria AG, Gregor Horvath, IBAN: AT47 1100 0014 1436 2200, BIC: BKAUATWW")