|
7 | 7 | ]
|
8 | 8 |
|
9 | 9 | import html
|
10 |
| -import os |
11 | 10 | import re
|
| 11 | +from pathlib import Path |
12 | 12 |
|
13 | 13 | import numpy as np
|
14 | 14 | from pygments import highlight
|
@@ -88,7 +88,7 @@ def construct(self):
|
88 | 88 |
|
89 | 89 | Parameters
|
90 | 90 | ----------
|
91 |
| - file_name : :class:`str` |
| 91 | + file_name |
92 | 92 | Name of the code file to display.
|
93 | 93 | code : :class:`str`
|
94 | 94 | If ``file_name`` is not specified, a code string can be
|
@@ -153,7 +153,7 @@ def construct(self):
|
153 | 153 |
|
154 | 154 | def __init__(
|
155 | 155 | self,
|
156 |
| - file_name=None, |
| 156 | + file_name: str | os.PathLike = None, |
157 | 157 | code=None,
|
158 | 158 | tab_width=3,
|
159 | 159 | line_spacing=0.3,
|
@@ -199,8 +199,7 @@ def __init__(
|
199 | 199 | self.file_name = file_name
|
200 | 200 | if self.file_name:
|
201 | 201 | self._ensure_valid_file()
|
202 |
| - with open(self.file_path, encoding="utf-8") as f: |
203 |
| - self.code_string = f.read() |
| 202 | + self.code_string = self.file_path.read_text(encoding="utf-8") |
204 | 203 | elif code:
|
205 | 204 | self.code_string = code
|
206 | 205 | else:
|
@@ -284,16 +283,16 @@ def _ensure_valid_file(self):
|
284 | 283 | if self.file_name is None:
|
285 | 284 | raise Exception("Must specify file for Code")
|
286 | 285 | possible_paths = [
|
287 |
| - os.path.join(os.path.join("assets", "codes"), self.file_name), |
288 |
| - os.path.expanduser(self.file_name), |
| 286 | + Path() / "assets" / "codes" / self.file_name, |
| 287 | + Path(self.file_name).expanduser(), |
289 | 288 | ]
|
290 | 289 | for path in possible_paths:
|
291 |
| - if os.path.exists(path): |
| 290 | + if path.exists(): |
292 | 291 | self.file_path = path
|
293 | 292 | return
|
294 | 293 | error = (
|
295 |
| - f"From: {os.getcwd()}, could not find {self.file_name} at either " |
296 |
| - + f"of these locations: {possible_paths}" |
| 294 | + f"From: {Path.cwd()}, could not find {self.file_name} at either " |
| 295 | + + f"of these locations: {list(map(str, possible_paths))}" |
297 | 296 | )
|
298 | 297 | raise OSError(error)
|
299 | 298 |
|
@@ -369,20 +368,9 @@ def _gen_html_string(self):
|
369 | 368 | )
|
370 | 369 |
|
371 | 370 | if self.generate_html_file:
|
372 |
| - os.makedirs( |
373 |
| - os.path.join("assets", "codes", "generated_html_files"), |
374 |
| - exist_ok=True, |
375 |
| - ) |
376 |
| - with open( |
377 |
| - os.path.join( |
378 |
| - "assets", |
379 |
| - "codes", |
380 |
| - "generated_html_files", |
381 |
| - self.file_name + ".html", |
382 |
| - ), |
383 |
| - "w", |
384 |
| - ) as file: |
385 |
| - file.write(self.html_string) |
| 371 | + output_folder = Path() / "assets" / "codes" / "generated_html_files" |
| 372 | + output_folder.mkdir(parents=True, exist_ok=True) |
| 373 | + (output_folder / f"{self.file_name}.html").write_text(self.html_string) |
386 | 374 |
|
387 | 375 | def _gen_code_json(self):
|
388 | 376 | """Function to background_color, generate code_json and tab_spaces from html_string.
|
@@ -559,7 +547,7 @@ def _hilite_me(
|
559 | 547 | Defines whether line numbers should be inserted in the html file.
|
560 | 548 | divstyles : :class:`str`
|
561 | 549 | Some html css styles.
|
562 |
| - file_path : :class:`str` |
| 550 | + file_path : :class:`pathlib.Path` |
563 | 551 | Path of code file.
|
564 | 552 | line_no_from : :class:`int`
|
565 | 553 | Defines the first line's number in the line count.
|
|
0 commit comments