PySnooper/pysnooper/pycompat.py

38 lines
1.0 KiB
Python
Raw Normal View History

2019-04-24 09:10:46 +00:00
# Copyright 2019 Ram Rachum and collaborators.
2019-04-21 13:53:18 +00:00
# This program is distributed under the MIT license.
2019-04-22 20:38:49 +00:00
'''Python 2/3 compatibility'''
2019-04-21 13:53:18 +00:00
import abc
import os
if hasattr(abc, 'ABC'):
ABC = abc.ABC
else:
class ABC(object):
"""Helper class that provides a standard way to create an ABC using
inheritance.
"""
__metaclass__ = abc.ABCMeta
__slots__ = ()
2019-04-21 18:39:32 +00:00
2019-04-21 13:53:18 +00:00
if hasattr(os, 'PathLike'):
PathLike = os.PathLike
else:
class PathLike(ABC):
"""Abstract base class for implementing the file system path protocol."""
2019-04-21 18:39:32 +00:00
2019-04-21 13:53:18 +00:00
@abc.abstractmethod
def __fspath__(self):
"""Return the file system path representation of the object."""
raise NotImplementedError
2019-04-21 18:39:32 +00:00
2019-04-21 13:53:18 +00:00
@classmethod
def __subclasshook__(cls, subclass):
2019-05-03 18:31:39 +00:00
return (
hasattr(subclass, '__fspath__') or
# Make a concession for older `pathlib` versions:g
(hasattr(subclass, 'open') and
'path' in subclass.__name__.lower())
)