/mandos/trunk

To get this branch, use:
bzr branch /loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to mandos-ctl

  • Committer: Teddy Hogeborn
  • Date: 2019-08-10 15:19:47 UTC
  • Revision ID: teddy@recompile.se-20190810151947-wyw7cetrh1pvtw37
Simplification of Python 3 compatibility code

Normally, "class Foo:" in Python 2 creates a "classic" class, but in
Python 3, all classes are "new-style" classes, which you can get in
Python 2 by doing "class Foo(object):", i.e. inheriting from "object".
But, you can also get a new-style classes from "class Foo:" in Python
2 by setting the global "__metaclass__" variable to "type", which
makes the code less cluttered.  Note: it is still necessary to inherit
from "object" in Python 2 to get a new-style class if the class is
otherwise inheriting only from "classic" classes.

* mandos: Set "__metaclass__ = type" globally for Python 2, and remove
          inheriting from "object" in all places possible.
* mandos-ctl: - '' -

Show diffs side-by-side

added added

removed removed

46
46
import tempfile
47
47
import contextlib
48
48
 
 
49
if sys.version_info.major == 2:
 
50
    __metaclass__ = type
 
51
 
49
52
try:
50
53
    import pydbus
51
54
    import gi
53
56
except ImportError:
54
57
    import dbus as dbus_python
55
58
    pydbus = None
56
 
    class gi(object):
 
59
    class gi:
57
60
        """Dummy gi module, for the tests"""
58
 
        class repository(object):
59
 
            class GLib(object):
 
61
        class repository:
 
62
            class GLib:
60
63
                class Error(Exception):
61
64
                    pass
62
65
 
467
470
        parser.error("--remove can only be combined with --deny")
468
471
 
469
472
 
470
 
class dbus(object):
 
473
class dbus:
471
474
 
472
 
    class SystemBus(object):
 
475
    class SystemBus:
473
476
 
474
477
        object_manager_iface = "org.freedesktop.DBus.ObjectManager"
475
478
        def get_managed_objects(self, busname, objectpath):
521
524
        pass
522
525
 
523
526
 
524
 
class dbus_python_adapter(object):
 
527
class dbus_python_adapter:
525
528
 
526
529
    class SystemBus(dbus.MandosBus):
527
530
        """Use dbus-python"""
581
584
                                     self.client_interface, key,
582
585
                                     value)
583
586
 
584
 
    class SilenceLogger(object):
 
587
    class SilenceLogger:
585
588
        "Simple context manager to silence a particular logger"
586
589
        def __init__(self, loggername):
587
590
            self.logger = logging.getLogger(loggername)
616
619
                return new_object
617
620
 
618
621
 
619
 
class pydbus_adapter(object):
 
622
class pydbus_adapter:
620
623
    class SystemBus(dbus.MandosBus):
621
624
        def __init__(self, module=pydbus):
622
625
            self.pydbus = module
712
715
    return commands
713
716
 
714
717
 
715
 
class command(object):
 
718
class command:
716
719
    """A namespace for command classes"""
717
720
 
718
 
    class Base(object):
 
721
    class Base:
719
722
        """Abstract base class for commands"""
720
723
        def run(self, clients, bus=None):
721
724
            """Normal commands should implement run_on_one_client(),
784
787
                keywords = self.all_keywords
785
788
            print(self.TableOfClients(clients.values(), keywords))
786
789
 
787
 
        class TableOfClients(object):
 
790
        class TableOfClients:
788
791
            tableheaders = {
789
792
                "Name": "Name",
790
793
                "Enabled": "Enabled",
1021
1024
                                                     "output"))
1022
1025
 
1023
1026
 
1024
 
class Unique(object):
 
1027
class Unique:
1025
1028
    """Class for objects which exist only to be unique objects, since
