wiederverwendbar 0.8.5__py3-none-any.whl → 0.9.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. wiederverwendbar/__init__.py +8 -6
  2. wiederverwendbar/branding/__init__.py +1 -0
  3. wiederverwendbar/branding/settings.py +85 -0
  4. wiederverwendbar/console/__init__.py +3 -0
  5. wiederverwendbar/console/console.py +199 -0
  6. wiederverwendbar/console/out_files.py +19 -0
  7. wiederverwendbar/console/settings.py +9 -0
  8. wiederverwendbar/default.py +4 -1
  9. wiederverwendbar/fastapi/__init__.py +3 -0
  10. wiederverwendbar/fastapi/app.py +385 -0
  11. wiederverwendbar/fastapi/dependencies.py +11 -0
  12. wiederverwendbar/fastapi/settings.py +39 -0
  13. wiederverwendbar/functions/is_coroutine_function.py +19 -0
  14. wiederverwendbar/inspect.py +26 -0
  15. wiederverwendbar/logger/__init__.py +0 -1
  16. wiederverwendbar/logger/handlers/rich_console_handler.py +15 -6
  17. wiederverwendbar/logger/handlers/stream_console_handler.py +3 -16
  18. wiederverwendbar/logger/log_levels.py +4 -0
  19. wiederverwendbar/logger/settings.py +15 -20
  20. wiederverwendbar/pydantic/file_config.py +20 -4
  21. wiederverwendbar/rich/__init__.py +2 -0
  22. wiederverwendbar/rich/console.py +215 -0
  23. wiederverwendbar/rich/settings.py +26 -0
  24. wiederverwendbar/sqlalchemy/base.py +4 -4
  25. wiederverwendbar/task_manger/task.py +1 -4
  26. wiederverwendbar/task_manger/task_manager.py +14 -19
  27. wiederverwendbar/typer/__init__.py +3 -1
  28. wiederverwendbar/typer/app.py +172 -0
  29. wiederverwendbar/typer/settings.py +14 -0
  30. wiederverwendbar/warnings.py +6 -0
  31. {wiederverwendbar-0.8.5.dist-info → wiederverwendbar-0.9.0.dist-info}/METADATA +9 -6
  32. {wiederverwendbar-0.8.5.dist-info → wiederverwendbar-0.9.0.dist-info}/RECORD +34 -49
  33. wiederverwendbar/examples/__init__.py +0 -0
  34. wiederverwendbar/examples/before_after_wrap.py +0 -74
  35. wiederverwendbar/examples/colors.py +0 -16
  36. wiederverwendbar/examples/extended_thread.py +0 -28
  37. wiederverwendbar/examples/file_config.py +0 -11
  38. wiederverwendbar/examples/indexable_model.py +0 -19
  39. wiederverwendbar/examples/logger.py +0 -31
  40. wiederverwendbar/examples/logger_context/__init__.py +0 -0
  41. wiederverwendbar/examples/logger_context/example.py +0 -58
  42. wiederverwendbar/examples/logger_context/example_module.py +0 -13
  43. wiederverwendbar/examples/mongoengine/__init__.py +0 -0
  44. wiederverwendbar/examples/mongoengine/automatic_reference.py +0 -25
  45. wiederverwendbar/examples/mongoengine/db.py +0 -7
  46. wiederverwendbar/examples/mongoengine/log_streamer.py +0 -9
  47. wiederverwendbar/examples/mongoengine/logger.py +0 -25
  48. wiederverwendbar/examples/post_init.py +0 -29
  49. wiederverwendbar/examples/route.py +0 -12
  50. wiederverwendbar/examples/singletons.py +0 -59
  51. wiederverwendbar/examples/sqlalchemy/__init__.py +0 -0
  52. wiederverwendbar/examples/sqlalchemy/db.py +0 -89
  53. wiederverwendbar/examples/starlette_admin/__init__.py +0 -0
  54. wiederverwendbar/examples/starlette_admin/action_log.py +0 -126
  55. wiederverwendbar/examples/starlette_admin/action_log_file_download.py +0 -99
  56. wiederverwendbar/examples/starlette_admin/action_log_form.py +0 -149
  57. wiederverwendbar/examples/starlette_admin/action_log_thread.py +0 -192
  58. wiederverwendbar/examples/starlette_admin/automatic_reference_admin.py +0 -47
  59. wiederverwendbar/examples/starlette_admin/generic_embedded_document_field.py +0 -74
  60. wiederverwendbar/examples/starlette_admin/multi_path_admin.py +0 -18
  61. wiederverwendbar/examples/task_manager.py +0 -55
  62. wiederverwendbar/examples/test_file.py +0 -14
  63. wiederverwendbar/examples/typer_resolve_defaults.py +0 -15
  64. wiederverwendbar/examples/uvicorn_server.py +0 -32
  65. wiederverwendbar/logger/terminal_out_files.py +0 -10
  66. {wiederverwendbar-0.8.5.dist-info → wiederverwendbar-0.9.0.dist-info}/WHEEL +0 -0
  67. {wiederverwendbar-0.8.5.dist-info → wiederverwendbar-0.9.0.dist-info}/entry_points.txt +0 -0
