-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathhelpers.py
More file actions
64 lines (50 loc) · 1.71 KB
/
helpers.py
File metadata and controls
64 lines (50 loc) · 1.71 KB
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
import argparse
import logging
import os
import pathlib
from functools import partial
from urllib.parse import urlparse
def setup_logger(name: str | None = None, level: int = logging.DEBUG):
"""Standardize our logging.
Configures the root logger to prefix messages with a timestamp
and to output the log level we want to see by default.
params:
name: logger hierarchy or root logger
level: default log level (DEBUG)
"""
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S %z",
level=level,
)
return logging.getLogger(name)
def boolean_args(val):
normalised = str(val).upper()
if normalised in ["YES", "TRUE", "T", "1"]:
return True
elif normalised in ["NO", "FALSE", "F", "N", "0"]:
return False
else:
raise argparse.ArgumentTypeError("boolean expected")
def _valid_url(value):
parsed = urlparse(value)
if not all([parsed.scheme, parsed.netloc]):
raise argparse.ArgumentTypeError(f"Invalid URL: '{value}'")
return value
def parser_nautobot_args(parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
parser.add_argument(
"--nautobot_url",
type=_valid_url,
required=False,
help="Nautobot API %(default)s",
default="http://nautobot-default.nautobot.svc.cluster.local",
)
parser.add_argument("--nautobot_token", type=str, required=False)
return parser
comma_list_args = partial(str.split, sep=",")
def credential(subpath, item):
ref = (
pathlib.Path(os.getenv("SECRETS_DIR", "/etc")).joinpath(subpath).joinpath(item)
)
with ref.open() as f:
return f.read().strip()