ns3 metadata and design test in progress
[nepi.git] / src / nepi / util / validation.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import ipaddr
5 import re
6
7 def is_enum(attribute, value):
8     return isinstance(value, str) and value in attribute.allowed
9
10 def is_bool(attribute, value):
11     return isinstance(value, bool)
12
13 def is_double(attribute, value):
14     return isinstance(value, float)
15
16 def is_integer(attribute, value):
17     return isinstance(value, int)
18
19 def is_string(attribute, value):
20     return isinstance(value, str)
21
22 def is_time(attribute, value):
23     return isinstance(value, str) # TODO: Missing validation!
24
25 # TODO: Allow netrefs!
26 def is_ip4_address(attribute, value):
27     try:
28         ipaddr.IPv4Address(value)
29     except ipaddr.AddressValueError:
30         return False
31     return True
32
33 # TODO: Allow netrefs!
34 def is_ip6_address(attribute, value):
35     try:
36         ipaddr.IPv6Address(value)
37     except ipaddr.AddressValueError:
38         return False
39     return True
40
41 def is_mac_address(attribute, value):
42     regex = r'^([0-9a-zA-Z]{0,2}:)*[0-9a-zA-Z]{0,2}'
43     found = re.search(regex, value)
44     if not found or value.count(':') != 5:
45         return False
46     return True
47