@@ -1,15 +1,19 @@
1
1
  import json
2
+ import sys
3
+ from warnings import warn
2
4
  from pathlib import Path
3
- from typing import Any, Union
5
+ from typing import Any, Union, Literal
4
6
 
5
7
  from pydantic import BaseModel, create_model
6
8
 
9
+ from wiederverwendbar.warnings import FileNotFoundWarning
10
+
7
11
 
8
12
  class FileConfig(BaseModel):
9
13
  def __init__(self,
10
14
  file_path: Union[Path, str, None] = None,
11
15
  file_postfix: str = ".json",
12
- file_must_exist: bool = False,
16
+ file_must_exist: Union[bool, Literal["yes_print", "yes_warn", "yes_raise", "no"]] = "no",
13
17
  **overwrite_data: Any):
14
18
  if file_path is None:
15
19
  file_path = Path(Path.cwd() / self.__class__.__name__.lower()).with_suffix(file_postfix)
@@ -17,6 +21,7 @@ class FileConfig(BaseModel):
17
21
  file_path = Path(file_path)
18
22
  if file_path.suffix == "":
19
23
  file_path = file_path.with_suffix(file_postfix)
24
+ file_path = file_path.absolute()
20
25
 
21
26
  # read data from file
22
27
  if file_path.is_file():
@@ -25,8 +30,19 @@ class FileConfig(BaseModel):
25
30
  elif file_path.is_dir():
26
31
  raise ValueError(f"{self.__class__.__name__} file path '{file_path}' is a directory.")
27
32
  else:
28
- if file_must_exist:
29
- raise FileNotFoundError(f"{self.__class__.__name__} file '{file_path}' not found.")
33
+ if file_must_exist is True:
34
+ file_must_exist = "yes_raise"
35
+ elif file_must_exist is False:
36
+ file_must_exist = "no"
37
+ msg = f"{self.__class__.__name__} file '{file_path}' not found."
38
+ if file_must_exist == "yes_print":
39
+ print(msg)
40
+ sys.exit(1)
41
+ elif file_must_exist == "yes_warn":
42
+ warn(msg, FileNotFoundWarning)
43
+ sys.exit(1)
44
+ elif file_must_exist == "yes_raise":
45
+ raise FileNotFoundError(msg)
30
46
  data = {}
31
47
 
32
48
  # overwrite data
