Path: blob/main/scripts/content_checks/tools.py
3855 views
"""This file contains functions shared by scripts in this folder1"""2import sys3from pathlib import Path45NB_ROOT = 'notebooks'6NB_PATHS_LIST = './scripts/content_checks/notebook_paths.txt'78def parse_args(argv):9"""Parses sys.argv to find notebook paths and switches, otherwise gets10list of paths from `NB_PATHS_LIST`1112Returns a tuple with:13- A set of switches (arguments starting with '--')14- A list of filepaths15"""16argv = argv[1:] if len(argv) > 1 else []1718switches = set()19for a in argv:20if a.startswith('--'):21switches.add(a)2223filepaths = []24for a in argv:25if a not in switches:26filepaths.append(a)2728if filepaths == []:29# No files passed; read from text file30with open(NB_PATHS_LIST, encoding='utf-8') as f:31for path in f.readlines():32path = path.strip()33if path == '' or path.startswith('#'):34continue35filepaths.append(path)3637# Make all paths of form ./notebook_root/folder/notebook.ipynb38for idx, path in enumerate(filepaths):39path = Path(path)40if path.suffix == '':41path = path.with_suffix('.ipynb')4243if not path.exists():44path = Path(NB_ROOT) / path4546filepaths[idx] = path4748return switches, filepaths495051TSTYLE = { # Terminal styling codes52'bold': '\033[1m',53'faint': '\033[30m',54'suggestion': '\033[94m',55'warning': '\033[93m',56'error': '\033[91m',57'success': '\033[32m',58'end': '\033[0m'59}606162def style(style, text):63"""Style string using terminal escape codes"""64return f"{TSTYLE[style]}{text}{TSTYLE['end']}"656667def indent(s):68"""Indent text block with vertical line margins"""69s = s.replace('\n', '\n' + style('faint', '│ '))70s = s[::-1].replace('│', '╵', 1)[::-1]71return style('faint', '╷ ') + s727374