This commit is contained in:
Peter Howell 2025-11-18 19:04:55 +00:00
parent 5ee55a19af
commit 9f956b3421
2 changed files with 21 additions and 3 deletions

View File

@ -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

16
util.py
View File

@ -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:
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','+')):