Improved installation checks again.
[openoffice-python:openoffice-python.git] / sample-scripts / open-stream.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright (c) 2008 by Hartmut Goebel <h.goebel@goebel-consult.de>
5 # Licenced under the GNU General Public License v3 (GPLv3)
6 # see file LICENSE-gpl-3.0.txt
7 #
8
9 __author__ = "Hartmut Goebel <h.goebel@goebel-consult.de>"
10 __copyright__ = "Copyright (c) 2008 by Hartmut Goebel <h.goebel@goebel-consult.de>"
11 __licence__ = "GPLv3 - GNU General Public License v3"
12
13 import openoffice.interact
14
15 def loadAsStream(odf_filename, opts=None):
16     desktop = openoffice.interact.Desktop(host=opts.host, port=opts.port)
17
18     stream = open(odf_filename)
19     if 0:
20         # using a file-like object works, too
21         import StringIO
22         stream = StringIO.StringIO(stream.read())
23
24     doc = desktop.openStream(stream, hidden=True)
25     doc.dispose()
26
27
28 if __name__ == '__main__':
29     import optparse
30     parser = optparse.OptionParser('%prog [options] ODF-Filename')
31     group = parser.add_option_group('To connect to already running server use:')
32     group.add_option('--host',  #default='localhost',
33                      help="hostname/ip of server (default: %default)")
34     group.add_option('--port',  default=2002, type=int,
35                      help="port the server is listening on (default: %default)")
36
37     opts, args = parser.parse_args()
38     if len(args) != 1:
39         parser.error('expects exactly one argument')
40     if not opts.host:
41         opts.port = None
42     loadAsStream(*args, **{'opts': opts})