timmerk / ipaddr-py

Automatically exported from code.google.com/p/ipaddr-py
0 stars 0 forks source link

support tuple constructor argument for IPAddress/IPNetwork #53

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I often have ipaddresses as tuples (from e.g. SNMP OIDS).

feature=ipaddr.IPAddress(tuple([127,0,0,1]))
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "ipaddr.py", line 74, in IPAddress
    raise ValueError('%r does not appear to be an IPv4 or IPv6 address' %
TypeError: not all arguments converted during string formatting

It would be handy to be able to pass tuples (and perhaps lists) to the
contructor:
---8<---
class OIDip(object):
    def __init__(self, tupl):
        """For the given tuple interpreted as dotted quad, set IPAddress."""
        if type(tupl) == type(tuple()):
            if len(tupl) == 4:
                self.ipaddr = ipaddr.IPAddress(".".join([str(i) for i in
tupl]), 4)
            elif len(tupl) == 16:
                ii = 0
                for p in range(0,16):
                    ii |= tupl[p] << (128 - 8 - (p * 8))
                self.ipaddr = ipaddr.IPAddress(ii, 6)
            else:
                raise TypeError("Unhandled tuple length %d for IPAddress,
neither IPv4 nor IPv6" % (len(tupl)))
            self.tupl = tupl
        elif type(tupl) == type(str()):
            if "." in tupl:
                self.tupl = tuple([int(ii) for ii in tupl.split(".")])
            elif ":" in tupl:
                tmp = []
                for x in [int(ii, 16) for ii in
ipaddr.IPAddress(tupl).exploded.split(":")]:
                    tmp.append(x>>8)
                    tmp.append(x&0x00ff)
                self.tupl = tuple(tmp)
            else:
                raise TypeError("Unhandled string flavour for IPAddress,
neither IPv4 nor IPv6")
        else:
            raise TypeError("Unhandled type for IPAddress, neither tuple
nor string")
    def __repr__(self):
        return str(self.ipaddr)
    def __call__(self):
        return self.ipaddr
    def tuple(self):
        return self.tupl

class OIDnet(object):
    def __init__(self, tupl, prefixlen):
        """For the given tuple interpreted as dotted quad, set IPNetwork."""
        if type(tupl) == type(tuple()):
            if len(tupl) == 4:
                self.ipaddr = ipaddr.IPNetwork(".".join([str(i) for i in
tupl]) + "/" + str(prefixlen), 4)
            elif len(tupl) == 16:
                i = 0
                for x in range(0,16):
                    i |= tupl[x] << (128 - 8 - (x * 8))
                self.ipaddr = ipaddr.IPNetwork(str(ipaddr.IPAddress(i,6)) +
"/" + str(prefixlen), 6)
            else:
                raise TypeError("Unhandled tuple length %d for IPNetwork,
neither IPv4 nor IPv6" % (len(tupl)))
        elif type(tuple) == type(str()):
            raise TypeError("string not handled")
        else:
            raise TypeError("Unhandled type for IPNetwork, neither tuple
nor string")
        self.tupl = tupl
    def __repr__(self):
        return str(self.ipaddr)
    def __call__(self):
        return self.ipaddr
    def tuple(self):
        return self.tupl

---8<---

Original issue reported on code.google.com by rep.dot....@gmail.com on 11 Jan 2010 at 9:14

GoogleCodeExporter commented 9 years ago
the tuple creator seems to be a special case, so I don't think that it belongs 
in ipaddr 
itself.

can you explain a little more about the list creator? Is the idea that you'd 
pass it a list of 
strings and get back a list of address objects?

Original comment by pmo...@google.com on 2 Feb 2010 at 7:19

GoogleCodeExporter commented 9 years ago
Supporting tuples (and lists) in the initializer would be extremely handy for
handling SNMP OIDS:

They look like:
$ snmpwalk -v2c -c public localhost 1.3.6.1.2.1.4.20.1 | head -n1
IP-MIB::ipAdEntAddr.127.0.0.1 = IpAddress: 127.0.0.1
$ snmpwalk -On -v2c -c public localhost 1.3.6.1.2.1.4.20.1 | head -n1
.1.3.6.1.2.1.4.20.1.1.127.0.0.1 = IpAddress: 127.0.0.1

so (suppose i know that the prefixlen for 127.0.0.1 is 4):
print [int(i) for i in ".1.3.6.1.2.1.4.20.1.1.127.0.0.1".split(".")[-4:]]
[127, 0, 0, 1]
or (for handy use as a key in a dict), a tuple:
print tuple([int(i) for i in ".1.3.6.1.2.1.4.20.1.1.127.0.0.1".split(".")[-4:]])
(127, 0, 0, 1)

Note that i don't really care if the list entries are string or int, it can be
trivially both.

See?

Original comment by rep.dot....@gmail.com on 2 Feb 2010 at 8:42

GoogleCodeExporter commented 9 years ago
I don't think that this belongs in the core library, but you can take this up 
on ipaddr-
py-dev@googlegroups.com and see if other folks want it. 

for now, I'm going to close this out.

Original comment by pmo...@google.com on 5 Feb 2010 at 11:07