def write_metadata_local(inputfile, outputfile, metadata_dictionary, verbose):
    """
    EXIF and IPTC metadata writing, previous tag printing, to
    images. If some tag not set, it is auto-added, but be a valid exif
    or iptc tag.

    @param inputfile: path to the image
    @type inputfile: string
    @param outputfile: path to the resulting image
    @type outputfile: string
    @param verbose: verbosity
    @type verbose: int
    @param metadata_dictionary: metadata information to update inputfile
    @rtype: dict
    """
    if inputfile != outputfile:
        # Create copy of inputfile
        try:
            shutil.copy2(inputfile, outputfile)
        except Exception, err:
            raise InvenioWebSubmitFileMetadataRuntimeError(err)
Exemple #2
0
def write_metadata_local(inputfile, outputfile, metadata_dictionary, verbose):
    """
    Metadata write method, takes the .pdf as input and creates a new
    one with the new info.

    @param inputfile: path to the pdf
    @type inputfile: string
    @param outputfile: path to the resulting pdf
    @type outputfile: string
    @param verbose: verbosity
    @type verbose: int
    @param metadata_dictionary: metadata information to update inputfile
    @type metadata_dictionary: dict
    """
    # Take the file name (0 base, 1 name, 2 ext)
    filename = decompose_file(inputfile)[1]

    # Print pdf metadata
    if verbose > 1:
        print 'Metadata information in the PDF file ' + filename + ': \n'
        try:
            os.system(CFG_PATH_PDFTK + ' ' + inputfile + ' dump_data')
        except Exception:
            print 'Problem with inputfile to PDFTK'

    # Info file for pdftk
    (fd, path_to_info) = tempfile.mkstemp(prefix="wsm_pdf_plugin_info_", \
                                             dir=CFG_TMPDIR)
    os.close(fd)
    file_in = open(path_to_info, 'w')
    if verbose > 5:
        print "Saving PDFTK info file to %s" % path_to_info

    # User interaction to form the info file
    # Main Case: Dictionary received through option -d
    if not metadata_dictionary == {}:
        for tag in metadata_dictionary:
            line = 'InfoKey: ' + tag + '\nInfoValue: ' + \
                   metadata_dictionary[tag] + '\n'
            if verbose > 0:
                print line
            file_in.writelines(line)
    else:
        data_modified = False
        user_input = 'user_input'
        print "Entering interactive mode. Choose what you want to do:"
        while (user_input):
            if not data_modified:
                try:
                    user_input = raw_input('[w]rite / [q]uit\n')
                except:
                    print "Aborting"
                    return
            else:
                try:
                    user_input = raw_input(
                        '[w]rite / [q]uit and apply / [a]bort \n')
                except:
                    print "Aborting"
                    return
            if user_input == 'q':
                if not data_modified:
                    return
                break
            elif user_input == 'w':
                try:
                    tag = raw_input('Tag to update:\n')
                    value = raw_input('With value:\n')
                except:
                    print "Aborting"
                    return
                # Write to info file
                line = 'InfoKey: ' + tag + '\nInfoValue: ' + value + '\n'
                data_modified = True
                file_in.writelines(line)
            elif user_input == 'a':
                return
            else:
                print "Invalid option: "
    file_in.close()

    (fd, pdf_temp_path) = tempfile.mkstemp(prefix="wsm_pdf_plugin_pdf_", \
                                              dir=CFG_TMPDIR)
    os.close(fd)

    # Now we call pdftk tool to update the info on a pdf
    #try:
    cmd_pdftk = '%s %s update_info %s output %s'
    (exit_status, output_std, output_err) = \
                  run_shell_command(cmd_pdftk,
                                    args=(CFG_PATH_PDFTK, inputfile,
                                          path_to_info, pdf_temp_path))
    if verbose > 5:
        print output_std, output_err

    if os.path.exists(pdf_temp_path):
        # Move to final destination if exist
        try:
            shutil.move(pdf_temp_path, outputfile)
        except Exception, err:
            raise InvenioWebSubmitFileMetadataRuntimeError("Could not move %s to %s" % \
                                                           (pdf_temp_path, outputfile))
Exemple #3
0
                return
            else:
                print "Invalid option: "
    file_in.close()

    (fd, pdf_temp_path) = tempfile.mkstemp(prefix="wsm_pdf_plugin_pdf_", \
                                              dir=CFG_TMPDIR)
    os.close(fd)

    # Now we call pdftk tool to update the info on a pdf
    #try:
    cmd_pdftk = '%s %s update_info %s output %s'
    (exit_status, output_std, output_err) = \
                  run_shell_command(cmd_pdftk,
                                    args=(CFG_PATH_PDFTK, inputfile,
                                          path_to_info, pdf_temp_path))
    if verbose > 5:
        print output_std, output_err

    if os.path.exists(pdf_temp_path):
        # Move to final destination if exist
        try:
            shutil.move(pdf_temp_path, outputfile)
        except Exception, err:
            raise InvenioWebSubmitFileMetadataRuntimeError("Could not move %s to %s" % \
                                                           (pdf_temp_path, outputfile))
    else:
        # Something bad happened
        raise InvenioWebSubmitFileMetadataRuntimeError(
            "Could not update metadata " + output_err)
