[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:
parent
678876325e
commit
d874782ee4
@ -11,7 +11,8 @@ INDENT = " "
|
|||||||
|
|
||||||
class XMLWriter(object):
|
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':
|
if encoding.lower().replace('-','').replace('_','') != 'utf8':
|
||||||
raise Exception('Only UTF-8 encoding is supported.')
|
raise Exception('Only UTF-8 encoding is supported.')
|
||||||
if fileOrPath == '-':
|
if fileOrPath == '-':
|
||||||
@ -33,7 +34,10 @@ class XMLWriter(object):
|
|||||||
self.file.write(tounicode(''))
|
self.file.write(tounicode(''))
|
||||||
self.totype = tounicode
|
self.totype = tounicode
|
||||||
self.indentwhite = self.totype(indentwhite)
|
self.indentwhite = self.totype(indentwhite)
|
||||||
|
if newlinestr is None:
|
||||||
self.newlinestr = self.totype(os.linesep)
|
self.newlinestr = self.totype(os.linesep)
|
||||||
|
else:
|
||||||
|
self.newlinestr = self.totype(newlinestr)
|
||||||
self.indentlevel = 0
|
self.indentlevel = 0
|
||||||
self.stack = []
|
self.stack = []
|
||||||
self.needindent = 1
|
self.needindent = 1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user