|
1 | 1 | from __future__ import annotations as _annotations |
2 | 2 |
|
3 | 3 | import re |
4 | | -import sys |
5 | 4 | from dataclasses import dataclass |
6 | 5 | from pathlib import Path |
7 | 6 | from textwrap import dedent |
@@ -99,29 +98,30 @@ def __str__(self): |
99 | 98 | return f'{path}:{self.start_line}-{self.end_line}' |
100 | 99 |
|
101 | 100 |
|
102 | | -def find_examples(*directories: str, run_on_windows: bool = False) -> Iterable[CodeExample]: |
| 101 | +def find_examples(*paths: str, skip: bool = False) -> Iterable[CodeExample]: |
103 | 102 | """ |
104 | 103 | Find Python code examples in markdown files and python file docstrings. |
105 | 104 |
|
106 | | - Yields `CodeExample` objects wrapped in a `pytest.param` object. |
| 105 | + :param paths: Directories or files to search for examples in. |
| 106 | + :param skip: Whether to exit early and not search for examples, useful when running on windows where search fails. |
| 107 | + :return: A generator of `CodeExample` objects. |
107 | 108 | """ |
108 | | - if not run_on_windows and sys.platform == 'win32': |
109 | | - # this avoids errors reading files on windows as pytest parametrize is eager |
| 109 | + if skip: |
110 | 110 | return |
111 | 111 |
|
112 | | - for d in directories: |
113 | | - dir_path = Path(d) |
114 | | - if dir_path.is_file(): |
115 | | - paths = [dir_path] |
116 | | - elif dir_path.is_dir(): |
117 | | - paths = dir_path.glob('**/*') |
| 112 | + for s in paths: |
| 113 | + path = Path(s) |
| 114 | + if path.is_file(): |
| 115 | + sub_paths = [path] |
| 116 | + elif path.is_dir(): |
| 117 | + sub_paths = path.glob('**/*') |
118 | 118 | else: |
119 | | - raise ValueError(f'Not a file or directory: {d!r}') |
| 119 | + raise ValueError(f'Not a file or directory: {s!r}') |
120 | 120 |
|
121 | | - for path in paths: |
| 121 | + for path in sub_paths: |
122 | 122 | group = uuid4() |
123 | 123 | if path.suffix == '.py': |
124 | | - code = path.read_text() |
| 124 | + code = path.read_text('utf-8') |
125 | 125 | for m_docstring in re.finditer(r'(^ *)(r?"""\n)(.+?)\1"""', code, flags=re.M | re.S): |
126 | 126 | start_line = code[: m_docstring.start()].count('\n') + 1 |
127 | 127 | docstring = m_docstring.group(3) |
|
0 commit comments