-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIpv4.py
83 lines (67 loc) · 2.23 KB
/
Ipv4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""
@Copyrights 2020 Rakesh Kumar T
github: rakesht2499
"""
from ip_model.IpObject import IpObject
from ip_model.util.IpTree import IpTree
from ip_model.util.Validator import Validator
from ip_model.util.Common import to_number
validate = Validator.validate
validate_cidr = Validator.validate_cidr
class Ipv4(IpObject):
def __init__(self):
"""
Creates an object of :class:`ip_model.util.IpTree.IpTree`
"""
self.head = IpTree()
@validate
def add(self, ip: str) -> bool:
"""
Adds the given valid IPv4
:param ip: An Ipv4 as String
:returns: True for every new addition
:raises :class:`ip_model.Exceptions.InvalidIpException` for invalid IPv4
"""
num_ip = to_number(ip)
self.head.add(num_ip)
return True
@validate_cidr
def add_cidr(self, cidr) -> bool:
"""
Adds the given valid IPv4 CIDR
:param cidr: String in CIDR notation
:return: True
:raises :class:`ip_model.Exceptions.InvalidIpException` for invalid IPv4 CIDR
"""
return self._insert_cidr(cidr)
@validate
def remove(self, ip) -> bool:
"""
Removed the given valid IPv4
:param ip: An Ipv4 as String
:returns: str: The IP which is removed
:raises :class:`ip_model.Exceptions.InvalidIpException` for invalid IPv4
"""
num_ip = to_number(ip)
self.head.remove(num_ip)
return ip
@validate_cidr
def remove_cidr(self, cidr) -> bool:
"""
Removes the given valid IPv4 CIDR
:param cidr: String in CIDR notation
:return: str: CIDR range which is removed
:raises :class:`ip_model.Exceptions.InvalidIpException` for invalid IPv4 CIDR
"""
return self._remove_cidr(cidr)
@validate
def is_present(self, ip) -> bool:
"""
Checks if a given valid IPv4 is present or not
:param ip: An Ipv4 as String
:returns: True, if the element is present
False, if the element is not present
:raises :class:`ip_model.Exceptions.InvalidIpException` for invalid IPv4
"""
num_ip = to_number(ip)
return self.head.is_present(num_ip)