Author: mvidner Date: Wed Apr 15 14:49:54 2009 New Revision: 56790 URL: http://svn.opensuse.org/viewcvs/yast?rev=56790&view=rev Log: Added an example of YaPI::USERS call. (--explicit makes core segfault, yum!) Moved polkit helpers to separate file. Added: trunk/core/dbus/namespace_service/doc/example-users.py (with props) trunk/core/dbus/namespace_service/doc/polkit_helper.py Modified: trunk/core/dbus/namespace_service/doc/Makefile.am trunk/core/dbus/namespace_service/doc/dbus_samba_export.py Modified: trunk/core/dbus/namespace_service/doc/Makefile.am URL: http://svn.opensuse.org/viewcvs/yast/trunk/core/dbus/namespace_service/doc/Makefile.am?rev=56790&r1=56789&r2=56790&view=diff ============================================================================== --- trunk/core/dbus/namespace_service/doc/Makefile.am (original) +++ trunk/core/dbus/namespace_service/doc/Makefile.am Wed Apr 15 14:49:54 2009 @@ -10,6 +10,8 @@ dbus_samba_export.desktop \ dbus_samba_export.py \ example-arch.py \ + example-users.py \ + polkit_helper.py \ org.opensuse.yast.modules.yapi-samba.policy \ YaPI_Samba.examples Modified: trunk/core/dbus/namespace_service/doc/dbus_samba_export.py URL: http://svn.opensuse.org/viewcvs/yast/trunk/core/dbus/namespace_service/doc/dbus_samba_export.py?rev=56790&r1=56789&r2=56790&view=diff ============================================================================== --- trunk/core/dbus/namespace_service/doc/dbus_samba_export.py (original) +++ trunk/core/dbus/namespace_service/doc/dbus_samba_export.py Wed Apr 15 14:49:54 2009 @@ -2,10 +2,9 @@ # a simple example showing how to integrate Yast Dbuse service into KDE desktop +from polkit_helper import retry # in . import dbus import sys -import os -import re def main(): @@ -35,38 +34,8 @@ call = lambda: samba.AddShare(share_name, {'path':directory, 'comment':'Exported directory ' + directory, 'read only':'Yes'}, dbus_interface='org.opensuse.YaST.Values') - result = polkit_retry(call) + result = retry(call) print "Result:", result -def polkit_retry(alambda): - try: - result = alambda() - except dbus.exceptions.DBusException, e: - if e.get_dbus_name() == 'org.freedesktop.PolicyKit.Error.NotAuthorized': - message = e.get_dbus_message() - - # get result and action name - parts = re.split(' +', message) - - if len(parts) == 2 and re.match("auth_.*", parts[1]): - print 'Authorization ' + parts[0] + ' is needed' - polkit_res = obtain_authorization(parts[0]) - print 'Authorization obtained: ' + str(polkit_res) - - result = alambda() - else: - print 'PolicyKit error: ' + message - raise - else: - raise - return result - -def obtain_authorization(action_id): - session_bus = dbus.SessionBus() - window_id = 0 - polkit_obj = session_bus.get_object('org.freedesktop.PolicyKit.AuthenticationAgent', '/'); - polkit_res = polkit_obj.ObtainAuthorization(action_id, window_id, os.getpid(), dbus_interface='org.freedesktop.PolicyKit.AuthenticationAgent') - return polkit_res - if __name__ == "__main__": main() Added: trunk/core/dbus/namespace_service/doc/example-users.py URL: http://svn.opensuse.org/viewcvs/yast/trunk/core/dbus/namespace_service/doc/example-users.py?rev=56790&view=auto ============================================================================== --- trunk/core/dbus/namespace_service/doc/example-users.py (added) +++ trunk/core/dbus/namespace_service/doc/example-users.py Wed Apr 15 14:49:54 2009 @@ -0,0 +1,33 @@ +#! /usr/bin/python +# example-users.py +# see example-arch.py +# +# As root: +# # polkit-auth --user $USER --grant org.opensuse.yast.modules.yapi-users.usersget +# work around a bug in yast2-core-2.18.6: +# # polkit-auth --user $USER --grant org.opensuse.yast.module-manager.modules.import + +from polkit_helper import retry # in . +import dbus + +bus = dbus.SystemBus() +O = bus.get_object # shorthand +I = dbus.Interface # shorthand +MOD_S = 'org.opensuse.YaST.modules' + +import sys +if len(sys.argv) > 1 and sys.argv[1] == "--explicit": + mm_o = O(MOD_S, '/org/opensuse/YaST/modules') + mm = I(mm_o, 'org.opensuse.YaST.modules.ModuleManager') + # polkit will ask + retry(lambda: mm.Import('YaPI::USERS')) + +Users_o = O(MOD_S, '/org/opensuse/YaST/modules/YaPI__USERS') +Users = I(Users_o, 'org.opensuse.YaST.Values') +cfg = { + "type": "system", + "index": "uid", + } +call = lambda: Users.UsersGet(cfg) +print "System users:", retry(call) +# it returns an empty hash, weird. but it works through, yay. Added: trunk/core/dbus/namespace_service/doc/polkit_helper.py URL: http://svn.opensuse.org/viewcvs/yast/trunk/core/dbus/namespace_service/doc/polkit_helper.py?rev=56790&view=auto ============================================================================== --- trunk/core/dbus/namespace_service/doc/polkit_helper.py (added) +++ trunk/core/dbus/namespace_service/doc/polkit_helper.py Wed Apr 15 14:49:54 2009 @@ -0,0 +1,38 @@ +import dbus +import re +import os + +def retry(alambda): + """Calls a function, obtaining authorization if required. + + Instead of RET = FOO(BAR, BAZ) + Call RET = retry(lambda: FOO(BAR, BAZ)) + """ + try: + result = alambda() + except dbus.exceptions.DBusException, e: + if e.get_dbus_name() == 'org.freedesktop.PolicyKit.Error.NotAuthorized': + message = e.get_dbus_message() + + # get result and action name + parts = re.split(' +', message) + + if len(parts) == 2 and re.match("auth_.*", parts[1]): + print 'Authorization ' + parts[0] + ' is needed' + polkit_res = obtain_authorization(parts[0]) + print 'Authorization obtained: ' + str(polkit_res) + + result = alambda() + else: + print 'PolicyKit error: ' + message + raise + else: + raise + return result + +def obtain_authorization(action_id): + session_bus = dbus.SessionBus() + window_id = 0 + polkit_obj = session_bus.get_object('org.freedesktop.PolicyKit.AuthenticationAgent', '/'); + polkit_res = polkit_obj.ObtainAuthorization(action_id, window_id, os.getpid(), dbus_interface='org.freedesktop.PolicyKit.AuthenticationAgent') + return polkit_res -- To unsubscribe, e-mail: yast-commit+unsubscribe@opensuse.org For additional commands, e-mail: yast-commit+help@opensuse.org