Skip to content

Commit

Permalink
files.py utf-8 encoding
Browse files Browse the repository at this point in the history
Force utf-8 when writing/reading framework files
  • Loading branch information
frdel committed Oct 16, 2024
1 parent 763ec9e commit 3d4f391
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions python/helpers/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

import re

def read_file(relative_path, backup_dirs=None, **kwargs):
def read_file(relative_path, backup_dirs=None, encoding="utf-8", **kwargs):
if backup_dirs is None:
backup_dirs = []

# Try to get the absolute path for the file from the original directory or backup directories
absolute_path = find_file_in_dirs(relative_path, backup_dirs)

# Read the file content
with open(absolute_path) as f:
with open(absolute_path, 'r', encoding=encoding) as f:
content = remove_code_fences(f.read())

# Replace placeholders with values from kwargs
Expand Down Expand Up @@ -63,10 +63,10 @@ def find_file_in_dirs(file_path, backup_dirs):
def remove_code_fences(text):
return re.sub(r'~~~\w*\n|~~~', '', text)

def write_file(relative_path:str, content:str):
def write_file(relative_path:str, content:str, encoding:str="utf-8"):
abs_path = get_abs_path(relative_path)
os.makedirs(os.path.dirname(abs_path), exist_ok=True)
with open(abs_path, 'w') as f:
with open(abs_path, 'w', encoding=encoding) as f:
f.write(content)

def delete_file(relative_path:str):
Expand Down

0 comments on commit 3d4f391

Please sign in to comment.