forked from jaraco/cssutils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrorhandler.py
More file actions
124 lines (101 loc) · 3.68 KB
/
errorhandler.py
File metadata and controls
124 lines (101 loc) · 3.68 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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"""cssutils ErrorHandler
ErrorHandler
used as log with usual levels (debug, info, warn, error)
if instanciated with ``raiseExceptions=True`` raises exeptions instead
of logging
log
defaults to instance of ErrorHandler for any kind of log message from
lexerm, parser etc.
- raiseExceptions = [False, True]
- setloglevel(loglevel)
"""
__all__ = ['ErrorHandler']
import logging
import urllib.error
import urllib.parse
import urllib.request
import xml.dom
class _ErrorHandler:
"""
handles all errors and log messages
"""
def __init__(self, log, defaultloglevel=logging.INFO, raiseExceptions=True):
"""
inits log if none given
log
for parse messages, default logs to sys.stderr
defaultloglevel
if none give this is logging.DEBUG
raiseExceptions
- True: Errors will be raised e.g. during building
- False: Errors will be written to the log, this is the
default behaviour when parsing
"""
# may be disabled during setting of known valid items
self.enabled = True
if log:
self._log = log
else:
import sys
self._log = logging.getLogger('CSSUTILS')
hdlr = logging.StreamHandler(sys.stderr)
formatter = logging.Formatter('%(levelname)s\t%(message)s')
hdlr.setFormatter(formatter)
self._log.addHandler(hdlr)
self._log.setLevel(defaultloglevel)
self.raiseExceptions = raiseExceptions
def __getattr__(self, name):
"use self._log items"
calls = ('debug', 'info', 'warn', 'error', 'critical', 'fatal')
other = ('setLevel', 'getEffectiveLevel', 'addHandler', 'removeHandler')
if name in calls:
if name == 'warn':
name = 'warning'
self._logcall = getattr(self._log, name)
return self.__handle
elif name in other:
return getattr(self._log, name)
else:
raise AttributeError('(errorhandler) No Attribute %r found' % name)
def __handle(
self, msg='', token=None, error=xml.dom.SyntaxErr, neverraise=False, args=None
):
"""
handles all calls
logs or raises exception
"""
if self.enabled:
if error is None:
error = xml.dom.SyntaxErr
line, col = None, None
if token:
if isinstance(token, tuple):
value, line, col = token[1], token[2], token[3]
else:
value, line, col = token.value, token.line, token.col
msg = f'{msg} [{line}:{col}: {value}]'
if error and self.raiseExceptions and not neverraise:
if isinstance(error, urllib.error.HTTPError) or isinstance(
error, urllib.error.URLError
):
raise
elif issubclass(error, xml.dom.DOMException):
error.line = line
error.col = col
raise error(msg)
else:
self._logcall(msg)
def setLog(self, log):
"""set log of errorhandler's log"""
self._log = log
class ErrorHandler(_ErrorHandler):
"Singleton, see _ErrorHandler"
instance = None
def __init__(self, log=None, defaultloglevel=logging.INFO, raiseExceptions=True):
if ErrorHandler.instance is None:
ErrorHandler.instance = _ErrorHandler(
log=log,
defaultloglevel=defaultloglevel,
raiseExceptions=raiseExceptions,
)
self.__dict__ = ErrorHandler.instance.__dict__