def test_should_construct_empty(self): ident = DeviceIdentifier() self.assertEqual(repr(ident), "DeviceId()") self.assertEqual(len(ident.keys()), 0) self.assertRaises(DeviceIdentifierException, lambda: ident.string) self.assertRaises(DeviceIdentifierException, lambda: str(ident)) self.assertRaises(AttributeError, lambda: ident.whatevs)
def test_should_merge_naming_schemas(self): self.ident.append(DeviceIdentifier("{one}")) self.assertEqual(self.ident.naming_schema, "{one}") self.ident.append(DeviceIdentifier("{one}")) self.assertEqual(self.ident.naming_schema, "{one}") self.ident.append(DeviceIdentifier("{one}{two}")) self.assertEqual(self.ident.naming_schema, "{one}{one}{two}")
def test_copy(self): ident = DeviceIdentifier("{platform}") ident.set("platform", "stm32") ident2 = ident.copy() self.assertEqual(ident2.platform, "stm32") self.assertEqual(ident2.naming_schema, "{platform}") ident2.set("platform", "avr") self.assertEqual(ident2.platform, "avr") self.assertEqual(ident.platform, "stm32") ident2.naming_schema = "{platform}{family}" self.assertEqual(ident2.naming_schema, "{platform}{family}") self.assertEqual(ident.naming_schema, "{platform}")
def from_string(string): string = string.lower() if string.startswith("nrf"): matchString = r"nrf(?P<family>[0-9]{2})(?P<series>[0-9]{3})-(?P<package>\w{2})(?P<function>\w{2})" match = re.search(matchString, string) if match: i = DeviceIdentifier( "{platform}{family}{series}-{package}{function}") i.set("platform", "nrf") i.set("family", match.group("family").lower()) i.set("series", match.group("series").lower()) i.set("package", match.group("package").lower()) i.set("function", match.group("function").lower()) return i LOGGER.error("Parse Error: unknown platform. Device string: '%s'", string) exit(1)
def test_setter_getter(self): ident = DeviceIdentifier() ident.set("platform", "stm32") self.assertEqual(ident.get("platform"), "stm32") self.assertEqual(ident["platform"], "stm32") self.assertEqual(ident.platform, "stm32") self.assertEqual(repr(ident), "DeviceId(platformstm32)") self.assertRaises(DeviceIdentifierException, lambda: ident.string) self.assertRaises(DeviceIdentifierException, lambda: str(ident)) ident.set("platform", "avr") self.assertEqual(ident.get("platform"), "avr") self.assertEqual(ident["platform"], "avr") self.assertEqual(ident.platform, "avr") self.assertEqual(ident.get("whatevs"), None) self.assertEqual(ident.get("whatevs", "default"), "default") self.assertEqual(ident["whatevs"], None) self.assertRaises(AttributeError, lambda: ident.whatevs)
def test_naming_schema(self): ident = DeviceIdentifier("{platform}{family}{name}") self.assertEqual(ident.string, "") ident.set("platform", "stm32") self.assertEqual(ident.string, "stm32") ident.set("name", "03") self.assertEqual(ident.string, "stm3203") ident.set("family", "f1") self.assertEqual(ident.string, "stm32f103") self.assertEqual(str(ident), "stm32f103") self.assertEqual(repr(ident), "stm32f103") self.assertEqual( hash(ident), hash("familyf1name03platformstm32{platform}{family}{name}")) ident2 = DeviceIdentifier("{platform}{family}{name}") ident2.set("platform", "stm32") ident2.set("family", "f1") ident2.set("name", "03") self.assertEqual( hash(ident2), hash("familyf1name03platformstm32{platform}{family}{name}")) self.assertTrue(ident == ident2) self.assertFalse(ident != ident2) self.assertEqual(ident, ident2) ident3 = DeviceIdentifier("{platform}{family}") ident3.set("platform", "stm32") ident3.set("family", "f1") self.assertEqual(hash(ident3), hash("familyf1platformstm32{platform}{family}")) self.assertTrue(ident != ident3) self.assertFalse(ident == ident3) self.assertNotEqual(ident, ident3)
def from_string(string): string = string.lower() # AVR platform with AT90, ATtiny, ATmega and ATxmega family if string.startswith("at"): matchString = r"at(?P<family>tiny|mega|xmega)(?P<name>\d+)" if string.startswith("at90"): matchString = r"at(?P<family>90)(?P<type>can|pwm|usb)(?P<name>\d+)-(?P<speed>\d+)(?P<package>\w+)" match = re.search(matchString, string) if match: i = DeviceIdentifier() i.set("platform", "avr") i.set("family", match.group("family").lower()) i.set("name", match.group("name").lower()) if i.family == "90": i.naming_schema = "at{family}{type}{name}-{speed}{package}" i.set("type", match.group("type").lower()) i.set("speed", match.group("speed")) i.set("package", match.group("package")) return i elif i.family in ["tiny", "mega"]: i.naming_schema = "at{family}{name}{type}-{speed}{package}" searchstr = "at" + i.family + i.name + r"(?P<type>\w*)-(?P<speed>\d*)(?P<package>\w+)" match = re.search(searchstr, string) if match: i.set("type", match.group("type").lower()) i.set("speed", match.group("speed")) i.set("package", match.group("package")) return i elif i.family == "xmega": i.naming_schema = "at{family}{name}{type}{pin}" searchstr = "at" + i.family + i.name + r"(?P<type>[A-Ea-e]?[1-5]?)(?P<package>[Bb]?[Uu]?)" match = re.search(searchstr, string) if match: if match.group("type") != "": i.set("type", match.group("type").lower()) if match.group("package") != "": i.set("pin", match.group("package")) return i return None
def from_string(string): string = string.lower() # SAM platform with SAMD, SAML, SAMC, SAM4, SAMG, SAMS, SAME, and SAMV if string.startswith("sam") or string.startswith("atsam"): matchString = r"sam(?P<family>\w)(?P<series>\d{2})(?P<pin>\w)(?P<flash>\d{2})(?P<variant>\w)?-(?P<package>\w)(?P<grade>\w)" match = re.search(matchString, string.lower()) if match: i = DeviceIdentifier( "{platform}{family}{series}{pin}{flash}{variant}-{package}{grade}" ) i.set("platform", "sam") i.set("family", match.group("family")) i.set("series", match.group("series")) i.set("pin", match.group("pin")) i.set("flash", match.group("flash")) i.set("variant", match.group("variant") or "") i.set("package", match.group("package")) i.set("grade", match.group("grade")) return i LOGGER.error("Parse Error: unknown platform. Device string: '%s'", string) exit(1)
def from_string(string): string = string.lower() # AVR platform with AT90, ATtiny, ATmega and ATxmega family if string.startswith("at"): matchString = r"at(?P<family>tiny|mega|xmega)(?P<name>\d+)" if string.startswith("at90"): matchString = r"at(?P<family>90)(?P<type>can|pwm|usb)(?P<name>\d+)" match = re.search(matchString, string) if match: i = DeviceIdentifier() i.set("platform", "avr") i.set("family", match.group("family").lower()) i.set("name", match.group("name").lower()) if i.family == "90": i.naming_schema = "at{family}{type}{name}" i.set("type", match.group("type").lower()) return i elif i.family in ["tiny", "mega"]: i.naming_schema = "at{family}{name}{type}" searchstr = "at" + i.family + i.name + r"(?P<type>\w*)-?(?P<package>\w*)" match = re.search(searchstr, string) if match: i.set("type", match.group("type").lower()) if match.group("package") != "": i.set("pin", match.group("package").lower()) i.naming_schema = i.naming_schema + "-{pin}" return i elif i.family == "xmega": i.naming_schema = "at{family}{name}{type}{pin}" searchstr = "at" + i.family + i.name + r"(?P<type>[A-Ea-e]?[1-5]?)(?P<package>[Bb]?[Uu]?)" match = re.search(searchstr, string) if match: if match.group("type") != "": i.set("type", match.group("type").lower()) if match.group("package") != "": i.set("pin", match.group("package")) return i LOGGER.error("Parse Error: unknown platform. Device string: '%s'", string) exit(1)
def from_string(string): string = string.lower() if string.startswith("stm32"): i = DeviceIdentifier( "{platform}{family}{name}{pin}{size}{package}{temperature}{variant}" ) i.set("platform", "stm32") i.set("family", string[5:7]) i.set("name", string[7:9]) i.set("pin", string[9]) i.set("size", string[10]) i.set("package", string[11]) i.set("temperature", string[12]) if len(string) >= 14: i.set("variant", string[13]) else: i.set("variant", "") return i LOGGER.error("Parse Error: unknown platform. Device string: %s", string) exit(1)
def from_string(string): string = string.lower() if string.startswith("rp"): i = DeviceIdentifier("{platform}{cores}{type}{ram}{flash}") i.set("platform", "rp") i.set("cores", string[2]) i.set("type", string[3]) i.set("ram", string[4]) i.set("flash", string[5]) i.set("family", string[2:4]) return i LOGGER.error("Parse Error: unknown platform. Device string: '%s'", string) exit(1)