Imported Upstream version 1.40
[platform/upstream/connman.git] / test / simple-agent
index 2487b7b..04de3f6 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/python
 
-import gobject
+from gi.repository import GLib
 
 import dbus
 import dbus.service
@@ -32,8 +32,8 @@ class Agent(dbus.service.Object):
                response = {}
 
                if not self.identity and not self.passphrase and not self.wpspin:
-                       print "Service credentials requested, type cancel to cancel"
-                       args = raw_input('Answer: ')
+                       print("Service credentials requested, type cancel to cancel")
+                       args = input('Answer: ')
 
                        for arg in args.split():
                                if arg.startswith("cancel"):
@@ -62,9 +62,9 @@ class Agent(dbus.service.Object):
                response = {}
 
                if not self.username and not self.password:
-                       print "User login requested, type cancel to cancel"
-                       print "or browser to login through the browser by yourself."
-                       args = raw_input('Answer: ')
+                       print("User login requested, type cancel to cancel")
+                       print("or browser to login through the browser by yourself.")
+                       args = input('Answer: ')
 
                        for arg in args.split():
                                if arg.startswith("cancel") or arg.startswith("browser"):
@@ -87,7 +87,7 @@ class Agent(dbus.service.Object):
                response = {}
 
                if not self.name and not self.ssid:
-                       args = raw_input('Answer ')
+                       args = input('Answer ')
 
                        for arg in args.split():
                                if arg.startswith("Name="):
@@ -110,18 +110,18 @@ class Agent(dbus.service.Object):
                                        in_signature='oa{sv}',
                                        out_signature='a{sv}')
        def RequestInput(self, path, fields):
-               print "RequestInput (%s,%s)" % (path, fields)
+               print("RequestInput (%s,%s)" % (path, fields))
 
                response = {}
 
-               if fields.has_key("Name"):
+               if "Name" in fields:
                        response.update(self.input_hidden())
-               if fields.has_key("Passphrase"):
+               if "Passphrase" in fields:
                        response.update(self.input_passphrase())
-               if fields.has_key("Username"):
+               if "Username" in fields:
                        response.update(self.input_username())
 
-               if response.has_key("Error"):
+               if "Error" in response:
                        if response["Error"] == "cancel":
                                raise Canceled("canceled")
                                return
@@ -129,7 +129,7 @@ class Agent(dbus.service.Object):
                                raise LaunchBrowser("launch browser")
                                return
 
-               print "returning (%s)" % (response)
+               print("returning (%s)" % (response))
 
                return response
 
@@ -137,12 +137,12 @@ class Agent(dbus.service.Object):
                                        in_signature='os',
                                        out_signature='')
        def RequestBrowser(self, path, url):
-               print "RequestBrowser (%s,%s)" % (path, url)
+               print("RequestBrowser (%s,%s)" % (path, url))
 
-               print "Please login through the given url in a browser"
-               print "Then press enter to accept or some text to cancel"
+               print("Please login through the given url in a browser")
+               print("Then press enter to accept or some text to cancel")
 
-               args = raw_input('> ')
+               args = input('> ')
 
                if len(args) > 0:
                        raise Canceled("canceled")
@@ -153,8 +153,8 @@ class Agent(dbus.service.Object):
                                        in_signature='os',
                                        out_signature='')
        def ReportError(self, path, error):
-               print "ReportError %s, %s" % (path, error)
-               retry = raw_input("Retry service (yes/no): ")
+               print("ReportError %s, %s" % (path, error))
+               retry = input("Retry service (yes/no): ")
                if (retry == "yes"):
                        class Retry(dbus.DBusException):
                                _dbus_error_name = "net.connman.Agent.Error.Retry"
@@ -167,7 +167,7 @@ class Agent(dbus.service.Object):
        @dbus.service.method("net.connman.Agent",
                                        in_signature='', out_signature='')
        def Cancel(self):
-               print "Cancel"
+               print("Cancel")
 
 class VpnAgent(dbus.service.Object):
        name = None
@@ -185,8 +185,8 @@ class VpnAgent(dbus.service.Object):
                response = {}
 
                if not self.cookie:
-                       print "VPN credentials requested, type cancel to cancel"
-                       args = raw_input('Answer: ')
+                       print("VPN credentials requested, type cancel to cancel")
+                       args = input('Answer: ')
 
                        for arg in args.split():
                                if arg.startswith("cancel"):
@@ -204,8 +204,8 @@ class VpnAgent(dbus.service.Object):
                response = {}
 
                if not self.username and not self.password:
-                       print "User login requested, type cancel to cancel"
-                       args = raw_input('Answer: ')
+                       print("User login requested, type cancel to cancel")
+                       args = input('Answer: ')
 
                        for arg in args.split():
                                if arg.startswith("cancel"):
@@ -228,21 +228,21 @@ class VpnAgent(dbus.service.Object):
                                        in_signature='oa{sv}',
                                        out_signature='a{sv}')
        def RequestInput(self, path, fields):
