Skip to content

Instantly share code, notes, and snippets.

@bewest
Created February 22, 2016 09:59
Show Gist options
  • Select an option

  • Save bewest/9c8332ac60a3ea20b59b to your computer and use it in GitHub Desktop.

Select an option

Save bewest/9c8332ac60a3ea20b59b to your computer and use it in GitHub Desktop.

Revisions

  1. bewest created this gist Feb 22, 2016.
    3 changes: 3 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    # figuring out python gdbus

    I'd like to transition https://github.com/openaps/oacids and https://github.com/bewest/openxshareble/tree/master/openxshareble and https://github.com/bewest/mmblelink and several other things to internet of things style daemons using linux to prototype these services.
    72 changes: 72 additions & 0 deletions pmain.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    import sys

    from gi.repository import Gio, Gtk


    class App(Gio.Application):

    def __init__(self):
    Gio.Application.__init__(self,
    application_id="org.gnome.example",
    flags=Gio.ApplicationFlags.FLAGS_NONE)
    # flags=Gio.ApplicationFlags.HANDLES_COMMAND_LINE)

    self.connect("activate", self.activateCb)
    # self.connect("command-line", self.my_argv)

    def xyzdo_command_line (self, args):
    # adopted from multiple examples do_command_line is needed any time
    # HANDLES_COMMAND_LINE is triggered, which can happen in multiple ways.
    # by default, it calls signals activate, which invokes some kind of main
    # loop with an appropriate context
    print "XYZ", args.get_arguments( )
    # help(args)
    # help(line)
    # self.do_activate(self)
    self.activate( )
    return 0
    return Gio.Application.do_activate(self)

    def activateCb(self, app):
    print "ACTIVATED!"
    # sans GUI default loop taking over, there is nothing to do
    """
    many apps supply GUIs, and will launch windows in the "main" instance.
    What about internet of things apps, which need to export informatin
    over dbus/websocket and simply want to export data, maybe signals, and
    if lucky methods, on a bus?
    With a gui this works, and even registers on properly on d-feet.
    For now it's not clear how to launch managed objects purely as a
    library, and potentially export them on new threads as appropriate for
    library usage.
    It has something to do with iterating a MainLoop using MainContext's
    push_thread_default, but no documentation or examples makes this
    explicit.
    """
    """
    window = Gio.ApplicationWindow()
    app.add_window(window)
    window.show()
    """

    def do_local_command_line (self, args):
    print "XXX", self, args
    # run any argparse here, including argcomplete?
    # Let Gio/Gio
    foo = Gio.Application.do_local_command_line(self, args[:1])
    # print foo
    ret = (True, None, 0) # allows dispatchin inner mainloop
    # ret = (False, None, 0) # continue invoking do_command_line logic, regardless of registered Flags!!
    # print foo == ret
    print ret
    return ret

    if __name__ == '__main__':
    app = App()
    # this does not work either...
    try:
    app.run(sys.argv)
    except KeyboardInterrupt, e:
    print "Quitting"
    app.quit( )
    sys.exit(0)