Esempio n. 1
0
def scan():
    transport = TransportBluepy()
    registry = SkoobotRegistry()

    rawDevices = transport.findRawDevices()
    skoobots = []
    for device in rawDevices:
        scanList = device.getScanData()
        for scanItem in scanList:
            if scanItem[0] == 9 and scanItem[2] == "Skoobot":
                skoobots.append(device)

    for skoobot in skoobots:
        # print(transport.rawDeviceInfoStr(skoobot))
        addr = skoobot.addr
        registry.addSkoobot(addr)
        name = registry.getSkoobotsByAddress(addr)[0][1]
        if registry.getDefaultName() == None:
            registry.setDefault(name)
        defaultText = " (default)" if registry.getDefaultName() == name else ""
        msg = "Added Skoobot {0:s} to registry with name {1:s}{2:s}"
        print(msg.format(addr, name, defaultText))
    print("Saving to list of Skoobots to registry {0:s}".format(
        registry.registryPath))
    registry.save()
    shutil.chown(registry.registryPath, os.getlogin())
Esempio n. 2
0
    def testSave(self):
        """
        Tests for the save() method

        Make sure that save() works, except when the registry is
        marked invalid.
        """
        registry = SkoobotRegistry(self.tempPath)
        altSkooAddr = "aa:aa:aa:aa:aa:aa"
        altSkooName = "Alt"
        extraSkooAddr = "ee:ee:ee:ee:ee:ee"
        extraSkooName = "Extra"

        with self.subTest("Undo alterations"):
            registry.addSkoobot(altSkooAddr, altSkooName)
            registry.setDefault(altSkooAddr)
            self.assertEqual(4, len(registry.registry))

            registry.load()

            self.assertEqual(3, len(registry.registry))
            self.assertEqual(self.skooName, registry.getDefaultName())

        with self.subTest("Alter and save"):
            registry.addSkoobot(altSkooAddr, altSkooName)
            registry.setDefault(altSkooAddr)
            self.assertEqual(4, len(registry.registry))

            # Save the state with the AltSkootbot entry
            registry.save()

            registry.addSkoobot(extraSkooAddr, extraSkooName)
            registry.setDefault(extraSkooAddr)
            self.assertEqual(5, len(registry.registry))
            self.assertEqual(extraSkooName, registry.getDefaultName())

            # Restore to the save() state
            registry.load()

            self.assertEqual(4, len(registry.registry))
            self.assertEqual(altSkooName, registry.getDefaultName())

        with self.subTest("Don't save invalid"):
            registry.addSkoobot(extraSkooAddr, altSkooName)
            registry.setDefault(extraSkooAddr)
            self.assertEqual(5, len(registry.registry))
            registry.valid = False

            # Fail to save the state with the Extra entry
            registry.save()

            # Restore to the previous save() state
            registry.load()

            self.assertEqual(4, len(registry.registry))
            self.assertEqual(altSkooName, registry.getDefaultName())
Esempio n. 3
0
    def testLoad(self):
        """
        Test for loading the registry.

        Most of this is already tested by the constructor tests,
        however, we need to check that a reload works and that a
        failed load sets the valid flag to false
        """
        registry = SkoobotRegistry(self.tempPath)

        with self.subTest("Empty dict"):
            emptyDict = {}
            with open(self.tempPath, "w") as registryFile:
                json.dump(emptyDict, registryFile)
            self.assertEqual(3, len(registry.registry))
            registry.load()
            self.assertEqual(0, len(registry.registry))
            self.assertEqual(True, registry.valid)
            self.assertEqual(None, registry.getDefaultName())

        with self.subTest("Invalid dict"):
            with open(self.tempPath, "w") as registryFile:
                registryFile.write("rubbish")
            registry.addSkoobot(self.skooAddr)
            self.assertEqual(True, registry.valid)

            with self.assertRaises(json.JSONDecodeError):
                registry.load()

            self.assertEqual(0, len(registry.registry))
            self.assertEqual(False, registry.valid)
            self.assertEqual(None, registry.getDefaultName())

        with self.subTest("Reload good dict"):
            with open(self.tempPath, "w") as registryFile:
                json.dump(self.registryDict, registryFile)
            self.assertEqual(0, len(registry.registry))
            registry.load()
            self.assertEqual(3, len(registry.registry))
            self.assertEqual(True, registry.valid)
            self.assertEqual(self.skooName, registry.getDefaultName())
Esempio n. 4
0
    def testGenerateName(self):
        """
        Tests for the generateName() method
        """
        registry = SkoobotRegistry(self.tempPath)
        altSkooAddr = "aa:aa:aa:aa:aa:aa"
        altSkooName = "Alt"

        with self.subTest("Generate name from default list"):
            name = registry.generateName()
            self.assertIn(name, registry.skoobotNames)

        with self.subTest("Generate Alt name"):
            registry.skoobotNames = set([altSkooName])
            name = registry.generateName()
            self.assertEqual(altSkooName, name)

        with self.subTest("Names all used"):
            registry.skoobotNames = set([altSkooName])
            registry.addSkoobot(altSkooAddr)
            with self.assertRaises(KeyError):
                name = registry.generateName()
Esempio n. 5
0
    def testAddSkoobot(self):
        """
        Test addition of skoobots using the addSkoobot() method

        The method adds a skoobot to the registry using an address
        and an optional name.
        """
        registry = SkoobotRegistry(self.tempPath)
        namedAddr = "ff:ff:ff:ff:ff:ff"
        namedName = "newSkoobot"
        unnamedAddr = "ff:ff:ff:ff:ff:fe"

        with self.subTest("Add named Skoobot"):
            registry.addSkoobot(namedAddr, namedName)
            self.assertEqual(4, len(registry.registry))
            self.assertEqual(1, len(registry.getSkoobotsByAddress(namedAddr)))
            self.assertEqual(1, len(registry.getSkoobotsByName(namedName)))

        with self.subTest("Add unnamed Skoobot"):
            registry.addSkoobot(unnamedAddr)
            self.assertEqual(5, len(registry.registry))
            skoobots = registry.getSkoobotsByAddress(unnamedAddr)
            self.assertEqual(1, len(skoobots))
            self.assertIn(skoobots[0][1], registry.skoobotNames)

        with self.subTest("Add duplicate Skoobot"):
            # Bug #7: By default this replaces the existing
            # skoobot. If the replace=False parameter is set,
            # it raises a RuntimeError unless the parameters
            # are compatible with the existing entry.
            #
            # It is always true that it does not result in a
            # duplicate address.
            registry.addSkoobot(namedAddr, namedName)
            self.assertEqual(5, len(registry.registry))

            registry.addSkoobot(namedAddr, replace=False)
            self.assertEqual(5, len(registry.registry))

            with self.assertRaises(RuntimeError):
                registry.addSkoobot(unnamedAddr, namedName, replace=False)

        with self.subTest("Test invalid parameters"):
            with self.assertRaises(TypeError):
                registry.addSkoobot((namedAddr, namedName))

            with self.assertRaises(TypeError):
                registry.addSkoobot(namedAddr, (namedAddr, namedName))