From f49f82b74596eda33ee5e2fc14e51fd67351b9ec Mon Sep 17 00:00:00 2001 From: Julien Valroff Date: Tue, 1 Jun 2010 20:31:41 +0200 Subject: Imported Upstream version 0.2.0 --- rapid/media.py | 157 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 76 insertions(+), 81 deletions(-) (limited to 'rapid/media.py') diff --git a/rapid/media.py b/rapid/media.py index def73e6..4ed5380 100755 --- a/rapid/media.py +++ b/rapid/media.py @@ -21,17 +21,27 @@ import os import config import common +import metadata +import videometadata import operator -def getDefaultPhotoLocation(): - for default in config.DEFAULT_PHOTO_LOCATIONS: +def _getDefaultLocation(options, ignore_missing_dir=False): + if ignore_missing_dir: + return common.getFullPath(options[0]) + for default in options: path = common.getFullPath(default) if os.path.isdir(path): return path return common.getFullPath('') + +def getDefaultPhotoLocation(ignore_missing_dir=False): + return _getDefaultLocation(config.DEFAULT_PHOTO_LOCATIONS, ignore_missing_dir) + +def getDefaultVideoLocation(ignore_missing_dir=False): + return _getDefaultLocation(config.DEFAULT_VIDEO_LOCATIONS, ignore_missing_dir) -def isImageMedia(path): +def is_DCIM_Media(path): """ Returns true if directory specifies some media with photos on it """ if os.path.isdir(os.path.join(path, "DCIM")): @@ -41,27 +51,60 @@ def isImageMedia(path): return False -def isBackupMedia(path, identifier, writeable=True): - """ Test to see if path is used as a backup medium for storing images +def isBackupMedia(path, identifiers, writeable=True): + """ Test to see if path is used as a backup medium for storing photos or videos + + Identifiers is expected to be a list of folder names to check to see + if the path is a backup path. Only one of them needs to be present + for the path to be considered a backup medium. If writeable is True, the directory must be writeable by the user """ suitable = False - if os.path.isdir(os.path.join(path, identifier)): - if writeable: - suitable = os.access(os.path.join(path, identifier), os.W_OK) - else: - suitable = True - return suitable + + for identifier in identifiers: + if os.path.isdir(os.path.join(path, identifier)): + if writeable: + suitable = os.access(os.path.join(path, identifier), os.W_OK) + else: + suitable = True + if suitable: + return True + return False def isImage(fileName): ext = os.path.splitext(fileName)[1].lower()[1:] - return (ext in config.RAW_FILE_EXTENSIONS) or (ext in config.NON_RAW_IMAGE_FILE_EXTENSIONS) + return (ext in metadata.RAW_FILE_EXTENSIONS) or (ext in metadata.NON_RAW_IMAGE_FILE_EXTENSIONS) + +def isVideo(fileName): + ext = os.path.splitext(fileName)[1].lower()[1:] + return (ext in videometadata.VIDEO_FILE_EXTENSIONS) + +def getVideoThumbnailFile(fullFileName): + """ + Checks to see if a thumbnail file is in the same directory as the + file. Expects a full path to be part of the file name. + + Returns the filename, including path, if found, else returns None. + """ + + f = None + name, ext = os.path.splitext(fullFileName) + for e in videometadata.VIDEO_THUMBNAIL_FILE_EXTENSIONS: + if os.path.exists(name + '.' + e): + f = name + '.' + e + break + if os.path.exists(name + '.' + e.upper()): + f = name + '.' + e.upper() + break + + return f + class Media: - """ Generic class for media holding images """ + """ Generic class for media holding images and videos """ def __init__(self, path, volume = None): """ - volume is a gnomevfs or gio volume, see class Volume in rapid.py + volume is a gnomevfs or gio volume: see class Volume in rapid.py """ self.path = path @@ -94,87 +137,39 @@ class Media: class CardMedia(Media): - """Compact Flash cards, etc.""" + """Compact Flash cards, hard drives, etc.""" def __init__(self, path, volume = None, doNotScan=True): """ volume is a gnomevfs or gio volume, see class Volume in rapid.py """ Media.__init__(self, path, volume) - if not doNotScan: - self.scanMedia() - def scanMedia(self): - """ creates a list of images on a path, recursively scanning - - images are sorted by modification time""" - - self.images = [] - self.imageSizeSum = 0 - for root, dirs, files in os.walk(self.path): - for name in files: - if isImage(name): - image = os.path.join(root, name) - size = os.path.getsize(image) - modificationTime = os.path.getmtime(image) - self.images.append((name, root, size, modificationTime),) - self.imageSizeSum += size - self.images.sort(key=operator.itemgetter(3)) - self.noImages = len(self.images) - def setMedia(self, images, imageSizeSum, noImages): - self.images = images - self.imageSizeSum = imageSizeSum - self.noImages = noImages + def setMedia(self, imagesAndVideos, fileSizeSum, noFiles): + self.imagesAndVideos = imagesAndVideos + self.fileSizeSum = fileSizeSum + self.noFiles = noFiles - def numberOfImages(self): - return self.noImages + def numberOfImagesAndVideos(self): + return self.noFiles - def sizeOfImages(self, humanReadable = True): + def sizeOfImagesAndVideos(self, humanReadable = True): if humanReadable: - return common.formatSizeForUser(self.imageSizeSum) + return common.formatSizeForUser(self.fileSizeSum) else: - return self.imageSizeSum + return self.fileSizeSum - def firstImage(self): - if self.images: - return self.images[0] + def _firstFile(self, isCorrectFile): + if self.imagesAndVideos: + for i in range(len(self.imagesAndVideos)): + if isCorrectFile(self.imagesAndVideos[i]): + return self.imagesAndVideos[i] else: return None - -def scanForImageMedia(path): - """ returns a list of paths that contain images on media produced by a digital camera """ - - media = [] - for i in os.listdir(path): - p = os.path.join(path, i) - if os.path.isdir(p): - if isImageMedia(p): - media.append(p) - return media - -def scanForBackupMedia(path, identifier): - """ returns a list of paths that contains backed up images """ - - media = [] - for i in os.listdir(path): - p = os.path.join(path, i) - if os.path.isdir(p): - if isBackupMedia(p, identifier): - media.append(os.path.join(p, identifier)) - return media - + def firstImage(self): + return self._firstFile(isImage) -if __name__ == '__main__': - print "Card media:" - for m in scanForImageMedia('/media'): - media = CardMedia(m) - print media.prettyName() - print media.numberOfImages() - print media.sizeOfImages() + def firstVideo(self): + return self._firstFile(isVideo) - print "\nBackup media:" - for m in scanForBackupMedia('/media', 'photos'): - print m - - print "\nDefault download folder: ", getDefaultPhotoLocation() -- cgit v1.2.3