Skip to content

Commit ab183e5

Browse files
author
Oliver Scott
committed
Decorator for controlling the rdkit logger.
1 parent 136b2ca commit ab183e5

1 file changed

Lines changed: 95 additions & 0 deletions

File tree

scaffoldgraph/utils/logging.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
"""
2+
scaffoldgraph.utils.logging
3+
4+
Utilities for dealing with rdkit logging.
5+
"""
6+
7+
import functools
8+
9+
from rdkit import __version__ as rdkversion
10+
from rdkit import RDLogger, rdBase
11+
12+
13+
DEFAULT_RDLOGGER_STATUS = {
14+
'rdApp.debug': True,
15+
'rdApp.info': True,
16+
'rdApp.warning': True,
17+
'rdApp.error': True
18+
}
19+
20+
21+
def get_rdlogger_status():
22+
"""dict : Return the status of the rdlogger."""
23+
status_dict = {}
24+
for status in rdBase.LogStatus().split('\n'):
25+
level, state = status.split(':')
26+
status_dict[level] = True if state == 'enabled' else False
27+
return status_dict
28+
29+
30+
def set_rdlogger_status(status_dict):
31+
"""Set the state of the rdlogger."""
32+
for level, state in status_dict.items():
33+
if state is True:
34+
rdBase.EnableLog(level)
35+
else:
36+
rdBase.DisableLog(level)
37+
38+
39+
def supress_rdlogger(
40+
suppress_info=True,
41+
suppress_warning=True,
42+
suppress_error=True,
43+
suppress_debug=True
44+
):
45+
"""Decorator for controlling the output level of the rdkit logger.
46+
47+
Useful for supressing the output of noisy functions related to
48+
the rdkit logger. The previous status of the logger is returned
49+
after the function has been executed.
50+
51+
Parameters
52+
----------
53+
suppress_info : bool, optional
54+
Suppress logs from rdApp.info. The default is True.
55+
suppress_warning : bool, optional
56+
Suppress logs from rdApp.warning. The default is True.
57+
suppress_error : bool, optional
58+
Suppress logs from rdApp.error. The default is True.
59+
suppress_debug : bool, optional
60+
Suppress logs from rdApp.debug. The default is True.
61+
62+
Returns
63+
-------
64+
decorator : function
65+
66+
Notes
67+
-----
68+
The prior state of the logger can only be returned in the newer
69+
versions of rdkit (>= '2020.09.01'). In previous versions the
70+
logger status is returned to its default state.
71+
72+
"""
73+
rdlogger, altered_status = RDLogger.logger(), {}
74+
altered_status['rdApp.info'] = not suppress_info
75+
altered_status['rdApp.warning'] = not suppress_warning
76+
altered_status['rdApp.error'] = not suppress_error
77+
altered_status['rdApp.debug'] = not suppress_debug
78+
79+
def decorator(func):
80+
@functools.wraps(func)
81+
def wrap_supress(*args, **kwargs):
82+
# rdkit version compatability.
83+
prior_status = DEFAULT_RDLOGGER_STATUS
84+
if rdkversion >= '2020.09.01':
85+
prior_status = get_rdlogger_status()
86+
set_rdlogger_status(altered_status)
87+
try: # restore status of rdlogger on failure.
88+
result = func(*args, **kwargs)
89+
except Exception as e:
90+
set_rdlogger_status(prior_status)
91+
raise e
92+
set_rdlogger_status(prior_status)
93+
return result
94+
return wrap_supress
95+
return decorator

0 commit comments

Comments
 (0)