import sys
from argparse import ArgumentParser

from apertium.quality.testing import Test
from apertium.quality import Statistics, ParseError

class Frontend(Test, ArgumentParser):
	def __init__(self, stats=True, colour=False):
		ArgumentParser.__init__(self)
		Test.__init__(self)
		if colour:
			self.add_argument("-c", "--colour", dest="colour", action="store_true",
							  help="Colours the output") 
		if stats:
			self.add_argument("-X", "--statistics", dest="statfile", 
							  nargs='?', const='quality-stats.xml', default=None,
							  help="XML file that statistics are to be stored in")

	def start(self):
		ret = self.run()
		sys.stdout.buffer.write((self.to_string() + "\n").encode('utf-8'))
		if self.args.statfile:
			try:
				stats = Statistics(self.args.statfile)
				stats.add(*self.to_xml())
				stats.write()
			except ParseError:
				print("ERROR: your statistics file is either an unsupported version or not an XML file.")
		self.exit(ret)

@@ -3,16 +3,24 @@
 port sys
+, codecs
 %0Afrom ar
@@ -144,16 +144,77 @@
 eError%0A%0A
+sys.stdout = codecs.getwriter(%22utf-8%22)(sys.stdout.detach())%0A%0A
 class Fr
@@ -709,72 +709,29 @@
 )%0A%09%09
-sys.stdout.buffer.write((self.to_string() + %22%5Cn%22).encode('utf-8'
+print(self.to_string(
 ))%0A%09

UTF-8, take two