Skip to content

Commit

Permalink
Update postmangler.py
Browse files Browse the repository at this point in the history
Improve NZB generation by using pretty printing, better readability + compatibility
  • Loading branch information
kwaaak committed Nov 2, 2014
1 parent 918e312 commit 72f829e
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion newsmangler/postmangler.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,9 +435,27 @@ def generate_nzb(self):
)
segment.text = str(article.headers['Message-ID'][1:-1])

# pretty print
def indent(elem, level=0):
i = "\n" + level*" "
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + " "
if not elem.tail or not elem.tail.strip():
elem.tail = i
for elem in elem:
indent(elem, level+1)
if not elem.tail or not elem.tail.strip():
elem.tail = i
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i


with open(filename, 'wb') as nzbfile:
indent(root)
ET.ElementTree(root).write(nzbfile, xml_declaration=True)

self.logger.info('End generation of %s', filename)

# ---------------------------------------------------------------------------
Expand Down

0 comments on commit 72f829e

Please sign in to comment.