Source code for kvalikirstu2.data_file_tempwriter
""" A module for writing temporary data files, that can be modified by the user.
"""
import logging
import os
from kvalikirstu2 import utils
logger = logging.getLogger(__name__)
[docs]def write_to_file(temp_path, paragraphs):
""" Writes the study to file.
:param list(Paragraph) paragraphs: The paragraphs parsed from the file.
"""
utils.create_folder_if_not_exists(os.path.dirname(temp_path))
logger.info("Writing study to file %s", temp_path)
with open(temp_path, 'w', encoding='utf-8', newline='') as file_stream:
for par in paragraphs:
par.write_to_stream(file_stream)
file_stream.write(os.linesep)
[docs]def generate_temp_path(orig_path, study_path):
""" Generates a path for the temporary file.
:param str orig_path: Path to the original file.
:param str study_path: The path of the study.
"""
if not study_path:
study_path = os.path.dirname(orig_path)
relative_path = os.path.relpath(orig_path, study_path)
temp_path = relative_path.replace(os.sep, "-")
temp_path = os.path.join(study_path, "temp", "%s.temp" % temp_path)
logger.debug("Generating temp path %s.temp", temp_path)
return temp_path