1026
1029
unittest.mock.sentinel only exists in Python 3.3"""
1027
1030
 
1311
1314
class Test_dbus_python_adapter_SystemBus(TestCaseWithAssertLogs):
1312
1315
 
1313
1316
    def MockDBusPython_func(self, func):
1314
 
        class mock_dbus_python(object):
 
1317
        class mock_dbus_python:
1315
1318
            """mock dbus-python module"""
1316
 
            class exceptions(object):
 
1319
            class exceptions:
1317
1320
                """Pseudo-namespace"""
1318
1321
                class DBusException(Exception):
1319
1322
                    pass
1320
 
            class SystemBus(object):
 
1323
            class SystemBus:
1321
1324
                @staticmethod
1322
1325
                def get_object(busname, objectpath):
1323
1326
                    DBusObject = collections.namedtuple(
1337
1340
                                    dbus_interface=dbus_interface)
1338
1341
                    return DBusObject(methodname=method,
1339
1342
                                      Set=set_property)
1340
 
            class Boolean(object):
 
1343
            class Boolean:
1341
1344
                def __init__(self, value):
1342
1345
                    self.value = bool(value)
1343
1346
                def __bool__(self):
1555
1558
            self.call_method(bus, "methodname", "busname",
1556
1559
                             "objectpath", "interface")
1557
1560
 
1558
 
    class fake_dbus_python_raises_exception_on_connect(object):
 
1561
    class fake_dbus_python_raises_exception_on_connect:
1559
1562
        """fake dbus-python module"""
1560
 
        class exceptions(object):
 
1563
        class exceptions:
1561
1564
            """Pseudo-namespace"""
1562
1565
            class DBusException(Exception):
1563
1566
                pass
1571
1574
 
1572
1575
 
1573
1576
class Test_dbus_python_adapter_CachingBus(unittest.TestCase):
1574
 
    class mock_dbus_python(object):
 
1577
    class mock_dbus_python:
1575
1578
        """mock dbus-python modules"""
1576
 
        class SystemBus(object):
 
1579
        class SystemBus:
1577
1580
            @staticmethod
1578
1581
            def get_object(busname, objectpath):
1579
1582
                return Unique()
1625
1628
class Test_pydbus_adapter_SystemBus(TestCaseWithAssertLogs):
1626
1629
 
1627
1630
    def Stub_pydbus_func(self, func):
1628
 
        class stub_pydbus(object):
 
1631
        class stub_pydbus:
1629
1632
            """stub pydbus module"""
1630
 
            class SystemBus(object):
 
1633
            class SystemBus:
1631
1634
                @staticmethod
1632
1635
                def get(busname, objectpath):
1633
1636
                    DBusObject = collections.namedtuple(
1679
1682
            self.call_method(bus, "methodname", "busname",
1680
1683
                             "objectpath", "interface")
1681
1684
 
1682
 
    class fake_pydbus_raises_exception_on_connect(object):
 
1685
    class fake_pydbus_raises_exception_on_connect:
1683
1686
        """fake dbus-python module"""
1684
1687
        @classmethod
1685
1688
        def SystemBus(cls):
1689
1692
            return Bus(get=get)
1690
1693
 
1691
1694
    def test_set_property_uses_setattr(self):
1692
 
        class Object(object):
 
1695
        class Object:
1693
1696
            pass
1694
1697
        obj = Object()
1695
 
        class pydbus_spy(object):
1696
 
            class SystemBus(object):
 
1698
        class pydbus_spy:
 
1699
            class SystemBus:
1697
1700
                @staticmethod
1698
1701
                def get(busname, objectpath):
1699
1702
                    return {"interface": obj}
1706
1709
    def test_get_suppresses_xml_deprecation_warning(self):
1707
1710
        if sys.version_info.major >= 3:
1708
1711
            return
1709
 
        class stub_pydbus_get(object):
1710
 
            class SystemBus(object):
 
1712
        class stub_pydbus_get:
 
1713
            class SystemBus:
1711
1714
                @staticmethod
1712
1715
                def get(busname, objectpath):
1713
1716
                    warnings.warn_explicit(
1721
1724
 
1722
1725
 
1723
1726
class Test_pydbus_adapter_CachingBus(unittest.TestCase):
1724
 
    class stub_pydbus(object):
 
1727
    class stub_pydbus:
1725
1728
        """stub pydbus module"""
1726
 
        class SystemBus(object):
 
1729
        class SystemBus:
1727
1730
            @staticmethod
1728
1731
            def get(busname, objectpath):
1729
1732
                return Unique()