#!/usr/bin/python3
import argparse
+import datetime
import logging
import os
-import string_utils
+# This module is commonly used by others in here and should avoid
+# taking any unnecessary dependencies back on them.
logger = logging.getLogger(__name__)
def valid_bool(v):
if isinstance(v, bool):
return v
- return string_utils.to_bool(v)
+ from string_utils import to_bool
+ return to_bool(v)
def valid_ip(ip: str) -> str:
- s = string_utils.extract_ip_v4(ip.strip())
+ from string_utils import extract_ip_v4
+ s = extract_ip_v4(ip.strip())
if s is not None:
return s
msg = f"{ip} is an invalid IP address"
def valid_mac(mac: str) -> str:
- s = string_utils.extract_mac_address(mac)
+ from string_utils import extract_mac_address
+ s = extract_mac_address(mac)
if s is not None:
return s
msg = f"{mac} is an invalid MAC address"
def valid_percentage(num: str) -> float:
+ num = num.strip('%')
n = float(num)
if 0.0 <= n <= 100.0:
return n
msg = f"{filename} was not found and is therefore invalid."
logger.warning(msg)
raise argparse.ArgumentTypeError(msg)
+
+
+def valid_date(txt: str) -> datetime.date:
+ from string_utils import to_date
+ date = to_date(txt)
+ if date is not None:
+ return date
+ msg = f'Cannot parse argument as a date: {txt}'
+ logger.warning(msg)
+ raise argparse.ArgumentTypeError(msg)
+
+
+def valid_datetime(txt: str) -> datetime.datetime:
+ from string_utils import to_datetime
+ dt = to_datetime(txt)
+ if dt is not None:
+ return dt
+ msg = f'Cannot parse argument as datetime: {txt}'
+ logger.warning(msg)
+ raise argparse.ArgumentTypeError(msg)