files.py utf-8 encoding

Force utf-8 when writing/reading framework files

frdel committed Oct 16, 2024 at 22:03 UTC 3d4f391cfafc0c3289faddd14da8726f773c039f
1 file changed +4 -4
python/helpers/files.py
+4 -4
@@ -3,7 +3,7 @@ import os, re
3
4 import re
5
6 -def read_file(relative_path, backup_dirs=None, **kwargs):
6 +def read_file(relative_path, backup_dirs=None, encoding="utf-8", **kwargs):
7 if backup_dirs is None:
8 backup_dirs = []
9
@@ -11,7 +11,7 @@ def read_file(relative_path, backup_dirs=None, **kwargs):
11 absolute_path = find_file_in_dirs(relative_path, backup_dirs)
12
13 # Read the file content
14 - with open(absolute_path) as f:
14 + with open(absolute_path, 'r', encoding=encoding) as f:
15 content = remove_code_fences(f.read())
16
17 # Replace placeholders with values from kwargs
@@ -63,10 +63,10 @@ def find_file_in_dirs(file_path, backup_dirs):
63 def remove_code_fences(text):
64 return re.sub(r'~~~\w*\n|~~~', '', text)
65
66 -def write_file(relative_path:str, content:str):
66 +def write_file(relative_path:str, content:str, encoding:str="utf-8"):
67 abs_path = get_abs_path(relative_path)
68 os.makedirs(os.path.dirname(abs_path), exist_ok=True)
69 - with open(abs_path, 'w') as f:
69 + with open(abs_path, 'w', encoding=encoding) as f:
70 f.write(content)
71
72 def delete_file(relative_path:str):