def makeFolder(self, imgDatetime, handledFilePath): print "making a new folder" dateFolders = mover.findDateFolders(self._config.rename_folder) if dateFolders.has_key(mover.mkDateFolderName(imgDatetime)): print "folder exists" dateFolderPath = dateFolders[mover.mkDateFolderName(imgDatetime)][1] else: print "creating new folder" # make dir if it does not exist dateFolderPath = os.path.join(self._config.rename_folder, mover.mkDateFolderName(imgDatetime)) if self._config.use_original_folder_names: print "import original folder name" originalFolderName = os.path.basename(os.path.dirname(handledFilePath)) dateFolderPath += ' '+ originalFolderName print "make dir",dateFolderPath os.mkdir( dateFolderPath ) if self._config.make_fusioned_folder_names: print "try to merge folders" self.makeRangeFolders(imgDatetime, dateFolders) else: print "I won't try merging folders" return dateFolderPath
def makeRangeFolders(self,imgDatetime,dateFolders): # append to a prevRange if dir exists for previous or next day # search for possible matches prevDay = imgDatetime-datetime.timedelta(days=1) #prev = os.path.join(self._config.rename_folder,mkDateFolderName(prevDay)) nextDay = imgDatetime+datetime.timedelta(days=1) #next = os.path.join(self._config.rename_folder,mkDateFolderName(nextDay)) rangeFolders = mover.findRangeFolders(self._config.rename_folder) prevRange = None if dateFolders.has_key(mover.mkDateFolderName(prevDay)): print "prev exists" if len(rangeFolders): for start,finish,rangeFoldCurrentPath in rangeFolders: #@UnusedVariable if mover.sameDay(finish,prevDay): print "prevRange with prev" prevRange = rangeFoldCurrentPath break if None != prevRange: newPrevRange = mover.mkRangeFolderName(start, imgDatetime,self._config.rename_folder) print "we have a prevRange. Rename from",prevRange,"to",newPrevRange os.rename(prevRange,newPrevRange) prevRange = newPrevRange prevRangeStart = start else: prevRange = mover.mkRangeFolderName(prevDay,imgDatetime,self._config.rename_folder) print "No prevRange. We make it from ",prevDay,"and",imgDatetime,":",prevRange os.mkdir(prevRange) prevRangeStart = prevDay nextRange = None if dateFolders.has_key(mover.mkDateFolderName(nextDay)): print "next exists" if len(rangeFolders): for start,finish,rangeFoldCurrentPath in rangeFolders: #@UnusedVariable if mover.sameDay(start,nextDay): print "nextRange with next" nextRange = rangeFoldCurrentPath break if None != nextRange: print "We have a nextRange",nextRange if None == prevRange: newNextRange = mover.mkRangeFolderName(imgDatetime,finish,self._config.rename_folder) print "but no prev: rename nextRange from ",nextRange,"to",newNextRange os.rename(nextRange,newNextRange) else: mergeRange = mover.mkRangeFolderName(prevRangeStart,finish,self._config.rename_folder) print "with prev: We rename from ",prevRange,"to",mergeRange os.rename(prevRange,mergeRange) print "we delete",nextRange os.rmdir(nextRange) else: if None == prevRange: nextRange = mover.mkRangeFolderName(imgDatetime,nextDay,self._config.rename_folder) print "No nextRange. We make it:",nextRange os.mkdir(nextRange) else: print "No nextRange but we have a prevRange: we merge" mergeRange = mover.mkRangeFolderName(prevRangeStart,nextDay,self._config.rename_folder) print "with prev: We rename from ",prevRange,"to",mergeRange os.rename(prevRange,mergeRange)