@@ -0,0 +1,2 @@
1
+ from wiederverwendbar.rich.console import (RichConsole)
2
+ from wiederverwendbar.rich.settings import (RichConsoleSettings)
@@ -0,0 +1,215 @@
1
+ from typing import Optional, Literal, Union, Any
2
+
3
+ from rich.console import Console as _RichConsole
4
+
5
+ from wiederverwendbar.console.console import Console as _Console
6
+ from wiederverwendbar.console.out_files import OutFiles
7
+ from wiederverwendbar.rich.settings import RichConsoleSettings
8
+
9
+
10
+ class RichConsole(_Console, _RichConsole):
11
+ print_function = _RichConsole.print
12
+ print_function_blacklist_kwargs = _Console.print_function_blacklist_kwargs + ["color", "header_color", "border_color"]
13
+
14
+ def __init__(self,
15
+ *,
16
+ console_file: Optional[OutFiles] = None,
17
+ console_seperator: Optional[str] = None,
18
+ console_end: Optional[str] = None,
19
+ console_color_system: Optional[Literal["auto", "standard", "256", "truecolor", "windows"]] = None,
20
+ console_force_terminal: Optional[bool] = None,
21
+ console_force_jupyter: Optional[bool] = None,
22
+ console_force_interactive: Optional[bool] = None,
23
+ console_soft_wrap: Optional[bool] = None,
24
+ console_quiet: Optional[bool] = None,
25
+ console_width: Optional[int] = None,
26
+ console_height: Optional[int] = None,
27
+ console_no_color: Optional[bool] = None,
28
+ console_tab_size: Optional[int] = None,
29
+ console_record: Optional[bool] = None,
30
+ console_markup: Optional[bool] = None,
31
+ console_emoji: Optional[bool] = None,
32
+ console_emoji_variant: Optional[Literal["emoji", "text"]] = None,
33
+ console_highlight: Optional[bool] = None,
34
+ console_log_time: Optional[bool] = None,
35
+ console_log_path: Optional[bool] = None,
36
+ settings: Optional[RichConsoleSettings] = None,
37
+ **kwargs):
38
+ """
39
+ Create a new rich console.
40
+
41
+ :param console_file: Console file. Default is STDOUT.
42
+ :param console_seperator: Console seperator. Default is a space.
43
+ :param console_end: Console end. Default is a newline.
44
+ :param console_color_system: Rich Console color system.
45
+ :param console_force_terminal: Rich Console force terminal.
46
+ :param console_force_jupyter: Rich Console force jupyter.
47
+ :param console_force_interactive: Rich Console force interactive.
48
+ :param console_soft_wrap: Rich Console soft wrap.
49
+ :param console_quiet: Rich Console quiet.
50
+ :param console_width: Rich Console width.
51
+ :param console_height: Rich Console height.
52
+ :param console_no_color: Rich Console no color.
53
+ :param console_tab_size: Rich Console tab size.
54
+ :param console_record: Rich Console record.
55
+ :param console_markup: Rich Console markup.
56
+ :param console_emoji: Rich Console emoji.
57
+ :param console_emoji_variant: Rich Console emoji variant.
58
+ :param console_highlight: Rich Console highlight.
59
+ :param console_log_time: Rich Console log time.
60
+ :param console_log_path: Rich Console log path.
61
+ :param settings: A settings object to use. If None, defaults to ConsoleSettings().
62
+ """
63
+
64
+ if settings is None:
65
+ settings = RichConsoleSettings()
66
+
67
+ _Console.__init__(self,
68
+ console_file=console_file,
69
+ console_seperator=console_seperator,
70
+ console_end=console_end,
71
+ settings=settings)
72
+
73
+ if console_color_system is None:
74
+ console_color_system = settings.console_color_system
75
+
76
+ if console_force_terminal is None:
77
+ console_force_terminal = settings.console_force_terminal
78
+
79
+ if console_force_jupyter is None:
80
+ console_force_jupyter = settings.console_force_jupyter
81
+
82
+ if console_force_interactive is None:
83
+ console_force_interactive = settings.console_force_interactive
84
+
85
+ if console_soft_wrap is None:
86
+ console_soft_wrap = settings.console_soft_wrap
87
+
88
+ if console_quiet is None:
89
+ console_quiet = settings.console_quiet
90
+
91
+ if console_width is None:
92
+ console_width = settings.console_width
93
+
94
+ if console_height is None:
95
+ console_height = settings.console_height
96
+
97
+ if console_no_color is None:
98
+ console_no_color = settings.console_no_color
99
+
100
+ if console_tab_size is None:
101
+ console_tab_size = settings.console_tab_size
102
+
103
+ if console_record is None:
104
+ console_record = settings.console_record
105
+
106
+ if console_markup is None:
107
+ console_markup = settings.console_markup
108
+
109
+ if console_emoji is None:
110
+ console_emoji = settings.console_emoji
111
+
112
+ if console_emoji_variant is None:
113
+ console_emoji_variant = settings.console_emoji_variant
114
+
115
+ if console_highlight is None:
116
+ console_highlight = settings.console_highlight
117
+
118
+ if console_log_time is None:
119
+ console_log_time = settings.console_log_time
120
+
121
+ if console_log_path is None:
122
+ console_log_path = settings.console_log_path
123
+
124
+ _RichConsole.__init__(self,
125
+ color_system=console_color_system,
126
+ force_terminal=console_force_terminal,
127
+ force_jupyter=console_force_jupyter,
128
+ force_interactive=console_force_interactive,
129
+ soft_wrap=console_soft_wrap,
130
+ quiet=console_quiet,
131
+ width=console_width,
132
+ height=console_height,
133
+ no_color=console_no_color,
134
+ tab_size=console_tab_size,
135
+ record=console_record,
136
+ markup=console_markup,
137
+ emoji=console_emoji,
138
+ emoji_variant=console_emoji_variant,
139
+ highlight=console_highlight,
140
+ log_time=console_log_time,
141
+ log_path=console_log_path,
142
+ **kwargs)
143
+
144
+ def _card_kwargs(self, mode: Literal["text", "header", "border", "print"], **kwargs) -> dict[str, Any]:
145
+ out = super()._card_kwargs(mode=mode, **kwargs)
146
+ for key in kwargs:
147
+ if mode == "text":
148
+ if key not in ["color"]:
149
+ continue
150
+ out[key] = kwargs[key]
151
+ elif mode == "header":
152
+ if key not in ["header_color"]:
153
+ continue
154
+ out[key] = kwargs[key]
155
+ elif mode == "border":
156
+ if key not in ["border_color"]:
157
+ continue
158
+ out[key] = kwargs[key]
159
+ return out
160
+
161
+ def _card_get_text(self,
162
+ text: str,
163
+ color: Optional[str] = None,
164
+ **kwargs) -> str:
165
+ text = super()._card_get_text(text=text,
166
+ **kwargs)
167
+ if color is not None:
168
+ text = f"[{color}]{text}[/{color}]"
169
+ return text
170
+
171
+ def _card_get_header_text(self,
172
+ text: str,
173
+ header_color: Optional[str] = None,
174
+ **kwargs) -> str:
175
+ text = super()._card_get_header_text(text=text,
176
+ **kwargs)
177
+ if header_color is not None:
178
+ text = f"[{header_color}]{text}[/{header_color}]"
179
+ return text
180
+
181
+ def _card_get_border(self,
182
+ border_style: Literal["single_line", "double_line"],
183
+ border_part: Literal["horizontal", "vertical", "top_left", "top_right", "bottom_left", "bottom_right", "vertical_left", "vertical_right"],
184
+ border_color: Optional[str] = None,
185
+ **kwargs):
186
+ border = super()._card_get_border(border_style=border_style,
187
+ border_part=border_part,
188
+ **kwargs)
189
+ if border_color is not None:
190
+ border = f"[{border_color}]{border}[/{border_color}]"
191
+ return border
192
+
193
+ def card(self,
194
+ *sections: Union[str, tuple[str, str]],
195
+ min_width: Optional[int] = None,
196
+ max_width: Optional[int] = None,
197
+ border_style: Literal["single_line", "double_line"] = "single_line",
198
+ topic_offest: int = 1,
199
+ padding_left: int = 0,
200
+ padding_right: int = 0,
201
+ color: Optional[str] = None,
202
+ header_color: Optional[str] = None,
203
+ border_color: Optional[str] = None,
204
+ **kwargs) -> None:
205
+ return super().card(*sections,
206
+ min_width=min_width,
207
+ max_width=max_width,
208
+ border_style=border_style,
209
+ topic_offest=topic_offest,
210
+ padding_left=padding_left,
211
+ padding_right=padding_right,
212
+ color=color,
213
+ header_color=header_color,
214
+ border_color=border_color,
215
+ **kwargs)
@@ -0,0 +1,26 @@
1
+ from typing import Optional, Literal
2
+
3
+ from pydantic import Field
4
+
5
+ from wiederverwendbar.console.settings import ConsoleSettings
6
+
7
+
8
+ class RichConsoleSettings(ConsoleSettings):
9
+ console_color_system: Optional[Literal["auto", "standard", "256", "truecolor", "windows"]] = Field(default="auto", title="Console Color System",
10
+ description="The color system of the console.")
11
+ console_force_terminal: Optional[bool] = Field(default=None, title="Console Force Terminal", description="Whether to force the terminal.")
12
+ console_force_jupyter: Optional[bool] = Field(default=None, title="Console Force Jupyter", description="Whether to force Jupyter.")
13
+ console_force_interactive: Optional[bool] = Field(default=None, title="Console Force Interactive", description="Whether to force interactive mode.")
14
+ console_soft_wrap: bool = Field(default=False, title="Console Soft Wrap", description="Whether to soft wrap the console.")
15
+ console_quiet: bool = Field(default=False, title="Console Quiet", description="Whether to suppress all output.")
16
+ console_width: Optional[int] = Field(default=None, title="Console Width", description="The width of the console.")
17
+ console_height: Optional[int] = Field(default=None, title="Console Height", description="The height of the console.")
18
+ console_no_color: Optional[bool] = Field(default=None, title="Console No Color", description="Whether to disable color.")
19
+ console_tab_size: int = Field(default=8, title="Console Tab Size", description="The tab size of the console.")
20
+ console_record: bool = Field(default=False, title="Console Record", description="Whether to record the console output.")
21
+ console_markup: bool = Field(default=True, title="Console Markup", description="Whether to enable markup.")
22
+ console_emoji: bool = Field(default=True, title="Console Emoji", description="Whether to enable emoji.")
23
+ console_emoji_variant: Optional[Literal["emoji", "text"]] = Field(default=None, title="Console Emoji Variant", description="The emoji variant of the console.")
24
+ console_highlight: bool = Field(default=True, title="Console Highlight", description="Whether to enable highlighting.")
25
+ console_log_time: bool = Field(default=True, title="Console Log Time", description="Whether to log the time.")
26
+ console_log_path: bool = Field(default=True, title="Console Log Path", description="Whether to log the path (logging of the caller by).")
@@ -260,7 +260,7 @@ class Base:
260
260
  @classmethod
