Skip to content

Commit a673737

Browse files
committed
Update pyfoxfile.py
1 parent 22ee7af commit a673737

1 file changed

Lines changed: 84 additions & 7 deletions

File tree

pyfoxfile.py

Lines changed: 84 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,17 @@
7373
except ImportError:
7474
import json
7575

76+
testyaml = False
77+
try:
78+
import oyaml as yaml
79+
testyaml = True
80+
except ImportError:
81+
try:
82+
import yaml
83+
testyaml = True
84+
except ImportError:
85+
testyaml = False
86+
7687
try:
7788
import configparser
7889
except ImportError:
@@ -2620,7 +2631,7 @@ def _load_all_members_spooled(self):
26202631
scanned_leading = 0 # for tolerant header scan
26212632

26222633
while True:
2623-
data = self.file.read(1 << 20) # 1 MiB blocks
2634+
data = self.file.read(__filebuff_size__) # 1 MiB blocks
26242635
if not data:
26252636
if d is not None:
26262637
self._spool.write(d.flush())
@@ -2778,7 +2789,7 @@ def write(self, data):
27782789

27792790
# Buffer and compress in chunks to limit memory
27802791
self._write_buf += data
2781-
if len(self._write_buf) >= (1 << 20): # 1 MiB threshold
2792+
if len(self._write_buf) >= (__filebuff_size__): # 1 MiB threshold
27822793
chunk = self._compressor.compress(bytes(self._write_buf))
27832794
if chunk:
27842795
self.file.write(chunk)
@@ -3083,7 +3094,7 @@ def _load_all_members_spooled(self):
30833094

30843095
self._spool = tempfile.SpooledTemporaryFile(max_size=self.spool_threshold)
30853096

3086-
CHUNK = 1 << 20
3097+
CHUNK = __filebuff_size__
30873098
pending = b""
30883099
d = None
30893100
absolute_offset = 0
@@ -3246,7 +3257,7 @@ def write(self, data):
32463257