def read_metadata_remote(inputfile, loginpw, verbose):
    """
    EXIF and IPTC metadata extraction and printing from remote images

    @param inputfile: path to the remote image
    @type inputfile: string
    @param verbose: verbosity
    @type verbose: int
    @param loginpw: credentials to access secure servers (username:password)
    @type loginpw: string
    @return: dictionary with metadata
    @rtype: dict
    """
    # Check that inputfile is an URL
    secure = False
    pos = inputfile.lower().find('http://')
    if pos < 0:
        secure = True
        pos = inputfile.lower().find('https://')
    if pos < 0:
        raise InvenioWebSubmitFileMetadataRuntimeError("Inputfile (" + inputfile + ") is " + \
                                                       "not an URL, nor remote resource.")

    # Check if there is login and password
    if loginpw != None:
        (userid, passwd) = loginpw.split(':')

    # Make HTTPS Connection
    domain = inputfile.split('/')[2]
    if verbose > 3:
        print 'Domain: ', domain
    url = inputfile.split(domain)[1]
    if verbose > 3:
        print 'URL: ', url

    # Establish headers
    if loginpw != None:
        _headers = {"Accept": "*/*",
                    "Authorization": "Basic " + \
                    base64.encodestring(userid + ':' + passwd).strip()}
    else:
        _headers = {"Accept": "*/*"}

    conn = None

    # Establish connection
    # Case HTTPS
    if secure:
        try:
            conn = httplib.HTTPSConnection(domain)
            ## Request a connection
            conn.request("GET", url,
                  headers = _headers)
        except Exception:
            # Cannot connect
            print 'Could not connect'
    # Case HTTP
    else:
        try:
            conn = httplib.HTTPConnection(domain)
            ## Request a connection
            conn.request("GET", url,
                  headers = _headers)
        except Exception:
            # Cannot connect
            print 'Could not connect'

    # Get response
    if verbose > 5:
        print "Fetching data from remote server."
    response = conn.getresponse()
    if verbose > 2:
        print response.status, response.reason

    if response.status == 401:
        # Authentication required
        raise InvenioWebSubmitFileMetadataRuntimeError("URL requires authentication. Use --loginpw option")

    # Read first marker from image
    data = response.read(2)

    # Check if it is a valid image
    if data[0:2] != '\xff\xd8':
        raise InvenioWebSubmitFileMetadataRuntimeError("URL does not brings to a valid image file.")
    else:
        if verbose > 5:
            print 'Valid JPEG Standard-based image'

    # Start the fake image
    path_to_fake = fake_image_init(verbose)

    # Continue reading
    data = response.read(2)

    # Check if we find metadata (EXIF or IPTC)
    while data[0:2] != '\xff\xdb':
        if data[0:2] == '\xff\xe1' or data[0:2] == '\xff\xed':
            marker = data
            if verbose > 5:
                print 'Metadata Marker->', repr(marker), '\nGetting data'
            size = response.read(2)
            length = ord(size[0]) * 256 + ord(size[1])
            meta = response.read(length-2)
            insert_metadata(path_to_fake, marker, size, meta, verbose)
            break
        else:
            data = response.read(2)

    # Close connection
    conn.close()

    # Close fake image
    fake_image_close(path_to_fake, verbose)

    # Extract metadata once fake image is done
    return read_metadata_local(path_to_fake, verbose)
                    data_modified = True
                except:
                    print "Aborting"
                    return
                try:
                    image[tag] = value
                except Exception, err:
                    print 'Tag or Value incorrect'
            elif user_input == 'a':
                return
            else:
                print "Invalid option: "
        try:
            image.writeMetadata()
        except Exception, err:
            raise InvenioWebSubmitFileMetadataRuntimeError("Could not update metadata: " + err)

def read_metadata_remote(inputfile, loginpw, verbose):
    """
    EXIF and IPTC metadata extraction and printing from remote images

    @param inputfile: path to the remote image
    @type inputfile: string
    @param verbose: verbosity
    @type verbose: int
    @param loginpw: credentials to access secure servers (username:password)
    @type loginpw: string
    @return: dictionary with metadata
    @rtype: dict
    """
    # Check that inputfile is an URL