261
261
  def new(cls,
262
262
  session: Optional[Session] = None,
263
- **kwargs) -> "Base":
263
+ **kwargs) -> Union["Base", Any]:
264
264
  session_created, session = cls.session(session=session)
265
265
 
266
266
  # noinspection PyArgumentList
@@ -292,7 +292,7 @@ class Base:
292
292
  @classmethod
293
293
  def get_all(cls,
294
294
  *criterion: Union[ColumnExpressionArgument[bool], bool],
295
- order_by: Union[str, QueryableAttribute, None] = None,
295
+ order_by: Union[str, QueryableAttribute, Any, None] = None,
296
296
  order_desc: bool = False,
297
297
  rows_per_page: Optional[int] = None,
298
298
  page: int = 1,
@@ -300,7 +300,7 @@ class Base:
300
300
  as_dict: bool = False,
301
301
  dict_columns: Union[list[DictColumn], Default] = Default(),
302
302
  session: Optional[Session] = None,
303
- **kwargs: Any) -> list[Union["Base", None, dict[str, Any]]]:
303
+ **kwargs: Any) -> list[Union["Base", None, dict[str, Any], Any]]:
304
304
  session_created, session = cls.session(session=session)
305
305
 
306
306
  # get order by
@@ -364,7 +364,7 @@ class Base:
364
364
  as_dict: bool = False,
