Skip to content

Commit

Permalink
(fix) error where logs did not have passwords and other items censore…
Browse files Browse the repository at this point in the history
…d and UNDEFINED on first startup (#8777)

* (fix) error where logs did not have passwords and other items censored
(fix) error on first startup of UNDEFINED comparison if page loads before the error viewer has loaded
(refactor) move ErrorViewer and WarningViewer into a sinlge class called WebErrorViewer and add utility functions, which cleaned up code and moved it to sickchill.logging.weblog
(refactor) move sickchill.oldbeard.classes to sickchill.providers.result_classes

Signed-off-by: miigotu <[email protected]>

* (refactor) add missed changes in mako

Signed-off-by: miigotu <[email protected]>

* (fix) depends: explicitly include appdirs in dependencies and upgrade all yarn and poetry dependencies

Signed-off-by: miigotu <[email protected]>

---------

Signed-off-by: miigotu <[email protected]>
  • Loading branch information
miigotu authored Jul 25, 2024
1 parent 0e2751d commit abeb809
Show file tree
Hide file tree
Showing 30 changed files with 186 additions and 158 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,4 @@ beekeeper-alt = ">=2022.9.3"
stevedore = "^5.1.0"
cachecontrol = ">=0.13.1,<0.15.0"
cinemagoer = {version = "^2023.5.1", allow-prereleases = true}
appdirs = "^1.4.4"
10 changes: 5 additions & 5 deletions sickchill/gui/slick/views/errorlogs.mako
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<%inherit file="/layouts/main.mako" />
<%!
from sickchill.oldbeard import classes
import sickchill
from sickchill.logging.weblog import WebErrorViewer
import logging
%>
<%block name="content">
<%
if logLevel == sickchill.logger.WARNING:
errors = classes.WarningViewer.errors
if logLevel == logging.WARNING:
errors = WebErrorViewer.warnings
title = _('WARNING logs')
else:
errors = classes.ErrorViewer.errors
errors = WebErrorViewer.errors
title = _('ERROR logs')
%>
<div class="row">
Expand Down
5 changes: 5 additions & 0 deletions sickchill/gui/slick/views/layouts/main.mako
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@
<nav class="navbar navbar-default navbar-fixed-top hidden-print">
<div class="container-fluid">
<%
if error_count == UNDEFINED:
error_count = 0
if warning_count == UNDEFINED:
warning_count = 0
total_warning_error_count = error_count + warning_count + settings.NEWS_UNREAD
if total_warning_error_count:
if error_count:
Expand Down
16 changes: 8 additions & 8 deletions sickchill/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

from sickchill import settings
from sickchill.helper.common import dateTimeFormat
from sickchill.oldbeard import classes, notifiers
from sickchill.logging.weblog import WebErrorViewer
from sickchill.oldbeard import notifiers

# log levels
ERROR = logging.ERROR
Expand Down Expand Up @@ -58,7 +59,8 @@ def format(self, record):

for item in censored:
try:
# passwords that include ++ for example will error. Cannot escape or it wont match at all.
# passwords that include ++ for example will error. Cannot escape or it won't match at all.
# Always use 8 *'s, so people cant guess censored item length for things like passwords.
msg = re.sub(rf"\b({item})\b", "*" * 8, msg)
except re.error:
msg = msg.replace(item, "*" * 8)
Expand All @@ -68,12 +70,10 @@ def format(self, record):
# Needed because Newznab apikey isn't stored as key=value in a section.
msg = re.sub(r"([&?]r|[&?]apikey|[&?]jackett_apikey|[&?]api_key)(?:=|%3D)[^&]*([&\w]?)", r"\1=**********\2", msg, re.I)

if record.levelno == ERROR:
classes.ErrorViewer.add(classes.UIError(msg))
notifiers.notify_logged_error(classes.UIError(msg))
# Set the new message into the record!
record.msg = msg

elif record.levelno == WARNING:
classes.WarningViewer.add(classes.UIError(msg))
WebErrorViewer.add(record)

return super().format(record)

Expand Down Expand Up @@ -271,7 +271,7 @@ def log_data(min_level, log_filter, log_search, max_lines):
data = []
for _log_file in log_files:
if len(data) < max_lines:
with open(_log_file, "r") as f:
with open(_log_file) as f:
data += [line.strip() + "\n" for line in reversed(f.readlines()) if line.strip()]
else:
break
Expand Down
Empty file added sickchill/logging/__init__.py
Empty file.
106 changes: 106 additions & 0 deletions sickchill/logging/weblog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import datetime
import logging
import sys
from logging import ERROR, WARNING

from sickchill.helper.common import dateTimeFormat
from sickchill.oldbeard.notifiers import notify_logged_error


class __WebErrorViewer(object):
"""
Keeps a static list of UIErrors to be displayed on the UI and allows
the list to be cleared.
"""

__errors = []
__warnings = []

def __init__(self):
self.__errors = []
self.__warnings = []

def __len__(self):
return self.len()

def add_error(self, error):
self.__errors = [e for e in self.__errors if e.message != error.message]
self.__errors.append(error)

def add_warning(self, warning):
self.__warnings = [w for w in self.__warnings if w.message != warning.message]
self.__warnings.append(warning)

def add(self, record):
if record.levelno in (ERROR, WARNING):
ui_error = UIError(record.msg, record.levelno)
if record.levelno == ERROR:
self.add_error(ui_error)
if record.levelno == WARNING:
self.add_warning(ui_error)

if record.levelno == ERROR:
notify_logged_error(ui_error)

def get_errors(self):
return self.__errors

def get_warnings(self):
return self.__warnings

def have_errors(self):
return len(self.__errors)

def have_warnings(self):
return len(self.__warnings)

def clear_errors(self):
self.__errors = []

def clear_warnings(self):
self.__warnings = []

def clear(self, level: str) -> str:
level = logging.getLevelName(level.upper())
if level == logging.ERROR:
message = "Error logs cleared"
self.clear_errors()
elif level == logging.WARNING:
message = "Warning logs cleared"
self.clear_warnings()
else:
message = "Warning and Error logs cleared"
self.clear_warnings()
self.clear_errors()

return message

def num_errors(self):
return len(self.__errors)

def num_warnings(self):
return len(self.__warnings)

def has_errors(self):
return len(self.__errors) > 0

def has_warnings(self):
return len(self.__warnings) > 0

def len(self):
return self.num_errors() + self.num_warnings()


WebErrorViewer = __WebErrorViewer()


class UIError(object):
"""
Represents an error to be displayed in the web UI.
"""

def __init__(self, message, level):
self.title = sys.exc_info()[-2] or message
self.message = message
self.time = datetime.datetime.now().strftime(dateTimeFormat)
self.level = level
2 changes: 1 addition & 1 deletion sickchill/oldbeard/clients/download_station.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from sickchill.oldbeard.clients.generic import GenericClient

if TYPE_CHECKING:
from sickchill.oldbeard.classes import SearchResult
from sickchill.providers.result_classes import SearchResult


class Client(GenericClient):
Expand Down
2 changes: 1 addition & 1 deletion sickchill/oldbeard/clients/qbittorrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from sickchill.oldbeard.clients.generic import GenericClient

if TYPE_CHECKING: # pragma: no cover
from sickchill.oldbeard.classes import TorrentSearchResult
from sickchill.providers.result_classes import TorrentSearchResult


class Client(GenericClient):
Expand Down
2 changes: 1 addition & 1 deletion sickchill/oldbeard/clients/transmission.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from sickchill.oldbeard.clients.generic import GenericClient

if TYPE_CHECKING: # pragma: no cover
from sickchill.oldbeard.classes import TorrentSearchResult
from sickchill.providers.result_classes import TorrentSearchResult


class Client(GenericClient):
Expand Down
8 changes: 6 additions & 2 deletions sickchill/oldbeard/notifiers/discord.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from typing import TYPE_CHECKING

from sickchill import logger, settings
from sickchill.oldbeard import common
from sickchill.oldbeard.classes import UIError

if TYPE_CHECKING:
from sickchill.logging.weblog import UIError


class Notifier(object):
Expand Down Expand Up @@ -28,7 +32,7 @@ def notify_login(self, ipaddress=""):
title = common.notifyStrings[common.NOTIFY_LOGIN]
self._notify_discord(title + " - " + update_text.format(ipaddress))

def notify_logged_error(self, ui_error: UIError):
def notify_logged_error(self, ui_error: "UIError"):
if settings.USE_DISCORD:
update_text = ui_error.message
title = ui_error.title
Expand Down
7 changes: 5 additions & 2 deletions sickchill/oldbeard/notifiers/pushbullet.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from typing import TYPE_CHECKING
from urllib.parse import urljoin

from requests.structures import CaseInsensitiveDict

from sickchill import logger, settings
from sickchill.oldbeard import common, helpers
from sickchill.oldbeard.classes import UIError

if TYPE_CHECKING:
from sickchill.logging.weblog import UIError


class Notifier(object):
Expand Down Expand Up @@ -54,7 +57,7 @@ def notify_login(self, ipaddress=""):
pushbullet_api=None, event=common.notifyStrings[common.NOTIFY_LOGIN], message=common.notifyStrings[common.NOTIFY_LOGIN_TEXT].format(ipaddress)
)

