33from types import TracebackType
44from typing import Any , Literal
55
6- import click
76from rich .segment import Segment
87from rich .style import Style
98from rich .text import Text
1615)
1716
1817logger = logging .getLogger (__name__ )
19- VERSION_CHECK_CONTEXT_KEY = "fastapi_cloud_cli.version_check"
2018
2119
2220class FastAPIStyle (TaggedStyle ):
@@ -34,7 +32,9 @@ def _get_tag_segments(
3432 tag_segments , left_padding = super ()._get_tag_segments (
3533 metadata , is_animated , done , animation_status = animation_status
3634 )
35+
3736 tag_style = metadata .get ("tag_style" )
37+
3838 if isinstance (tag_style , (str , Style )):
3939 style = self .console .get_style (tag_style )
4040 tag_segments = [
@@ -71,83 +71,42 @@ def __init__(
7171 self ,
7272 style : BaseStyle | None = None ,
7373 theme : RichToolkitTheme | None = None ,
74- handle_keyboard_interrupts : bool = True ,
75- print_spacing : bool = True ,
7674 ) -> None :
77- super ().__init__ (
78- style = style ,
79- theme = theme ,
80- handle_keyboard_interrupts = handle_keyboard_interrupts ,
81- )
82- self ._print_spacing = print_spacing
83- self ._version_check : BackgroundVersionCheck | None = None
84- self ._print_update_on_exit = False
85-
86- def __enter__ (self ) -> "FastAPIRichToolkit" :
75+ super ().__init__ (style = style , theme = theme )
8776 self ._version_check = self ._get_version_check ()
8877
89- if self ._print_spacing :
90- self .console .print ()
91- return self
92-
9378 def __exit__ (
9479 self ,
9580 exc_type : type [BaseException ] | None ,
9681 exc_value : BaseException | None ,
9782 traceback : TracebackType | None ,
9883 ) -> bool | None :
99- is_keyboard_interrupt = exc_type is KeyboardInterrupt
100-
101- if is_keyboard_interrupt and self ._version_check is not None :
102- self ._version_check .suppress ()
103- elif self ._print_update_on_exit :
104- self ._print_update_message ()
84+ self ._print_update_message ()
10585
106- if self ._print_spacing and not is_keyboard_interrupt :
107- self .console .print ()
108-
109- if self .handle_keyboard_interrupts and is_keyboard_interrupt :
110- return True
111-
112- return None
86+ return super ().__exit__ (
87+ exc_type ,
88+ exc_value ,
89+ traceback ,
90+ )
11391
11492 def _get_version_check (self ) -> BackgroundVersionCheck | None :
11593 if os .environ .get (DISABLE_VERSION_CHECK_ENV ) == "1" :
11694 return None
11795
118- context = click .get_current_context (silent = True )
119- if context is None :
120- version_check = BackgroundVersionCheck ()
121- version_check .start ()
122- self ._print_update_on_exit = True
123- return version_check
124-
125- stored_version_check = context .meta .get (VERSION_CHECK_CONTEXT_KEY )
126- if isinstance (stored_version_check , BackgroundVersionCheck ):
127- return stored_version_check
128-
12996 version_check = BackgroundVersionCheck ()
13097 version_check .start ()
131- context .meta [VERSION_CHECK_CONTEXT_KEY ] = version_check
132- context .call_on_close (self ._print_update_message )
13398
13499 return version_check
135100
136101 def _print_update_message (self ) -> None :
137102 if self ._version_check is None :
138103 return
139104
140- message = self ._version_check .get_update_message ()
141- if message :
105+ if message := self ._version_check .get_update_message ():
142106 self .print (Text .from_markup (message ), tag = "update" , tag_style = "tag.update" )
143107
144108
145- def get_rich_toolkit (
146- minimal : bool = False ,
147- * ,
148- print_spacing : bool = True ,
149- handle_keyboard_interrupts : bool = True ,
150- ) -> RichToolkit :
109+ def get_rich_toolkit (minimal : bool = False ) -> RichToolkit :
151110 style = MinimalStyle () if minimal else FastAPIStyle (tag_width = 11 )
152111
153112 theme = RichToolkitTheme (
@@ -166,8 +125,4 @@ def get_rich_toolkit(
166125 },
167126 )
168127
169- return FastAPIRichToolkit (
170- theme = theme ,
171- handle_keyboard_interrupts = handle_keyboard_interrupts ,
172- print_spacing = print_spacing ,
173- )
128+ return FastAPIRichToolkit (theme = theme )
0 commit comments