/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

* mandos (transitional_dbus_metaclass): Renamed to
                                        "AlternateDBusNamesMetaclass".
                                        All users changed.  Also added
                                        comments, wrapped lines, and
                                        searching of all base classes
                                        for D-Bus attributes.  It now
                                        adds the *old* interface name,
                                        not the new one.
  (ClientDBus, MandosDBusService): Use the new interface names.

Show diffs side-by-side

added added

removed removed

Lines of Context:
754
754
    return dbus.String(dt.isoformat(),
755
755
                       variant_level=variant_level)
756
756
 
757
 
class transitional_dbus_metaclass(DBusObjectWithProperties.__metaclass__):
 
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
761
    def __new__(mcs, name, bases, attr):
759
 
        for attrname, old_dbusobj in inspect.getmembers(bases[0]):
760
 
            new_interface = getattr(old_dbusobj, "_dbus_interface", "").replace("se.bsnet.fukt.", "se.recompile.")
761
 
            if (getattr(old_dbusobj, "_dbus_is_signal", False)
762
 
                and old_dbusobj._dbus_interface.startswith("se.bsnet.fukt.Mandos")):
763
 
                unwrappedfunc = dict(zip(old_dbusobj.func_code.co_freevars,
764
 
                                    old_dbusobj.__closure__))["func"].cell_contents
765
 
                newfunc = types.FunctionType(unwrappedfunc.func_code,
766
 
                                             unwrappedfunc.func_globals,
767
 
                                             unwrappedfunc.func_name,
768
 
                                             unwrappedfunc.func_defaults,
769
 
                                             unwrappedfunc.func_closure)
770
 
                new_dbusfunc = dbus.service.signal(
771
 
                    new_interface, old_dbusobj._dbus_signature)(newfunc)            
772
 
                attr["_transitional_" + attrname] = new_dbusfunc
773
 
 
774
 
                def fixscope(func1, func2):
775
 
                    def newcall(*args, **kwargs):
776
 
                        func1(*args, **kwargs)
777
 
                        func2(*args, **kwargs)
778
 
                    return newcall
779
 
 
780
 
                attr[attrname] = fixscope(old_dbusobj, new_dbusfunc)
781
 
            
782
 
            elif (getattr(old_dbusobj, "_dbus_is_method", False)
783
 
                and old_dbusobj._dbus_interface.startswith("se.bsnet.fukt.Mandos")):
784
 
                new_dbusfunc = (dbus.service.method
785
 
                                (new_interface,
786
 
                                 old_dbusobj._dbus_in_signature,
787
 
                                 old_dbusobj._dbus_out_signature)
788
 
                                (types.FunctionType
789
 
                                 (old_dbusobj.func_code,
790
 
                                  old_dbusobj.func_globals,
791
 
                                  old_dbusobj.func_name,
792
 
                                  old_dbusobj.func_defaults,
793
 
                                  old_dbusobj.func_closure)))
794
 
 
795
 
                attr[attrname] = new_dbusfunc
796
 
            elif (getattr(old_dbusobj, "_dbus_is_property", False)
797
 
                  and old_dbusobj._dbus_interface.startswith("se.bsnet.fukt.Mandos")):
798
 
                new_dbusfunc = (dbus_service_property
799
 
                                (new_interface,
800
 
                                 old_dbusobj._dbus_signature,
801
 
                                 old_dbusobj._dbus_access,
802
 
                                 old_dbusobj._dbus_get_args_options["byte_arrays"])
803
 
                                (types.FunctionType
804
 
                                 (old_dbusobj.func_code,
805
 
                                  old_dbusobj.func_globals,
806
 
                                  old_dbusobj.func_name,
807
 
                                  old_dbusobj.func_defaults,
808
 
                                  old_dbusobj.func_closure)))
809
 
 
810
 
                attr[attrname] = new_dbusfunc
 
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)))
811
851
        return type.__new__(mcs, name, bases, attr)
812
852
 
813
853
class ClientDBus(Client, DBusObjectWithProperties):
840
880
    def notifychangeproperty(transform_func,
841
881
                             dbus_name, type_func=lambda x: x,
842
882
                             variant_level=1):
843
 
        """ Modify a variable so that its a property that announce its
844
 
        changes to DBus.
845
 
        transform_fun: Function that takes a value and transform it to
846
 
                       DBus type.
847
 
        dbus_name: DBus name of the variable
 
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
848
889
        type_func: Function that transform the value before sending it
849
 
                   to DBus
850
 
        variant_level: DBus variant level. default: 1
 
890
                   to the D-Bus.  Default: no transform
 
891
        variant_level: D-Bus variant level.  Default: 1
851
892
        """
852
893
        real_value = [None,]
853
894
        def setter(self, value):
948
989
    
949
990
    
950
991
    ## D-Bus methods, signals & properties
951
 
    _interface = "se.bsnet.fukt.Mandos.Client"
 
992
    _interface = "se.recompile.Mandos.Client"
952
993
    
953
994
    ## Signals
954
995
    
1226
1267
        self._pipe.send(('setattr', name, value))
1227
1268
 
1228
1269
class ClientDBusTransitional(ClientDBus):
1229
 
    __metaclass__ = transitional_dbus_metaclass
 
1270
    __metaclass__ = AlternateDBusNamesMetaclass
1230
1271
 
1231
1272
class ClientHandler(socketserver.BaseRequestHandler, object):
1232
1273
    """A class to handle client connections.
1936
1977
        try:
1937
1978
            bus_name = dbus.service.BusName("se.recompile.Mandos",
1938
1979
                                            bus, do_not_queue=True)
1939
 
            bus_name_transitional = dbus.service.BusName("se.bsnet.fukt.Mandos",
1940
 
                                                         bus, do_not_queue=True)
 
1980
            old_bus_name = dbus.service.BusName("se.bsnet.fukt.Mandos",
 
1981
                                                bus, do_not_queue=True)
1941
1982
        except dbus.exceptions.NameExistsException as e:
1942
1983
            logger.error(unicode(e) + ", disabling D-Bus")
1943
1984
            use_dbus = False
2001
2042
            """A D-Bus proxy object"""
2002
2043
            def __init__(self):
2003
2044
                dbus.service.Object.__init__(self, bus, "/")
2004
 
            _interface = "se.bsnet.fukt.Mandos"
 
2045
            _interface = "se.recompile.Mandos"
2005
2046
            
2006
2047
            @dbus.service.signal(_interface, signature="o")
2007
2048
            def ClientAdded(self, objpath):
2050
2091
            del _interface
2051
2092
        
2052
2093
        class MandosDBusServiceTransitional(MandosDBusService):
2053
 
            __metaclass__ = transitional_dbus_metaclass
 
2094
            __metaclass__ = AlternateDBusNamesMetaclass
2054
2095
        mandos_dbus_service = MandosDBusServiceTransitional()
2055
2096
    
2056
2097
    def cleanup():