def load(cls, path): ph = Photo() f = open(path) for line in f: if line.startswith("originalpath="): ph.srcPath = line[len("originalpath=") :] elif line.startswith("keywords="): keywords = line[len("keywords=") :] for keyword in keywords.split(","): ph.keywords.append(keyword.strip()) elif line.startswith("comment="): ph.comment = line[len("comment=") :] elif line.startswith("date"): ph.date = line[len("date=") :] else: if line.startswith("DateTimeOriginal=") and not ph.date: ph.date = line[len("DateTimeOriginal=") :] elif line.startswith("DateTime=") and not ph.date: ph.date = line[len("DateTime=") :] elif line.startswith("Orientation="): ph.orientation = line[len("Orientation") :] if ph.path: if os.path.exists(ph.path): exif = loadExif(ph.path, EXIF_TAGS) ph.setExif(exif) f.close() return ph
def doImport(self): libPath = getSettingStr("Paths/Library") fileExt = getSettingStr("FileExtensions") if libPath in (None, ''): QMessageBox.warning(self, "Import Failed", "You need to specify a library directory in your settings") return if not os.path.exists(libPath) or os.path.isfile(libPath): QMessageBox.warning(self, "Import Failed", "The library directory in your settings either doesn't exist, or its not a directory") return if not fileExt or fileExt in (None, ''): QMessageBox.warning(self, "Import Failed", "You need to specify file extensions to manage in your settings") return lastImport = getSettingStr("Paths/LastImport") importFrom = QFileDialog.getExistingDirectory(self, "Choose a Path to Import From", lastImport) if importFrom in (None, ''): return if not os.path.exists(importFrom) or os.path.isfile(importFrom): QMessageBox.warning(self, "Import Failed", "The import directory either doesn't exist, or is not a directory") return if importFrom == libPath: QMessageBox.warning(self, "Import Failed", "Your import directory and library directory can not be the same") return imd = ImportMetadataDialog(self) if imd.exec_(): album = imd.albumEdit.text() comments = imd.commentsEdit.text() keywords = imd.keywordsEdit.text() if album and album not in (None, ''): albumpath = album + os.sep else: album = None albumpath = "" if not keywords or keywords in (None, ''): keywords = "" if not comments or comments in (None, ''): comments = "" paths = self.buildFileList(importFrom) numTotal = len(paths) nonDupes = self.removeDuplicates(paths, importFrom, albumpath) numDuplicates = numTotal - len(nonDupes) if QMessageBox.question(self, "Import", "Out of %d files found, %d look to be duplicates. Continue with import?" % (numTotal, numDuplicates), QMessageBox.Yes|QMessageBox.No) == QMessageBox.Yes: saveSetting("Paths/LastImport", importFrom) for path in nonDupes: dest = self.buildLibPath(importFrom, path, albumpath) copyFileIncludingDirectories(path, dest) # TODO Handle copy failure exceptions! if not os.path.exists(dest): QMessageBox.warming(self, "Import Failed", "The file <%s> was not imported properly, aborting import" % (path)) return if self.isImageFile(path): exif = loadExif(unicode(path), EXIF_TAGS) ph = Photo() ph.path = dest ph.srcPath = path ph.comment = comments ph.keywords = keywords ph.setExif(exif) ph.save(dest) #Create Thumbnail createThumbnail(unicode(ph.path)) QMessageBox.information(self, "Import", "Import completed") self.loadLibrary()
def loadAlbum(self, path, title = None): album = Album() if title not in (None, ''): album.name = title else: album.name = path[path.rfind(os.sep)+1:] album.albums = {} album.photos = [] album.path = path files = os.listdir(path) files.sort() tmpPhotos = [] for fl in files: if not os.path.isfile(path + os.sep + fl): album.albums[fl] = self.loadAlbum(path + os.sep + fl) else: if self.isImageFile(path + os.sep + fl): ph = None if os.path.exists(path + os.sep + fl + ".sidecar"): ph = Photo.load(path + os.sep + fl + ".sidecar") else: ph = Photo() ph.comment = "" ph.keywords = {} ph.srcPath = None ph.path = path + os.sep + fl exif = loadExif(path + os.sep + fl, EXIF_TAGS) ph.setExif(exif) ph.save(path + os.sep + fl) ph.path = path + os.sep + fl tmpPhotos.append(ph) album.photos = sorted(tmpPhotos, key = lambda photo: photo.date) return album