365
365
  dict_columns: Union[list[DictColumn], Default] = Default(),
366
366
  session: Optional[Session] = None,
367
- **kwargs: Any) -> Union["Base", None, dict[str, Any]]:
367
+ **kwargs: Any) -> Union["Base", None, dict[str, Any], Any]:
368
368
  session_created, session = cls.session(session=session)
369
369
 
370
370
  if criterion:
@@ -59,16 +59,13 @@ class Task:
59
59
  raise ValueError("Manager object is required.")
60
60
  manager.add_task(self)
61
61
 
62
- def __str__(self):
63
- return f"{self.manager.name}.{self.name}"
64
-
65
62
  def init(self, manager):
66
63
  self.manager = manager
67
64
  self.trigger.init(manager)
68
65
  self.set_next_run()
69
66
 
70
67
  # log task creation
71
- self.manager.logger.debug(f"{self}: Task created.")
68
+ self.manager.logger.debug(f"Task created.")
72
69
 
73
70
  @property
74
71
  def last_run(self) -> Optional[datetime]:
@@ -8,9 +8,6 @@ from typing import Any, Optional
8
8
  from wiederverwendbar.task_manger.task import Task
9
9
  from wiederverwendbar.task_manger.trigger import Trigger
10
10
 
11
- LOGGER = logging.getLogger(__name__)
12
-
13
-
14
11
  class TaskManager:
