def rename_attributes_with_index(cls, attrs: List[Attr], rename: List[Attr]): """Append the next available index number to all the rename attributes names.""" for index in range(1, len(rename)): num = 1 name = rename[index].name while any(alnum(attr.name) == alnum(f"{name}_{num}") for attr in attrs): num += 1 rename[index].name = f"{name}_{num}"
def resolve_imports(self): """Walk the import qualified names, check for naming collisions and add the necessary code generator import instance.""" existing = { alnum(local_name(qname)) for qname in self.class_map.keys() } for qname in self.import_classes(): package = self.find_package(qname) name = alnum(local_name(qname)) exists = name in existing existing.add(name) self.add_import(qname=qname, package=package, exists=exists)
def resolve_conflicts(self): """Find classes with the same case insensitive qualified name and rename them.""" groups = group_by(self.container.iterate(), lambda x: alnum(x.qname)) for classes in groups.values(): if len(classes) > 1: self.rename_classes(classes)
def process_duplicate_attribute_names(cls, attrs: List[Attr]) -> None: """Sanitize duplicate attribute names that might exist by applying rename strategies.""" grouped = group_by(attrs, lambda attr: alnum(attr.name)) for items in grouped.values(): total = len(items) if total == 2 and not items[0].is_enumeration: cls.rename_attribute_by_preference(*items) elif total > 1: cls.rename_attributes_with_index(attrs, items)
def next_qname(self, namespace: str, name: str) -> str: """Append the next available index number for the given namespace and local name.""" index = 0 reserved = set(map(alnum, self.container.data.keys())) while True: index += 1 qname = build_qname(namespace, f"{name}_{index}") if alnum(qname) not in reserved: return qname
def test_alnum(self): self.assertEqual("foo1", alnum("foo 1")) self.assertEqual("foo1", alnum(" foo_1 ")) self.assertEqual("foo1", alnum("\tfoo*1")) self.assertEqual("foo1", alnum(" foo*1")) self.assertEqual("βιβλίο1", alnum(" βιβλίο*1"))