bug_parser = subparsers.add_parser("bug", help="report bugs")
bug_subparser = bug_parser.add_subparsers(title="bug subcommands")
bug_parser.add_argument(
- "--all", action="store_true", help="include already submitted bugs, too"
+ "--show", action="store_true", help="show list of unsubmitted bugs"
)
bug_parser.set_defaults(func=parser_funcs.bugs_list)
def bugs_list(args: argparse.Namespace) -> tuple:
"""List bug reports that have een saved"""
_bugs_list = ntclient.services.bugs.list_bugs()
+ n_bugs_total = len(_bugs_list)
+ n_bugs_unsubmitted = len([x for x in _bugs_list if not bool(x[-1])])
- print(f"You have: {len(_bugs_list)} total bugs amassed in your journey.")
- print(
- f"Of these, {len([x for x in _bugs_list if not bool(x[-1])])} "
- f"require submission/reporting."
- )
+ print(f"You have: {n_bugs_total} total bugs amassed in your journey.")
+ print(f"Of these, {n_bugs_unsubmitted} require submission/reporting.")
print()
for bug in _bugs_list:
+ if not args.show:
+ continue
# Skip submitted bugs by default
- if bool(bug[-1]) and not args.all:
+ if bool(bug[-1]) and not args.debug:
continue
- # Print all (except noisy stacktrace)
+ # Print all bug properties (except noisy stacktrace)
print(", ".join(str(x) for x in bug if "\n" not in str(x)))
+ if n_bugs_unsubmitted > 0:
+ print("NOTE: You have bugs awaiting submission. Please run the report command")
+
return 0, _bugs_list
@author: shane
"""
+import os
+
+from ntclient import NUTRA_HOME
+
+# TODO: create and maintain prefs.json file? See if there's a library for that, lol
+
+PREFS_JSON = os.path.join(NUTRA_HOME, "prefs.json")
+
+# if
import requests
-URL_API = "https://api.nutra.tk"
+REQUEST_READ_TIMEOUT = 18
+REQUEST_CONNECT_TIMEOUT = 5
+
# TODO: try all of these; cache (save in prefs.json) the one which works first
URLS_API = (
"https://api.nutra.tk",
- "https://216.218.216.163/api", # prod
- "https://216.218.228.93/api", # dev
+ "http://216.218.216.163", # prod
)
-REQUEST_READ_TIMEOUT = 18
-REQUEST_CONNECT_TIMEOUT = 5
class ApiClient:
def __init__(
self,
- host: str = URL_API,
+ host: str = URLS_API[0],
):
self.host = host
--- /dev/null
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+"""
+Created on Wed Feb 14 18:58:12 2024
+
+@author: shane
+"""
+
+import requests
+
+from ntclient.services.api import (
+ REQUEST_CONNECT_TIMEOUT,
+ REQUEST_READ_TIMEOUT,
+ URLS_API,
+)
+
+
+def cache_mirrors() -> bool:
+ """Cache mirrors"""
+ for mirror in URLS_API:
+ try:
+ _res = requests.get(
+ mirror,
+ timeout=(REQUEST_CONNECT_TIMEOUT, REQUEST_READ_TIMEOUT),
+ verify=mirror.startswith("https://"),
+ )
+
+ _res.raise_for_status()
+ print(f"INFO: mirror '{mirror}' SUCCEEDED! Saving it.")
+ return True
+ except requests.exceptions.ConnectionError:
+ print(f"INFO: mirror '{mirror}' failed")
+
+ return False
import ntclient.services.api
from ntclient.persistence.sql.nt import sql as sql_nt
+from ntclient.services.api.mirrorcache import cache_mirrors
def insert(args: list, exception: Exception) -> None:
def submit_bugs() -> int:
"""Submit bug reports to developer, return n_submitted."""
+ # Probe mirrors, cache best working one
+ is_mirror_alive = cache_mirrors()
+ if not is_mirror_alive:
+ print("ERROR: we couldn't find an active mirror, can't submit bugs.")
+ return -1
+
+ # Gather bugs for submission
sql_bugs = sql_nt("SELECT * FROM bug WHERE submitted = 0")
api_client = ntclient.services.api.ApiClient()