/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to mandos

  • Committer: Björn Påhlsson
  • Date: 2011-10-02 19:18:24 UTC
  • mto: This revision was merged to the branch mainline in revision 505.
  • Revision ID: belorn@fukt.bsnet.se-20111002191824-eweh4pvneeg3qzia
transitional stuff actually working
documented change to D-Bus API

Show diffs side-by-side

added added

removed removed

Lines of Context:
644
644
        raise DBusPropertyNotFound(self.dbus_object_path + ":"
645
645
                                   + interface_name + "."
646
646
                                   + property_name)
 
647
 
647
648
    
648
649
    @dbus.service.method(dbus.PROPERTIES_IFACE, in_signature="ss",
649
650
                         out_signature="v")
754
755
    return dbus.String(dt.isoformat(),
755
756
                       variant_level=variant_level)
756
757
 
757
 
class AlternateDBusNamesMetaclass(DBusObjectWithProperties.__metaclass__):
758
 
    """Applied to an empty subclass of a D-Bus object, this metaclass
759
 
    will add additional D-Bus attributes matching a certain pattern.
760
 
    """
 
758
class transitional_dbus_metaclass(DBusObjectWithProperties.__metaclass__):
761
759
    def __new__(mcs, name, bases, attr):
762
 
        # Go through all the base classes which could have D-Bus
763
 
        # methods, signals, or properties in them
764
 
        for base in (b for b in bases
765
 
                     if issubclass(b, dbus.service.Object)):
766
 
            # Go though all attributes of the base class
767
 
            for attrname, attribute in inspect.getmembers(base):
768
 
                # Ignore non-D-Bus attributes, and D-Bus attributes
769
 
                # with the wrong interface name
770
 
                if (not hasattr(attribute, "_dbus_interface")
771
 
                    or not attribute._dbus_interface
772
 
                    .startswith("se.recompile.Mandos")):
773
 
                    continue
774
 
                # Create an alternate D-Bus interface name based on
775
 
                # the current name
776
 
                alt_interface = (attribute._dbus_interface
777
 
                                 .replace("se.recompile.Mandos",
778
 
                                          "se.bsnet.fukt.Mandos"))
779
 
                # Is this a D-Bus signal?
780
 
                if getattr(attribute, "_dbus_is_signal", False):
781
 
                    # Extract the original non-method function by
782
 
                    # black magic
783
 
                    nonmethod_func = (dict(
784
 
                            zip(attribute.func_code.co_freevars,
785
 
                                attribute.__closure__))["func"]
786
 
                                      .cell_contents)
787
 
                    # Create a new, but exactly alike, function
788
 
                    # object, and decorate it to be a new D-Bus signal
789
 
                    # with the alternate D-Bus interface name
790
 
                    new_function = (dbus.service.signal
791
 
                                    (alt_interface,
792
 
                                     attribute._dbus_signature)
793
 
                                    (types.FunctionType(
794
 
                                nonmethod_func.func_code,
795
 
                                nonmethod_func.func_globals,
796
 
                                nonmethod_func.func_name,
797
 
                                nonmethod_func.func_defaults,
798
 
                                nonmethod_func.func_closure)))
799
 
                    # Define a creator of a function to call both the
800
 
                    # old and new functions, so both the old and new
801
 
                    # signals gets sent when the function is called
802
 
                    def fixscope(func1, func2):
803
 
                        """This function is a scope container to pass
804
 
                        func1 and func2 to the "call_both" function
805
 
                        outside of its arguments"""
806
 
                        def call_both(*args, **kwargs):
807
 
                            """This function will emit two D-Bus
808
 
                            signals by calling func1 and func2"""
809
 
                            func1(*args, **kwargs)
810
 
                            func2(*args, **kwargs)
811
 
                        return call_both
812
 
                    # Create the "call_both" function and add it to
813
 
                    # the class
814
 
                    attr[attrname] = fixscope(attribute,
815
 
                                              new_function)
816
 
                # Is this a D-Bus method?
817
 
                elif getattr(attribute, "_dbus_is_method", False):
818
 
                    # Create a new, but exactly alike, function
