safer 5.0.0__py3-none-any.whl → 5.2.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.
safer/.DS_Store ADDED
Binary file
safer/__init__.py CHANGED
@@ -147,6 +147,9 @@ With `safer`
147
147
  # Either the whole file is written, or nothing
148
148
 
149
149
  """
150
+
151
+ from __future__ import annotations
152
+
150
153
  import contextlib
151
154
  import functools
152
155
  import io
@@ -162,15 +165,15 @@ __all__ = 'writer', 'open', 'closer', 'dump', 'printer'
162
165
 
163
166
 
164
167
  def writer(
165
- stream: t.Union[t.Callable, None, t.IO, Path, str] = None,
166
- is_binary: t.Optional[bool] = None,
168
+ stream: t.Callable | None | t.IO | Path | str = None,
169
+ is_binary: bool | None = None,
167
170
  close_on_exit: bool = False,
168
171
  temp_file: bool = False,
169
172
  chunk_size: int = 0x100000,
170
173
  delete_failures: bool = True,
171
- dry_run: t.Union[bool, t.Callable] = False,
174
+ dry_run: bool | t.Callable = False,
172
175
  enabled: bool = True,
173
- ) -> t.Union[t.Callable, t.IO]:
176
+ ) -> t.Callable | t.IO:
174
177
  """
175
178
  Write safely to file streams, sockets and callables.
176
179
 
