def rename_in_csvs(timestring="", fileext=".JPG"): """ use backup in saves; rename file reverences in csv in setexif directory :param timestring: time of backup :param fileext: file extension """ log_function_call(rename_in_csvs.__name__) dirname = get_saves_dir() tagFile = os.path.join(dirname, "Tags" + fileext + "_" + timestring + ".npz") if not timestring or os.path.isfile(tagFile): tagFiles = [x for x in os.listdir(dirname) if ".npz" in x] tagFile = os.path.join(dirname, tagFiles[-1]) Tagdict = np.load(tagFile, allow_pickle=True)["Tagdict"].item() log().debug("length of Tagdict: %d", len(list(Tagdict.values())[0])) csv_filenames = filterFiles(os.listdir(get_setexif_dir()), [".csv"]) csv_filenames = [ os.path.join(get_setexif_dir(), csv_filename) for csv_filename in csv_filenames ] for csv_filename in csv_filenames: with open(csv_filename, "r", encoding="utf-8") as file: data = file.read() for i in range(len(list(Tagdict.values())[0])): data = data.replace(Tagdict["File Name"][i], Tagdict["File Name new"][i]) with open(csv_filename, "w", encoding="utf-8") as file: file.write(data)
def rename_back(timestring="", fileext=".JPG"): """ rename back using backup in saves; change to directory you want to rename back :param timestring: time of backup :param fileext: file extension :return: """ log_function_call(rename_back.__name__) dirname = get_saves_dir() tagFile = os.path.join(dirname, "Tags" + fileext + "_" + timestring + ".npz") if not timestring or os.path.isfile(tagFile): tagFiles = [x for x in os.listdir(dirname) if ".npz" in x] tagFile = os.path.join(dirname, tagFiles[-1]) Tagdict = np.load(tagFile, allow_pickle=True)["Tagdict"].item() temppostfix = renameTemp(Tagdict["Directory"], Tagdict["File Name new"]) log().debug("length of Tagdict: %d", len(list(Tagdict.values())[0])) for i in range(len(list(Tagdict.values())[0])): filename = Tagdict["File Name new"][i] + temppostfix if not os.path.isfile(os.path.join(Tagdict["Directory"][i], filename)): continue filename_old = Tagdict["File Name"][i] renameInPlace(Tagdict["Directory"][i], filename, filename_old) Tagdict["File Name new"][i], Tagdict["File Name"][i] = Tagdict[ "File Name"][i], Tagdict["File Name new"][i] timestring = dateformating(dt.datetime.now(), "_MMDDHHmmss") np.savez_compressed(os.path.join(dirname, "Tags" + fileext + timestring), Tagdict=Tagdict)
def print_info(tagGroupNames=(), allGroups=False): """ write tag info of tagGroupNames to a file in saves dir :param tagGroupNames: selectable groups (look into constants) :param allGroups: take all tagGroupNames """ tagdict = read_exiftags() model = create_model(tagdict, 0) tagnames = model.TagNames if allGroups: tagGroupNames = tagnames.keys() for tagGroupName in tagnames: if not tagGroupNames == [] and not tagGroupName in tagGroupNames: continue outstring = "%-80s\t" % "File Name" for entry in tagnames[tagGroupName]: if not entry in tagdict: continue outstring += "%-30s\t" % entry outstring += "\n" for i in range(len(tagdict["File Name"])): outstring += "%-80s\t" % tagdict["File Name"][i] for entry in tagnames[tagGroupName]: if not entry in tagdict: continue val = tagdict[entry] outstring += "%-30s\t" % val[i] outstring += "\n" dirname = get_saves_dir() writeToFile(os.path.join(dirname, "tags_" + tagGroupName + ".txt"), outstring)
def save_tagdict(fileext: str, timestring: str, Tagdict: OrderedDict): np.savez_compressed(get_saves_dir("Tags" + fileext + timestring), Tagdict=Tagdict)
def rename(Prefix="", dateformat='YYMM-DD', startindex=1, onlyprint=False, keeptags=True, is_video=False, name=""): """ Rename into Format: [Prefix][dateformat](_[name])_[Filenumber][SeriesType][SeriesSubNumber]_[PhotoMode] :param Prefix: prefix has to fulfil regex [-a-zA-Z]* :param dateformat: Y:Year,M:Month,D:Day,N:DayCounter; Number of occurrences determine number of digits :param startindex: minimal counter :param onlyprint: do not rename; only output file of proposed renaming into saves directory :param keeptags: any tags - name or postfixes will be preserved :param is_video: is video file extension :param name: optional name between date and filenumber, seldom used """ log_function_call(rename.__name__, Prefix, dateformat, startindex, onlyprint, keeptags, is_video, name) Tagdict = read_exiftags( file_types=settings.video_types if is_video else settings.image_types) if not Tagdict: return # rename temporary if not onlyprint: temppostfix = renameTemp(Tagdict["Directory"], Tagdict["File Name"]) else: temppostfix = "" # initialize outstring = "" Tagdict["File Name new"] = [] time_old = giveDatetime() counter = startindex - 1 digits = _count_files_for_each_date(Tagdict, startindex, dateformat) number_of_files = len(list(Tagdict.values())[0]) daystring = "" for i in range(number_of_files): model = create_model(Tagdict, i) time = giveDatetime(model.get_date()) filename = model.filename if newdate(time, time_old, 'D' in dateformat or 'N' in dateformat): daystring = dateformating(time, dateformat) if not i == 0: counter = 0 filenameBuilder = FilenameBuilder(filename) filenameBuilder.add_main(Prefix + daystring) filenameBuilder.add_main(model.get_model_abbr()) filenameBuilder.add_main(name) if is_video: counter += 1 counterString = "M%02d" % counter filenameBuilder.add_post(model.get_recMode()) else: # SequenceNumber sequence_number = model.get_sequence_number() if sequence_number < 2 and not time == time_old or model.ignore_same_date( ): counter += 1 counterString = ("%0" + digits + "d") % counter if not "HDR" in filename: counterString += model.get_sequence_string() filenameBuilder.add_main(counterString) filenameBuilder.add_post(model.get_mode()) if keeptags: filenameBuilder.use_old_tags() newname = filenameBuilder.build() # handle already exiting filename - its ok when they are in different directories # its not ok to have to files with one uppercase and another lowercase -> equals with ignore case if len(Tagdict["File Name new"]) > 0: if newname.lower() == Tagdict["File Name new"][-1].lower() and \ Tagdict["Directory"][i].lower() == Tagdict["Directory"][i - 1].lower(): log().warning("%s already exists - postfix it with V%d", os.path.join(model.dir, newname), 1) newname = filenameBuilder.set_version("V%d" % 1).build() for version in range(2, 100): indexes_samename = [ i for i, name in enumerate(Tagdict["File Name new"]) if name.lower() == newname.lower() ] if indexes_samename: if Tagdict["Directory"][i] == Tagdict["Directory"][ indexes_samename[0]]: log().warning( "%s already exists - postfix it with V%d", os.path.join(model.dir, newname), version) newname = filenameBuilder.set_version("V%d" % version).build() else: break else: break time_old = time Tagdict["File Name new"].append(newname) outstring += _write(model.dir, filename, temppostfix, newname, onlyprint) dirname = get_saves_dir() timestring = dateformating(dt.datetime.now(), "_MMDDHHmmss") video_str = "_video" if is_video else "" np.savez_compressed(os.path.join(dirname, "Tags" + video_str + timestring), Tagdict=Tagdict) writeToFile( os.path.join(dirname, "newnames" + video_str + timestring + ".txt"), outstring)