[xmlWriter] add 'newlinestr' argument for overriding os-specific line endings

By default (newlinestr=None), the XMLWriter will still use the `os.linesep` as the
newline string.
Otherwise, it will use the specified `newlinestr`.

This is useful when TTX files under version control are being written from
multiple platforms; in which case, one usually wants to always use one
specific line ending (most likely LF, which is what the XML spec itself
normalizes it to).
This commit is contained in:
Cosimo Lupo 2016-10-20 16:53:07 +01:00
parent 678876325e
commit d874782ee4

View File

@ -11,7 +11,8 @@ INDENT = " "
class XMLWriter(object):
def __init__(self, fileOrPath, indentwhite=INDENT, idlefunc=None, encoding="utf_8"):
def __init__(self, fileOrPath, indentwhite=INDENT, idlefunc=None, encoding="utf_8",
newlinestr=None):
if encoding.lower().replace('-','').replace('_','') != 'utf8':
raise Exception('Only UTF-8 encoding is supported.')
if fileOrPath == '-':
@ -33,7 +34,10 @@ class XMLWriter(object):
self.file.write(tounicode(''))
self.totype = tounicode
self.indentwhite = self.totype(indentwhite)
self.newlinestr = self.totype(os.linesep)
if newlinestr is None:
self.newlinestr = self.totype(os.linesep)
else:
self.newlinestr = self.totype(newlinestr)
self.indentlevel = 0
self.stack = []
self.needindent = 1