import sys
import os
import shutil


TEMP_DIR = 'temp'
CODECS = ['utf8', 'cp1252', 'win1251']


class Utf8LineConverter(object):
    def __init__(self, input_path):
        self.input_file = file(input_path)
        self.output_file = None

    def split_lines(self):
        os.makedirs(TEMP_DIR)

        for num, line in enumerate(self.input_file.readlines()):
            f = open(os.path.join(TEMP_DIR, "%06d.txt" % num), 'w+')
            f.write(line)
            f.close()

    def convert(self, output_path, encoding='utf8'):
        self.output_file = file(output_path, 'w+')

        self.split_lines()

        for fn in os.listdir(TEMP_DIR):
            fp = os.path.join(TEMP_DIR, fn)
            if os.path.isfile(fp):
                write_line = None
                read_line = open(fp).read()
                for c in CODECS:
                    try:
                        write_line = read_line.decode(c).encode(encoding)
                        print c
                        break
                    except (UnicodeEncodeError, UnicodeDecodeError):
                        pass

                if not write_line:
                    raise Exception(u'CANT ENCODE LINE: %s - %s' % (fn, read_line))
                self.output_file.write(write_line)
                print fp

        shutil.rmtree(TEMP_DIR)

        self.output_file.close()


if __name__ == "__main__":
    input_file = sys.argv[1]
    output_file = sys.argv[2]

    converter = Utf8LineConverter(input_file)
    converter.convert(output_file)


@@ -29,17 +29,16 @@
 shutil%0A%0A
-%0A
 TEMP_DIR
@@ -44,14 +44,14 @@
 R = 
-'
+%22
 temp
-'
+%22
 %0ACOD
@@ -61,35 +61,35 @@
  = %5B
-'
+%22
 utf8
-', '
+%22, %22
 cp1252
-', '
+%22, %22
 win1251
-'
+%22
 %5D%0A%0A%0A
@@ -419,20 +419,20 @@
 %25 num), 
-'w+'
+%22w+%22
 )%0A      
@@ -522,14 +522,14 @@
 ing=
-'
+%22
 utf8
-'
+%22
 ):%0A 
@@ -576,12 +576,12 @@
 th, 
-'w+'
+%22w+%22
 )%0A%0A 
@@ -965,18 +965,19 @@
    print
- c
+(c)
 %0A       
@@ -1170,17 +1170,17 @@
 eption(u
-'
+%22
 CANT ENC
@@ -1200,9 +1200,9 @@
 - %25s
-'
+%22
  %25 (
@@ -1289,19 +1289,20 @@
    print
- 
+(
 fp
+)
 %0A%0A      
@@ -1531,9 +1531,8 @@
 t_file)%0A
-%0A

Apply flake8 fixes and black formatting to translations package