# This file is part of the Coral project.
# Please read the copyright notice in the LICENSE file.

from coral.modeler.unicode import PyStringToUTF8

class CodeArtifact:
    def __init__(self):
        self.tabs=0
        self.text=[]
        self.indent_string = "\t"
    def indent(self,i):
        self.tabs=self.tabs+i
    def addpl(self,s):
        t = self.indent_string * self.tabs
        self.text.append(t + s)

    def setIndentString(self, s):
        self.indent_string = s

    def resetIndent(self):
        self.tabs=0

    def prepend(self,s):
        self.text.insert(0, s + '\n')
        
    def add(self,s):
        self.addpl(s+'\n')

    def addCodeArtifact(self, ca):
        assert isinstance(ca, CodeArtifact)
        for i in ca.text:
            self.addpl(i)

    def dump_fd(self, fd):
        for line in self.text:
            fd.write(PyStringToUTF8(line))

    def dump(self,file):
        f=open(file,'w')
        try:
            self.dump_fd(f)
        finally:
            f.close()
    