@@ -231,78 +234,85 @@ def writer(
231
234
  if not enabled:
232
235
  return stream
233
236
 
234
- write: t.Optional[t.Callable]
237
+ write: t.Callable | None
235
238
 
236
- if callable(dry_run):
237
- write, dry_run = dry_run, True
239
+ if close_on_exit and stream in (sys.stdout, sys.stderr):
240
+ raise ValueError('You cannot close stdout or stderr')
238
241
 
239
- elif dry_run:
240
- write = len
242
+ if dry_run:
243
+ close_on_exit = False
241
244
 
242
- elif close_on_exit and hasattr(stream, 'write'):
243
- if temp_file and BUG_MESSAGE:
244
- raise NotImplementedError(BUG_MESSAGE)
245
+ try:
246
+ if callable(dry_run):
247
+ write, dry_run = dry_run, True
245
248
 
246
- def write(v):
247
- with stream:
248
- stream.write(v)
249
+ elif dry_run:
250
+ write = len
249
251
 
250
- else:
251
- write = getattr(stream, 'write', None)
252
+ elif close_on_exit and hasattr(stream, 'write'):
253
+ if temp_file and BUG_MESSAGE:
254
+ raise NotImplementedError(BUG_MESSAGE)
252
255
 
253
- send = getattr(stream, 'send', None)
254
- mode = getattr(stream, 'mode', None)
256
+ def write(v):
257
+ assert isinstance(stream, t.ContextManager), (stream, type(stream))
258
+ with stream:
259
+ stream.write(v)
255
260
 
256
- if dry_run:
257
- close_on_exit = False
261
+ else:
262
+ write = getattr(stream, 'write', None)
258
263
 
259
- if close_on_exit and stream in (sys.stdout, sys.stderr):
260
- raise ValueError('You cannot close stdout or stderr')
264
+ send = getattr(stream, 'send', None)
265
+ mode = getattr(stream, 'mode', None)
261
266
 
262
- if write and mode:
263
- if not set('w+a').intersection(mode):
264
- raise ValueError('Stream mode "%s" is not a write mode' % mode)
267
+ if write and mode:
268
+ if not set('w+a').intersection(mode):
269
+ raise ValueError(f'Stream mode "{mode}" is not a write mode')
265
270
 
266
- binary_mode = 'b' in mode
267
- if is_binary is not None and is_binary is not binary_mode:
268
- raise ValueError('is_binary is inconsistent with the file stream')
271
+ binary_mode = 'b' in mode
272
+ if is_binary is not None and is_binary is not binary_mode:
273
+ raise ValueError('is_binary is inconsistent with the file stream')
269
274
 
270
- is_binary = binary_mode
275
+ is_binary = binary_mode
271
276
 
272
- elif dry_run:
273
- pass
277
+ elif dry_run:
278
+ pass
274
279
 
275
- elif send and hasattr(stream, 'recv'): # It looks like a socket:
276
- if not (is_binary is None or is_binary is True):
277
- raise ValueError('is_binary=False is inconsistent with a socket')
280
+ elif send and hasattr(stream, 'recv'): # It looks like a socket:
281
+ if not (is_binary is None or is_binary is True):
282
+ raise ValueError('is_binary=False is inconsistent with a socket')
278
283
 
279
- write = send
280
- is_binary = True
284
+ write = send
285
+ is_binary = True
281
286
 
282
- elif callable(stream):
283
- write = stream
287
+ elif callable(stream):
288
+ write = stream
284
289
 
285
- else:
286
- raise ValueError('Stream is not a file, a socket, or callable')
287
-
288
- closer: _StreamCloser
289
-
290
- if temp_file:
291
- closer = _FileStreamCloser(
292
- write,
293
- close_on_exit,
294
- is_binary,
295
- temp_file,
296
- chunk_size,
297
- delete_failures,
298
- )
299
- else:
300
- closer = _MemoryStreamCloser(write, close_on_exit, is_binary)
290
+ else:
291
+ raise ValueError('Stream is not a file, a socket, or callable')
292
+
293
+ closer: _StreamCloser
294
+
295
+ if temp_file:
296
+ closer = _FileStreamCloser(
297
+ write,
298
+ close_on_exit,
299
+ is_binary,
300
+ temp_file,
301
+ chunk_size,
302
+ delete_failures,
303
+ )
304
+ else:
305
+ closer = _MemoryStreamCloser(write, close_on_exit, is_binary)
306
+
307
+ if send is write:
308
+ closer.fp.send = write
301
309
 
302
- if send is write:
303
- closer.fp.send = write
310
+ return closer.fp
304
311
 
305
- return closer.fp
312
+ except Exception:
313
+ if close_on_exit:
314
+ getattr(stream, 'close', lambda: None)()
315
+ raise
306
316
 
307
317
 
308
318
  # There's an edge case in #23 I can't yet fix, so I fail
@@ -311,18 +321,18 @@ BUG_MESSAGE = 'Sorry, safer.writer fails if temp_file (#23)'
311
321
 
312
322
 
313
323
  def open(
314
- name: t.Union[Path, str],
324
+ name: Path | str,
315
325
  mode: str = 'r',
316
326
  buffering: int = -1,
317
- encoding: t.Optional[str] = None,
318
- errors: t.Optional[str] = None,
319
- newline: t.Optional[str] = None,
327
+ encoding: str | None = None,
328
+ errors: str | None = None,
329
+ newline: str | None = None,
320
330
  closefd: bool = True,
321
- opener: t.Optional[t.Callable] = None,
331
+ opener: t.Callable | None = None,
322
332
  make_parents: bool = False,
323
333
  delete_failures: bool = True,
324
334
  temp_file: bool = False,
325
- dry_run: t.Union[bool, t.Callable] = False,
335
+ dry_run: bool | t.Callable = False,
326
336
  enabled: bool = True,
327
337
  ) -> t.IO:
328
338
  """
@@ -385,13 +395,13 @@ def open(
385
395
  name = str(name)
386
396
 
387
397
  if not isinstance(name, str):
388
- raise TypeError('`name` must be string, not %s' % type(name).__name__)
398
+ raise TypeError(f'`name` must be string, not {type(name).__name__}')
389
399
 
390
400
  name = os.path.realpath(name)
391
401
  parent = os.path.dirname(os.path.abspath(name))
392
402
  if not os.path.exists(parent):
393
403
  if not make_parents:
394
- raise IOError('Directory does not exist')
404
+ raise OSError('Directory does not exist')
395
405
  os.makedirs(parent)
396
406
 
397
407
  def simple_open():
@@ -431,7 +441,7 @@ def open(
431
441
  raise ValueError("binary mode doesn't take an errors argument")
432
442
 
433
443
  if 'x' in mode and os.path.exists(name):
434
- raise FileExistsError("File exists: '%s'" % name)
444
+ raise FileExistsError(f"File exists: '{name}'")
435
445
 
436
446
  if buffering == -1:
437
447
  buffering = io.DEFAULT_BUFFER_SIZE
@@ -447,8 +457,8 @@ def open(
447
457
 
448
458
 
449
459
  def closer(
450
- stream: t.IO, is_binary: t.Optional[bool] = None, close_on_exit: bool = True, **kwds
451
- ) -> t.Union[t.Callable, t.IO]:
460
+ stream: t.IO, is_binary: bool | None = None, close_on_exit: bool = True, **kwds
461
+ ) -> t.Callable | t.IO:
452
462
  """
453
463
  Like `safer.writer()` but with `close_on_exit=True` by default
454
464
 
@@ -460,7 +470,7 @@ def closer(
460
470
 
461
471
  def dump(
462
472
  obj,
463
- stream: t.Union[t.Callable, None, t.IO, Path, str] = None,
473
+ stream: t.Callable | None | t.IO | Path | str = None,
464
474
  dump: t.Any = None,
465
475
  **kwargs,
466
476
  ) -> t.Any:
@@ -533,8 +543,8 @@ def _get_dumper(dump: t.Any) -> t.Callable:
533
543
 
534
544
  @contextlib.contextmanager
535
545
  def printer(
536
- name: t.Union[Path, str], mode: str = 'w', *args, **kwargs
537
- ) -> t.Generator[t.Callable, None, None]:
546
+ name: Path | str, mode: str = 'w', *args, **kwargs
547
+ ) -> t.Iterator[t.Callable]:
538
548
  """
539
549
  A context manager that yields a function that prints to the opened file,
540
550
  only writing to the original file at the exit of the context,
@@ -544,7 +554,7 @@ def printer(
544
554
  Same as for `safer.open()`
545
555
  """
546
556
  if 'r' in mode and '+' not in mode:
547
- raise IOError('File not open for writing')
557
+ raise OSError('File not open for writing')
548
558
 
549
559
  if 'b' in mode:
550
560
  raise ValueError('Cannot print to a file open in binary mode')
@@ -590,7 +600,7 @@ class _Closer:
590
600
 
591
601
 
592
602
  # Wrap an existing IO class so that it calls safer at the end
593
- @functools.lru_cache()
603
+ @functools.lru_cache
594
604
  def _wrap_class(stream_cls):
595
605
  @functools.wraps(stream_cls.__exit__)
596
606
  def exit(self, *args):
@@ -1,21 +1,20 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.4
2
2
  Name: safer
3
- Version: 5.0.0
3
+ Version: 5.2.0
4
4
  Summary: 🧿 A safer writer for files and streams 🧿
5
- Home-page: https://github.com/rec/safer
6
- License: MIT
7
5
  Author: Tom Ritchford
8
- Author-email: tom@swirly.com
9
- Requires-Python: >=3.8
10
- Classifier: License :: OSI Approved :: MIT License
6
+ Author-email: Tom Ritchford <tom@swirly.com>
7
+ License-Expression: MIT
11
8
  Classifier: Programming Language :: Python :: 3
12
- Classifier: Programming Language :: Python :: 3.8
13
- Classifier: Programming Language :: Python :: 3.9
14
9
  Classifier: Programming Language :: Python :: 3.10
15
10
  Classifier: Programming Language :: Python :: 3.11
16
11
  Classifier: Programming Language :: Python :: 3.12
17
- Project-URL: Documentation, https://rec.github.io/safer
12
+ Classifier: Programming Language :: Python :: 3.13
13
+ Classifier: Programming Language :: Python :: 3.14
14
+ Requires-Python: >=3.10
15
+ Project-URL: Homepage, https://github.com/rec/safer
18
16
  Project-URL: Repository, https://github.com/rec/safer
17
+ Project-URL: Documentation, https://rec.github.io/safer
19
18
  Description-Content-Type: text/markdown
20
19
 
21
20
  # 🧿 `safer`: A safer writer 🧿
@@ -164,4 +163,3 @@ With `safer`
164
163
 
165
164
 
166
165
  ### [API Documentation](https://rec.github.io/safer#safer--api-documentation)
167
-
@@ -0,0 +1,6 @@
1
+ safer/.DS_Store,sha256=9uTAJ6op3-OCDu24y8I5d1RJelGP0F0TYAFYXHowPNM,6148
2
+ safer/__init__.py,sha256=SBfhw68Hm2WC5nkzbTPBGGS5F8nk55Jj4YmicCSS3dw,22775
3
+ safer/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ safer-5.2.0.dist-info/WHEEL,sha256=fAguSjoiATBe7TNBkJwOjyL1Tt4wwiaQGtNtjRPNMQA,80
5
+ safer-5.2.0.dist-info/METADATA,sha256=nrneD72A3_qRFGuf9i0e4wDx2OXkmZH_maEmb73m-KU,5439
6
+ safer-5.2.0.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: poetry-core 1.9.0
2
+ Generator: uv 0.9.28
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2020 Tom Swirly
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
@@ -1,6 +0,0 @@
1
- safer/__init__.py,sha256=eRkfOSVTIr5Wd9aOF6EJBkfyizjeS_P6Uaeu0qBKe3Y,22455
2
- safer/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
- safer-5.0.0.dist-info/LICENSE,sha256=YrPqlE_MughiZSHUT2iVoduqlqmStMop9EEwCTdlzBw,1067
4
- safer-5.0.0.dist-info/METADATA,sha256=v2pjAfB1KHIGyg7W-fhSb0_mP2FOHePB8B7rPjzow8M,5449
5
- safer-5.0.0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
6
- safer-5.0.0.dist-info/RECORD,,