819
 
                    # object.  Decorate it to be a new D-Bus method
820
 
                    # with the alternate D-Bus interface name.  Add it
821
 
                    # to the class.
822
 
                    attr[attrname] = (dbus.service.method
823
 
                                      (alt_interface,
824
 
                                       attribute._dbus_in_signature,
825
 
                                       attribute._dbus_out_signature)
826
 
                                      (types.FunctionType
827
 
                                       (attribute.func_code,
828
 
                                        attribute.func_globals,
829
 
                                        attribute.func_name,
830
 
                                        attribute.func_defaults,
831
 
                                        attribute.func_closure)))
832
 
                # Is this a D-Bus property?
833
 
                elif getattr(attribute, "_dbus_is_property", False):
834
 
                    # Create a new, but exactly alike, function
835
 
                    # object, and decorate it to be a new D-Bus
836
 
                    # property with the alternate D-Bus interface
837
 
                    # name.  Add it to the class.
838
 
                    attr[attrname] = (dbus_service_property
839
 
                                      (alt_interface,
840
 
                                       attribute._dbus_signature,
841
 
                                       attribute._dbus_access,
842
 
                                       attribute
843
 
                                       ._dbus_get_args_options
844
 
                                       ["byte_arrays"])
845
 
                                      (types.FunctionType
846
 
                                       (attribute.func_code,
847
 
                                        attribute.func_globals,
848
 
                                        attribute.func_name,
849
 
                                        attribute.func_defaults,
850
 
                                        attribute.func_closure)))
 
760
        for attrname, old_dbusobj in inspect.getmembers(bases[0]):
 
761
            new_interface = getattr(old_dbusobj, "_dbus_interface", "").replace("se.bsnet.fukt.", "se.recompile.")
 
762
            if (getattr(old_dbusobj, "_dbus_is_signal", False)
 
763
                and old_dbusobj._dbus_interface.startswith("se.bsnet.fukt.Mandos")):
 
764
                unwrappedfunc = dict(zip(old_dbusobj.func_code.co_freevars,
 
765
                                    old_dbusobj.__closure__))["func"].cell_contents
 
766
                newfunc = types.FunctionType(unwrappedfunc.func_code,
 
767
                                             unwrappedfunc.func_globals,
 
768
                                             unwrappedfunc.func_name,
 
769
                                             unwrappedfunc.func_defaults,
 
770
                                             unwrappedfunc.func_closure)
 
771
                new_dbusfunc = dbus.service.signal(
 
772
                    new_interface, old_dbusobj._dbus_signature)(newfunc)            
 
773
                attr["_transitional_" + attrname] = new_dbusfunc
 
774
 
 
775
                def fixscope(func1, func2):
 
776
                    def newcall(*args, **kwargs):
 
777
                        func1(*args, **kwargs)
 
778
                        func2(*args, **kwargs)
 
779
                    return newcall
 
780
 
 
781
                attr[attrname] = fixscope(old_dbusobj, new_dbusfunc)
 
782
            
 
783
            elif (getattr(old_dbusobj, "_dbus_is_method", False)
 
784
                and old_dbusobj._dbus_interface.startswith("se.bsnet.fukt.Mandos")):
 
785
                new_dbusfunc = (dbus.service.method
 
786
                                (new_interface,
 
787
                                 old_dbusobj._dbus_in_signature,
 
788
                                 old_dbusobj._dbus_out_signature)
 
789
                                (types.FunctionType
 
790
                                 (old_dbusobj.func_code,
 
791
                                  old_dbusobj.func_globals,
 
792
                                  old_dbusobj.func_name,
 
793
                                  old_dbusobj.func_defaults,
 
794
                                  old_dbusobj.func_closure)))
 
795
 
 
796
                attr[attrname] = new_dbusfunc
 
797
            elif (getattr(old_dbusobj, "_dbus_is_property", False)
 
798
                  and old_dbusobj._dbus_interface.startswith("se.bsnet.fukt.Mandos")):
 