-               print "RequestInput (%s,%s)" % (path, fields)
+               print("RequestInput (%s,%s)" % (path, fields))
 
                response = {}
 
-               if fields.has_key("OpenConnect.Cookie"):
+               if "OpenConnect.Cookie" in fields:
                        response.update(self.input_cookie())
-               if fields.has_key("Username") or fields.has_key("Password"):
+               if "Username" in fields or "Password" in fields:
                        response.update(self.input_username())
 
-               if response.has_key("Error"):
+               if "Error" in response:
                        if response["Error"] == "cancel":
                                raise Canceled("canceled")
                                return
 
-               print "returning (%s)" % (response)
+               print("returning (%s)" % (response))
 
                return response
 
@@ -250,8 +250,8 @@ class VpnAgent(dbus.service.Object):
                                        in_signature='os',
                                        out_signature='')
        def ReportError(self, path, error):
-               print "ReportError %s, %s" % (path, error)
-               retry = raw_input("Retry service (yes/no): ")
+               print("ReportError %s, %s" % (path, error))
+               retry = input("Retry service (yes/no): ")
                if (retry == "yes"):
                        class Retry(dbus.DBusException):
                                _dbus_error_name = "net.connman.vpn.Agent.Error.Retry"
@@ -264,7 +264,7 @@ class VpnAgent(dbus.service.Object):
        @dbus.service.method("net.connman.vpn.Agent",
                                        in_signature='', out_signature='')
        def Cancel(self):
-               print "Cancel"
+               print("Cancel")
 
 def vpnNameOwnerChanged(proxy):
        if proxy:
@@ -276,22 +276,22 @@ def vpnNameOwnerChanged(proxy):
                                                     'net.connman.vpn.Manager')
                        vpn_manager.RegisterAgent(path)
                except:
-                       print "vpn agent registered"
+                       print("vpn agent is not registered")
        else:
                print("vpnd is disconnected from system bus")
                vpn_manager = None
 
 def print_usage():
-       print "Usage:"
-       print "For hidden service:"
-       print "%s Name=<hidden service name> [SSID=<hidden ssid>]" % (sys.argv[0])
-       print "For EAP/WPA input:"
-       print "%s Identity=<identity> Passphrase=<passphrase> WPS=<wpspin>" % (sys.argv[0])
-       print "For WISPr login, L2TP or PPTP input:"
-       print "%s Username=<username> Password=<password>" % (sys.argv[0])
-       print "For OpenConnect input:"
-       print "%s Cookie=<string>" % (sys.argv[0])
-       print "Help: %s help" % (sys.argv[0])
+       print("Usage:")
+       print("For hidden service:")
+       print("%s Name=<hidden service name> [SSID=<hidden ssid>]" % (sys.argv[0]))
+       print("For EAP/WPA input:")
+       print("%s Identity=<identity> Passphrase=<passphrase> WPS=<wpspin>" % (sys.argv[0]))
+       print("For WISPr login, L2TP or PPTP input:")
+       print("%s Username=<username> Password=<password>" % (sys.argv[0]))
+       print("For OpenConnect input:")
+       print("%s Cookie=<string>" % (sys.argv[0]))
+       print("Help: %s help" % (sys.argv[0]))
        sys.exit(1)
 
 if __name__ == '__main__':
@@ -307,10 +307,14 @@ if __name__ == '__main__':
        path = "/test/agent"
        object = Agent(bus, path)
 
-       vpn_manager = dbus.Interface(bus.get_object('net.connman.vpn', "/"),
-                                       'net.connman.vpn.Manager')
-       vpn_path = "/test/vpn_agent"
-       vpn_object = VpnAgent(bus, vpn_path)
+       try:
+               vpn_manager = dbus.Interface(bus.get_object('net.connman.vpn', "/"),
+                                           'net.connman.vpn.Manager')
+               vpn_path = "/test/vpn_agent"
+               vpn_object = VpnAgent(bus, vpn_path)
+       except:
+               vpn_manager = None
+               print("net.connman.vpn is not present")
 
        if len(sys.argv) >= 2:
                for arg in sys.argv[1:]:
@@ -338,13 +342,16 @@ if __name__ == '__main__':
        try:
                manager.RegisterAgent(path)
        except:
-               print "Cannot register connman agent."
+               print("Cannot register connman agent.")
 
-       vpn_manager.RegisterAgent(vpn_path)
-
-       bus.watch_name_owner('net.connman.vpn', vpnNameOwnerChanged)
+       if vpn_manager != None:
+               try:
+                       vpn_manager.RegisterAgent(vpn_path)
+                       bus.watch_name_owner('net.connman.vpn', vpnNameOwnerChanged)
+               except:
+                       "Cannot register vpn agent"
 
-       mainloop = gobject.MainLoop()
+       mainloop = GLib.MainLoop()
        mainloop.run()
 
        #manager.UnregisterAgent(path)