15
12
  lock = threading.Lock()
16
13
 
@@ -20,7 +17,8 @@ class TaskManager:
20
17
  daemon: bool = False,
21
18
  keep_done_tasks: bool = False,
22
19
  loop_delay: Optional[float] = None,
23
- logger: Optional[logging.Logger] = None):
20
+ logger: Optional[logging.Logger] = None,
21
+ log_self: bool = True):
24
22
  if name is None:
25
23
  name = self.__class__.__name__
26
24
  self.name = name
@@ -29,7 +27,7 @@ class TaskManager:
29
27
  self._stopped: bool = False
30
28
  self._creation_time: datetime = datetime.now()
31
29
  self._keep_done_tasks = keep_done_tasks
32
- self.logger = logger or LOGGER
30
+ self.logger = logger or logging.getLogger(self.name)
33
31
 
34
32
  # create workers
35
33
  if worker_count is None:
@@ -48,9 +46,6 @@ class TaskManager:
48
46
  loop_delay = 0.001
49
47
  self._loop_delay = loop_delay
50
48
 
51
- def __str__(self):
52
- return f"{self.__class__.__name__}({self.name})"
53
-
54
49
  def __del__(self):
55
50
  if not self.stopped:
56
51
  self.stop()
@@ -96,14 +91,14 @@ class TaskManager:
96
91
  :return: None
97
92
  """
98
93
 
99
- self.logger.debug(f"{self}: Starting manager ...")
94
+ self.logger.debug(f"Starting manager ...")
100
95
 
101
96
  # start workers
102
97
  for worker in self._workers:
103
- self.logger.debug(f"{self}: Starting worker '{worker.name}' ...")
98
+ self.logger.debug(f"Starting worker '{worker.name}' ...")
104
99
  worker.start()
105
100
 
106
- self.logger.debug(f"{self}: Manager started.")
101
+ self.logger.debug(f"Manager started.")
107
102
 
108
103
  def stop(self) -> None:
109
104
  """
@@ -112,7 +107,7 @@ class TaskManager:
112
107
  :return: None
113
108
  """
114
109
 
115
- self.logger.debug(f"{self}: Stopping manager ...")
110
+ self.logger.debug(f"Stopping manager ...")
116
111
 
117
112
  # set stopped flag
118
113
  with self.lock:
@@ -121,10 +116,10 @@ class TaskManager:
121
116
  # wait for workers to finish
122
117
  for worker in self._workers:
123
118
  if worker.is_alive():
124
- self.logger.debug(f"{self}: Waiting for worker '{worker.name}' to finish ...")
119
+ self.logger.debug(f"Waiting for worker '{worker.name}' to finish ...")
125
120
  worker.join()
126
121
 
127
- self.logger.debug(f"{self}: Manager stopped.")
122
+ self.logger.debug(f"Manager stopped.")
128
123
 
129
124
  def loop(self, stay_in_loop: Optional[bool] = None) -> None:
130
125
  """
@@ -155,7 +150,7 @@ class TaskManager:
155
150
  time.sleep(loop_delay)
156
151
  continue
157
152
 
158
- self.logger.debug(f"{self}: Running task '{current_task}' ...")
153
+ self.logger.debug(f"Running task '{current_task.name}' ...")
159
154
 
160
155
  with self.lock:
161
156
  if current_task.time_measurement_before_run:
@@ -165,7 +160,7 @@ class TaskManager:
165
160
  # run task
166
161
  current_task.payload()
167
162
 
168
- self.logger.debug(f"{self}: Task '{current_task}' successfully run.")
163
+ self.logger.debug(f"Task '{current_task.name}' successfully run.")
169
164
 
170
165
  with self.lock:
171
166
  if not current_task.time_measurement_before_run:
@@ -174,7 +169,7 @@ class TaskManager:
174
169
  if not current_task.is_done:
175
170
  self._tasks.append(current_task)
176
171
  else:
177
- self.logger.debug(f"{self}: Task '{current_task}' is done.")
172
+ self.logger.debug(f"Task '{current_task.name}' is done.")
178
173
  if self._keep_done_tasks:
179
174
  self._tasks.append(current_task)
180
175
 
@@ -192,7 +187,7 @@ class TaskManager:
192
187
  task.init(self)
