Mercurial > odfcast
annotate castclient.py @ 81:9ad8421dafb4 1.1
merge
author | Bernhard Herzog <bh@intevation.de> |
---|---|
date | Mon, 09 Mar 2015 17:07:48 +0100 |
parents | aee742cdd604 |
children | 349d49bb69f4 |
rev | line source |
---|---|
0
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
3 |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
4 import optparse |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
5 import sys |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
6 |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
7 import requests |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
8 |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
9 |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
10 def main(): |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
11 usage = "usage: %prog [options] infile outfile" |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
12 parser = optparse.OptionParser(usage=usage) |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
13 parser.add_option("-f", "--format", default="pdf") |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
14 parser.add_option("-s", "--host", default="localhost") |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
15 parser.add_option("-p", "--port", default="5000") |
7
c04edbd52967
Allow castclient to do template variable substition via a json file
Björn Ricks <bjoern.ricks@intevation.de>
parents:
0
diff
changeset
|
16 parser.add_option("--json", dest="json", metavar="FILE") |
0
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
17 (options, args) = parser.parse_args() |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
18 |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
19 if len(args) < 2: |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
20 parser.print_usage() |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
21 sys.exit(1) |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
22 |
35
aee742cdd604
Update castclient to use same url for template rendering and conversion
Björn Ricks <bjoern.ricks@intevation.de>
parents:
7
diff
changeset
|
23 url = "http://%s:%s/convert/" % (options.host, options.port) |
0
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
24 infilename = args[0] |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
25 outfilename = args[1] |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
26 format = options.format |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
27 |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
28 files = {'file': open(infilename, 'rb')} |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
29 data = {'format': format} |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
30 |
7
c04edbd52967
Allow castclient to do template variable substition via a json file
Björn Ricks <bjoern.ricks@intevation.de>
parents:
0
diff
changeset
|
31 if options.json: |
c04edbd52967
Allow castclient to do template variable substition via a json file
Björn Ricks <bjoern.ricks@intevation.de>
parents:
0
diff
changeset
|
32 with open(options.json, "r") as f: |
c04edbd52967
Allow castclient to do template variable substition via a json file
Björn Ricks <bjoern.ricks@intevation.de>
parents:
0
diff
changeset
|
33 data["datadict"] = f.read() |
c04edbd52967
Allow castclient to do template variable substition via a json file
Björn Ricks <bjoern.ricks@intevation.de>
parents:
0
diff
changeset
|
34 |
0
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
35 r = requests.post(url, data=data, files=files) |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
36 |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
37 if r.status_code == 200: |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
38 with open(outfilename, "wb") as f: |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
39 f.write(r.content) |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
40 print "OK" |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
41 else: |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
42 print "An error has occured" |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
43 print r.status_code, r.headers |
7
c04edbd52967
Allow castclient to do template variable substition via a json file
Björn Ricks <bjoern.ricks@intevation.de>
parents:
0
diff
changeset
|
44 print r.text |
0
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
45 sys.exit(2) |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
46 |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
47 |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
48 if __name__ == "__main__": |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
49 main() |