/mandos/release

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

« back to all changes in this revision

Viewing changes to mandos-ctl

  • Committer: Teddy Hogeborn
  • Date: 2019-03-12 20:24:58 UTC
  • mto: This revision was merged to the branch mainline in revision 382.
  • Revision ID: teddy@recompile.se-20190312202458-14d4f7bvc61kwp2l
mandos-ctl: Refactor

* mandos-ctl (milliseconds_to_string): Move to
  "PrintTableCmd.TableOfClients.milliseconds_to_string".
  (PrintTableCmd.TableOfClients.valuetostring): Change to a class
                                                method to be able to
                                                call
                                         cls.milliseconds_to_string().
  (TestCmd.other_client): Change "ApprovalDuration" to a more odd
                          value to compensate for the removal of
                          "Test_milliseconds_to_string", which
                          contained a test for this value.  All users
                          changed.
  (Test_milliseconds_to_string): Remove.

Show diffs side-by-side

added added

removed removed

Lines of Context:
666
666
        def string_from_client(self, client, key):
667
667
            return self.valuetostring(client[key], key)
668
668
 
669
 
        @staticmethod
670
 
        def valuetostring(value, keyword):
 
669
        @classmethod
 
670
        def valuetostring(cls, value, keyword):
671
671
            if isinstance(value, dbus.Boolean):
672
672
                return "Yes" if value else "No"
673
673
            if keyword in ("Timeout", "Interval", "ApprovalDelay",
674
674
                           "ApprovalDuration", "ExtendedTimeout"):
675
 
                return milliseconds_to_string(value)
 
675
                return cls.milliseconds_to_string(value)
676
676
            return str(value)
677
677
 
678
678
        def header_line(self, format_string):
683
683
                **{key: self.string_from_client(client, key)
684
684
                   for key in self.keywords})
685
685
 
686
 
 
687
 
def milliseconds_to_string(ms):
688
 
    td = datetime.timedelta(0, 0, 0, ms)
689
 
    return ("{days}{hours:02}:{minutes:02}:{seconds:02}"
690
 
            .format(days="{}T".format(td.days) if td.days else "",
691
 
                    hours=td.seconds // 3600,
692
 
                    minutes=(td.seconds % 3600) // 60,
693
 
                    seconds=td.seconds % 60))
 
686
        @staticmethod
 
687
        def milliseconds_to_string(ms):
 
688
            td = datetime.timedelta(0, 0, 0, ms)
 
689
            return ("{days}{hours:02}:{minutes:02}:{seconds:02}"
 
690
                    .format(days="{}T".format(td.days)
 
691
                            if td.days else "",
 
692
                            hours=td.seconds // 3600,
 
693
                            minutes=(td.seconds % 3600) // 60,
 
694
                            seconds=td.seconds % 60))
694
695
 
695
696
 
696
697
class PropertyCmd(Command):
1236
1237
            ApprovedByDefault=dbus.Boolean(False),
1237
1238
            LastApprovalRequest="2019-01-03T00:00:00",
1238
1239
            ApprovalDelay=30000,
1239
 
            ApprovalDuration=1000,
 
1240
            ApprovalDuration=93785000,
1240
1241
            Checker=":",
1241
1242
            ExtendedTimeout=900000,
1242
1243
            Expires="2019-02-05T00:00:00",
1361
1362
                "ApprovedByDefault": False,
1362
1363
                "LastApprovalRequest": "2019-01-03T00:00:00",
1363
1364
                "ApprovalDelay": 30000,
1364
 
                "ApprovalDuration": 1000,
 
1365
                "ApprovalDuration": 93785000,
1365
1366
                "Checker": ":",
1366
1367
                "ExtendedTimeout": 900000,
1367
1368
                "Expires": "2019-02-05T00:00:00",
1458
1459
            ),(
1459
1460
                "Approval Duration ",
1460
1461
                "00:00:01          ",
1461
 
                "00:00:01          ",
 
1462
                "1T02:03:05        ",
1462
1463
            ),(
1463
1464
                "Checker              ",
1464
1465
                "fping -q -- %(host)s ",
1491
1492
        self.assertEqual(output, expected_output)
1492
1493
 
1493
1494
 
1494
 
class Test_milliseconds_to_string(unittest.TestCase):
1495
 
    def test_all(self):
1496
 
        self.assertEqual(milliseconds_to_string(93785000),
1497
 
                         "1T02:03:05")
1498
 
    def test_no_days(self):
1499
 
        self.assertEqual(milliseconds_to_string(7385000), "02:03:05")
1500
 
    def test_all_zero(self):
1501
 
        self.assertEqual(milliseconds_to_string(0), "00:00:00")
1502
 
    def test_no_fractional_seconds(self):
1503
 
        self.assertEqual(milliseconds_to_string(400), "00:00:00")
1504
 
        self.assertEqual(milliseconds_to_string(900), "00:00:00")
1505
 
        self.assertEqual(milliseconds_to_string(1900), "00:00:01")
1506
 
 
1507
 
 
1508
1495
class Unique(object):
1509
1496
    """Class for objects which exist only to be unique objects, since
1510
1497
unittest.mock.sentinel only exists in Python 3.3"""