193
188
  with self.lock:
194
189
  self._tasks.append(task)
195
- self.logger.debug(f"{self}: Task '{task}' added.")
190
+ self.logger.debug(f"Task '{task.name}' added.")
196
191
 
197
192
  def remove_task(self, task: Task):
198
193
  """
@@ -204,7 +199,7 @@ class TaskManager:
204
199
 
205
200
  with self.lock:
206
201
  self._tasks.remove(task)
207
- self.logger.debug(f"{self}: Task '{task}' removed.")
202
+ self.logger.debug(f"Task '{task.name}' removed.")
208
203
 
209
204
  def task(self,
210
205
  name: Optional[str] = None,
@@ -1 +1,3 @@
1
- from wiederverwendbar.typer.typer_resolve_defaults import typer_resolve_defaults
1
+ from wiederverwendbar.typer.app import (Typer)
2
+ from wiederverwendbar.typer.settings import (TyperSettings)
3
+ from wiederverwendbar.typer.typer_resolve_defaults import (typer_resolve_defaults)
@@ -0,0 +1,172 @@
1
+ import inspect
2
+ from typing import Optional, Annotated, Union
3
+
4
+ from typer import Typer as _Typer, Option, Exit
5
+ from art import text2art
6
+
7
+ from wiederverwendbar.default import Default
8
+ from wiederverwendbar.rich.console import RichConsole
9
+ from wiederverwendbar.typer.settings import TyperSettings
10
+
11
+
12
+ class Typer(_Typer):
13
+ def __init__(self,
14
+ *,
15
+ title: Union[Default, str] = Default(),
16
+ description: Union[None, Default, str] = Default(),
17
+ version: Union[Default, str] = Default(),
18
+ author: Union[None, Default, str] = Default(),
19
+ author_email: Union[None, Default, str] = Default(),
20
+ license: Union[None, Default, str] = Default(),
21
+ license_url: Union[None, Default, str] = Default(),
22
+ terms_of_service: Union[None, Default, str] = Default(),
23
+ info_enabled: Union[Default, bool] = Default(),
24
+ version_enabled: Union[Default, bool] = Default(),
25
+ name: Union[None, Default, str] = Default(),
26
+ help: Union[None, Default, str] = Default(),
27
+ settings: Optional[TyperSettings] = None,
28
+ console: Optional[RichConsole] = None,
29
+ main_callback_parameters: Optional[list[inspect.Parameter]] = None,
30
+ **kwargs):
31
+
32
+ # set default
33
+ if settings is None:
34
+ settings = TyperSettings()
35
+ if type(title) is Default:
36
+ title = settings.branding_title
37
+ if title is None:
38
+ title = "Typer"
39
+ if type(description) is Default:
40
+ description = settings.branding_description
41
+ if type(version) is Default:
42
+ version = settings.branding_version
43
+ if version is None:
44
+ version = "0.1.0"
45
+ if type(author) is Default:
46
+ author = settings.branding_author
47
+ if type(author_email) is Default:
48
+ author_email = settings.branding_author_email
49
+ if type(license) is Default:
50
+ license = settings.branding_license
51
+ if type(license_url) is Default:
52
+ license_url = settings.branding_license_url
53
+ if type(terms_of_service) is Default:
54
+ terms_of_service = settings.branding_terms_of_service
55
+ if type(info_enabled) is Default:
56
+ info_enabled = settings.cli_info_enabled
57
+ if type(version_enabled) is Default:
58
+ version_enabled = settings.cli_version_enabled
59
+ if type(name) is Default:
60
+ name = settings.cli_name
61
+ if type(name) is Default:
62
+ name = title
63
+ if type(help) is Default:
64
+ help = settings.cli_help
65
+ if type(help) is Default:
66
+ help = description
67
+ if console is None:
68
+ console = RichConsole(settings=settings)
69
+ if main_callback_parameters is None:
70
+ main_callback_parameters = []
71
+
72
+ super().__init__(name=name, help=help, **kwargs)
73
+
74
+ # set attrs
75
+ self.title = title
76
+ self.description = description
77
+ self.version = version
78
+ self.author = author
79
+ self.author_email = author_email
80
+ self.license = license
81
+ self.license_url = license_url
82
+ self.terms_of_service = terms_of_service
83
+ self.info_enabled = info_enabled
84
+ self.version_enabled = version_enabled
85
+ self.name = name
86
+ self.help = help
87
+ self.console = console
88
+
89
+ # add info command parameter to main_callback_parameters
90
+ if info_enabled:
91
+ def info_callback(value: bool) -> None:
92
+ if not value:
93
+ return
94
+ code = self.info_command()
95
+ if code is None:
96
+ code = 0
97
+ raise Exit(code=code)
98
+
99
+ main_callback_parameters.append(inspect.Parameter(name="info",
100
+ kind=inspect.Parameter.KEYWORD_ONLY,
101
+ default=False,
102
+ annotation=Annotated[Optional[bool], Option("--info",
103
+ help="Show information of the application.",
104
+ callback=info_callback)]))
105
+
106
+ # add version command parameter to main_callback_parameters
107
+ if version_enabled:
108
+ def version_callback(value: bool):
109
+ if not value:
110
+ return
111
+ code = self.version_command()
112
+ if code is None:
113
+ code = 0
114
+ raise Exit(code=code)
115
+
116
+ main_callback_parameters.append(inspect.Parameter(name="version",
117
+ kind=inspect.Parameter.KEYWORD_ONLY,
118
+ default=False,
119
+ annotation=Annotated[Optional[bool], Option("-v",
120
+ "--version",
121
+ help="Show version of the application.",
122
+ callback=version_callback)]))
123
+
124
+ # backup main callback
125
+ orig_main_callback = self.main_callback
126
+
127
+ def main_callback(*a, **kw):
128
+ orig_main_callback(*a, **kw)
129
+
130
+ # update signature
131
+ main_callback.__signature__ = inspect.signature(self.main_callback).replace(parameters=main_callback_parameters)
132
+
133
+ # overwrite the main callback
134
+ self.main_callback = main_callback
135
+
136
+ # register the main callback
137
+ self.callback()(self.main_callback)
138
+
139
+ def main_callback(self, *args, **kwargs):
140
+ ...
141
+
142
+ def info_command(self) -> Optional[int]:
143
+ card_body = [text2art(self.title)]
144
+ second_section = ""
145
+ if self.description is not None:
146
+ second_section += f"{self.description}"
147
+ if self.author is not None:
148
+ if second_section != "":
149
+ second_section += "\n"
150
+ second_section += f"by {self.author}"
151
+ if self.author_email is not None:
152
+ second_section += f" ({self.author_email})"
153
+ if second_section != "":
154
+ second_section += "\n"
155
+ second_section += f"Version: v{self.version}"
156
+ if self.license is not None:
157
+ second_section += f"\nLicense: {self.license}"
158
+ if self.license_url is not None:
159
+ second_section += f" - {self.license_url}"
160
+ if self.terms_of_service is not None:
161
+ second_section += f"\nTerms of Service: {self.terms_of_service}"
162
+ card_body.append(second_section)
163
+
164
+ self.console.card(*card_body,
165
+ padding_left=1,
166
+ padding_right=1,
167
+ border_style="double_line",
168
+ color="white",
169
+ border_color="blue")
170
+
171
+ def version_command(self) -> Optional[int]:
172
+ self.console.print(f"{self.title} v[cyan]{self.version}[/cyan]")
@@ -0,0 +1,14 @@
1
+ from typing import Union
2
+
3
+ from pydantic import Field
4
+
5
+ from wiederverwendbar.branding.settings import BrandingSettings
6
+ from wiederverwendbar.default import Default
7
+ from wiederverwendbar.rich.settings import RichConsoleSettings
8
+
9
+
10
+ class TyperSettings(RichConsoleSettings, BrandingSettings):
11
+ cli_name: Union[None, Default, str] = Field(default=Default(), title="CLI Name", description="The name of the CLI.")
12
+ cli_help: Union[None, Default, str] = Field(default=Default(), title="CLI Help", description="The help of the CLI.")
13
+ cli_info_enabled: bool = Field(default=True, title="Info Command", description="Enable the info command.")
14
+ cli_version_enabled: bool = Field(default=True, title="Version Command", description="Enable the version command.")
@@ -0,0 +1,6 @@
1
+ class WiederverwendbarWarning(Warning):
2
+ ...
3
+
4
+
5
+ class FileNotFoundWarning(WiederverwendbarWarning):
6
+ ...