(lines, path)
| 85 | |
| 86 | |
| 87 | def _ensure_author(lines, path): |
| 88 | author_idx = get_author_idx(path, lines) |
| 89 | license_idx = get_license_idx(path, lines) |
| 90 | first_idx = first_commentable_line(lines) |
| 91 | # 1. Keep existing |
| 92 | if author_idx is not None: |
| 93 | # We have to be careful here -- examples and tutorials are allowed multiple |
| 94 | # authors |
| 95 | if path_multi_author(path): |
| 96 | # Just assume it's correct and return |
| 97 | return |
| 98 | assert license_idx is not None, f"{license_idx=} for {path=}" |
| 99 | for _ in range(license_idx - author_idx - 1): |
| 100 | lines.pop(author_idx + 1) |
| 101 | assert lines[author_idx + 1].startswith(LICENSE_STARTS), lines[license_idx + 1] |
| 102 | del license_idx |
| 103 | lines[author_idx] = AUTHOR_LINE |
| 104 | elif license_idx is not None: |
| 105 | # 2. Before license line if present |
| 106 | lines.insert(license_idx, AUTHOR_LINE) |
| 107 | else: |
| 108 | # 3. First line after docstring |
| 109 | lines.insert(first_idx, AUTHOR_LINE) |
| 110 | # Now make sure it's in the right spot |
| 111 | author_idx = get_author_idx(path, lines) |
| 112 | if author_idx != 0: |
| 113 | if author_idx == first_idx: |
| 114 | # Insert a blank line |
| 115 | lines.insert(author_idx, "") |
| 116 | author_idx += 1 |
| 117 | first_idx += 1 |
| 118 | if author_idx != first_idx: |
| 119 | raise RuntimeError( |
| 120 | "\nLine should have comments as docstring or author line needs to be moved " |
| 121 | "manually to be one blank line after the docstring:\n" |
| 122 | f"{path}: {author_idx=} != {first_idx=}" |
| 123 | ) |
| 124 | |
| 125 | |
| 126 | def _ensure_license(lines, path): |
no test coverage detected