|
| 1 | +import json |
| 2 | +import os |
| 3 | +import pathlib |
| 4 | +import sys |
| 5 | +import textwrap |
| 6 | +from dataclasses import dataclass |
| 7 | +from typing import Dict, List, Literal, Optional |
| 8 | + |
| 9 | + |
| 10 | +@dataclass |
| 11 | +class HandsOnConfig: |
| 12 | + hands_on_name: str |
| 13 | + requires_git: bool |
| 14 | + requires_github: bool |
| 15 | + |
| 16 | + |
| 17 | +def confirm(prompt: str, default: bool) -> bool: |
| 18 | + str_result = input(f"{prompt} (defaults to {'y' if default else 'N'}) [y/N]: ") |
| 19 | + bool_value = default if str_result.strip() == "" else str_result.lower() == "y" |
| 20 | + return bool_value |
| 21 | + |
| 22 | + |
| 23 | +def prompt(prompt: str, default: str) -> str: |
| 24 | + str_result = input(f"{prompt} (defaults to '{default}'): ") |
| 25 | + if str_result.strip() == "": |
| 26 | + return default |
| 27 | + return str_result.strip() |
| 28 | + |
| 29 | + |
| 30 | +def get_exercise_config() -> HandsOnConfig: |
| 31 | + hands_on_name = input("Hands-on name: ") |
| 32 | + requires_git = confirm("Requires Git?", True) |
| 33 | + requires_github = confirm("Requires Github?", True) |
| 34 | + |
| 35 | + return HandsOnConfig( |
| 36 | + hands_on_name=hands_on_name, |
| 37 | + requires_git=requires_git, |
| 38 | + requires_github=requires_github, |
| 39 | + ) |
| 40 | + |
| 41 | + |
| 42 | +def create_download_py_file(config: HandsOnConfig) -> None: |
| 43 | + with open(f"{config.hands_on_name}.py", "w") as download_script_file: |
| 44 | + download_script = f""" |
| 45 | + import subprocess |
| 46 | + from sys import exit |
| 47 | + from typing import List, Optional |
| 48 | +
|
| 49 | + __requires_git__ = {config.requires_git} |
| 50 | + __requires_github__ = {config.requires_github} |
| 51 | +
|
| 52 | +
|
| 53 | + def run_command(command: List[str], verbose: bool) -> Optional[str]: |
| 54 | + try: |
| 55 | + result = subprocess.run( |
| 56 | + command, |
| 57 | + capture_output=True, |
| 58 | + text=True, |
| 59 | + check=True, |
| 60 | + ) |
| 61 | + if verbose: |
| 62 | + print(result.stdout) |
| 63 | + return result.stdout |
| 64 | + except subprocess.CalledProcessError as e: |
| 65 | + if verbose: |
| 66 | + print(e.stderr) |
| 67 | + exit(1) |
| 68 | +
|
| 69 | +
|
| 70 | + def setup(verbose: bool = False): |
| 71 | + pass |
| 72 | + """ |
| 73 | + download_script_file.write(textwrap.dedent(download_script).lstrip()) |
| 74 | + |
| 75 | + |
| 76 | +def main(): |
| 77 | + config = get_exercise_config() |
| 78 | + os.chdir("hands_on") |
| 79 | + create_download_py_file(config) |
| 80 | + |
| 81 | + |
| 82 | +if __name__ == "__main__": |
| 83 | + main() |
0 commit comments