mirror of
https://github.com/cool-RR/PySnooper.git
synced 2026-07-25 20:03:51 +00:00
I like PathLike
This commit is contained in:
parent
5a82a65bde
commit
9345a5c324
2 changed files with 11 additions and 12 deletions
|
|
@ -29,4 +29,9 @@ else:
|
|||
|
||||
@classmethod
|
||||
def __subclasshook__(cls, subclass):
|
||||
return hasattr(subclass, '__fspath__')
|
||||
return (
|
||||
hasattr(subclass, '__fspath__') or
|
||||
# Make a concession for older `pathlib` versions:g
|
||||
(hasattr(subclass, 'open') and
|
||||
'path' in subclass.__name__.lower())
|
||||
)
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
import sys
|
||||
|
||||
from .third_party import six
|
||||
from .third_party import decorator
|
||||
|
||||
from . import utils
|
||||
|
|
@ -22,26 +23,19 @@ def get_write_and_truncate_functions(output):
|
|||
truncate = None
|
||||
elif isinstance(output, (pycompat.PathLike, str)):
|
||||
def write(s):
|
||||
with open(output, 'a') as output_file:
|
||||
with open(six.text_type(output), 'a') as output_file:
|
||||
output_file.write(s)
|
||||
def truncate():
|
||||
with open(output, 'w') as output_file:
|
||||
with open(six.text_type(output), 'w') as output_file:
|
||||
pass
|
||||
elif callable(output):
|
||||
write = output
|
||||
truncate = None
|
||||
elif isinstance(output, utils.WritableStream):
|
||||
else:
|
||||
assert isinstance(output, utils.WritableStream)
|
||||
def write(s):
|
||||
output.write(s)
|
||||
truncate = None
|
||||
elif hasattr(output, 'open'):
|
||||
def write(s):
|
||||
with output.open('a') as output_file:
|
||||
output_file.write(s)
|
||||
|
||||
def truncate():
|
||||
with output.open('w'):
|
||||
pass
|
||||
|
||||
return (write, truncate)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue