diff --git a/users.py b/users.py index 752d057..cc69a38 100644 --- a/users.py +++ b/users.py @@ -34,6 +34,10 @@ import datetime import queue from threading import Thread from os import path +import warnings +from cryptography.utils import CryptographyDeprecationWarning +# Suppress noisy TripleDES warnings originating from paramiko. +warnings.filterwarnings("ignore", category=CryptographyDeprecationWarning) # for NLP #import spacy diff --git a/util.py b/util.py index 2a76cbf..a3105dc 100755 --- a/util.py +++ b/util.py @@ -17,17 +17,31 @@ def _ensure_parent_dir(path): # Never block the open call due to directory check errors pass +# Wrap builtins.open to auto-create parent folders and sanitize buffering. def _open_with_dirs(file, mode='r', *args, **kwargs): try: if isinstance(file, (str, bytes, os.PathLike)) and any(m in mode for m in ('w','a','x','+')): _ensure_parent_dir(file) finally: # Avoid RuntimeWarning: line buffering (buffering=1) isn't supported in binary mode - if 'b' in mode and kwargs.get('buffering', None) == 1: - kwargs = dict(kwargs) - kwargs['buffering'] = -1 # use default buffering for binary + args_list = None + buffering_arg = None + if args: + args_list = list(args) + buffering_arg = args_list[0] + elif 'buffering' in kwargs: + buffering_arg = kwargs['buffering'] + if 'b' in mode and buffering_arg == 1: + if args_list is not None: + args_list[0] = -1 + else: + kwargs = dict(kwargs) + kwargs['buffering'] = -1 # use default buffering for binary + if args_list is not None: + return _orig_open(file, mode, *tuple(args_list), **kwargs) return _orig_open(file, mode, *args, **kwargs) +# Wrap codecs.open mirrors to ensure parent folders exist and fix buffering. def _codecs_open_with_dirs(filename, mode='r', encoding=None, errors='strict', buffering=1): try: if isinstance(filename, (str, bytes, os.PathLike)) and any(m in mode for m in ('w','a','x','+')):