cdapython 2.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cdapython/__init__.py +7 -0
- cdapython/application_utilities.py +54 -0
- cdapython/config/__init__.py +0 -0
- cdapython/config/logger_default_config.yml +19 -0
- cdapython/discover.py +1412 -0
- cdapython/get_data.py +1174 -0
- cdapython/logging_wrappers.py +186 -0
- cdapython/summarize.py +1801 -0
- cdapython/validation.py +622 -0
- cdapython-2.0.0.dist-info/LICENSE +201 -0
- cdapython-2.0.0.dist-info/METADATA +21 -0
- cdapython-2.0.0.dist-info/RECORD +13 -0
- cdapython-2.0.0.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import logging.config
|
|
3
|
+
import os
|
|
4
|
+
import sys
|
|
5
|
+
import yaml
|
|
6
|
+
|
|
7
|
+
#############################################################################################################################
|
|
8
|
+
#
|
|
9
|
+
# Enable console logging (on by default, but can be turned off).
|
|
10
|
+
#
|
|
11
|
+
#############################################################################################################################
|
|
12
|
+
|
|
13
|
+
def enable_console_logging():
|
|
14
|
+
"""
|
|
15
|
+
Enable console logging (log messages are written to standard output: this feature is enabled by default, but can be turned off).
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
os.environ['__CDA_LOG_TO_CONSOLE'] = 'True'
|
|
19
|
+
|
|
20
|
+
#############################################################################################################################
|
|
21
|
+
#
|
|
22
|
+
# Disable console logging.
|
|
23
|
+
#
|
|
24
|
+
#############################################################################################################################
|
|
25
|
+
|
|
26
|
+
def disable_console_logging():
|
|
27
|
+
"""
|
|
28
|
+
Prevent log messages from being displayed to standard output.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
os.environ['__CDA_LOG_TO_CONSOLE'] = 'False'
|
|
32
|
+
|
|
33
|
+
#############################################################################################################################
|
|
34
|
+
#
|
|
35
|
+
# Enable file logging.
|
|
36
|
+
#
|
|
37
|
+
#############################################################################################################################
|
|
38
|
+
|
|
39
|
+
def enable_file_logging( filename='cdapython_log.txt' ):
|
|
40
|
+
"""
|
|
41
|
+
Write log messages to `filename`. This does not prevent log messages from being written to standard output as well:
|
|
42
|
+
see disable_console_logging() to switch that feature off if desired.
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
os.environ['__CDA_LOG_TO_FILE'] = filename
|
|
46
|
+
|
|
47
|
+
#############################################################################################################################
|
|
48
|
+
#
|
|
49
|
+
# Disable file logging (off by default, but can be turned on).
|
|
50
|
+
#
|
|
51
|
+
#############################################################################################################################
|
|
52
|
+
|
|
53
|
+
def disable_file_logging():
|
|
54
|
+
"""
|
|
55
|
+
Stop writing log messages to a local file.
|
|
56
|
+
"""
|
|
57
|
+
|
|
58
|
+
os.environ['__CDA_LOG_TO_FILE'] = ''
|
|
59
|
+
|
|
60
|
+
#############################################################################################################################
|
|
61
|
+
#
|
|
62
|
+
# get_logger(): Returns logger instance that uses config file settings and optional user config inputs to initialize
|
|
63
|
+
#
|
|
64
|
+
#############################################################################################################################
|
|
65
|
+
|
|
66
|
+
def get_logger() -> logging.Logger:
|
|
67
|
+
"""
|
|
68
|
+
Returns a logging.Logger instance initalized according to settings in config/logger_default_config.yml.
|
|
69
|
+
|
|
70
|
+
Returns:
|
|
71
|
+
logging.Logger object: an interface that can be used to output messages of varying granularity/severity.
|
|
72
|
+
"""
|
|
73
|
+
|
|
74
|
+
# Establish the current log level. If none exists, default to `logging.WARNING`.
|
|
75
|
+
|
|
76
|
+
if '__CDA_LOG_LEVEL' not in os.environ:
|
|
77
|
+
os.environ['__CDA_LOG_LEVEL'] = 'WARNING'
|
|
78
|
+
|
|
79
|
+
level = os.environ['__CDA_LOG_LEVEL']
|
|
80
|
+
|
|
81
|
+
# Echo log messages to standard output? (Default: yes)
|
|
82
|
+
|
|
83
|
+
if '__CDA_LOG_TO_CONSOLE' not in os.environ:
|
|
84
|
+
os.environ['__CDA_LOG_TO_CONSOLE'] = 'True'
|
|
85
|
+
|
|
86
|
+
# Echo log messages to a file? (Default: no, i.e. __CDA_LOG_TO_FILE == None)
|
|
87
|
+
|
|
88
|
+
if '__CDA_LOG_TO_FILE' not in os.environ:
|
|
89
|
+
os.environ['__CDA_LOG_TO_FILE'] = ''
|
|
90
|
+
|
|
91
|
+
# Load the default logger configuration.
|
|
92
|
+
|
|
93
|
+
parent_dir = os.path.dirname( os.path.abspath( __file__ ) )
|
|
94
|
+
logger_default_config_file = os.path.join( parent_dir, 'config', 'logger_default_config.yml' )
|
|
95
|
+
|
|
96
|
+
with open( logger_default_config_file ) as IN:
|
|
97
|
+
logger_configuration = yaml.safe_load( IN )
|
|
98
|
+
|
|
99
|
+
# Set the log level in the configuration object to whatever the current environment dictates.
|
|
100
|
+
|
|
101
|
+
logger_configuration['loggers']['default']['level'] = level
|
|
102
|
+
|
|
103
|
+
# Modify logger configuration defaults according to user-modified session-level settings.
|
|
104
|
+
|
|
105
|
+
if os.environ['__CDA_LOG_TO_CONSOLE'] == 'False':
|
|
106
|
+
logger_configuration['loggers']['default']['handlers'].remove( 'console' )
|
|
107
|
+
|
|
108
|
+
if os.environ['__CDA_LOG_TO_FILE'] != '':
|
|
109
|
+
logger_configuration['loggers']['default']['handlers'].append( 'file' )
|
|
110
|
+
logger_configuration['handlers']['file']['filename'] = os.environ['__CDA_LOG_TO_FILE']
|
|
111
|
+
|
|
112
|
+
# Make sure we didn't remove all possible handlers.
|
|
113
|
+
|
|
114
|
+
if len( logger_configuration['loggers']['default']['handlers'] ) == 0:
|
|
115
|
+
print( 'ERROR: get_logger(): console and file output both disabled; can\'t create logger.', file=sys.stderr )
|
|
116
|
+
return
|
|
117
|
+
|
|
118
|
+
logging.config.dictConfig( logger_configuration )
|
|
119
|
+
|
|
120
|
+
logger = logging.getLogger('default')
|
|
121
|
+
|
|
122
|
+
return logger
|
|
123
|
+
|
|
124
|
+
#############################################################################################################################
|
|
125
|
+
#
|
|
126
|
+
# get_valid_log_levels(): Returns list of log level strings that can be used to set_log_level
|
|
127
|
+
#
|
|
128
|
+
#############################################################################################################################
|
|
129
|
+
|
|
130
|
+
def get_valid_log_levels():
|
|
131
|
+
"""
|
|
132
|
+
Returns list of log level strings that can be passed to set_log_level().
|
|
133
|
+
|
|
134
|
+
Returns:
|
|
135
|
+
set of module-defined strings: all valid human-readable labels for log levels that can be passed to set_log_level().
|
|
136
|
+
"""
|
|
137
|
+
return { 'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL' }
|
|
138
|
+
|
|
139
|
+
#############################################################################################################################
|
|
140
|
+
#
|
|
141
|
+
# set_log_level(): Changes the current log level
|
|
142
|
+
#
|
|
143
|
+
#############################################################################################################################
|
|
144
|
+
|
|
145
|
+
def set_log_level( level=None ):
|
|
146
|
+
"""
|
|
147
|
+
Changes the current log level. A list of valid values is returned by get_valid_log_levels().
|
|
148
|
+
"""
|
|
149
|
+
if level is None:
|
|
150
|
+
print( f"ERROR: set_log_level(): log level cannot be null. Try something like 'set_log_level( 'debug' )'.", file=sys.stderr )
|
|
151
|
+
return
|
|
152
|
+
|
|
153
|
+
level = level.upper()
|
|
154
|
+
|
|
155
|
+
if level not in get_valid_log_levels():
|
|
156
|
+
print( f"ERROR: set_log_level(): log level '{level}' invalid. Try get_valid_log_levels() for a list of valid level names.", file=sys.stderr )
|
|
157
|
+
return
|
|
158
|
+
|
|
159
|
+
os.environ['__CDA_LOG_LEVEL'] = level
|
|
160
|
+
|
|
161
|
+
#############################################################################################################################
|
|
162
|
+
#
|
|
163
|
+
# get_log_level(): Returns the current log level
|
|
164
|
+
#
|
|
165
|
+
#############################################################################################################################
|
|
166
|
+
|
|
167
|
+
def get_log_level():
|
|
168
|
+
"""
|
|
169
|
+
Returns the current user-specified log level, if set, or the default log level, if not.
|
|
170
|
+
"""
|
|
171
|
+
|
|
172
|
+
level = None
|
|
173
|
+
|
|
174
|
+
if '__CDA_LOG_LEVEL' in os.environ:
|
|
175
|
+
|
|
176
|
+
level = os.environ['__CDA_LOG_LEVEL']
|
|
177
|
+
|
|
178
|
+
if level is None:
|
|
179
|
+
|
|
180
|
+
return 'WARNING'
|
|
181
|
+
|
|
182
|
+
else:
|
|
183
|
+
|
|
184
|
+
return level
|
|
185
|
+
|
|
186
|
+
|