From 07495f9e5d6c2ba3774adcd705117ae75cb917fc Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Fri, 29 Oct 2021 15:33:52 +0100 Subject: [PATCH] Show warnings for exceptions --- utils.py | 49 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/utils.py b/utils.py index ed55ceb06..3b4a1a2f9 100644 --- a/utils.py +++ b/utils.py @@ -16,9 +16,9 @@ import json import idna import locale from pprint import pprint -from followingCalendar import addPersonToCalendar from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import hashes +from followingCalendar import addPersonToCalendar # posts containing these strings will always get screened out, # both incoming and outgoing. @@ -222,6 +222,8 @@ def validPostDate(published: str, maxAgeDays: int = 90, postTimeObject = \ datetime.datetime.strptime(published, "%Y-%m-%dT%H:%M:%SZ") except BaseException: + if debug: + print('WARN: Invalid published date ' + str(published)) return False daysDiff = postTimeObject - baselineTime @@ -625,6 +627,8 @@ def removeAvatarFromCache(baseDir: str, actorStr: str) -> None: try: os.remove(avatarFilename) except BaseException: + print('WARN: Unable to delete cached avatar ' + + str(avatarFilename)) pass @@ -656,7 +660,7 @@ def loadJson(filename: str, delaySec: int = 2, maxTries: int = 5) -> {}: jsonObject = json.loads(data) break except BaseException: - print('WARN: loadJson exception') + print('WARN: loadJson exception ' + str(filename)) if delaySec > 0: time.sleep(delaySec) tries += 1 @@ -681,7 +685,7 @@ def loadJsonOnionify(filename: str, domain: str, onionDomain: str, jsonObject = json.loads(data) break except BaseException: - print('WARN: loadJson exception') + print('WARN: loadJson exception ' + str(filename)) if delaySec > 0: time.sleep(delaySec) tries += 1 @@ -1286,7 +1290,7 @@ def clearFromPostCaches(baseDir: str, recentPostsCache: {}, os.remove(postFilename) except BaseException: print('WARN: clearFromPostCaches file not removed ' + - postFilename) + str(postFilename)) pass # if the post is in the recent posts cache then remove it if recentPostsCache.get('index'): @@ -1384,6 +1388,9 @@ def setReplyIntervalHours(baseDir: str, nickname: str, domain: str, fp.write(str(replyIntervalHours)) return True except BaseException: + print('WARN: unable to save reply interval ' + + str(replyIntervalFilename) + ' ' + + str(replyIntervalHours)) pass return False @@ -1411,6 +1418,7 @@ def canReplyTo(baseDir: str, nickname: str, domain: str, try: pubDate = datetime.datetime.strptime(published, '%Y-%m-%dT%H:%M:%SZ') except BaseException: + print('WARN: Unrecognized published date ' + str(published)) return False if not currDateStr: currDate = datetime.datetime.utcnow() @@ -1419,6 +1427,7 @@ def canReplyTo(baseDir: str, nickname: str, domain: str, currDate = datetime.datetime.strptime(currDateStr, '%Y-%m-%dT%H:%M:%SZ') except BaseException: + print('WARN: Unrecognized current date ' + str(currDateStr)) return False hoursSincePublication = int((currDate - pubDate).total_seconds() / 3600) if hoursSincePublication < 0 or \ @@ -1442,12 +1451,14 @@ def _removeAttachment(baseDir: str, httpPrefix: str, domain: str, try: os.remove(mediaFilename) except BaseException: + print('WARN: unable to delete media file ' + str(mediaFilename)) pass etagFilename = mediaFilename + '.etag' if os.path.isfile(etagFilename): try: os.remove(etagFilename) except BaseException: + print('WARN: unable to delete etag file ' + str(etagFilename)) pass postJson['attachment'] = [] @@ -1516,6 +1527,7 @@ def _deletePostRemoveReplies(baseDir: str, nickname: str, domain: str, try: os.remove(repliesFilename) except BaseException: + print('WARN: unable to delete replies file ' + str(repliesFilename)) pass @@ -1575,6 +1587,8 @@ def _deleteCachedHtml(baseDir: str, nickname: str, domain: str, try: os.remove(cachedPostFilename) except BaseException: + print('WARN: unable to delete cached post file ' + + str(cachedPostFilename)) pass @@ -1622,6 +1636,8 @@ def _deleteHashtagsOnPost(baseDir: str, postJsonObject: {}) -> None: try: os.remove(tagIndexFilename) except BaseException: + print('WARN: Unable to delete tag index ' + + str(tagIndexFilename)) pass else: # write the new hashtag index without the given post in it @@ -1660,10 +1676,14 @@ def _deleteConversationPost(baseDir: str, nickname: str, domain: str, try: os.remove(conversationFilename + '.muted') except BaseException: + print('WARN: Unable to remove conversation ' + + str(conversationFilename) + '.muted') pass try: os.remove(conversationFilename) except BaseException: + print('WARN: Unable to remove conversation ' + + str(conversationFilename)) pass @@ -1682,6 +1702,8 @@ def deletePost(baseDir: str, httpPrefix: str, try: os.remove(postFilename) except BaseException: + if debug: + print('WARN: Unable to delete post ' + str(postFilename)) pass return @@ -1710,6 +1732,7 @@ def deletePost(baseDir: str, httpPrefix: str, try: os.remove(extFilename) except BaseException: + print('WARN: unable to remove ext ' + str(extFilename)) pass # remove cached html version of the post @@ -1739,6 +1762,8 @@ def deletePost(baseDir: str, httpPrefix: str, try: os.remove(postFilename) except BaseException: + if debug: + print('WARN: Unable to delete post ' + str(postFilename)) pass @@ -2179,6 +2204,8 @@ def undoLikesCollectionEntry(recentPostsCache: {}, try: os.remove(cachedPostFilename) except BaseException: + print('WARN: Unable to delete cached post ' + + str(cachedPostFilename)) pass removePostFromCache(postJsonObject, recentPostsCache) @@ -2241,6 +2268,9 @@ def undoAnnounceCollectionEntry(recentPostsCache: {}, try: os.remove(cachedPostFilename) except BaseException: + if debug: + print('WARN: Unable to delete cached post ' + + str(cachedPostFilename)) pass removePostFromCache(postJsonObject, recentPostsCache) @@ -2305,6 +2335,9 @@ def updateAnnounceCollection(recentPostsCache: {}, try: os.remove(cachedPostFilename) except BaseException: + if debug: + print('WARN: Unable to delete cached post ' + + str(cachedPostFilename)) pass removePostFromCache(postJsonObject, recentPostsCache) @@ -2407,6 +2440,7 @@ def isRecentPost(postJsonObject: {}, maxDays: int = 3) -> bool: datetime.datetime.strptime(publishedDateStr, "%Y-%m-%dT%H:%M:%SZ") except BaseException: + print('WARN: Unrecognized published date ' + str(publishedDateStr)) return False publishedDaysSinceEpoch = \ @@ -2552,7 +2586,7 @@ def loadTranslationsFromFile(baseDir: str, language: str) -> ({}, str): """ if not os.path.isdir(baseDir + '/translations'): print('ERROR: translations directory not found') - return + return None, None if not language: systemLanguage = locale.getdefaultlocale()[0] else: @@ -2849,6 +2883,7 @@ def dateStringToSeconds(dateStr: str) -> int: expiryTime = \ datetime.datetime.strptime(dateStr, '%Y-%m-%dT%H:%M:%SZ') except BaseException: + print('WARN: Unable to parse date ' + str(dateStr)) return None return int(datetime.datetime.timestamp(expiryTime)) @@ -2983,7 +3018,7 @@ def getSupportedLanguages(baseDir: str) -> []: """ translationsDir = baseDir + '/translations' languagesStr = [] - for subdir, dirs, files in os.walk(translationsDir): + for _, _, files in os.walk(translationsDir): for f in files: if not f.endswith('.json'): continue @@ -2999,7 +3034,7 @@ def getCategoryTypes(baseDir: str) -> []: """ ontologyDir = baseDir + '/ontology' categories = [] - for subdir, dirs, files in os.walk(ontologyDir): + for _, _, files in os.walk(ontologyDir): for f in files: if not f.endswith('.json'): continue