.:: Blackc0de Forum ::.
Would you like to react to this message? Create an account in a few clicks or log in to continue.

-=Explore The World From Our Binary=-
 
HomeIndeksLatest imagesPendaftaranLogin

 

 Writing PolicyKit applications without D-Bus (python)

Go down 
PengirimMessage
Dots
Top Nubie
Top Nubie
Dots


Jumlah posting : 32
Points : 87
Reputation : 1
Join date : 02.11.11

Writing PolicyKit applications without D-Bus (python) Empty
PostSubyek: Writing PolicyKit applications without D-Bus (python)   Writing PolicyKit applications without D-Bus (python) Icon_minitimeSat Nov 05, 2011 9:05 pm

I'm always happy when more libraries get Python bindings, and today I've been especially excited by the fact that GObject introspection support for PolicyKit has finally appeared in Natty repositories. Basically it means, that PolicyKit is now accessible from Python through a native API without direct D-Bus communication. I've tried to port the querying example from PolicyKit manual and it did work!

The Python example below is an almost line-by-line port of the original, but it should give you a basic idea on how to use the API for your own scripts.
view plainprint?

Code:
    #! /usr/bin/env python 
     
    import sys, os 
    from gi.repository import GObject, Gio, Polkit 
     
    def on_tensec_timeout(loop): 
      print("Ten seconds have passed. Now exiting.") 
      loop.quit() 
      return False 
     
    def check_authorization_cb(authority, res, loop): 
        try: 
            result = authority.check_authorization_finish(res) 
            if result.get_is_authorized(): 
                print("Authorized") 
            elif result.get_is_challenge(): 
                print("Challenge") 
            else: 
                print("Not authorized") 
        except GObject.GError as error: 
            print("Error checking authorization: %s" % error.message) 
             
        print("Authorization check has been cancelled " 
              "and the dialog should now be hidden.\n" 
              "This process will exit in ten seconds.") 
        GObject.timeout_add(10000, on_tensec_timeout, loop) 
     
    def do_cancel(cancellable): 
        print("Timer has expired; cancelling authorization check") 
        cancellable.cancel() 
        return False 
     
    if __name__ == "__main__": 
        if len(sys.argv) != 2: 
            print("usage: %s <action_id>" % sys.argv[0]) 
            sys.exit(1) 
        action_id = sys.argv[1] 
     
        mainloop = GObject.MainLoop() 
        authority = Polkit.Authority.get() 
        subject = Polkit.UnixProcess.new(os.getppid()) 
     
        cancellable = Gio.Cancellable() 
        GObject.timeout_add(10 * 1000, do_cancel, cancellable) 
     
        authority.check_authorization(subject, 
            action_id, #"org.freedesktop.policykit.exec", 
            None, 
            Polkit.CheckAuthorizationFlags.ALLOW_USER_INTERACTION, 
            cancellable, 
            check_authorization_cb, 
            mainloop) 
     
        mainloop.run() </action_id>




In order to run this example, make sure you have the gir1.2-polkit-1.0 package installed and provide an action in the command line, for example:

view plainprint?

./polkit-test org.freedesktop.policykit.exec


I suppose, the API is still a little rough on the edges, but it's already usable and I'm going to try it for an upcoming D-Bus service in indicator-cpufreq. Looks like it's perfect time to start moving things to GObject introspection already.
Kembali Ke Atas Go down
 
Writing PolicyKit applications without D-Bus (python)
Kembali Ke Atas 
Halaman 1 dari 1

Permissions in this forum:Anda tidak dapat menjawab topik
.:: Blackc0de Forum ::. :: BoarD Blackc0de :: International Room :: Programming (Int.) :: Command Line-
Navigasi: