Refactors app

This commit is contained in:
2026-05-21 00:46:33 +02:00
parent 0e31246471
commit a823892822
2 changed files with 48 additions and 44 deletions
+2
View File
@@ -12,6 +12,8 @@ characters:
Œ: Ś
Æ: Ć
Ñ: Ń
¥: Ą
Ê: Ę
ignored:
- ą
+46 -44
View File
@@ -6,61 +6,63 @@ import subprocess
import chardet
import yaml
def notify(header, body, exitCode):
def notify(header, body, exit_code):
subprocess.run(['/usr/bin/notify-send', header, body])
print(body)
exit(exitCode)
sys.exit(exit_code)
def detectEncodingOfAFile(filepath):
def read_file(filepath):
with open(filepath, 'rb') as f:
encoding = chardet.detect(f.read())
f.close()
print('Detected encoding: ' + encoding['encoding'])
return encoding['encoding']
absPath = os.path.dirname(os.path.abspath(__file__))
raw = f.read()
result = chardet.detect(raw)
encoding = result['encoding'] or 'cp1250'
print(f'Detected encoding: {encoding}')
ignored = raw.decode(encoding, errors='replace').count('')
if ignored:
print(f'Warning: {ignored} undecodable bytes were ignored')
return raw.decode(encoding, errors='ignore')
if len(sys.argv) < 2:
notify('Błąd', 'Aby naprawić napisy, musisz wskazać plik', 1)
filepath = sys.argv[1]
if not os.path.isfile(filepath):
notify('Błąd', 'Aby naprawić napisy, musisz wskazać plik', 1)
if __name__ == '__main__':
abs_path = os.path.dirname(os.path.abspath(__file__))
with open(absPath + os.sep + 'config.yaml', 'r') as configFile:
config = yaml.safe_load(configFile)
configFile.close()
map = config['characters']
if len(sys.argv) < 2:
notify('Błąd', 'Aby naprawić napisy, musisz wskazać plik', 1)
try:
encoding = detectEncodingOfAFile(filepath)
with open(filepath, 'r', encoding=encoding, errors='ignore') as f:
text = f.read()
f.close()
filepath = sys.argv[1]
if not os.path.isfile(filepath):
notify('Błąd', 'Aby naprawić napisy, musisz wskazać plik', 1)
if text[0] == '?':
file = file[1:]
with open(abs_path + os.sep + 'config.yaml', 'r') as config_file:
config = yaml.safe_load(config_file)
char_map = config['characters']
for badChar, goodChar in map.items():
text = text.replace(badChar, goodChar)
try:
text = read_file(filepath)
badChars = []
for c in text:
if ord(c) > 127 and not c in badChars:
badChars.append(c)
for char in config['ignored']:
if char in badChars:
badChars.remove(char)
if len(badChars):
print('Znalezione znaki, które można dodać do mapy: ' + badChars.__str__())
if text and text[0] == '?':
text = text[1:]
with open(filepath, 'w', newline='\n') as f:
f.write(text)
f.close()
except Exception as e:
notify('Błąd', 'Naprawianie napisów nie powiodło się: ' + e.__str__(), 2)
for bad_char, good_char in char_map.items():
text = text.replace(bad_char, good_char)
filename = filepath.split(os.sep)
filename = filename[-1]
bad_chars = []
for c in text:
if ord(c) > 127 and c not in bad_chars:
bad_chars.append(c)
for char in config['ignored']:
if char in bad_chars:
bad_chars.remove(char)
if bad_chars:
print('Znalezione znaki, które można dodać do mapy: ' + str(bad_chars))
notify('Naprawiono napisy', 'Napisy ' + filename + ' zostały naprawione', 0)
with open(filepath, 'w', encoding='utf-8', newline='\n') as f:
f.write(text)
except Exception as e:
notify('Błąd', 'Naprawianie napisów nie powiodło się: ' + str(e), 2)
filename = os.path.basename(filepath)
notify('Naprawiono napisy', 'Napisy ' + filename + ' zostały naprawione', 0)