-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchecks.py
91 lines (62 loc) · 2.43 KB
/
checks.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
84
85
86
87
88
89
90
91
#pylint: disable=invalid-name
import argparse
import datetime as dt
import os
import yaml
def file_exists(arg):
''' Checks whether a file exists, and returns the path if it does. '''
if not os.path.exists(arg):
msg = f'{arg} does not exist!'
raise argparse.ArgumentTypeError(msg)
return arg
def load_config_section(arg):
'''
Checks whether the config file exists and if it contains the input
section. Returns the config as a Python dict.
'''
if len(arg) > 2:
msg = f'{len(arg)} arguments were provided for config. Only 2 allowed!'
raise argparse.ArgumentTypeError(msg)
file_name = file_exists(arg[0])
section_name = arg[1] if len(arg) == 2 else None
# Load the YAML file into a dictionary
with open(file_name, 'r') as fn:
cfg = yaml.load(fn, Loader=yaml.Loader)
err_msg = 'Section {section_name} does not exist in top level of {file_name}'
if section_name:
if isinstance(section_name, str):
section_name = [section_name]
# Support multi-layer configurations and single level
for sect in section_name:
try:
cfg = cfg[sect]
except KeyError:
try:
cfg = cfg[sect.lower()]
except:
raise KeyError(err_msg.format(section_name=sect, file_name=file_name))
return [cfg, section_name]
def load_config_file(arg):
'''
Check to ensure that the provided config file exists. If it does, load it
with YAML's safe loader and return the corresponding Python dict.
'''
# Check for existence of file
arg = file_exists(arg)
# Load the yaml config and return the Python dict
with open(arg, 'r') as fn:
ret = yaml.safe_load(fn)
return ret
def load_str(arg):
''' Load a dict string safely using YAML. Return the resulting dict. '''
return yaml.load(arg, Loader=yaml.SafeLoader)
def to_datetime(arg):
''' Return a datetime object from input in the form YYYYMMDDHH[mm[ss]]. '''
arg_len = len(arg)
if arg_len not in [10, 12, 14]:
msg = f'{arg} does not conform to input format YYYYMMDDHH[MM[SS]]'
raise argparse.ArgumentTypeError(msg)
# Use a subset of the string corresponding to the input length of the string
# 2 chosen here since Y is a 4 char year.
date_format = '%Y%m%d%H%M%S'[0:arg_len-2]
return dt.datetime.strptime(arg, date_format)