-
Notifications
You must be signed in to change notification settings - Fork 24
/
_common.py
51 lines (44 loc) 路 1.04 KB
/
_common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# built-in
from pathlib import Path
from typing import Iterator
import pygments
from pygments.formatters import TerminalFormatter
from pygments.lexers import PythonLexer
COLORS = dict(
red='\033[91m',
green='\033[92m',
yellow='\033[93m',
blue='\033[94m',
magenta='\033[95m',
end='\033[0m',
)
NOCOLORS = dict(
red='',
green='',
yellow='',
blue='',
magenta='',
end='',
)
def highlight(source: str) -> str:
source = pygments.highlight(
code=source,
lexer=PythonLexer(),
formatter=TerminalFormatter(),
)
return source.rstrip()
def get_paths(path: Path) -> Iterator[Path]:
"""Recursively yields python files.
"""
if not path.exists():
raise FileNotFoundError(str(path))
if path.is_file():
if path.suffix == '.py':
yield path
return
for subpath in path.iterdir():
if subpath.name[0] == '.':
continue
if subpath.name == '__pycache__':
continue
yield from get_paths(subpath)