make helper/unomimemapper.py's JSON output more readable.
[cloudooo.git] / cloudooo / handler / ooo / helper / unomimemapper.py
1 #!/usr/bin/env python
2 ##############################################################################
3 #
4 # Copyright (c) 2009-2010 Nexedi SA and Contributors. All Rights Reserved.
5 # Gabriel M. Monnerat <gabriel@tiolive.com>
6 #
7 # WARNING: This program as such is intended to be used by professional
8 # programmers who take the whole responsibility of assessing all potential
9 # consequences resulting from its eventual inadequacies and bugs
10 # End users who are looking for a ready-to-use solution with commercial
11 # guarantees and support are strongly adviced to contract a Free Software
12 # Service Company
13 #
14 # This program is Free Software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public License
16 # as published by the Free Software Foundation; either version 2
17 # of the License, or (at your option) any later version.
18 #
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #
28 ##############################################################################
29
30 import sys
31 try:
32 import json
33 except ImportError:
34 import simplejson as json
35 import helper_util
36 from getopt import getopt, GetoptError
37
38 try:
39 basestring
40 except NameError:
41 basestring = str
42
43 __doc__ = """
44
45 usage: unomimemapper [options]
46
47 Options:
48 -h, --help this help screen
49 --hostname=STRING OpenOffice Instance address
50
51 --port=STRING OpenOffice Instance port
52
53 --office_binary_path=STRING_URL
54 Folder path were is the binary openoffice
55 --uno_path=STRING_URL
56 Folter path were is the uno library
57 """
58
59
60 class UnoMimemapper(object):
61 """ """
62
63 def __init__(self, hostname, port, uno_path=None, office_binary_path=None):
64 """ Receives hostname and port from openoffice and create a service manager"""
65 self.service_manager = helper_util.getServiceManager(hostname, port,
66 uno_path,
67 office_binary_path)
68
69 def _getElementNameByService(self, uno_service, ignore_name_list=[]):
70 """Returns an dict with elements."""
71 name_list = uno_service.getElementNames()
72 service_dict = {}
73 for name in iter(name_list):
74 element_dict = {}
75 element_list = uno_service.getByName(name)
76 for obj in iter(element_list):
77 if obj.Name in ignore_name_list:
78 continue
79 if not isinstance(obj.Value, (bool, int, basestring, tuple)):
80 continue
81 element_dict[obj.Name] = obj.Value
82 service_dict[name] = element_dict
83
84 return service_dict
85
86 def getFilterDict(self):
87 """Return all filters and your properties"""
88 filter_service = self.service_manager.createInstance("com.sun.star.document.FilterFactory")
89 filter_dict = self._getElementNameByService(filter_service, ["UINames", "UserData"])
90 return filter_dict
91
92 def getTypeDict(self):
93 """Return all types and your properties"""
94 type_service = self.service_manager.createInstance("com.sun.star.document.TypeDetection")
95 type_dict = self._getElementNameByService(type_service, ["UINames", "URLPattern"])
96 return type_dict
97
98
99 def help():
100 sys.stderr.write(__doc__)
101 sys.exit(1)
102
103
104 def main():
105 try:
106 opt_list, arg_list = getopt(sys.argv[1:], "h", ["help",
107 "uno_path=", "office_binary_path=",
108 "hostname=", "port="])
109 except GetoptError as msg:
110 msg = msg.msg + "\nUse --help or -h\n"
111 sys.stderr.write(msg)
112 sys.exit(2)
113
114 if not opt_list:
115 help()
116
117 port = hostname = uno_path = office_binary_path = None
118 for opt, arg in opt_list:
119 if opt in ("-h", "--help"):
120 help()
121 if opt == "--uno_path":
122 uno_path = arg
123 elif opt == "--office_binary_path":
124 office_binary_path = arg
125 elif opt == '--hostname':
126 hostname = arg
127 elif opt == "--port":
128 port = arg
129
130 mimemapper = UnoMimemapper(hostname, port, uno_path, office_binary_path)
131 filter_dict = mimemapper.getFilterDict()
132 type_dict = mimemapper.getTypeDict()
133
134 sys.stdout.write(json.dumps((filter_dict, type_dict),
135 sort_keys=True, indent=2, separators=(',', ':')))
136
137 if "__main__" == __name__:
138 main()