view castclient.py @ 33:00e12392f8d4

Fix saving uploaded form file to a temporary file The file must be saved to a temporary file for conversion via pyuno. pyuno can only accept filenames. Also the file position must be reset to the start. If the position is not reset we will get a IllegalArgumentException: Unsupported URL from pyuno when converting a file without template rendering.
author Björn Ricks <bjoern.ricks@intevation.de>
date Thu, 16 Oct 2014 11:45:51 +0200
parents c04edbd52967
children aee742cdd604
line wrap: on
line source
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import optparse
import sys

import requests


def main():
    usage = "usage: %prog [options] infile outfile"
    parser = optparse.OptionParser(usage=usage)
    parser.add_option("-f", "--format", default="pdf")
    parser.add_option("-s", "--host", default="localhost")
    parser.add_option("-p", "--port", default="5000")
    parser.add_option("--json", dest="json", metavar="FILE")
    (options, args) = parser.parse_args()

    if len(args) < 2:
        parser.print_usage()
        sys.exit(1)

    if options.json:
        service = "template"
    else:
        service = "convert"

    url = "http://%s:%s/%s/" % (options.host, options.port, service)
    infilename = args[0]
    outfilename = args[1]
    format = options.format

    files = {'file': open(infilename, 'rb')}
    data = {'format': format}

    if options.json:
        with open(options.json, "r") as f:
            data["datadict"] = f.read()

    r = requests.post(url, data=data, files=files)

    if r.status_code == 200:
        with open(outfilename, "wb") as f:
            f.write(r.content)
        print "OK"
    else:
        print "An error has occured"
        print r.status_code, r.headers
        print r.text
        sys.exit(2)


if __name__ == "__main__":
    main()
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)