This renders the file to the out_file location savig the new file to tmp_file location, the copying it to out-file and deleting tmp_file this is done to prevent issues if the in and the out file are the same
(in_file, out_file, library)
| 14 | |
| 15 | # TODO: refine this logic |
| 16 | def render(in_file, out_file, library): |
| 17 | """ |
| 18 | This renders the file to the out_file location |
| 19 | savig the new file to tmp_file location, the copying it to out-file and deleting tmp_file |
| 20 | this is done to prevent issues if the in and the out file are the same |
| 21 | """ |
| 22 | tmp_file = "./.tmp-file.md" |
| 23 | open(tmp_file, "a").close() |
| 24 | books_not_reached = True |
| 25 | with open(tmp_file, "w") as out_file_tmp: |
| 26 | with open(in_file) as original_file: |
| 27 | for line in original_file: |
| 28 | |
| 29 | if line.strip() in library: |
| 30 | if not books_not_reached: |
| 31 | out_file_tmp.write("\n") |
| 32 | books_not_reached = False |
| 33 | |
| 34 | # render chapter and start of the table |
| 35 | out_file_tmp.write(line) |
| 36 | if len(library[line.strip()]) > 0: |
| 37 | out_file_tmp.write( |
| 38 | "| Name | Author | Goodreads Rating | Year Published | \n" |
| 39 | ) |
| 40 | out_file_tmp.write( |
| 41 | "|------|--------|------------------|----------------| \n" |
| 42 | ) |
| 43 | # render books |
| 44 | for book in library[line.strip()]: |
| 45 | out_file_tmp.write(render_book_line(book)) |
| 46 | elif books_not_reached: |
| 47 | out_file_tmp.write(line) |
| 48 | elif line.startswith("## License"): |
| 49 | out_file_tmp.write("\n") |
| 50 | out_file_tmp.write("\n") |
| 51 | out_file_tmp.write(line) |
| 52 | books_not_reached = True |
| 53 | |
| 54 | copyfile(tmp_file, out_file) |
| 55 | os.remove(tmp_file) |
no test coverage detected