<commit_before>"""A module to handle generic operations.
"""
import mimetypes
import os
import sys
import typing as ty
from functools import wraps

if ty.TYPE_CHECKING:
	import typing_extensions as ty_ext
else:
	ty_ext = ty

T = ty.TypeVar("T")

if sys.version_info >= (3, 8):  #PY38+
	Literal = ty_ext.Literal
	Protocol = ty_ext.Protocol
	
	Literal_True = ty.Literal[True]
	Literal_False = ty.Literal[False]
else:  #PY37-
	class Literal(ty.Generic[T]):
		...
	
	class Protocol:
		...
	
	Literal_True = Literal_False = bool

# `os.PathLike` only has a type param while type checking
if ty.TYPE_CHECKING:
	PathLike = os.PathLike
	PathLike_str = os.PathLike[str]
	PathLike_bytes = os.PathLike[bytes]
else:
	class PathLike(Protocol, ty.Generic[ty.AnyStr]):
		def __fspath__(self) -> ty.AnyStr:
			...

	PathLike_str = PathLike_bytes = os.PathLike

path_str_t = ty.Union[str, PathLike_str]
path_bytes_t = ty.Union[bytes, PathLike_bytes]
path_t = ty.Union[path_str_t, path_bytes_t]
AnyPath = ty.TypeVar("AnyPath", str, PathLike_str, bytes, PathLike_bytes)

path_types = (str, bytes, os.PathLike,)
path_obj_types = (os.PathLike,)


# work around GH/mypy/mypy#731: no recursive structural types yet
json_primitive_t = ty.Union[bool, float, int, str]
json_value_t = ty.Union[
	json_primitive_t,
	"json_list_t",
	"json_dict_t"
]


class json_list_t(ty.List[json_value_t]):
	pass


class json_dict_t(ty.Dict[str, json_value_t]):
	pass


def maybe_fsencode(val: str, ref: ty.AnyStr) -> ty.AnyStr:
	"""Encodes the string *val* using the system filesystem encoding if *ref* is
	   of type :type:`bytes`"""
	if isinstance(ref, bytes):
		return os.fsencode(val)
	else:
		return val


def guess_mimetype(filename: str) -> str:
	"""Guesses the mimetype of a file based on the given ``filename``.
	
	.. code-block:: python
	
		>>> guess_mimetype('example.txt')
		'text/plain'
		>>> guess_mimetype('/foo/bar/example')
		'application/octet-stream'<commit_msg>Fix Sphinx warning - Cannot resolve forward reference in type annotations of "ipfshttpclient.Client.config.set": name 'json_list_t' is not defined<commit_after>"""A module to handle generic operations.
"""
import mimetypes
import os
import sys
import typing as ty
from functools import wraps

if ty.TYPE_CHECKING:
	import typing_extensions as ty_ext
else:
	ty_ext = ty

T = ty.TypeVar("T")

if sys.version_info >= (3, 8):  #PY38+
	Literal = ty_ext.Literal
	Protocol = ty_ext.Protocol
	
	Literal_True = ty.Literal[True]
	Literal_False = ty.Literal[False]
else:  #PY37-
	class Literal(ty.Generic[T]):
		...
	
	class Protocol:
		...
	
	Literal_True = Literal_False = bool

# `os.PathLike` only has a type param while type checking
if ty.TYPE_CHECKING:
	PathLike = os.PathLike
	PathLike_str = os.PathLike[str]
	PathLike_bytes = os.PathLike[bytes]
else:
	class PathLike(Protocol, ty.Generic[ty.AnyStr]):
		def __fspath__(self) -> ty.AnyStr:
			...

	PathLike_str = PathLike_bytes = os.PathLike

path_str_t = ty.Union[str, PathLike_str]
path_bytes_t = ty.Union[bytes, PathLike_bytes]
path_t = ty.Union[path_str_t, path_bytes_t]
AnyPath = ty.TypeVar("AnyPath", str, PathLike_str, bytes, PathLike_bytes)

path_types = (str, bytes, os.PathLike,)
path_obj_types = (os.PathLike,)


# work around GH/mypy/mypy#731: no recursive structural types yet
json_primitive_t = ty.Union[bool, float, int, str]


# noinspection PyPep8Naming
class json_list_t(ty.List["json_value_t"]):
	pass


# noinspection PyPep8Naming
class json_dict_t(ty.Dict[str, "json_value_t"]):
	pass


json_value_t = ty.Union[
	json_primitive_t,
	json_list_t,
	json_dict_t
]


def maybe_fsencode(val: str, ref: ty.AnyStr) -> ty.AnyStr:
	"""Encodes the string *val* using the system filesystem encoding if *ref* is
	   of type :type:`bytes`"""
	if isinstance(ref, bytes):
		return os.fsencode(val)
	else:
		return val


def guess_mimetype(filename: str) -> str:
	"""Guesses the mimetype of a file based on the given ``filename``.
	
	.. code-block:: python
	
		>>> guess_mimetype('example.txt')
		'text/plain'
		>>> guess_mimetype('/foo/bar/example')
		'application/octet-stream'
{'fraction_non_alphanumeric': 0.1226345539444581, 'fraction_numerical': 0.004915212582944212, 'mean_word_length': 4.652777777777778, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1323135', 'n_tokens_mistral': 1581, 'n_tokens_neox': 1489, 'n_words': 372}