Mercurial > odfcast
annotate castclient.py @ 34:89ffa12bd48d
Allow to not passing any datadict to the conversion form
author | Björn Ricks <bjoern.ricks@intevation.de> |
---|---|
date | Thu, 16 Oct 2014 11:47:05 +0200 |
parents | c04edbd52967 |
children | aee742cdd604 |
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 |
7
c04edbd52967
Allow castclient to do template variable substition via a json file
Björn Ricks <bjoern.ricks@intevation.de>
parents:
0
diff
changeset
|
23 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
|
24 service = "template" |
c04edbd52967
Allow castclient to do template variable substition via a json file
Björn Ricks <bjoern.ricks@intevation.de>
parents:
0
diff
changeset
|
25 else: |
c04edbd52967
Allow castclient to do template variable substition via a json file
Björn Ricks <bjoern.ricks@intevation.de>
parents:
0
diff
changeset
|
26 service = "convert" |
c04edbd52967
Allow castclient to do template variable substition via a json file
Björn Ricks <bjoern.ricks@intevation.de>
parents:
0
diff
changeset
|
27 |
c04edbd52967
Allow castclient to do template variable substition via a json file
Björn Ricks <bjoern.ricks@intevation.de>
parents:
0
diff
changeset
|
28 url = "http://%s:%s/%s/" % (options.host, options.port, service) |
0
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
29 infilename = args[0] |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
30 outfilename = args[1] |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
31 format = options.format |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
32 |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
33 files = {'file': open(infilename, 'rb')} |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
34 data = {'format': format} |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
35 |
7
c04edbd52967
Allow castclient to do template variable substition via a json file
Björn Ricks <bjoern.ricks@intevation.de>
parents:
0
diff
changeset
|
36 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
|
37 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
|
38 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
|
39 |
0
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
40 r = requests.post(url, data=data, files=files) |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
41 |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
42 if r.status_code == 200: |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
43 with open(outfilename, "wb") as f: |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
44 f.write(r.content) |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
45 print "OK" |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
46 else: |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
47 print "An error has occured" |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
48 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
|
49 print r.text |
0
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
50 sys.exit(2) |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
51 |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
52 |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
53 if __name__ == "__main__": |
4a34f72f036b
Add initial conversion service
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
54 main() |