コード例 #1
0
ファイル: opParser.py プロジェクト: jgrinder/metamorphose2
    def __get_exif_date(self, path, tag):
        ext = os.path.splitext(path)[1][1:].lower()
        if re.match('tif|tiff|jpg|jpeg|jtif|thm', ext, re.IGNORECASE):
            try:
                file = open(path, 'rb')
            except IOError:
                self.__add_to_errors(path, _(u"Could not open file!"))
            else:
                tags = EXIF.process_file(file, details=False, stop_tag=tag)
                # see if the tag exists
                if tags.has_key(tag):
                    try:
                        itemDateTime = str(tags[tag])
                        itemDateTime = re.compile(r'\D').split(itemDateTime)
                        itemDateTime = map(int, itemDateTime)
                    except ValueError, err:
                        self.__add_to_warnings(path, _(u"Exif error: %s") % err)
                        return False

                    # attempt to convert tag's text to a date
                    try:
                        dayWeek = calendar.weekday(itemDateTime[0],
                                                   itemDateTime[1],
                                                   itemDateTime[2])
                    # invalid date
                    except ValueError, err:
                        self.__add_to_warnings(path, _(u"Exif error: %s") % err)
                    else:
                        itemDateTime.extend([dayWeek, 1, 0])
                        return itemDateTime
コード例 #2
0
    def __get_exif_date(self, path, tag):
        ext = os.path.splitext(path)[1][1:].lower()
        if re.match('tif|tiff|jpg|jpeg|jtif|thm', ext, re.IGNORECASE):
            try:
                file = open(path, 'rb')
            except IOError:
                self.__add_to_errors(path, _(u"Could not open file!"))
            else:
                tags = EXIF.process_file(file, details=False, stop_tag=tag)
                # see if the tag exists
                if tags.has_key(tag):
                    try:
                        itemDateTime = str(tags[tag])
                        itemDateTime = re.compile(r'\D').split(itemDateTime)
                        itemDateTime = map(int, itemDateTime)
                    except ValueError, err:
                        self.__add_to_warnings(path,
                                               _(u"Exif error: %s") % err)
                        return False

                    # attempt to convert tag's text to a date
                    try:
                        dayWeek = calendar.weekday(itemDateTime[0],
                                                   itemDateTime[1],
                                                   itemDateTime[2])
                    # invalid date
                    except ValueError, err:
                        self.__add_to_warnings(path,
                                               _(u"Exif error: %s") % err)
                    else:
                        itemDateTime.extend([dayWeek, 1, 0])
                        return itemDateTime
コード例 #3
0
ファイル: __init__.py プロジェクト: RavenB/metamorphose2
 def _get_from_exif(self, path, selection):
     """Get wanted Exif info from image for sorting."""
     ref = {7: 'EXIF DateTimeOriginal', 8: 'Image DateTime'}
     try:
         file = open(path, 'rb')
     except:
         return False
     else:
         # get the tags
         data = EXIF.process_file(file, details=False)
         if not data:
             return False
         else:
             try:
                 datetime = unicode(data[ref[selection]])
             except KeyError:
                 return False
             else:
                 datetime = datetime.replace(' ', ':')
                 return datetime
コード例 #4
0
 def _get_from_exif(self, path, selection):
     """Get wanted Exif info from image for sorting."""
     ref = {7: 'EXIF DateTimeOriginal', 8: 'Image DateTime'}
     try:
         file = open(path, 'rb')
     except:
         return False
     else:
         # get the tags
         data = EXIF.process_file(file, details=False)
         if not data:
             return False
         else:
             try:
                 datetime = unicode(data[ref[selection]])
             except KeyError:
                 return False
             else:
                 datetime = datetime.replace(' ', ':')
                 return datetime
