Skip to content

Commit

Permalink
Try to parse in two encodings
Browse files Browse the repository at this point in the history
  • Loading branch information
canihavesomecoffee committed Jan 20, 2020
1 parent b1a0f61 commit f5ed6cd
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions mod_test/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,22 @@ def generate_html_diff(self, base_path: str, to_view: bool = True) -> str:
file_ok = os.path.join(base_path, self.expected + self.regression_test_output.correct_extension)
file_fail = os.path.join(base_path, self.got + self.regression_test_output.correct_extension)
log.debug(f"Generate diff for {file_ok} vs {file_fail}")
lines_ok = open(file_ok, 'U').readlines()
lines_fail = open(file_fail, 'U').readlines()
lines_ok = self.read_lines(file_ok)
lines_fail = self.read_lines(file_fail)

return diff.get_html_diff(lines_ok, lines_fail, to_view)

@staticmethod
def read_lines(file_name: str) -> List[str]:
"""
Tries to load a file in different encodings.
:param file_name: The name to read lines from.
:type file_name: str
:return: A list of lines.
:rtype: List[str]
"""
try:
return open(file_name, encoding='utf8').readlines()
except UnicodeDecodeError:
return open(file_name, encoding='cp1252').readlines()

0 comments on commit f5ed6cd

Please sign in to comment.