gui: remove unused and duplicate dbus helper code
[ladish.git] / ladish_control
blobb7003271eb967f3ef672e7dfcc0d00dfc2c9f22f
1 #!/usr/bin/env python
3 # LADI Session Handler (ladish)
5 # Copyright (C) 2008, 2009 Nedko Arnaudov <nedko@arnaudov.name>
7 #*************************************************************************
8 # This file contains code of the commandline control app
9 #*************************************************************************
11 # LADI Session Handler is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
16 # LADI Session Handler is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
23 # or write to the Free Software Foundation, Inc.,
24 # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26 service_name = 'org.ladish'
28 control_object_path = "/org/ladish/Control"
29 studio_object_path = "/org/ladish/Studio"
31 control_interface_name = 'org.ladish.Control'
32 studio_interface_name = 'org.ladish.Studio'
34 import sys
35 import os
36 import time
37 from traceback import print_exc
39 import dbus
41 def bool_convert(str_value):
42 if str_value.lower() == "false":
43 return False
45 if str_value.lower() == "off":
46 return False
48 if str_value.lower() == "no":
49 return False
51 if str_value == "0":
52 return False
54 if str_value.lower() == "(null)":
55 return False
57 return bool(str_value)
59 def dbus_type_to_python_type(dbus_value):
60 if type(dbus_value) == dbus.Boolean:
61 return bool(dbus_value)
62 if type(dbus_value) == dbus.Int32 or type(dbus_value) == dbus.UInt32:
63 return int(dbus_value)
64 return dbus_value
66 def dbus_type_to_type_string(dbus_value):
67 if type(dbus_value) == dbus.Boolean:
68 return "bool"
69 if type(dbus_value) == dbus.Int32:
70 return "sint"
71 if type(dbus_value) == dbus.UInt32:
72 return "uint"
73 if type(dbus_value) == dbus.Byte:
74 return "char"
75 if type(dbus_value) == dbus.String:
76 return "str"
78 return None # throw exception here?
80 def dbus_typesig_to_type_string(type_char):
81 type_char = str(type_char)
82 if type_char == 'i':
83 return "sint"
84 if type_char == 'u':
85 return "uint"
86 if type_char == 'y':
87 return "char"
88 if type_char == 's':
89 return "str"
90 if type_char == 'b':
91 return "bool"
93 print('unknown dbus typesig')
94 return None # throw exception here?
96 def main():
97 if len(sys.argv) == 1:
98 print("Usage: %s [command] [command] ..." % os.path.basename(sys.argv[0]))
99 print("Commands:")
100 print(" exit - exit ladish dbus service")
101 print(" slist - list studios")
102 print(" alist - list apps")
103 print(" sload <studioname> - load studio")
104 print(" sdel <studioname> - delete studio")
105 print(" snew [studioname] - new studio")
106 print(" sisloaded - is studio loaded?")
107 print(" sname - get studio name")
108 print(" ssave - save studio")
109 print(" sunload - unload studio")
110 print(" srename <studioname> - rename studio")
111 print(" sstart - start studio")
112 print(" sstop - stop studio")
113 sys.exit(0)
115 bus = dbus.SessionBus()
116 control_obj = None
117 studio_obj = None
119 # check arguments
120 index = 1
121 while index < len(sys.argv):
122 arg = sys.argv[index]
123 index += 1
124 try:
125 if not control_obj:
126 control_obj = bus.get_object(service_name, control_object_path)
127 control_iface = dbus.Interface(control_obj, control_interface_name)
129 if arg == "exit":
130 print("--- exit")
131 control_iface.Exit()
132 time.sleep(1)
133 # we have deactivated the object and we need to get new connection if there are more commands
134 control_obj = None
135 control_iface = None
136 elif arg == 'slist':
137 print("--- studio list")
138 for studio in control_iface.GetStudioList():
139 name = studio[0]
140 mtime = studio[1]['Modification Time']
141 print('"%s" last modified on %s' % (name, time.ctime(mtime)))
142 elif arg == 'alist':
143 print("--- app list")
144 for app in control_iface.GetApplicationList():
145 print(app)
146 elif arg == 'sload':
147 print("--- studio load")
148 if index >= len(sys.argv):
149 print("load studio command requires studio name argument")
150 sys.exit()
152 arg = sys.argv[index]
153 index += 1
155 open_options = {}
156 #open_options["option1"] = "asd"
157 #open_options["option2"] = True
159 control_iface.LoadStudio(arg, open_options)
160 elif arg == 'sdel':
161 print("--- studio delete")
162 if index >= len(sys.argv):
163 print("delete studio command requires studio name argument")
164 sys.exit()
166 arg = sys.argv[index]
167 index += 1
169 control_iface.DeleteStudio(arg)
170 elif arg == 'snew':
171 print("--- studio new")
172 name = ""
173 if index < len(sys.argv):
174 name = sys.argv[index]
175 index += 1
177 control_iface.NewStudio(name)
178 elif arg == 'sisloaded':
179 print("--- studio is loaded")
180 if control_iface.IsStudioLoaded():
181 print("yes")
182 else:
183 print("no")
184 else:
185 if not studio_obj:
186 studio_obj = bus.get_object(service_name, studio_object_path)
187 studio_iface = dbus.Interface(studio_obj, studio_interface_name)
189 if arg == 'sname':
190 print("--- studio get name")
191 print("\"%s\"" % studio_iface.GetName())
192 elif arg == 'ssave':
193 print("--- studio save")
194 studio_iface.Save()
195 elif arg == 'sunload':
196 print("--- studio unload")
197 studio_iface.Unload()
198 studio_obj = None
199 studio_iface = None
200 elif arg == 'srename':
201 print("--- studio rename")
202 if index >= len(sys.argv):
203 print("rename studio command requires studio name argument")
204 sys.exit()
206 arg = sys.argv[index]
207 index += 1
209 studio_iface.Rename(arg)
210 elif arg == 'sstart':
211 print("--- studio start")
212 studio_iface.Start()
213 elif arg == 'sstop':
214 print("--- studio stop")
215 studio_iface.Stop()
216 else:
217 print("Unknown command '%s'" % arg)
218 except dbus.DBusException, e:
219 print("DBus exception: %s" % str(e))
221 if __name__ == '__main__':
222 main()