array-split 0.6.5__zip

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.
@@ -0,0 +1,14 @@
1
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this
2
+ software and associated documentation files (the "Software"), to deal in the Software
3
+ without restriction, including without limitation the rights to use, copy, modify, merge,
4
+ publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
5
+ to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or
8
+ substantial portions of the Software.
9
+
10
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
11
+ BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
12
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
13
+ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
14
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,242 @@
1
+ """
2
+ =====================================
3
+ The :mod:`array_split.logging` Module
4
+ =====================================
5
+
6
+ Default initialisation of python logging.
7
+
8
+
9
+ Some simple wrappers of python built-in :mod:`logging` module
10
+ for :mod:`array_split` logging.
11
+
12
+ .. currentmodule:: array_split.logging
13
+
14
+ Classes and Functions
15
+ =====================
16
+
17
+ .. autosummary::
18
+ :toctree: generated/
19
+
20
+ SplitStreamHandler - A :obj:`logging.StreamHandler` which splits errors and warnings to *stderr*.
21
+ initialise_loggers - Initialises handlers and formatters for loggers.
22
+ get_formatter - "Returns :obj:`logging.Formatter` with time prefix string.
23
+ """
24
+
25
+ # pylint: disable=unused-wildcard-import
26
+ # pylint: disable=wildcard-import
27
+ # pylint: disable=invalid-name
28
+
29
+ from __future__ import absolute_import
30
+
31
+ import sys
32
+ import logging as _builtin_logging
33
+ from logging import * # noqa: F401,F403
34
+ from .license import license as _license, copyright as _copyright, version as _version
35
+
36
+ __author__ = "Shane J. Latham"
37
+ __license__ = _license()
38
+ __copyright__ = _copyright()
39
+ __version__ = _version()
40
+
41
+
42
+ class _Python2SplitStreamHandler(_builtin_logging.Handler):
43
+
44
+ """
45
+ A python :obj:`logging.handlers` :samp:`Handler` class for
46
+ splitting logging messages to different streams depending on
47
+ the logging-level.
48
+ """
49
+
50
+ def __init__(self, outstr=sys.stdout, errstr=sys.stderr, splitlevel=_builtin_logging.WARNING):
51
+ """
52
+ Initialise with a pair of streams and a threshold level which determines
53
+ the stream where the messages are writting.
54
+
55
+ :type outstr: file-like
56
+ :param outstr: Logging messages are written to this stream if
57
+ the message level is less than :samp:`self.splitLevel`.
58
+ :type errstr: stream
59
+ :param errstr: Logging messages are written to this stream if
60
+ the message level is greater-than-or-equal-to :samp:`self.splitLevel`.
61
+ :type splitlevel: int
62
+ :param splitlevel: Logging level threshold determining split streams for log messages.
63
+ """
64
+ self.outStream = outstr
65
+ self.errStream = errstr
66
+ self.splitLevel = splitlevel
67
+ _builtin_logging.Handler.__init__(self)
68
+
69
+ def emit(self, record):
70
+ """
71
+ Mostly copy-paste from :obj:`logging.StreamHandler`.
72
+ """
73
+ try:
74
+ msg = self.format(record)
75
+ if record.levelno < self.splitLevel:
76
+ stream = self.outStream
77
+ else:
78
+ stream = self.errStream
79
+ fs = "%s\n"
80
+
81
+ try:
82
+ if (isinstance(msg, unicode) and # noqa: F405
83
+ getattr(stream, 'encoding', None)):
84
+ ufs = fs.decode(stream.encoding)
85
+ try:
86
+ stream.write(ufs % msg)
87
+ except UnicodeEncodeError:
88
+ stream.write((ufs % msg).encode(stream.encoding))
89
+ else:
90
+ stream.write(fs % msg)
91
+ except UnicodeError:
92
+ stream.write(fs % msg.encode("UTF-8"))
93
+
94
+ stream.flush()
95
+ except (KeyboardInterrupt, SystemExit):
96
+ raise
97
+ except Exception:
98
+ self.handleError(record)
99
+
100
+
101
+ class _Python3SplitStreamHandler(_builtin_logging.Handler):
102
+
103
+ """
104
+ A python :obj:`logging.handlers` :samp:`Handler` class for
105
+ splitting logging messages to different streams depending on
106
+ the logging-level.
107
+ """
108
+
109
+ terminator = '\n'
110
+
111
+ def __init__(self, outstr=sys.stdout, errstr=sys.stderr, splitlevel=_builtin_logging.WARNING):
112
+ """
113
+ Initialise with a pair of streams and a threshold level which determines
114
+ the stream where the messages are writting.
115
+
116
+ :type outstr: file-like
117
+ :param outstr: Logging messages are written to this stream if
118
+ the message level is less than :samp:`self.splitLevel`.
119
+ :type errstr: stream
120
+ :param errstr: Logging messages are written to this stream if
121
+ the message level is greater-than-or-equal-to :samp:`self.splitLevel`.
122
+ :type splitlevel: int
123
+ :param splitlevel: Logging level threshold determining split streams for log messages.
124
+ """
125
+ self.outStream = outstr
126
+ self.errStream = errstr
127
+ self.splitLevel = splitlevel
128
+ _builtin_logging.Handler.__init__(self)
129
+
130
+ def flush(self):
131
+ """
132
+ Flushes the stream.
133
+ """
134
+ self.acquire()
135
+ try:
136
+ if self.outStream and hasattr(self.outStream, "flush"):
137
+ self.outStream.flush()
138
+ if self.errStream and hasattr(self.errStream, "flush"):
139
+ self.errStream.flush()
140
+ finally:
141
+ self.release()
142
+
143
+ def emit(self, record):
144
+ """
145
+ Emit a record.
146
+
147
+ If a formatter is specified, it is used to format the record.
148
+ The record is then written to the stream with a trailing newline. If
149
+ exception information is present, it is formatted using
150
+ traceback.print_exception and appended to the stream. If the stream
151
+ has an 'encoding' attribute, it is used to determine how to do the
152
+ output to the stream.
153
+ """
154
+ try:
155
+ msg = self.format(record)
156
+ if record.levelno < self.splitLevel:
157
+ stream = self.outStream
158
+ else:
159
+ stream = self.errStream
160
+ stream.write(msg)
161
+ stream.write(self.terminator)
162
+ self.flush()
163
+ except (KeyboardInterrupt, SystemExit): # pragma: no cover
164
+ raise
165
+ except Exception:
166
+ self.handleError(record)
167
+
168
+
169
+ if sys.version_info[0] <= 2:
170
+ class SplitStreamHandler(_Python2SplitStreamHandler):
171
+
172
+ """
173
+ To be replaced.
174
+ """
175
+
176
+ __doc__ = _Python2SplitStreamHandler.__doc__
177
+ else:
178
+ class SplitStreamHandler(_Python3SplitStreamHandler):
179
+
180
+ """
181
+ To be replaced.
182
+ """
183
+
184
+ __doc__ = _Python3SplitStreamHandler.__doc__
185
+
186
+
187
+ def get_formatter(prefix_string="ARRSPLT| "):
188
+ """
189
+ Returns :obj:`logging.Formatter` object which produces messages
190
+ with *time* and :samp:`prefix_string` prefix.
191
+
192
+ :type prefix_string: :obj:`str` or :samp:`None`
193
+ :param prefix_string: Prefix for all logging messages.
194
+ :rtype: :obj:`logging.Formatter`
195
+ :return: Regular formatter for logging.
196
+ """
197
+ if prefix_string is None:
198
+ prefix_string = ""
199
+ formatter = \
200
+ _builtin_logging.Formatter(
201
+ "%(asctime)s|" + prefix_string + "%(message)s",
202
+ "%H:%M:%S"
203
+ )
204
+
205
+ return formatter
206
+
207
+
208
+ def initialise_loggers(names, log_level=_builtin_logging.WARNING, handler_class=SplitStreamHandler):
209
+ """
210
+ Initialises specified loggers to generate output at the
211
+ specified logging level. If the specified named loggers do not exist,
212
+ they are created.
213
+
214
+ :type names: :obj:`list` of :obj:`str`
215
+ :param names: List of logger names.
216
+ :type log_level: :obj:`int`
217
+ :param log_level: Log level for messages, typically
218
+ one of :obj:`logging.DEBUG`, :obj:`logging.INFO`, :obj:`logging.WARN`, :obj:`logging.ERROR`
219
+ or :obj:`logging.CRITICAL`.
220
+ See :ref:`levels`.
221
+ :type handler_class: One of the :obj:`logging.handlers` classes.
222
+ :param handler_class: The handler class for output of log messages,
223
+ for example :obj:`SplitStreamHandler` or :obj:`logging.StreamHandler`.
224
+
225
+ Example::
226
+
227
+ >>> from array_split import logging
228
+ >>> logging.initialise_loggers(["my_logger",], log_level=logging.INFO)
229
+ >>> logger = logging.getLogger("my_logger")
230
+ >>> logger.info("This is info logging.")
231
+ 16:35:09|ARRSPLT| This is info logging.
232
+ >>> logger.debug("Not logged at logging.INFO level.")
233
+ >>>
234
+
235
+ """
236
+ frmttr = get_formatter()
237
+ for name in names:
238
+ logr = _builtin_logging.getLogger(name)
239
+ handler = handler_class()
240
+ handler.setFormatter(frmttr)
241
+ logr.addHandler(handler)
242
+ logr.setLevel(log_level)