Mercurial > odfcast
annotate mergeclient.py @ 100:bf1a998fff48 1.6.0
Add service file for running ODFCast via systemd.
author | Bernhard Herzog <bh@intevation.de> |
---|---|
date | Tue, 26 Jun 2018 21:48:01 +0200 |
parents | 349d49bb69f4 |
children |
rev | line source |
---|---|
21
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
3 |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
4 import optparse |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
5 import sys |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
6 |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
7 import requests |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
8 |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
9 |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
10 def main(): |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
11 usage = "usage: %prog [options] file1 file2 [ file3, ...]" |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
12 parser = optparse.OptionParser(usage=usage) |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
13 parser.add_option("-s", "--host", default="localhost") |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
14 parser.add_option("-p", "--port", default="5000") |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
15 parser.add_option("-o", "--out", default="merged.pdf", dest="out") |
63
3918c3c69485
Add ignore_file_errors option to mergeclient
Björn Ricks <bjoern.ricks@intevation.de>
parents:
21
diff
changeset
|
16 parser.add_option("-i", "--ignore-file-errors", action="store_true", |
3918c3c69485
Add ignore_file_errors option to mergeclient
Björn Ricks <bjoern.ricks@intevation.de>
parents:
21
diff
changeset
|
17 default=False, dest="ignorefileerrors") |
68
4645e50539ff
Add support for posting files with own form names for each file
Björn Ricks <bjoern.ricks@intevation.de>
parents:
63
diff
changeset
|
18 parser.add_option("-m", "--multi", action="store_true", |
4645e50539ff
Add support for posting files with own form names for each file
Björn Ricks <bjoern.ricks@intevation.de>
parents:
63
diff
changeset
|
19 default=False, dest="multi") |
21
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
20 (options, args) = parser.parse_args() |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
21 |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
22 if len(args) < 2: |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
23 parser.print_usage() |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
24 sys.exit(1) |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
25 |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
26 service = "merge" |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
27 url = "http://%s:%s/%s/" % (options.host, options.port, service) |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
28 |
63
3918c3c69485
Add ignore_file_errors option to mergeclient
Björn Ricks <bjoern.ricks@intevation.de>
parents:
21
diff
changeset
|
29 if options.ignorefileerrors: |
3918c3c69485
Add ignore_file_errors option to mergeclient
Björn Ricks <bjoern.ricks@intevation.de>
parents:
21
diff
changeset
|
30 url += "?ignore_file_errors=1" |
3918c3c69485
Add ignore_file_errors option to mergeclient
Björn Ricks <bjoern.ricks@intevation.de>
parents:
21
diff
changeset
|
31 |
21
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
32 files = [] |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
33 |
68
4645e50539ff
Add support for posting files with own form names for each file
Björn Ricks <bjoern.ricks@intevation.de>
parents:
63
diff
changeset
|
34 if options.multi: |
4645e50539ff
Add support for posting files with own form names for each file
Björn Ricks <bjoern.ricks@intevation.de>
parents:
63
diff
changeset
|
35 for i, filename in enumerate(args): |
4645e50539ff
Add support for posting files with own form names for each file
Björn Ricks <bjoern.ricks@intevation.de>
parents:
63
diff
changeset
|
36 files.append(("file%s" % i, open(filename, 'rb'))) |
4645e50539ff
Add support for posting files with own form names for each file
Björn Ricks <bjoern.ricks@intevation.de>
parents:
63
diff
changeset
|
37 else: |
4645e50539ff
Add support for posting files with own form names for each file
Björn Ricks <bjoern.ricks@intevation.de>
parents:
63
diff
changeset
|
38 for filename in args: |
4645e50539ff
Add support for posting files with own form names for each file
Björn Ricks <bjoern.ricks@intevation.de>
parents:
63
diff
changeset
|
39 files.append(('files', open(filename, 'rb'))) |
21
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
40 |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
41 r = requests.post(url, files=files) |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
42 |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
43 if r.status_code == 200: |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
44 with open(options.out, "wb") as f: |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
45 f.write(r.content) |
99
349d49bb69f4
Porting on python3
Magnus Schieder <mschieder@intevation.de>
parents:
68
diff
changeset
|
46 print("OK") |
21
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
47 else: |
99
349d49bb69f4
Porting on python3
Magnus Schieder <mschieder@intevation.de>
parents:
68
diff
changeset
|
48 print("An error has occured") |
349d49bb69f4
Porting on python3
Magnus Schieder <mschieder@intevation.de>
parents:
68
diff
changeset
|
49 print((r.status_code, r.headers)) |
349d49bb69f4
Porting on python3
Magnus Schieder <mschieder@intevation.de>
parents:
68
diff
changeset
|
50 print((r.text)) |
21
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
51 sys.exit(2) |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
52 |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
53 if __name__ == "__main__": |
a8c628466a9d
Add an example client for MergeView based on requests
Björn Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
54 main() |