Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quantum-kittens
GitHub Repository: quantum-kittens/platypus
Path: blob/main/scripts/content_checks/tools.py
3855 views
1
"""This file contains functions shared by scripts in this folder
2
"""
3
import sys
4
from pathlib import Path
5
6
NB_ROOT = 'notebooks'
7
NB_PATHS_LIST = './scripts/content_checks/notebook_paths.txt'
8
9
def parse_args(argv):
10
"""Parses sys.argv to find notebook paths and switches, otherwise gets
11
list of paths from `NB_PATHS_LIST`
12
13
Returns a tuple with:
14
- A set of switches (arguments starting with '--')
15
- A list of filepaths
16
"""
17
argv = argv[1:] if len(argv) > 1 else []
18
19
switches = set()
20
for a in argv:
21
if a.startswith('--'):
22
switches.add(a)
23
24
filepaths = []
25
for a in argv:
26
if a not in switches:
27
filepaths.append(a)
28
29
if filepaths == []:
30
# No files passed; read from text file
31
with open(NB_PATHS_LIST, encoding='utf-8') as f:
32
for path in f.readlines():
33
path = path.strip()
34
if path == '' or path.startswith('#'):
35
continue
36
filepaths.append(path)
37
38
# Make all paths of form ./notebook_root/folder/notebook.ipynb
39
for idx, path in enumerate(filepaths):
40
path = Path(path)
41
if path.suffix == '':
42
path = path.with_suffix('.ipynb')
43
44
if not path.exists():
45
path = Path(NB_ROOT) / path
46
47
filepaths[idx] = path
48
49
return switches, filepaths
50
51
52
TSTYLE = { # Terminal styling codes
53
'bold': '\033[1m',
54
'faint': '\033[30m',
55
'suggestion': '\033[94m',
56
'warning': '\033[93m',
57
'error': '\033[91m',
58
'success': '\033[32m',
59
'end': '\033[0m'
60
}
61
62
63
def style(style, text):
64
"""Style string using terminal escape codes"""
65
return f"{TSTYLE[style]}{text}{TSTYLE['end']}"
66
67
68
def indent(s):
69
"""Indent text block with vertical line margins"""
70
s = s.replace('\n', '\n' + style('faint', '│ '))
71
s = s[::-1].replace('│', '╵', 1)[::-1]
72
return style('faint', '╷ ') + s
73
74