-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathastare.py
126 lines (98 loc) · 3.45 KB
/
astare.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"""The main application code for astare."""
##############################################################################
# Python imports.
import argparse
from typing import Any
from pathlib import Path
from importlib.metadata import version
##############################################################################
# Pygments imports.
from pygments.styles import get_all_styles
##############################################################################
# Textual imports.
from textual.app import App
##############################################################################
# Local imports.
from textual_astview import Source, __version__
from .screens import MainDisplay
##############################################################################
class Astare(App[None]):
"""Main Textual application class."""
TITLE = "astare"
"""The main title of the app."""
SUB_TITLE = f"A Python AST Explorer ({__version__})"
"""The sub title of the app."""
def __init__(self, cli_args: argparse.Namespace, *args: Any, **kwargs: Any) -> None:
"""Initialise the app."""
super().__init__(*args, **kwargs)
self._args = cli_args
def on_mount(self) -> None:
"""Set up the application on startup."""
self.push_screen(MainDisplay(self._args))
##############################################################################
def py_file(path: str) -> Path:
"""Check that the file we're being asked to look at seems fine
Args:
path: The argument.
Returns:
The `Path` to the file if it looks okay.
"""
if not (candidate := Path(path)).exists():
raise argparse.ArgumentTypeError(f"{path} does not exist")
return candidate
##############################################################################
def get_args() -> argparse.Namespace:
"""Get the command line arguments.
Returns:
The arguments.
"""
# Create the argument parser object.
parser = argparse.ArgumentParser(
prog="astare",
description="A simple terminal-based AST explorer.",
epilog=f"v{__version__}",
)
# Add --dark-theme
parser.add_argument(
"-t",
"--dark-theme",
choices=list(get_all_styles()),
metavar="THEME",
default=Source.DEFAULT_DARK_THEME,
help="Set the dark mode theme for the source display.",
)
# Add --light-theme
parser.add_argument(
"-T",
"--light-theme",
choices=list(get_all_styles()),
metavar="THEME",
default=Source.DEFAULT_LIGHT_THEME,
help="Set the light mode theme for the source display.",
)
# Add --themes
parser.add_argument(
"--themes",
action="version",
help="List the available theme names.",
version=", ".join(sorted(list(get_all_styles()))),
)
# Add --version
parser.add_argument(
"-v",
"--version",
help="Show version information.",
action="version",
version=f"%(prog)s {__version__} (Textual v{version( 'textual' )})",
)
# The remainder is the file to explore.
parser.add_argument(
"file", help="The file to explore", type=py_file, default=".", nargs="?"
)
# Return the arguments.
return parser.parse_args()
##############################################################################
def main() -> None:
"""Main entry point for astare."""
Astare(get_args()).run()
### astare.py ends here