Show warnings for exceptions

main
Bob Mottram 2021-10-29 15:33:52 +01:00
parent 09bb7f99b5
commit 07495f9e5d
1 changed files with 42 additions and 7 deletions

View File

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