annotate castclient.py @ 69:dcf870775c26

Add compability with flask 0.8 In flask version 0.10 (current stable version) the behaviour changed. With this version it is possible to specify the server and port via the config variable SERVER_NAME. To allow this behaviour also with flask 0.8 which is the version shipped with Debian Wheezy this commit extracts the host and port from the SERVER_NAME config variable.
author Björn Ricks <bjoern.ricks@intevation.de>
date Mon, 05 Jan 2015 12:37:56 +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()
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)