Skip to content

Commit

Permalink
Merge pull request #136 from IDEMSInternational/fix/windows-linebreak…
Browse files Browse the repository at this point in the history
…-and-utf8-encoding

Fix Windows linebreaks for table export and force utf-8 encoding
  • Loading branch information
geoo89 authored Jun 21, 2024
2 parents 23dfb4a + c0febb6 commit f1804ba
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/rpft/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def create_flows(args):
tags=args.tags,
)

with open(args.output, "w") as export:
with open(args.output, "w", encoding="utf-8") as export:
json.dump(flows, export, indent=4)


Expand Down
4 changes: 2 additions & 2 deletions src/rpft/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def create_flows(input_files, output_file, sheet_format, data_models=None, tags=
flows = parser.parse_all().render()

if output_file:
with open(output_file, "w") as export:
with open(output_file, "w", encoding='utf8') as export:
json.dump(flows, export, indent=4)

return flows
Expand Down Expand Up @@ -70,7 +70,7 @@ def flows_to_sheets(
:param numbered: Use sequential numbers instead of short reps for row IDs.
:returns: None.
"""
with open(input_file, "r") as f:
with open(input_file, "r", encoding='utf-8') as f:
data = json.load(f)
container = RapidProContainer.from_dict(data)
for flow in container.flows:
Expand Down
8 changes: 6 additions & 2 deletions src/rpft/parsers/common/rowdatasheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ def export(self, filename, file_format="csv"):
"""
data = self.convert_to_tablib()
exported_data = data.export(file_format)
write_type = "w" if type(exported_data) is str else "wb"
with open(filename, write_type) as f:
if type(exported_data) is str:
# There is a strange bug where on Windows, csv files would get
# exported with \r\r\n linebreaks. The below fixes this.
exported_data = exported_data.replace("\r", "")
exported_data = exported_data.encode("utf-8")
with open(filename, "wb") as f:
f.write(exported_data)

def convert_to_tablib(self):
Expand Down

0 comments on commit f1804ba

Please sign in to comment.