Mercurial > odfcast
comparison castclient.py @ 7:c04edbd52967
Allow castclient to do template variable substition via a json file
author | Björn Ricks <bjoern.ricks@intevation.de> |
---|---|
date | Fri, 26 Sep 2014 15:36:58 +0200 |
parents | 4a34f72f036b |
children | aee742cdd604 |
comparison
equal
deleted
inserted
replaced
6:7f546e8a6e7a | 7:c04edbd52967 |
---|---|
4 import optparse | 4 import optparse |
5 import sys | 5 import sys |
6 | 6 |
7 import requests | 7 import requests |
8 | 8 |
9 url = "http://127.0.0.1:5000/convert/" | |
10 | |
11 | 9 |
12 def main(): | 10 def main(): |
13 usage = "usage: %prog [options] infile outfile" | 11 usage = "usage: %prog [options] infile outfile" |
14 parser = optparse.OptionParser(usage=usage) | 12 parser = optparse.OptionParser(usage=usage) |
15 parser.add_option("-f", "--format", default="pdf") | 13 parser.add_option("-f", "--format", default="pdf") |
16 parser.add_option("-s", "--host", default="localhost") | 14 parser.add_option("-s", "--host", default="localhost") |
17 parser.add_option("-p", "--port", default="5000") | 15 parser.add_option("-p", "--port", default="5000") |
16 parser.add_option("--json", dest="json", metavar="FILE") | |
18 (options, args) = parser.parse_args() | 17 (options, args) = parser.parse_args() |
19 | 18 |
20 if len(args) < 2: | 19 if len(args) < 2: |
21 parser.print_usage() | 20 parser.print_usage() |
22 sys.exit(1) | 21 sys.exit(1) |
23 | 22 |
24 url = "http://%s:%s/convert/" % (options.host, options.port) | 23 if options.json: |
24 service = "template" | |
25 else: | |
26 service = "convert" | |
27 | |
28 url = "http://%s:%s/%s/" % (options.host, options.port, service) | |
25 infilename = args[0] | 29 infilename = args[0] |
26 outfilename = args[1] | 30 outfilename = args[1] |
27 format = options.format | 31 format = options.format |
28 | 32 |
29 files = {'file': open(infilename, 'rb')} | 33 files = {'file': open(infilename, 'rb')} |
30 data = {'format': format} | 34 data = {'format': format} |
35 | |
36 if options.json: | |
37 with open(options.json, "r") as f: | |
38 data["datadict"] = f.read() | |
31 | 39 |
32 r = requests.post(url, data=data, files=files) | 40 r = requests.post(url, data=data, files=files) |
33 | 41 |
34 if r.status_code == 200: | 42 if r.status_code == 200: |
35 with open(outfilename, "wb") as f: | 43 with open(outfilename, "wb") as f: |
36 f.write(r.content) | 44 f.write(r.content) |
37 print "OK" | 45 print "OK" |
38 else: | 46 else: |
39 print "An error has occured" | 47 print "An error has occured" |
40 print r.status_code, r.headers | 48 print r.status_code, r.headers |
49 print r.text | |
41 sys.exit(2) | 50 sys.exit(2) |
42 | 51 |
43 | 52 |
44 if __name__ == "__main__": | 53 if __name__ == "__main__": |
45 main() | 54 main() |