stav121/i3wm-themer

Problem when default platform encoding is utf-8.

Closed this issue · 1 comments

Hey I stumbled upon an error while trying to run the themer. My platform encoding is utf-8 so the themer threw this error:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 6821: ordinal not in range(128)

I fixed it with an ok solution in my opinion and decided to share. Basically i added import sys command and used it to resolve default platform encoding and added it to read files commands as a parameter. Changes to other files are not neccesary.
file src/replace_line.py

from tempfile import mkstemp
from shutil import move
from os import fdopen, remove
import sys

import msgfunc as prnt

def replace_line(file, pattern, new_line):
    fh, abs_path = mkstemp()
    with fdopen(fh, 'w', encoding=sys.getdefaultencoding()) as new_file:
        with open(file, encoding=sys.getdefaultencoding()) as old_file:
            for line in old_file:
                if line.startswith( pattern ):
                    pl1 = line
                    pl1 = pl1.rstrip()
                    pl2 = new_line
                    pl2 = pl2.rstrip()
                    prnt.prnt( '-s', 'Replacing line: \''+pl1+'\' with \''+pl2+'\'')
                    if(new_file.write( new_line+'\n' )):
                        prnt.prnt( '-s', 'Success!')
                    else:
                        prnt.prnt( '-f', 'Failed!')
                else:
                    new_file.write( line )
                #new_file.write( new_line if pattern in line else line)
    remove(file)
    move(abs_path, file)

Oh dog ! It was my bad locale configuration all along that caused this issue.