799
                new_dbusfunc = (dbus_service_property
 
800
                                (new_interface,
 
801
                                 old_dbusobj._dbus_signature,
 
802
                                 old_dbusobj._dbus_access,
 
803
                                 old_dbusobj._dbus_get_args_options["byte_arrays"])
 
804
                                (types.FunctionType
 
805
                                 (old_dbusobj.func_code,
 
806
                                  old_dbusobj.func_globals,
 
807
                                  old_dbusobj.func_name,
 
808
                                  old_dbusobj.func_defaults,
 
809
                                  old_dbusobj.func_closure)))
 
810
 
 
811
                attr[attrname] = new_dbusfunc
851
812
        return type.__new__(mcs, name, bases, attr)
852
813
 
853
814
class ClientDBus(Client, DBusObjectWithProperties):
880
841
    def notifychangeproperty(transform_func,
881
842
                             dbus_name, type_func=lambda x: x,
882
843
                             variant_level=1):
883
 
        """ Modify a variable so that it's a property which announces
884
 
        its changes to DBus.
885
 
 
886
 
        transform_fun: Function that takes a value and transforms it
887
 
                       to a D-Bus type.
888
 
        dbus_name: D-Bus name of the variable
 
844
        """ Modify a variable so that its a property that announce its
 
845
        changes to DBus.
 
846
        transform_fun: Function that takes a value and transform it to
 
847
                       DBus type.
 
848
        dbus_name: DBus name of the variable
889
849
        type_func: Function that transform the value before sending it
890
 
                   to the D-Bus.  Default: no transform
891
 
        variant_level: D-Bus variant level.  Default: 1
 
850
                   to DBus
 
851
        variant_level: DBus variant level. default: 1
892
852
        """
893
853
        real_value = [None,]
894
854
        def setter(self, value):
989
949
    
990
950
    
991
951
    ## D-Bus methods, signals & properties
992
 
    _interface = "se.recompile.Mandos.Client"
993
 
    
 
952
    _interface = "se.bsnet.fukt.Mandos.Client"
 
953
 
994
954
    ## Signals
995
955
    
996
956
    # CheckerCompleted - signal
1267
1227
        self._pipe.send(('setattr', name, value))
1268
1228
 
1269
1229
class ClientDBusTransitional(ClientDBus):
1270
 
    __metaclass__ = AlternateDBusNamesMetaclass
 
1230
    __metaclass__ = transitional_dbus_metaclass
1271
1231
 
1272
1232
class ClientHandler(socketserver.BaseRequestHandler, object):
1273
1233
    """A class to handle client connections.
1975
1935
    # End of Avahi example code
1976
1936
    if use_dbus:
1977
1937
        try:
1978
 
            bus_name = dbus.service.BusName("se.recompile.Mandos",
1979
 
                                            bus, do_not_queue=True)
1980
 
            old_bus_name = dbus.service.BusName("se.bsnet.fukt.Mandos",
1981
 
                                                bus, do_not_queue=True)
 
1938
            bus_name = dbus.service.BusName("se.bsnet.fukt.Mandos",
 
1939
                                            bus, do_not_queue=True)
 
1940
            bus_name2 = dbus.service.BusName("se.recompile.Mandos",
 
1941
                                            bus, do_not_queue=True)
1982
1942
        except dbus.exceptions.NameExistsException as e:
1983
1943
            logger.error(unicode(e) + ", disabling D-Bus")
1984
1944
            use_dbus = False
2042
2002
            """A D-Bus proxy object"""
2043
2003
            def __init__(self):
2044
2004
                dbus.service.Object.__init__(self, bus, "/")
2045
 
            _interface = "se.recompile.Mandos"
 
2005
            _interface = "se.bsnet.fukt.Mandos"
2046
2006
            
2047
2007
            @dbus.service.signal(_interface, signature="o")
2048
2008
            def ClientAdded(self, objpath):
2091
2051
            del _interface
2092
2052
        
2093
2053
        class MandosDBusServiceTransitional(MandosDBusService):
2094
 
            __metaclass__ = AlternateDBusNamesMetaclass
 
2054
            __metaclass__ = transitional_dbus_metaclass
2095
2055
        mandos_dbus_service = MandosDBusServiceTransitional()
2096
2056
    
2097
2057
    def cleanup():