def notify_logged_error(self, ui_error: UIError):
def notify_logged_error(self, ui_error: "UIError"):
self._sendPushbullet(pushbullet_api=None, event=ui_error.title, message=ui_error.message)

def _sendPushbullet(self, pushbullet_api=None, pushbullet_device=None, pushbullet_channel=None, event=None, message=None, link=None, force=False):
Expand Down
8 changes: 6 additions & 2 deletions sickchill/oldbeard/notifiers/pushover.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Author: Marvin Pinto <[email protected]>
# Author: Dennis Lutter <[email protected]>
import time
from typing import TYPE_CHECKING

import requests

from sickchill import logger, settings
from sickchill.oldbeard.classes import UIError
from sickchill.oldbeard.common import (
NOTIFY_DOWNLOAD,
NOTIFY_LOGIN,
Expand All @@ -18,6 +18,10 @@
)
from sickchill.oldbeard.helpers import make_session

if TYPE_CHECKING:
from sickchill.logging.weblog import UIError


API_URL = "https://api.pushover.net/1/messages.json"


Expand Down Expand Up @@ -128,7 +132,7 @@ def notify_login(self, ipaddress=""):
title = notifyStrings[NOTIFY_LOGIN]
self._notify_pushover(title, update_text.format(ipaddress))

