def _sanitize_oids(oids): # type: (List[Union[str, OIDSpec, CompatibleOIDEnd]]) -> List[Union[OIDSpec, CompatibleOIDEnd]] if not isinstance(oids, list): raise TypeError("oids must be a list") # remove the "int" once CompatibleOIDEnd is not needed anymore typed_oids = [ oid if isinstance(oid, (OIDSpec, OIDEnd, int)) else OIDSpec(oid) for oid in oids ] # remaining validations only regard true OIDSpec objects oid_specs = [o for o in typed_oids if isinstance(o, OIDSpec)] if len(oid_specs) < 2: return typed_oids for oid in oid_specs: if str(oid).startswith('.'): raise ValueError("column %r must not start with '.'" % (oid, )) # make sure the base is as long as possible heads_counter = collections.Counter( str(oid).split('.', 1)[0] for oid in oid_specs) head, count = max(heads_counter.items(), key=lambda x: x[1]) if count == len(oid_specs) and all(str(o) != head for o in oid_specs): raise ValueError("base can be extended by '.%s'" % head) return typed_oids
def test_snmptree(base, oids): tree = SNMPTree(base=base, oids=oids) assert tree.base == OIDSpec(base) assert isinstance(tree.oids, list) for oid in tree.oids: assert isinstance(oid, (OIDSpec, OIDEnd))
def _sanitize_base(base): # type: (str) -> OIDSpec oid_base = OIDSpec(base) if not str(oid_base).startswith('.'): raise ValueError("%r must start with '.'" % (oid_base, )) return oid_base