Skip to content

Commit

Permalink
Change the buffer in TextFormat to BytesIO
Browse files Browse the repository at this point in the history
  • Loading branch information
MBartkowiakSTFC committed Mar 7, 2024
1 parent 83b3840 commit 2bb55e6
Showing 1 changed file with 22 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,19 @@
import os
import io
import tarfile
import codecs

import numpy as np


from MDANSE.Framework.Formats.IFormat import IFormat


def length_stringio(input: "io.BytesIO") -> int:
result = input.getbuffer().nbytes
return result


class TextFormat(IFormat):
"""
This class handles the writing of output variables in Text format. Each output variable is written into separate Text files which are further
Expand Down Expand Up @@ -53,29 +59,28 @@ def write(cls, filename, data, header=""):

tf = tarfile.open(filename, "w")

if header:
real_buffer = io.BytesIO()
tempStr = codecs.getwriter("utf-8")(real_buffer)
for line in header:
tempStr.write(str(line))
tempStr.write("\n\n")
real_buffer.seek(0)
info = tarfile.TarInfo(name="jobinfo.txt")
info.size = length_stringio(real_buffer)
tf.addfile(tarinfo=info, fileobj=real_buffer)

for var in list(data.values()):
tempStr = io.StringIO()
real_buffer = io.BytesIO()
tempStr = codecs.getwriter("utf-8")(real_buffer)
tempStr.write(var.info())
tempStr.write("\n\n")
cls.write_data(tempStr, var, data)
tempStr.seek(0)
real_buffer.seek(0)

info = tarfile.TarInfo(name="%s%s" % (var.varname, cls.extensions[0]))
try:
info.size = len(tempStr)
except:
print("Ignoring the error in TextFormat, see if it matters.")
tf.addfile(tarinfo=info, fileobj=tempStr)

if header:
tempStr = io.StringIO()
for line in header:
tempStr.write(str(line))
tempStr.write("\n\n")
tempStr.seek(0)
info = tarfile.TarInfo(name="jobinfo.txt")
info.size = tempStr.len
tf.addfile(tarinfo=info, fileobj=tempStr)
info.size = length_stringio(real_buffer)
tf.addfile(tarinfo=info, fileobj=real_buffer)

tf.close()

Expand Down

0 comments on commit 2bb55e6

Please sign in to comment.