# Path: elbepack/treeutils.py
# class etree(ebase):
#     def __init__(self, fname):
#         if fname is not None:
#             parser = XMLParser(huge_tree=True, remove_comments=False)
#             et = parse(fname, parser=parser)
#         else:
#             et = ElementTree(file=None)
# 
#         ebase.__init__(self, et)
# 
#     def write(self, fname, encoding=None):
#         # Make sure, that we end with a newline
#         self.et.getroot().tail = '\n'
#         self.et.write(fname, encoding=encoding)
# 
#     def tostring(self):
#         return self.et.tostring()
# 
#     def ensure_child(self, tag):
#         retval = self.et.find("./" + tag)
#         if retval is not None:
#             return elem(retval)
#         return elem(SubElement(self.et.getroot(), tag))
# 
#     def set_child_position(self, child, pos):
#         root = self.et.getroot()
#         root.remove(child.et)
#         root.insert(pos, child.et)
# 
#     def setroot(self, tag):
#         retval = elem(Element(tag))
#         # pylint: disable=protected-access
#         self.et._setroot(retval.et)
#         return retval
# 
#     @property
#     def root(self):
#         return elem(self.et.getroot())
# 
# Path: elbepack/directories.py
# def init_directories(elbe_relpath):
# def get_cmdlist():


import os
from mako.template import Template
from mako import exceptions
from elbepack.treeutils import etree
from elbepack.directories import mako_template_dir, default_preseed_fname
# SPDX-License-Identifier: GPL-3.0-or-later




def fix_linebreak_escapes(s):
    return s.replace('\\\n', '${"\\\\"}\n')


def template(fname, d, linebreak=False):
    try:
        if linebreak:
            return Template(
                filename=fname,
                preprocessor=fix_linebreak_escapes).render(
                **d)

        return Template(filename=fname).render(**d)
    except BaseException:
        print(exceptions.text_error_template().render())
        raise


def write_template(outname, fname, d, linebreak=False):
    outfile = open(outname, "w")
    outfile.write(template(fname, d, linebreak))
    outfile.close()


def write_pack_template(outname, fname, d, linebreak=False):

# Path: elbepack/treeutils.py
# class etree(ebase):
#     def __init__(self, fname):
#         if fname is not None:
#             parser = XMLParser(huge_tree=True, remove_comments=False)
#             et = parse(fname, parser=parser)
#         else:
#             et = ElementTree(file=None)
# 
#         ebase.__init__(self, et)
# 
#     def write(self, fname, encoding=None):
#         # Make sure, that we end with a newline
#         self.et.getroot().tail = '\n'
#         self.et.write(fname, encoding=encoding)
# 
#     def tostring(self):
#         return self.et.tostring()
# 
#     def ensure_child(self, tag):
#         retval = self.et.find("./" + tag)
#         if retval is not None:
#             return elem(retval)
#         return elem(SubElement(self.et.getroot(), tag))
# 
#     def set_child_position(self, child, pos):
#         root = self.et.getroot()
#         root.remove(child.et)
#         root.insert(pos, child.et)
# 
#     def setroot(self, tag):
#         retval = elem(Element(tag))
#         # pylint: disable=protected-access
#         self.et._setroot(retval.et)
#         return retval
# 
#     @property
#     def root(self):
#         return elem(self.et.getroot())
# 
# Path: elbepack/directories.py
# def init_directories(elbe_relpath):
# def get_cmdlist():


# Path: elbepack/templates.py
import os
from mako.template import Template
from mako import exceptions
from elbepack.treeutils import etree
from elbepack.directories import mako_template_dir, default_preseed_fname

# SPDX-License-Identifier: GPL-3.0-or-later




def fix_linebreak_escapes(s):
    return s.replace('\\\n', '${"\\\\"}\n')


def template(fname, d, linebreak=False):
    try:
        if linebreak:
            return Template(
                filename=fname,
                preprocessor=fix_linebreak_escapes).render(
                **d)

        return Template(filename=fname).render(**d)
    except BaseException:
        print(exceptions.text_error_template().render())
        raise


def write_template(outname, fname, d, linebreak=False):
    outfile = open(outname, "w")
    outfile.write(template(fname, d, linebreak))
    outfile.close()


def write_pack_template(outname, fname, d, linebreak=False):

    template_name = os.path.join(mako_template_dir, fname)