From 1296ba6e5032f75177306d66f425b8a079f23306 Mon Sep 17 00:00:00 2001 From: raphael60650 Date: Fri, 18 Jun 2021 16:58:16 +0200 Subject: [PATCH] simplified code and added more special case support --- source/patch_track.py | 64 +++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/source/patch_track.py b/source/patch_track.py index 1578075..7316d51 100644 --- a/source/patch_track.py +++ b/source/patch_track.py @@ -13,13 +13,14 @@ def count_track(self): for cup in ctconfig["cup"].values(): if not (cup["locked"]): tracks.extend(cup["courses"].values()) tracks.extend(ctconfig["tracks_list"]) + tracks = [dict(t) for t in {tuple(d.items()) for d in tracks}] total_track = len(tracks) return tracks, total_track def patch_autoadd(self): if os.path.exists("./file/auto-add"): shutil.rmtree("./file/auto-add") - if not (os.path.exists(self.path_mkwf + "/tmp/")): os.makedirs(self.path_mkwf + "/tmp/") + if not os.path.exists(self.path_mkwf + "/tmp/"): os.makedirs(self.path_mkwf + "/tmp/") subprocess.run(["./tools/szs/wszst", "AUTOADD", get_nodir(self.path_mkwf) + "/files/Race/Course/", "--DEST", get_nodir(self.path_mkwf) + "/tmp/auto-add/"], creationflags=CREATE_NO_WINDOW, cwd=get_dir(self.path_mkwf), @@ -34,54 +35,53 @@ def patch_track(self, tracks, total_track="?"): error_count, error_max = 0, 3 for i, track in enumerate(tracks): - track_file = track["name"] - if "prefix" in track: track_file = track["prefix"] + " " + track_file - if "suffix" in track: track_file = track_file + " (" + track["suffix"] + ")" - track_wu8_file = f"./file/Track-WU8/{track_file}.wu8" - track_szs_file = f"./file/Track/{track_file}.szs" - + track_file = get_trackname(track=track) while True: if len(process_list) < max_process: - process_list[(track_szs_file, track_wu8_file)] = None + process_list[track_file] = None # Used for self.Progress(statut=self.translate("Conversion des courses") + f"\n({i + 1}/{total_track})\n" + - "\n".join([get_nodir(process[0]) for process in process_list]), add=1) + "\n".join(process_list.keys()), add=1) - for track_file in [track_szs_file, track_wu8_file]: - if os.path.exists(track_file): - if os.path.getsize(track_file) < 1000: # File under this size are corrupted - os.remove(track_file) + for _track in [get_track_szs(track_file), get_track_wu8(track_file)]: + if os.path.exists(_track): + if os.path.getsize(_track) < 1000: # File under this size are corrupted + os.remove(_track) - dl_code = self.get_github_file(track_wu8_file) - if dl_code == -1: + download_returncode = self.get_github_file(get_track_wu8(track_file)) + if download_returncode == -1: # can't download error_count += 1 if error_count > error_max: # Too much track wasn't correctly converted messagebox.showerror( self.translate("Erreur"), self.translate("Trop de course ont eu une erreur du téléchargement.")) - return + return -1 else: messagebox.showwarning(self.translate("Attention"), self.translate("Impossible de télécharger cette course ! (") + str(error_count) + "/" + str(error_max) + ")") - if not (os.path.exists(track_szs_file)): - process_list[(track_szs_file, track_wu8_file)] = subprocess.Popen([ - "./tools/szs/wszst", "NORMALIZE", track_wu8_file, "--DEST", - "./file/Track/%N.szs", "--szs", "--overwrite", "--autoadd-path", - "./file/auto-add/"], creationflags=CREATE_NO_WINDOW, stderr=subprocess.PIPE) + if not (os.path.exists(get_track_szs(track_file))) or download_returncode == 3: # returncode 3 is track has been updated + if os.path.exists(get_track_wu8(track_file)): + process_list[track_file] = subprocess.Popen([ + "./tools/szs/wszst", "NORMALIZE", get_track_wu8(track_file), "--DEST", + "./file/Track/%N.szs", "--szs", "--overwrite", "--autoadd-path", + "./file/auto-add/"], creationflags=CREATE_NO_WINDOW, stderr=subprocess.PIPE) + else: + messagebox.showerror(self.translate("Erreur"), + self.translate("Impossible de convertir la course.\n" + "Réactiver le téléchargement des courses et réessayer.")) + return -1 + elif self.boolvar_del_track_after_conv.get(): os.remove(get_track_wu8(track_file)) break else: - for (track_szs_file, track_wu8_file), process in process_list.items(): - key = (track_szs_file, track_wu8_file) + for _track_file, process in process_list.items(): if process is not None: - returncode = process.poll() - if returncode is None: - pass # if the process is still running + if process.poll() is None: pass # if the process is still running else: # process ended + process_list.pop(_track_file) stderr = process.stderr.read() if b"wszst: ERROR" in stderr: # Error occured - process_list.pop(key) - os.remove(track_szs_file) + os.remove(get_track_szs(_track_file)) error_count += 1 if error_count > error_max: # Too much track wasn't correctly converted messagebox.showerror( @@ -92,15 +92,15 @@ def patch_track(self, tracks, total_track="?"): messagebox.showwarning( self.translate("Attention"), self.translate("La course ") + - track_wu8_file + + get_track_wu8(_track_file) + self.translate(" n'a pas été correctement converti. (") + str(error_count) + "/" + str(error_max) + ")") break else: - if self.boolvar_del_track_after_conv.get(): os.remove(track_wu8_file) - process_list.pop(key) + if self.boolvar_del_track_after_conv.get(): os.remove(get_track_wu8(_track_file)) break else: - process_list.pop(key) + process_list.pop(_track_file) break + return 0 \ No newline at end of file