def notify_logged_error(self, ui_error: UIError):
def notify_logged_error(self, ui_error: "UIError"):
if settings.USE_PUSHOVER:
self._notify_pushover(title=ui_error.title, message=ui_error.message)

Expand Down
7 changes: 5 additions & 2 deletions sickchill/oldbeard/notifiers/synologynotifier.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import os
import subprocess
from typing import TYPE_CHECKING

from sickchill import logger, settings
from sickchill.oldbeard import common
from sickchill.oldbeard.classes import UIError

if TYPE_CHECKING:
from sickchill.logging.weblog import UIError


class Notifier(object):
Expand Down Expand Up @@ -31,7 +34,7 @@ def notify_login(self, ipaddress=""):
title = common.notifyStrings[common.NOTIFY_LOGIN]
self._send_synologyNotifier(update_text.format(ipaddress), title)

def notify_logged_error(self, ui_error: UIError):
def notify_logged_error(self, ui_error: "UIError"):
if settings.USE_SYNOLOGYNOTIFIER and ui_error:
update_text = ui_error.message
title = ui_error.title
Expand Down
5 changes: 3 additions & 2 deletions sickchill/oldbeard/nzbSplitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
from xml.etree import ElementTree

from sickchill import logger
from sickchill.oldbeard import classes, helpers
from sickchill.oldbeard import helpers
from sickchill.oldbeard.name_parser.parser import InvalidNameException, InvalidShowException, NameParser
from sickchill.providers import result_classes


def get_season_nzbs(name, url_data, season):
Expand Down Expand Up @@ -175,7 +176,7 @@ def split_result(obj):
ep_obj_list = [obj.show.get_episode(season, ep) for ep in parsed_obj.episode_numbers]

# make a result
cur_obj = classes.NZBDataSearchResult(ep_obj_list)
cur_obj = result_classes.NZBDataSearchResult(ep_obj_list)
cur_obj.name = new_nzb
cur_obj.provider = obj.provider
cur_obj.quality = obj.quality
Expand Down
2 changes: 1 addition & 1 deletion sickchill/oldbeard/nzbget.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from sickchill.oldbeard.helpers import make_context

if TYPE_CHECKING:
from sickchill.oldbeard.classes import SearchResult
from sickchill.providers.result_classes import SearchResult


def get_proxy(https: bool, host: str, username: str, password: str, verify: bool) -> xmlrpc.client.ServerProxy:
Expand Down
5 changes: 3 additions & 2 deletions sickchill/oldbeard/providers/btn.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
from sickchill import logger, settings
from sickchill.helper.common import episode_num
from sickchill.helper.exceptions import AuthException
from sickchill.oldbeard import classes, scene_exceptions, tvcache
from sickchill.oldbeard import scene_exceptions, tvcache
from sickchill.oldbeard.common import cpu_presets
from sickchill.oldbeard.helpers import sanitizeSceneName
from sickchill.providers import result_classes
from sickchill.providers.torrent.TorrentProvider import TorrentProvider

if TYPE_CHECKING:
Expand Down Expand Up @@ -233,7 +234,7 @@ def find_propers(self, search_date=None):
if result_date and (not search_date or result_date > search_date):
title, url = self._get_title_and_url(item)
if title and url:
results.append(classes.Proper(title, url, result_date, self.show))
results.append(result_classes.Proper(title, url, result_date, self.show))

return results

Expand Down
5 changes: 3 additions & 2 deletions sickchill/oldbeard/providers/hdbits.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

from sickchill import logger
from sickchill.helper.exceptions import AuthException
from sickchill.oldbeard import classes, tvcache
from sickchill.oldbeard import tvcache
from sickchill.providers import result_classes
from sickchill.providers.torrent.TorrentProvider import TorrentProvider

if TYPE_CHECKING:
Expand Down Expand Up @@ -91,7 +92,7 @@ def find_propers(self, search_date=None):

if result_date and (not search_date or result_date > search_date):
title, url = self._get_title_and_url(item)
results.append(classes.Proper(title, url, result_date, self.show))
results.append(result_classes.Proper(title, url, result_date, self.show))

return results

Expand Down
2 changes: 1 addition & 1 deletion sickchill/oldbeard/sab.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
session = helpers.make_session()

if TYPE_CHECKING:
from sickchill.oldbeard.classes import SearchResult
from sickchill.providers.result_classes import SearchResult


def send_nzb(result: "SearchResult"):
Expand Down
Loading

0 comments on commit abeb809

Please sign in to comment.