mirror of
https://github.com/morpheus65535/bazarr.git
synced 2025-04-24 14:47:16 -04:00
Upgraded vendored Python dependencies to the latest versions and removed the unused dependencies.
This commit is contained in:
parent
36bf0d219d
commit
0c3c5a02a7
2108 changed files with 306789 additions and 151391 deletions
|
@ -1,28 +1,44 @@
|
|||
|
||||
from gzip import GzipFile
|
||||
from io import BytesIO
|
||||
import warnings
|
||||
from json import loads as json_loads
|
||||
from os import fsync
|
||||
from sys import exc_info, version
|
||||
from .utils import NoNumpyException # keep 'unused' imports
|
||||
from .comment import strip_comment_line_with_symbol, strip_comments # keep 'unused' imports
|
||||
from .encoders import TricksEncoder, json_date_time_encode, class_instance_encode, ClassInstanceEncoder, \
|
||||
json_complex_encode, json_set_encode, numeric_types_encode, numpy_encode, nonumpy_encode, NoNumpyEncoder, \
|
||||
nopandas_encode, pandas_encode # keep 'unused' imports
|
||||
from .decoders import DuplicateJsonKeyException, TricksPairHook, json_date_time_hook, ClassInstanceHook, \
|
||||
json_complex_hook, json_set_hook, numeric_types_hook, json_numpy_obj_hook, json_nonumpy_obj_hook, \
|
||||
nopandas_hook, pandas_hook # keep 'unused' imports
|
||||
from json import JSONEncoder
|
||||
from sys import exc_info
|
||||
|
||||
from json_tricks.utils import is_py3, dict_default, gzip_compress, gzip_decompress, JsonTricksDeprecation
|
||||
from .utils import str_type, NoNumpyException # keep 'unused' imports
|
||||
from .comment import strip_comments # keep 'unused' imports
|
||||
#TODO @mark: imports removed?
|
||||
from .encoders import TricksEncoder, json_date_time_encode, \
|
||||
class_instance_encode, json_complex_encode, json_set_encode, numeric_types_encode, numpy_encode, \
|
||||
nonumpy_encode, nopandas_encode, pandas_encode, noenum_instance_encode, \
|
||||
enum_instance_encode, pathlib_encode # keep 'unused' imports
|
||||
from .decoders import TricksPairHook, \
|
||||
json_date_time_hook, ClassInstanceHook, \
|
||||
json_complex_hook, json_set_hook, numeric_types_hook, json_numpy_obj_hook, \
|
||||
json_nonumpy_obj_hook, \
|
||||
nopandas_hook, pandas_hook, EnumInstanceHook, \
|
||||
noenum_hook, pathlib_hook, nopathlib_hook # keep 'unused' imports
|
||||
|
||||
|
||||
is_py3 = (version[:2] == '3.')
|
||||
str_type = str if is_py3 else (basestring, unicode,)
|
||||
ENCODING = 'UTF-8'
|
||||
|
||||
|
||||
_cih_instance = ClassInstanceHook()
|
||||
DEFAULT_ENCODERS = [json_date_time_encode, class_instance_encode, json_complex_encode, json_set_encode, numeric_types_encode,]
|
||||
DEFAULT_HOOKS = [json_date_time_hook, _cih_instance, json_complex_hook, json_set_hook, numeric_types_hook,]
|
||||
_eih_instance = EnumInstanceHook()
|
||||
DEFAULT_ENCODERS = [json_date_time_encode, json_complex_encode, json_set_encode,
|
||||
numeric_types_encode, class_instance_encode, ]
|
||||
DEFAULT_HOOKS = [json_date_time_hook, json_complex_hook, json_set_hook,
|
||||
numeric_types_hook, _cih_instance, ]
|
||||
|
||||
|
||||
#TODO @mark: add properties to all built-in encoders (for speed - but it should keep working without)
|
||||
try:
|
||||
import enum
|
||||
except ImportError:
|
||||
DEFAULT_ENCODERS = [noenum_instance_encode,] + DEFAULT_ENCODERS
|
||||
DEFAULT_HOOKS = [noenum_hook,] + DEFAULT_HOOKS
|
||||
else:
|
||||
DEFAULT_ENCODERS = [enum_instance_encode,] + DEFAULT_ENCODERS
|
||||
DEFAULT_HOOKS = [_eih_instance,] + DEFAULT_HOOKS
|
||||
|
||||
try:
|
||||
import numpy
|
||||
|
@ -43,13 +59,25 @@ else:
|
|||
DEFAULT_ENCODERS = [pandas_encode,] + DEFAULT_ENCODERS
|
||||
DEFAULT_HOOKS = [pandas_hook,] + DEFAULT_HOOKS
|
||||
|
||||
|
||||
DEFAULT_NONP_ENCODERS = [nonumpy_encode,] + DEFAULT_ENCODERS # DEPRECATED
|
||||
DEFAULT_NONP_HOOKS = [json_nonumpy_obj_hook,] + DEFAULT_HOOKS # DEPRECATED
|
||||
try:
|
||||
import pathlib
|
||||
except:
|
||||
# No need to include a "nopathlib_encode" hook since we would not encounter
|
||||
# the Path object if pathlib isn't available. However, we *could* encounter
|
||||
# a serialized Path object (produced by a version of Python with pathlib).
|
||||
DEFAULT_HOOKS = [nopathlib_hook,] + DEFAULT_HOOKS
|
||||
else:
|
||||
DEFAULT_ENCODERS = [pathlib_encode,] + DEFAULT_ENCODERS
|
||||
DEFAULT_HOOKS = [pathlib_hook,] + DEFAULT_HOOKS
|
||||
|
||||
|
||||
def dumps(obj, sort_keys=None, cls=TricksEncoder, obj_encoders=DEFAULT_ENCODERS, extra_obj_encoders=(),
|
||||
primitives=False, compression=None, allow_nan=False, conv_str_byte=False, **jsonkwargs):
|
||||
DEFAULT_NONP_ENCODERS = [nonumpy_encode,] + DEFAULT_ENCODERS # DEPRECATED
|
||||
DEFAULT_NONP_HOOKS = [json_nonumpy_obj_hook,] + DEFAULT_HOOKS # DEPRECATED
|
||||
|
||||
|
||||
def dumps(obj, sort_keys=None, cls=None, obj_encoders=DEFAULT_ENCODERS, extra_obj_encoders=(),
|
||||
primitives=False, compression=None, allow_nan=False, conv_str_byte=False, fallback_encoders=(),
|
||||
properties=None, **jsonkwargs):
|
||||
"""
|
||||
Convert a nested data structure to a json string.
|
||||
|
||||
|
@ -58,8 +86,10 @@ def dumps(obj, sort_keys=None, cls=TricksEncoder, obj_encoders=DEFAULT_ENCODERS,
|
|||
:param cls: The json encoder class to use, defaults to NoNumpyEncoder which gives a warning for numpy arrays.
|
||||
:param obj_encoders: Iterable of encoders to use to convert arbitrary objects into json-able promitives.
|
||||
:param extra_obj_encoders: Like `obj_encoders` but on top of them: use this to add encoders without replacing defaults. Since v3.5 these happen before default encoders.
|
||||
:param fallback_encoders: These are extra `obj_encoders` that 1) are ran after all others and 2) only run if the object hasn't yet been changed.
|
||||
:param allow_nan: Allow NaN and Infinity values, which is a (useful) violation of the JSON standard (default False).
|
||||
:param conv_str_byte: Try to automatically convert between strings and bytes (assuming utf-8) (default False).
|
||||
:param properties: A dictionary of properties that is passed to each encoder that will accept it.
|
||||
:return: The string containing the json-encoded version of obj.
|
||||
|
||||
Other arguments are passed on to `cls`. Note that `sort_keys` should be false if you want to preserve order.
|
||||
|
@ -67,8 +97,15 @@ def dumps(obj, sort_keys=None, cls=TricksEncoder, obj_encoders=DEFAULT_ENCODERS,
|
|||
if not hasattr(extra_obj_encoders, '__iter__'):
|
||||
raise TypeError('`extra_obj_encoders` should be a tuple in `json_tricks.dump(s)`')
|
||||
encoders = tuple(extra_obj_encoders) + tuple(obj_encoders)
|
||||
properties = properties or {}
|
||||
dict_default(properties, 'primitives', primitives)
|
||||
dict_default(properties, 'compression', compression)
|
||||
dict_default(properties, 'allow_nan', allow_nan)
|
||||
if cls is None:
|
||||
cls = TricksEncoder
|
||||
txt = cls(sort_keys=sort_keys, obj_encoders=encoders, allow_nan=allow_nan,
|
||||
primitives=primitives, **jsonkwargs).encode(obj)
|
||||
primitives=primitives, fallback_encoders=fallback_encoders,
|
||||
properties=properties, **jsonkwargs).encode(obj)
|
||||
if not is_py3 and isinstance(txt, str):
|
||||
txt = unicode(txt, ENCODING)
|
||||
if not compression:
|
||||
|
@ -76,28 +113,32 @@ def dumps(obj, sort_keys=None, cls=TricksEncoder, obj_encoders=DEFAULT_ENCODERS,
|
|||
if compression is True:
|
||||
compression = 5
|
||||
txt = txt.encode(ENCODING)
|
||||
sh = BytesIO()
|
||||
with GzipFile(mode='wb', fileobj=sh, compresslevel=compression) as zh:
|
||||
zh.write(txt)
|
||||
gzstring = sh.getvalue()
|
||||
gzstring = gzip_compress(txt, compresslevel=compression)
|
||||
return gzstring
|
||||
|
||||
|
||||
def dump(obj, fp, sort_keys=None, cls=TricksEncoder, obj_encoders=DEFAULT_ENCODERS, extra_obj_encoders=(),
|
||||
primitives=False, compression=None, force_flush=False, allow_nan=False, conv_str_byte=False, **jsonkwargs):
|
||||
def dump(obj, fp, sort_keys=None, cls=None, obj_encoders=DEFAULT_ENCODERS, extra_obj_encoders=(),
|
||||
primitives=False, compression=None, force_flush=False, allow_nan=False, conv_str_byte=False,
|
||||
fallback_encoders=(), properties=None, **jsonkwargs):
|
||||
"""
|
||||
Convert a nested data structure to a json string.
|
||||
|
||||
:param fp: File handle or path to write to.
|
||||
:param compression: The gzip compression level, or None for no compression.
|
||||
:param force_flush: If True, flush the file handle used, when possibly also in the operating system (default False).
|
||||
|
||||
|
||||
The other arguments are identical to `dumps`.
|
||||
"""
|
||||
if (isinstance(obj, str_type) or hasattr(obj, 'write')) and isinstance(fp, (list, dict)):
|
||||
raise ValueError('json-tricks dump arguments are in the wrong order: provide the data to be serialized before file handle')
|
||||
txt = dumps(obj, sort_keys=sort_keys, cls=cls, obj_encoders=obj_encoders, extra_obj_encoders=extra_obj_encoders,
|
||||
primitives=primitives, compression=compression, allow_nan=allow_nan, conv_str_byte=conv_str_byte, **jsonkwargs)
|
||||
primitives=primitives, compression=compression, allow_nan=allow_nan, conv_str_byte=conv_str_byte,
|
||||
fallback_encoders=fallback_encoders, properties=properties, **jsonkwargs)
|
||||
if isinstance(fp, str_type):
|
||||
fh = open(fp, 'wb+')
|
||||
if compression:
|
||||
fh = open(fp, 'wb+')
|
||||
else:
|
||||
fh = open(fp, 'w+')
|
||||
else:
|
||||
fh = fp
|
||||
if conv_str_byte:
|
||||
|
@ -106,9 +147,9 @@ def dump(obj, fp, sort_keys=None, cls=TricksEncoder, obj_encoders=DEFAULT_ENCODE
|
|||
except TypeError:
|
||||
pass
|
||||
# if not isinstance(txt, str_type):
|
||||
# # Cannot write bytes, so must be in text mode, but we didn't get a text
|
||||
# if not compression:
|
||||
# txt = txt.decode(ENCODING)
|
||||
# # Cannot write bytes, so must be in text mode, but we didn't get a text
|
||||
# if not compression:
|
||||
# txt = txt.decode(ENCODING)
|
||||
else:
|
||||
try:
|
||||
fh.write(u'')
|
||||
|
@ -116,7 +157,7 @@ def dump(obj, fp, sort_keys=None, cls=TricksEncoder, obj_encoders=DEFAULT_ENCODE
|
|||
if isinstance(txt, str_type):
|
||||
txt = txt.encode(ENCODING)
|
||||
try:
|
||||
if 'b' not in getattr(fh, 'mode', 'b?') and not isinstance(txt, str_type) and compression:
|
||||
if compression and 'b' not in getattr(fh, 'mode', 'b?') and not isinstance(txt, str_type):
|
||||
raise IOError('If compression is enabled, the file must be opened in binary mode.')
|
||||
try:
|
||||
fh.write(txt)
|
||||
|
@ -137,8 +178,9 @@ def dump(obj, fp, sort_keys=None, cls=TricksEncoder, obj_encoders=DEFAULT_ENCODE
|
|||
return txt
|
||||
|
||||
|
||||
def loads(string, preserve_order=True, ignore_comments=True, decompression=None, obj_pairs_hooks=DEFAULT_HOOKS,
|
||||
extra_obj_pairs_hooks=(), cls_lookup_map=None, allow_duplicates=True, conv_str_byte=False, **jsonkwargs):
|
||||
def loads(string, preserve_order=True, ignore_comments=None, decompression=None, obj_pairs_hooks=DEFAULT_HOOKS,
|
||||
extra_obj_pairs_hooks=(), cls_lookup_map=None, allow_duplicates=True, conv_str_byte=False,
|
||||
properties=None, **jsonkwargs):
|
||||
"""
|
||||
Convert a nested data structure to a json string.
|
||||
|
||||
|
@ -160,29 +202,43 @@ def loads(string, preserve_order=True, ignore_comments=True, decompression=None,
|
|||
if not hasattr(extra_obj_pairs_hooks, '__iter__'):
|
||||
raise TypeError('`extra_obj_pairs_hooks` should be a tuple in `json_tricks.load(s)`')
|
||||
if decompression is None:
|
||||
decompression = string[:2] == b'\x1f\x8b'
|
||||
decompression = isinstance(string, bytes) and string[:2] == b'\x1f\x8b'
|
||||
if decompression:
|
||||
with GzipFile(fileobj=BytesIO(string), mode='rb') as zh:
|
||||
string = zh.read()
|
||||
string = string.decode(ENCODING)
|
||||
string = gzip_decompress(string).decode(ENCODING)
|
||||
if not isinstance(string, str_type):
|
||||
if conv_str_byte:
|
||||
string = string.decode(ENCODING)
|
||||
else:
|
||||
raise TypeError(('Cannot automatically encode object of type "{0:}" in `json_tricks.load(s)` since '
|
||||
'the encoding is not known. You should instead encode the bytes to a string and pass that '
|
||||
'string to `load(s)`, for example bytevar.encode("utf-8") if utf-8 is the encoding.').format(type(string)))
|
||||
if ignore_comments:
|
||||
string = strip_comments(string)
|
||||
obj_pairs_hooks = tuple(obj_pairs_hooks)
|
||||
_cih_instance.cls_lookup_map = cls_lookup_map or {}
|
||||
hooks = tuple(extra_obj_pairs_hooks) + obj_pairs_hooks
|
||||
hook = TricksPairHook(ordered=preserve_order, obj_pairs_hooks=hooks, allow_duplicates=allow_duplicates)
|
||||
raise TypeError(('The input was of non-string type "{0:}" in `json_tricks.load(s)`. '
|
||||
'Bytes cannot be automatically decoding since the encoding is not known. Recommended '
|
||||
'way is to instead encode the bytes to a string and pass that string to `load(s)`, '
|
||||
'for example bytevar.encode("utf-8") if utf-8 is the encoding. Alternatively you can '
|
||||
'force an attempt by passing conv_str_byte=True, but this may cause decoding issues.')
|
||||
.format(type(string)))
|
||||
if ignore_comments or ignore_comments is None:
|
||||
new_string = strip_comments(string)
|
||||
if ignore_comments is None and not getattr(loads, '_ignore_comments_warned', False) and string != new_string:
|
||||
warnings.warn('`json_tricks.load(s)` stripped some comments, but `ignore_comments` was '
|
||||
'not passed; in the next major release, the behaviour when `ignore_comments` is not '
|
||||
'passed will change; it is recommended to explicitly pass `ignore_comments=True` if '
|
||||
'you want to strip comments; see https://github.com/mverleg/pyjson_tricks/issues/74',
|
||||
JsonTricksDeprecation)
|
||||
loads._ignore_comments_warned = True
|
||||
string = new_string
|
||||
properties = properties or {}
|
||||
dict_default(properties, 'preserve_order', preserve_order)
|
||||
dict_default(properties, 'ignore_comments', ignore_comments)
|
||||
dict_default(properties, 'decompression', decompression)
|
||||
dict_default(properties, 'cls_lookup_map', cls_lookup_map)
|
||||
dict_default(properties, 'allow_duplicates', allow_duplicates)
|
||||
hooks = tuple(extra_obj_pairs_hooks) + tuple(obj_pairs_hooks)
|
||||
hook = TricksPairHook(ordered=preserve_order, obj_pairs_hooks=hooks, allow_duplicates=allow_duplicates, properties=properties)
|
||||
return json_loads(string, object_pairs_hook=hook, **jsonkwargs)
|
||||
|
||||
|
||||
def load(fp, preserve_order=True, ignore_comments=True, decompression=None, obj_pairs_hooks=DEFAULT_HOOKS,
|
||||
extra_obj_pairs_hooks=(), cls_lookup_map=None, allow_duplicates=True, conv_str_byte=False, **jsonkwargs):
|
||||
def load(fp, preserve_order=True, ignore_comments=None, decompression=None, obj_pairs_hooks=DEFAULT_HOOKS,
|
||||
extra_obj_pairs_hooks=(), cls_lookup_map=None, allow_duplicates=True, conv_str_byte=False,
|
||||
properties=None, **jsonkwargs):
|
||||
"""
|
||||
Convert a nested data structure to a json string.
|
||||
|
||||
|
@ -192,7 +248,14 @@ def load(fp, preserve_order=True, ignore_comments=True, decompression=None, obj_
|
|||
"""
|
||||
try:
|
||||
if isinstance(fp, str_type):
|
||||
with open(fp, 'rb') as fh:
|
||||
if decompression is not None:
|
||||
open_binary = bool(decompression)
|
||||
else:
|
||||
with open(fp, 'rb') as fh:
|
||||
# This attempts to detect gzip mode; gzip should always
|
||||
# have this header, and text json can't have it.
|
||||
open_binary = (fh.read(2) == b'\x1f\x8b')
|
||||
with open(fp, 'rb' if open_binary else 'r') as fh:
|
||||
string = fh.read()
|
||||
else:
|
||||
string = fp.read()
|
||||
|
@ -202,6 +265,6 @@ def load(fp, preserve_order=True, ignore_comments=True, decompression=None, obj_
|
|||
'opened in binary mode; be sure to set file mode to something like "rb".').with_traceback(exc_info()[2])
|
||||
return loads(string, preserve_order=preserve_order, ignore_comments=ignore_comments, decompression=decompression,
|
||||
obj_pairs_hooks=obj_pairs_hooks, extra_obj_pairs_hooks=extra_obj_pairs_hooks, cls_lookup_map=cls_lookup_map,
|
||||
allow_duplicates=allow_duplicates, conv_str_byte=conv_str_byte, **jsonkwargs)
|
||||
allow_duplicates=allow_duplicates, conv_str_byte=conv_str_byte, properties=properties, **jsonkwargs)
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue