py4web-debug-tools 0.1.2__py3-none-any.whl → 0.2.0.dev0__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.
py4web_debug/core.py CHANGED
@@ -19,11 +19,20 @@ class DebugTools:
19
19
 
20
20
  def enable(
21
21
  self,
22
+ # general settings:
22
23
  db: P4WDAL,
23
- enabled: bool | None = None,
24
- fancy_rendering=True,
25
- bar_style: typing.Literal["bootstrap"] = "bootstrap",
26
- slow_threshold_ms=10,
24
+ enabled: bool | None = None, # OVERWRITES errorpage_enabled and debugbar_enabled
25
+ set_env_var: bool = True,
26
+
27
+ # error screen settings:
28
+ errorpage_enabled: bool = None, # value of 'enabled' is used by default
29
+ errorpage_renderer: typing.Callable = None,
30
+
31
+ # debugbar settings:
32
+ debugbar_enabled: bool = None, # value of 'enabled' is used by default
33
+ debugbar_fancy_rendering: bool = True,
34
+ debugbar_style: typing.Literal["bootstrap"] = "bootstrap",
35
+ debugbar_slow_threshold_ms: int = 10,
27
36
  ):
28
37
  """
29
38
  By default, on_off looks at PY4WEB_DEBUG_MODE in the env
@@ -38,13 +47,18 @@ class DebugTools:
38
47
 
39
48
  self.IS_DEBUG = enabled
40
49
  if enabled:
41
- patch_py4()
42
- self.debug_bar = DebugBar(db, fancy_rendering, bar_style, slow_threshold_ms)
50
+ if errorpage_enabled in (True, None):
51
+ patch_py4(errorpage_renderer)
43
52
 
44
- # will change the result of is_debug (hopefully)
45
- os.environ["PY4WEB_DEBUG_MODE"] = "1"
53
+ if debugbar_enabled in (True, None):
54
+ self.debug_bar = DebugBar(db, debugbar_fancy_rendering, debugbar_style, debugbar_slow_threshold_ms)
55
+
56
+ if set_env_var:
57
+ # will change the result of is_debug (hopefully)
58
+ os.environ["PY4WEB_DEBUG_MODE"] = "1"
46
59
  else:
47
60
  self.debug_bar = DummyDebugBar()
48
61
 
49
62
 
50
63
  tools = DebugTools()
64
+
py4web_debug/internals.py CHANGED
@@ -30,6 +30,8 @@ def custom_error_page(
30
30
  traceback="",
31
31
  err_type: str | typing.Type | None = None,
32
32
  bare_exception=None,
33
+
34
+ renderer=None
33
35
  ):
34
36
  if err_type and not isinstance(err_type, str):
35
37
  err_type = err_type.__name__
@@ -64,6 +66,9 @@ def custom_error_page(
64
66
  return json.dumps(context)
65
67
  # else - return html error-page
66
68
 
69
+ if renderer:
70
+ return renderer(context)
71
+
67
72
  templates = {
68
73
  "default": "error_default.html",
69
74
  "DumpDieError": "dumpdie.html",
@@ -88,44 +93,45 @@ def custom_error_page(
88
93
  )
89
94
 
90
95
 
91
- def custom_catch_errors(app_name, func):
92
- """Catches and logs errors in an action; also sets request.app_name"""
93
-
94
- @functools.wraps(func)
95
- def wrapper(*func_args, **func_kwargs):
96
- try:
97
- request.app_name = app_name
98
- ret = func(*func_args, **func_kwargs)
99
- if isinstance(ret, dict):
100
- response.headers["Content-Type"] = "application/json"
101
- ret = dumps(ret)
102
- return ret
103
- except HTTP as http:
104
- response.status = http.status
105
- response.headers.update(http.headers)
106
- return http.body
107
- except bottle.HTTPResponse:
108
- raise
109
- except Exception as e:
110
- snapshot = get_error_snapshot()
111
- logging.error(snapshot["traceback"])
112
- ticket_uuid = error_logger.log(request.app_name, snapshot) or "unknown"
113
- response.status = 500
114
-
115
- raise bottle.HTTPResponse(
116
- body=custom_error_page(
117
- 500,
118
- button_text=ticket_uuid,
119
- href="/_dashboard/ticket/" + ticket_uuid,
120
- traceback=traceback.format_exc(),
121
- err_type=type(e),
122
- bare_exception=e,
123
- ),
124
- status=500,
125
- )
126
-
127
- return wrapper
128
-
129
-
130
- def patch_py4():
96
+ def patch_py4(renderer=None):
97
+ def custom_catch_errors(app_name, func):
98
+ """Catches and logs errors in an action; also sets request.app_name"""
99
+
100
+ @functools.wraps(func)
101
+ def wrapper(*func_args, **func_kwargs):
102
+ try:
103
+ request.app_name = app_name
104
+ ret = func(*func_args, **func_kwargs)
105
+ if isinstance(ret, dict):
106
+ response.headers["Content-Type"] = "application/json"
107
+ ret = dumps(ret)
108
+ return ret
109
+ except HTTP as http:
110
+ response.status = http.status
111
+ response.headers.update(http.headers)
112
+ return http.body
113
+ except bottle.HTTPResponse:
114
+ raise
115
+ except Exception as e:
116
+ snapshot = get_error_snapshot()
117
+ logging.error(snapshot["traceback"])
118
+ ticket_uuid = error_logger.log(request.app_name, snapshot) or "unknown"
119
+ response.status = 500
120
+
121
+ raise bottle.HTTPResponse(
122
+ body=custom_error_page(
123
+ 500,
124
+ button_text=ticket_uuid,
125
+ href="/_dashboard/ticket/" + ticket_uuid,
126
+ traceback=traceback.format_exc(),
127
+ err_type=type(e),
128
+ bare_exception=e,
129
+
130
+ renderer=renderer
131
+ ),
132
+ status=500,
133
+ )
134
+
135
+ return wrapper
136
+
131
137
  action.catch_errors = custom_catch_errors
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: py4web-debug-tools
3
- Version: 0.1.2
3
+ Version: 0.2.0.dev0
4
4
  Summary: Debug Tools for py4web
5
5
  Home-page: https://github.com/trialandsuccess/py4web-debug-tools
6
6
  Author: Robin van der Noord
@@ -1,14 +1,14 @@
1
1
  py4web_debug/__init__.py,sha256=OC9yuI3jIBjwi7KGL4uUNtQbyuzZcq913z31gfgSK7s,108
2
- py4web_debug/core.py,sha256=LzteofsHOBcoRWM4W5lHlaKTrDfXJ1K_e3qZJcKCflw,1373
2
+ py4web_debug/core.py,sha256=Vydfzbiv0USgdrT_4hB6OtkXd3lNQOFTNRboI1sxejQ,1989
3
3
  py4web_debug/debugbar.py,sha256=bzuJ4FX_bYzz0-V8f9A1bjkEmLbj8q00CAerCAu3aMs,7243
4
4
  py4web_debug/dumping.py,sha256=AF4UVSyqI43JgxffjeoozQ0az0J5nvpeZVYtWuUlC6s,3496
5
5
  py4web_debug/env.py,sha256=CXJX6nmNgGYL0Z9rt8fmQPO1UAydhW-Cv-zWObPlleQ,82
6
- py4web_debug/internals.py,sha256=8hE659RPLrdxL0EGiWfQkVM7hBEblTyhbw_GOm1gT6Q,3583
6
+ py4web_debug/internals.py,sha256=J8pmil-GzxT2BPs4QSoVos5k27vFIi8nGfujTIX90U0,3844
7
7
  py4web_debug/templates/debugbar.html,sha256=gbaBLBJpDIti9QAkDWXRP10_LEvCrQmK1GxFlbsHasA,7169
8
8
  py4web_debug/templates/dumpdie.html,sha256=pFjRjs2VBZPo1nyfObCO5kksyQS9q-K4EqPdZNFo9lg,60
9
9
  py4web_debug/templates/error_default.html,sha256=OGMvbHWb7xX91GH5Kx9WPXqv93ee7UcII5B-nUu3TYk,868
10
10
  py4web_debug/templates/fancy_dumpdie.html,sha256=eaquCyAOM1QU3QMv__fIqgbmyFHOnwd495PMGMVOzo8,532
11
- py4web_debug_tools-0.1.2.dist-info/METADATA,sha256=BfBSilvd1AyTu_qpOVuwytcQeS9r2FcuhPW0eHjQXxU,2622
12
- py4web_debug_tools-0.1.2.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
13
- py4web_debug_tools-0.1.2.dist-info/top_level.txt,sha256=sZikb7uiewpax6SbMw-PL1La6xqDq8ZUbN7J9kFKodA,13
14
- py4web_debug_tools-0.1.2.dist-info/RECORD,,
11
+ py4web_debug_tools-0.2.0.dev0.dist-info/METADATA,sha256=j3kOMOtXp9ftrCYPhqw1NJ4Lwgj7xcy6qn7Upv0V1JA,2627
12
+ py4web_debug_tools-0.2.0.dev0.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
13
+ py4web_debug_tools-0.2.0.dev0.dist-info/top_level.txt,sha256=sZikb7uiewpax6SbMw-PL1La6xqDq8ZUbN7J9kFKodA,13
14
+ py4web_debug_tools-0.2.0.dev0.dist-info/RECORD,,