32473258
# Stage and compress in chunks
32483259
self._write_buf += data
3249-
if len(self._write_buf) >= (1 << 20): # 1 MiB threshold
3260+
if len(self._write_buf) >= (__filebuff_size__): # 1 MiB threshold
32503261
out = self._compressor.compress(bytes(self._write_buf))
32513262
if out:
32523263
self.file.write(out)
@@ -3699,7 +3710,7 @@ def GetFileChecksum(inbytes, checksumtype="md5", encodedata=True, formatspecs=__
36993710
if CheckSumSupport(algo_key, hashlib_guaranteed):
37003711
h = hashlib.new(algo_key)
37013712
while True:
3702-
chunk = inbytes.read(1 << 20)
3713+
chunk = inbytes.read(__filebuff_size__)
37033714
if not chunk:
37043715
break
37053716
if not isinstance(chunk, (bytes, bytearray, memoryview)):
@@ -4151,6 +4162,28 @@ def ReadFileHeaderDataWithContent(fp, listonly=False, uncompress=True, skipcheck
41514162
fprejsoncontent = ""
41524163
fjsonrawcontent = fprejsoncontent
41534164
fjsoncontent = {}
4165+
elif(testyaml and fjsontype == "yaml"):
4166+
fjsoncontent = {}
4167+
fprejsoncontent = fp.read(fjsonsize).decode("UTF-8")
4168+
if (fjsonsize > 0):
4169+
try:
4170+
# try base64 → utf-8 → YAML
4171+
fjsonrawcontent = base64.b64decode(fprejsoncontent.encode("UTF-8")).decode("UTF-8")
4172+
fjsoncontent = yaml.safe_load(fjsonrawcontent) or {}
4173+
except (binascii.Error, UnicodeDecodeError, yaml.YAMLError):
4174+
try:
4175+
# fall back to treating the bytes as plain text YAML
4176+
fjsonrawcontent = fprejsoncontent
4177+
fjsoncontent = yaml.safe_load(fjsonrawcontent) or {}
4178+
except (UnicodeDecodeError, yaml.YAMLError):
4179+
# final fallback: empty
4180+
fprejsoncontent = ""
4181+
fjsonrawcontent = fprejsoncontent
4182+
fjsoncontent = {}
4183+
else:
4184+
fprejsoncontent = ""
4185+
fjsonrawcontent = fprejsoncontent
4186+
fjsoncontent = {}
41544187
elif(fjsontype=="list"):
41554188
fprejsoncontent = fp.read(fjsonsize).decode("UTF-8")
41564189
flisttmp = MkTempFile()
@@ -4324,6 +4357,28 @@ def ReadFileHeaderDataWithContentToArray(fp, listonly=False, contentasfile=True,
43244357
fprejsoncontent = ""
43254358
fjsonrawcontent = fprejsoncontent
43264359
fjsoncontent = {}
4360+
elif(testyaml and fjsontype == "yaml"):
4361+
fjsoncontent = {}
4362+
fprejsoncontent = fp.read(fjsonsize).decode("UTF-8")
4363+
if (fjsonsize > 0):
4364+
try:
4365+
# try base64 → utf-8 → YAML
4366+
fjsonrawcontent = base64.b64decode(fprejsoncontent.encode("UTF-8")).decode("UTF-8")
4367+
fjsoncontent = yaml.safe_load(fjsonrawcontent) or {}
4368+
except (binascii.Error, UnicodeDecodeError, yaml.YAMLError):
4369+
try:
4370+
# fall back to treating the bytes as plain text YAML
4371+
fjsonrawcontent = fprejsoncontent
4372+
fjsoncontent = yaml.safe_load(fjsonrawcontent) or {}
4373+
except (UnicodeDecodeError, yaml.YAMLError):
4374+
# final fallback: empty
4375+
fprejsoncontent = ""
4376+
fjsonrawcontent = fprejsoncontent
4377+
fjsoncontent = {}
4378+
else:
4379+
fprejsoncontent = ""
4380+
fjsonrawcontent = fprejsoncontent
4381+
fjsoncontent = {}
43274382
elif(fjsontype=="list"):
43284383
fprejsoncontent = fp.read(fjsonsize).decode("UTF-8")
43294384
flisttmp = MkTempFile()
@@ -4510,6 +4565,28 @@ def ReadFileHeaderDataWithContentToList(fp, listonly=False, contentasfile=False,
45104565
fprejsoncontent = ""
45114566
fjsonrawcontent = fprejsoncontent
45124567
fjsoncontent = {}
4568+
elif(testyaml and fjsontype == "yaml"):
4569+
fjsoncontent = {}
4570+
fprejsoncontent = fp.read(fjsonsize).decode("UTF-8")
4571+
if (fjsonsize > 0):
4572+
try:
4573+
# try base64 → utf-8 → YAML
4574+
fjsonrawcontent = base64.b64decode(fprejsoncontent.encode("UTF-8")).decode("UTF-8")
4575+
fjsoncontent = yaml.safe_load(fjsonrawcontent) or {}
4576+
except (binascii.Error, UnicodeDecodeError, yaml.YAMLError):
4577+
try:
4578+
# fall back to treating the bytes as plain text YAML
4579+
fjsonrawcontent = fprejsoncontent
4580+
fjsoncontent = yaml.safe_load(fjsonrawcontent) or {}
4581+
except (UnicodeDecodeError, yaml.YAMLError):
4582+
# final fallback: empty
4583+
fprejsoncontent = ""
4584+
fjsonrawcontent = fprejsoncontent
4585+
fjsoncontent = {}
4586+
else:
4587+
fprejsoncontent = ""
4588+
fjsonrawcontent = fprejsoncontent
4589+
fjsoncontent = {}
45134590
elif(fjsontype=="list"):
45144591
fprejsoncontent = fp.read(fjsonsize).decode("UTF-8")
45154592
flisttmp = MkTempFile()
@@ -8766,7 +8843,7 @@ def ensure_filelike(infile, mode="rb", use_mmap=False, **adapter_kw):
87668843

87678844
# ========= copy helpers =========
87688845

8769-
def fast_copy(infp, outfp, bufsize=1 << 20):
8846+
def fast_copy(infp, outfp, bufsize=__filebuff_size__):
87708847
"""
87718848
Efficient copy from any readable file-like to any writable file-like.
87728849
Uses readinto() when available to avoid extra allocations.
@@ -8810,7 +8887,7 @@ def copy_file_to_mmap_dest(src_path, outfp, chunk_size=__spoolfile_size__):
88108887
shutil.copyfileobj(fp, outfp, length=chunk_size)
88118888

88128889

8813-
def copy_opaque(src, dst, bufsize=1 << 20, grow_step=64 << 20):
8890+
def copy_opaque(src, dst, bufsize=__filebuff_size__, grow_step=64 << 20):
88148891
"""
88158892
Copy opaque bytes from 'src' (any readable file-like) to 'dst'
88168893
(your mmap-backed FileLikeAdapter or any writable file-like).

0 commit comments

Comments
 (0)