コード例 #5
0
ファイル: opParser.py プロジェクト: jgrinder/metamorphose2
    def parse_input(self, text, file, operation):
        """
        Parse the active fields for sub operations and return
        the corresponding result in place.
        """
        ext = os.path.splitext(file)[1][1:]
        # possible special operations:
        commands = [_(u"numb"), _(u"date"), _(u"time"), _(u"folder")]

        # audio
        audioInfoStart = len(commands)
        for i in AUDIO_INFO.keys():
            commands.append(i)
        audioInfoEnd = len(commands)

        # Exif
        for i in IMAGE_INFO.keys():
            commands.append(i)

        parsedText = u''

        # any text in between this character will be considered an operation:
        text = text.strip(u':').split(u':')

        isImage = re.match('tif|tiff|jpg|jpeg|jtif|thm', ext, re.IGNORECASE)
        isAudio = re.match('ape|asf|flac|m4a|mp3|mp4|ogg', ext, re.IGNORECASE)
        audioMetadata = False

        # process here to load Exif metadata only once
        if isImage:
            for segment in text:
                if segment in commands[10:]:
                    file_rb = open(file, 'rb')
                    imageMetadata = EXIF.process_file(file_rb, details=False)
                    break
        # process here to load audio metadata only once
        elif isAudio:
            for segment in text:
                if segment in commands[audioInfoStart:audioInfoEnd]:
                    audioMetadata = self.__get_audio_metadata(file)
                    break
        # execute functions based on user input:
        for segment in text:
            value = False
            # numbering
            if segment == commands[0]:
                value = self.__enumber(file, operation)
            # date
            elif segment == commands[1]:
                value = self.__date_time(0, file, operation)
            # time
            elif segment == commands[2]:
                value = self.__date_time(1, file, operation)
            # folder name
            elif segment[:len(commands[3])] == commands[3]:
                value = self.__add_folder(file, segment[len(commands[3]):])
            # audio
            elif isAudio and audioMetadata and segment in commands[audioInfoStart:audioInfoEnd]:
                value = self.__get_audio_tag(audioMetadata, segment)
            # EXIF
            elif isImage and segment in commands[audioInfoEnd + 1:]:
                value = self.__get_image_tag(file, imageMetadata, segment)
            # if segment doesn't match a command, reprint segment:
            else:
                parsedText += segment
            if value:
                parsedText += value
        return parsedText
コード例 #6
0
    def parse_input(self, text, file, operation):
        """
        Parse the active fields for sub operations and return
        the corresponding result in place.
        """
        ext = os.path.splitext(file)[1][1:]
        # possible special operations:
        commands = [_(u"numb"), _(u"date"), _(u"time"), _(u"folder")]

        # audio
        audioInfoStart = len(commands)
        for i in AUDIO_INFO.keys():
            commands.append(i)
        audioInfoEnd = len(commands)

        # Exif
        for i in IMAGE_INFO.keys():
            commands.append(i)

        parsedText = u''

        # any text in between this character will be considered an operation:
        text = text.strip(u':').split(u':')

        isImage = re.match('tif|tiff|jpg|jpeg|jtif|thm', ext, re.IGNORECASE)
        isAudio = re.match('ape|asf|flac|m4a|mp3|mp4|ogg', ext, re.IGNORECASE)
        audioMetadata = False

        # process here to load Exif metadata only once
        if isImage:
            for segment in text:
                if segment in commands[10:]:
                    file_rb = open(file, 'rb')
                    imageMetadata = EXIF.process_file(file_rb, details=False)
                    break
        # process here to load audio metadata only once
        elif isAudio:
            for segment in text:
                if segment in commands[audioInfoStart:audioInfoEnd]:
                    audioMetadata = self.__get_audio_metadata(file)
                    break
        # execute functions based on user input:
        for segment in text:
            value = False
            # numbering
            if segment == commands[0]:
                value = self.__enumber(file, operation)
            # date
            elif segment == commands[1]:
                value = self.__date_time(0, file, operation)
            # time
            elif segment == commands[2]:
                value = self.__date_time(1, file, operation)
            # folder name
            elif segment[:len(commands[3])] == commands[3]:
                value = self.__add_folder(file, segment[len(commands[3]):])
            # audio
            elif isAudio and audioMetadata and segment in commands[
                    audioInfoStart:audioInfoEnd]:
                value = self.__get_audio_tag(audioMetadata, segment)
            # EXIF
            elif isImage and segment in commands[audioInfoEnd + 1:]:
                value = self.__get_image_tag(file, imageMetadata, segment)
            # if segment doesn't match a command, reprint segment:
            else:
                parsedText += segment
            if value:
                parsedText += value
        return parsedText