From c11dfe84ab6844a229053eaf7ad7c3814ee550be Mon Sep 17 00:00:00 2001 From: Shane Jaroch Date: Tue, 13 Feb 2024 14:31:37 -0500 Subject: [PATCH] wip bug report --- ntclient/__main__.py | 11 ++--- ntclient/ntsqlite | 2 +- .../services/{bugs.py => api/__init__.py} | 2 +- ntclient/services/api/funcs.py | 12 ++++++ ntclient/services/bugs.py | 42 +++++++++++++++++++ ntclient/utils/__init__.py | 9 ---- ntclient/utils/sql.py | 19 +++++++++ 7 files changed, 81 insertions(+), 16 deletions(-) copy ntclient/services/{bugs.py => api/__init__.py} (66%) create mode 100644 ntclient/services/api/funcs.py create mode 100644 ntclient/utils/sql.py diff --git a/ntclient/__main__.py b/ntclient/__main__.py index 6de1e07..0d48b65 100644 --- a/ntclient/__main__.py +++ b/ntclient/__main__.py @@ -23,8 +23,9 @@ from ntclient import ( __version__, ) from ntclient.argparser import build_subcommands -from ntclient.utils import CLI_CONFIG, handle_runtime_exception +from ntclient.utils import CLI_CONFIG from ntclient.utils.exceptions import SqlException +from ntclient.utils.sql import handle_runtime_exception def build_arg_parser() -> argparse.ArgumentParser: @@ -101,19 +102,19 @@ def main(args: list = None) -> int: # type: ignore exit_code, *_results = func(_parser) except SqlException as sql_exception: # pragma: no cover print("Issue with an sqlite database: " + repr(sql_exception)) - handle_runtime_exception(sql_exception) + handle_runtime_exception(args, sql_exception) except HTTPError as http_error: # pragma: no cover err_msg = "{0}: {1}".format(http_error.code, repr(http_error)) print("Server response error, try again: " + err_msg) - handle_runtime_exception(http_error) + handle_runtime_exception(args, http_error) except URLError as url_error: # pragma: no cover print("Connection error, check your internet: " + repr(url_error.reason)) - handle_runtime_exception(url_error) + handle_runtime_exception(args, url_error) except Exception as exception: # pylint: disable=broad-except # pragma: no cover print("Unforeseen error, run with --debug for more info: " + repr(exception)) print("You can open an issue here: %s" % __url__) print("Or send me an email with the debug output: %s" % __email__) - handle_runtime_exception(exception) + handle_runtime_exception(args, exception) finally: if CLI_CONFIG.debug: exc_time = time.time() - start_time diff --git a/ntclient/ntsqlite b/ntclient/ntsqlite index 939f550..c5c64d3 160000 --- a/ntclient/ntsqlite +++ b/ntclient/ntsqlite @@ -1 +1 @@ -Subproject commit 939f55077b97bd1fe1c8e2b897b032dbc1564487 +Subproject commit c5c64d3371a5f1e5c600989e79563c5827486224 diff --git a/ntclient/services/bugs.py b/ntclient/services/api/__init__.py similarity index 66% copy from ntclient/services/bugs.py copy to ntclient/services/api/__init__.py index b29e6c1..83fe132 100644 --- a/ntclient/services/bugs.py +++ b/ntclient/services/api/__init__.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ -Created on Tue Feb 13 09:51:48 2024 +Created on Tue Feb 13 14:28:20 2024 @author: shane """ diff --git a/ntclient/services/api/funcs.py b/ntclient/services/api/funcs.py new file mode 100644 index 0000000..c955dd0 --- /dev/null +++ b/ntclient/services/api/funcs.py @@ -0,0 +1,12 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Created on Tue Feb 13 14:28:44 2024 + +@author: shane +""" + + +def post_bug(bug: tuple) -> None: + """Post a bug report to the developer.""" + print("posting bug report...") diff --git a/ntclient/services/bugs.py b/ntclient/services/bugs.py index b29e6c1..efd99c7 100644 --- a/ntclient/services/bugs.py +++ b/ntclient/services/bugs.py @@ -5,3 +5,45 @@ Created on Tue Feb 13 09:51:48 2024 @author: shane """ +import os +import traceback + +import ntclient.services.api.funcs +from ntclient.persistence.sql.nt import sql as sql_nt + + +def insert(args: list, exception: Exception) -> None: + """Insert bug report into nt.sqlite3, return True/False.""" + print("inserting bug reports...", end="") + sql_nt( + """ +INSERT INTO bug + (profile_id, arguments, repr, stack, client_info, app_info, user_details) + VALUES + (?,?,?,?,?,?,?) + """, + ( + 1, + " ".join(args), + repr(exception), + os.linesep.join(traceback.format_tb(exception.__traceback__)), + "client_info", + "app_info", + "user_details", + ), + ) + + +def submit() -> int: + """Submit bug reports to developer, return n_submitted.""" + n_submitted = 0 + sql_bugs = sql_nt("SELECT * FROM bug WHERE submitted = 0") + print(f"submitting {len(sql_bugs)} bug reports...") + for bug in sql_bugs: + # print(", ".join(str(x) for x in bug)) + ntclient.services.api.funcs.post_bug(bug) + n_submitted += 1 + # 1 / 0 # force exception + # raise Exception("submitting bug reports failed") + + return n_submitted diff --git a/ntclient/utils/__init__.py b/ntclient/utils/__init__.py index f5fc7b0..042f66f 100644 --- a/ntclient/utils/__init__.py +++ b/ntclient/utils/__init__.py @@ -150,12 +150,3 @@ def activity_factor_from_index(activity_factor: int) -> float: raise ValueError( # pragma: no cover "No such ActivityFactor for value: %s" % activity_factor ) - - -def handle_runtime_exception(exception: Exception) -> None: - """ - Handles exceptions raised during runtime. - """ - print("Exception: %s" % exception) - if CLI_CONFIG.debug: - raise exception diff --git a/ntclient/utils/sql.py b/ntclient/utils/sql.py new file mode 100644 index 0000000..2c3a151 --- /dev/null +++ b/ntclient/utils/sql.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Created on Tue Feb 13 14:15:21 2024 + +@author: shane +""" +from ntclient.services.bugs import insert as bug_insert +from ntclient.utils import CLI_CONFIG + + +def handle_runtime_exception(args: list, exception: Exception) -> None: + """ + Handles exceptions raised during runtime. + """ + print("Exception: %s" % exception) + if CLI_CONFIG.debug: + bug_insert(args, exception) + raise exception -- 2.52.0