File write exceptions

merge-requests/30/head
Bob Mottram 2021-11-25 21:18:53 +00:00
parent 6953b2cd43
commit a2b4efcaaf
16 changed files with 346 additions and 176 deletions

View File

@ -171,8 +171,9 @@ def storeBasicCredentials(baseDir: str, nickname: str, password: str) -> bool:
fout.write(line) fout.write(line)
else: else:
fout.write(storeStr + '\n') fout.write(storeStr + '\n')
except OSError: except OSError as e:
print('WARN: unable to save password ' + passwordFile) print('WARN: unable to save password ' + passwordFile +
' ' + str(e))
return False return False
try: try:
@ -210,8 +211,8 @@ def removePassword(baseDir: str, nickname: str) -> None:
for line in fin: for line in fin:
if not line.startswith(nickname + ':'): if not line.startswith(nickname + ':'):
fout.write(line) fout.write(line)
except OSError: except OSError as e:
print('WARN: unable to remove password from file') print('WARN: unable to remove password from file ' + str(e))
return return
try: try:

View File

@ -153,9 +153,9 @@ def removeGlobalBlock(baseDir: str,
line.replace('\n', '').replace('\r', '') line.replace('\n', '').replace('\r', '')
if unblockHandle not in line: if unblockHandle not in line:
fpnew.write(handle + '\n') fpnew.write(handle + '\n')
except OSError: except OSError as e:
print('WARN: failed to remove global block ' + print('WARN: failed to remove global block ' +
unblockingFilename) unblockingFilename + ' ' + str(e))
return False return False
if os.path.isfile(unblockingFilename + '.new'): if os.path.isfile(unblockingFilename + '.new'):
os.rename(unblockingFilename + '.new', unblockingFilename) os.rename(unblockingFilename + '.new', unblockingFilename)
@ -172,9 +172,9 @@ def removeGlobalBlock(baseDir: str,
line.replace('\n', '').replace('\r', '') line.replace('\n', '').replace('\r', '')
if unblockHashtag not in line: if unblockHashtag not in line:
fpnew.write(blockLine + '\n') fpnew.write(blockLine + '\n')
except OSError: except OSError as e:
print('WARN: failed to remove global hashtag block ' + print('WARN: failed to remove global hashtag block ' +
unblockingFilename) unblockingFilename + ' ' + str(e))
return False return False
if os.path.isfile(unblockingFilename + '.new'): if os.path.isfile(unblockingFilename + '.new'):
os.rename(unblockingFilename + '.new', unblockingFilename) os.rename(unblockingFilename + '.new', unblockingFilename)
@ -198,8 +198,9 @@ def removeBlock(baseDir: str, nickname: str, domain: str,
handle = line.replace('\n', '').replace('\r', '') handle = line.replace('\n', '').replace('\r', '')
if unblockHandle not in line: if unblockHandle not in line:
fpnew.write(handle + '\n') fpnew.write(handle + '\n')
except OSError: except OSError as e:
print('WARN: failed to remove block ' + unblockingFilename) print('WARN: failed to remove block ' +
unblockingFilename + ' ' + str(e))
return False return False
if os.path.isfile(unblockingFilename + '.new'): if os.path.isfile(unblockingFilename + '.new'):
os.rename(unblockingFilename + '.new', unblockingFilename) os.rename(unblockingFilename + '.new', unblockingFilename)
@ -907,8 +908,8 @@ def setBrochMode(baseDir: str, domainFull: str, enabled: bool) -> None:
for d in allowedDomains: for d in allowedDomains:
allowFile.write(d + '\n') allowFile.write(d + '\n')
print('Broch mode enabled') print('Broch mode enabled')
except OSError: except OSError as e:
print('WARN: Broch mode not enabled due to file write') print('WARN: Broch mode not enabled due to file write ' + str(e))
return return
setConfigParam(baseDir, "brochMode", enabled) setConfigParam(baseDir, "brochMode", enabled)

View File

@ -98,9 +98,9 @@ def _noOfBlogReplies(baseDir: str, httpPrefix: str, translate: {},
replyPostId.replace('\n', '').replace('\r', '') replyPostId.replace('\n', '').replace('\r', '')
if replyPostId not in removals: if replyPostId not in removals:
f.write(replyPostId + '\n') f.write(replyPostId + '\n')
except OSError: except OSError as e:
print('WARN: unable to remove replies from post ' + postFilename) print('WARN: unable to remove replies from post ' +
pass postFilename + ' ' + str(e))
return replies return replies

View File

@ -167,8 +167,9 @@ def setHashtagCategory(baseDir: str, hashtag: str, category: str,
if update: if update:
updateHashtagCategories(baseDir) updateHashtagCategories(baseDir)
return True return True
except OSError: except OSError as e:
print('WARN: unable to write category ' + categoryFilename) print('WARN: unable to write category ' + categoryFilename +
' ' + str(e))
pass pass
return False return False

View File

@ -2362,8 +2362,9 @@ class PubServer(BaseHTTPRequestHandler):
with open(nwFilename, 'w+') as noNewswireFile: with open(nwFilename, 'w+') as noNewswireFile:
noNewswireFile.write('\n') noNewswireFile.write('\n')
refreshNewswire(self.server.baseDir) refreshNewswire(self.server.baseDir)
except OSError: except OSError as e:
print('WARN: unable to write ' + nwFilename) print('WARN: unable to write ' + nwFilename +
' ' + str(e))
usersPathStr = \ usersPathStr = \
usersPath + '/' + self.server.defaultTimeline + \ usersPath + '/' + self.server.defaultTimeline + \
'?page=' + str(pageNumber) '?page=' + str(pageNumber)
@ -2404,8 +2405,9 @@ class PubServer(BaseHTTPRequestHandler):
with open(featFilename, 'w+') as noFeaturesFile: with open(featFilename, 'w+') as noFeaturesFile:
noFeaturesFile.write('\n') noFeaturesFile.write('\n')
refreshNewswire(self.server.baseDir) refreshNewswire(self.server.baseDir)
except OSError: except OSError as e:
print('WARN: unable to write ' + featFilename) print('WARN: unable to write ' + featFilename +
' ' + str(e))
usersPathStr = \ usersPathStr = \
usersPath + '/' + self.server.defaultTimeline + \ usersPath + '/' + self.server.defaultTimeline + \
'?page=' + str(pageNumber) '?page=' + str(pageNumber)

View File

@ -296,12 +296,15 @@ def unfollowAccount(baseDir: str, nickname: str, domain: str,
return return
with open(filename, 'r') as f: with open(filename, 'r') as f:
lines = f.readlines() lines = f.readlines()
with open(filename, 'w+') as f: try:
for line in lines: with open(filename, 'w+') as f:
checkHandle = line.strip("\n").strip("\r").lower() for line in lines:
if checkHandle != handleToUnfollowLower and \ checkHandle = line.strip("\n").strip("\r").lower()
checkHandle != '!' + handleToUnfollowLower: if checkHandle != handleToUnfollowLower and \
f.write(line) checkHandle != '!' + handleToUnfollowLower:
f.write(line)
except OSError as e:
print('WARN: unable to write ' + filename + ' ' + str(e))
# write to an unfollowed file so that if a follow accept # write to an unfollowed file so that if a follow accept
# later arrives then it can be ignored # later arrives then it can be ignored
@ -312,8 +315,11 @@ def unfollowAccount(baseDir: str, nickname: str, domain: str,
with open(unfollowedFilename, 'a+') as f: with open(unfollowedFilename, 'a+') as f:
f.write(handleToUnfollow + '\n') f.write(handleToUnfollow + '\n')
else: else:
with open(unfollowedFilename, 'w+') as f: try:
f.write(handleToUnfollow + '\n') with open(unfollowedFilename, 'w+') as f:
f.write(handleToUnfollow + '\n')
except OSError:
print('WARN: unable to write ' + unfollowedFilename)
return True return True
@ -650,8 +656,11 @@ def _storeFollowRequest(baseDir: str,
print('DEBUG: ' + approveHandleStored + print('DEBUG: ' + approveHandleStored +
' is already awaiting approval') ' is already awaiting approval')
else: else:
with open(approveFollowsFilename, 'w+') as fp: try:
fp.write(approveHandleStored + '\n') with open(approveFollowsFilename, 'w+') as fp:
fp.write(approveHandleStored + '\n')
except OSError:
print('WARN: unable to write ' + approveFollowsFilename)
# store the follow request in its own directory # store the follow request in its own directory
# We don't rely upon the inbox because items in there could expire # We don't rely upon the inbox because items in there could expire
@ -851,8 +860,11 @@ def receiveFollowRequest(session, baseDir: str, httpPrefix: str,
'Failed to write entry to followers file ' + 'Failed to write entry to followers file ' +
str(e)) str(e))
else: else:
with open(followersFilename, 'w+') as followersFile: try:
followersFile.write(approveHandle + '\n') with open(followersFilename, 'w+') as followersFile:
followersFile.write(approveHandle + '\n')
except OSError:
print('WARN: unable to write ' + followersFilename)
print('Beginning follow accept') print('Beginning follow accept')
return followedAccountAccepts(session, baseDir, httpPrefix, return followedAccountAccepts(session, baseDir, httpPrefix,
@ -1046,8 +1058,11 @@ def sendFollowRequest(session, baseDir: str,
unfollowedFile = \ unfollowedFile = \
unfollowedFile.replace(followHandle + '\n', '') unfollowedFile.replace(followHandle + '\n', '')
if unfollowedFile: if unfollowedFile:
with open(unfollowedFilename, 'w+') as fp: try:
fp.write(unfollowedFile) with open(unfollowedFilename, 'w+') as fp:
fp.write(unfollowedFile)
except OSError:
print('WARN: unable to write ' + unfollowedFilename)
newFollowJson = { newFollowJson = {
'@context': 'https://www.w3.org/ns/activitystreams', '@context': 'https://www.w3.org/ns/activitystreams',

View File

@ -46,8 +46,11 @@ def receivingCalendarEvents(baseDir: str, nickname: str, domain: str,
# create a new calendar file from the following file # create a new calendar file from the following file
with open(followingFilename, 'r') as followingFile: with open(followingFilename, 'r') as followingFile:
followingHandles = followingFile.read() followingHandles = followingFile.read()
with open(calendarFilename, 'w+') as fp: try:
fp.write(followingHandles) with open(calendarFilename, 'w+') as fp:
fp.write(followingHandles)
except OSError:
print('WARN: unable to write ' + calendarFilename)
return handle + '\n' in open(calendarFilename).read() return handle + '\n' in open(calendarFilename).read()
@ -89,8 +92,11 @@ def _receiveCalendarEvents(baseDir: str, nickname: str, domain: str,
with open(followingFilename, 'r') as followingFile: with open(followingFilename, 'r') as followingFile:
followingHandles = followingFile.read() followingHandles = followingFile.read()
if add: if add:
with open(calendarFilename, 'w+') as fp: try:
fp.write(followingHandles + handle + '\n') with open(calendarFilename, 'w+') as fp:
fp.write(followingHandles + handle + '\n')
except OSError:
print('WARN: unable to write ' + calendarFilename)
# already in the calendar file? # already in the calendar file?
if handle + '\n' in followingHandles: if handle + '\n' in followingHandles:
@ -100,16 +106,22 @@ def _receiveCalendarEvents(baseDir: str, nickname: str, domain: str,
return return
# remove from calendar file # remove from calendar file
followingHandles = followingHandles.replace(handle + '\n', '') followingHandles = followingHandles.replace(handle + '\n', '')
with open(calendarFilename, 'w+') as fp: try:
fp.write(followingHandles) with open(calendarFilename, 'w+') as fp:
fp.write(followingHandles)
except OSError:
print('WARN: unable to write ' + calendarFilename)
else: else:
print(handle + ' not in followingCalendar.txt') print(handle + ' not in followingCalendar.txt')
# not already in the calendar file # not already in the calendar file
if add: if add:
# append to the list of handles # append to the list of handles
followingHandles += handle + '\n' followingHandles += handle + '\n'
with open(calendarFilename, 'w+') as fp: try:
fp.write(followingHandles) with open(calendarFilename, 'w+') as fp:
fp.write(followingHandles)
except OSError:
print('WARN: unable to write ' + calendarFilename)
def addPersonToCalendar(baseDir: str, nickname: str, domain: str, def addPersonToCalendar(baseDir: str, nickname: str, domain: str,

15
git.py
View File

@ -208,11 +208,14 @@ def receiveGitPatch(baseDir: str, nickname: str, domain: str,
return False return False
patchStr = \ patchStr = \
_gitAddFromHandle(patchStr, '@' + fromNickname + '@' + fromDomain) _gitAddFromHandle(patchStr, '@' + fromNickname + '@' + fromDomain)
with open(patchFilename, 'w+') as patchFile: try:
patchFile.write(patchStr) with open(patchFilename, 'w+') as patchFile:
patchNotifyFilename = \
acctDir(baseDir, nickname, domain) + '/.newPatchContent'
with open(patchNotifyFilename, 'w+') as patchFile:
patchFile.write(patchStr) patchFile.write(patchStr)
return True patchNotifyFilename = \
acctDir(baseDir, nickname, domain) + '/.newPatchContent'
with open(patchNotifyFilename, 'w+') as patchFile:
patchFile.write(patchStr)
return True
except OSError as e:
print('WARN unable to write patch ' + patchFilename + ' ' + str(e))
return False return False

View File

@ -41,9 +41,8 @@ def _removeEventFromTimeline(eventId: str, tlEventsFilename: str) -> None:
try: try:
with open(tlEventsFilename, 'w+') as fp2: with open(tlEventsFilename, 'w+') as fp2:
fp2.write(eventsTimeline) fp2.write(eventsTimeline)
except BaseException: except OSError:
print('EX: ERROR: unable to save events timeline') print('EX: ERROR: unable to save events timeline')
pass
def saveEventPost(baseDir: str, handle: str, postId: str, def saveEventPost(baseDir: str, handle: str, postId: str,
@ -105,13 +104,16 @@ def saveEventPost(baseDir: str, handle: str, postId: str,
if eventId + '\n' not in content: if eventId + '\n' not in content:
tlEventsFile.seek(0, 0) tlEventsFile.seek(0, 0)
tlEventsFile.write(eventId + '\n' + content) tlEventsFile.write(eventId + '\n' + content)
except Exception as e: except OSError as e:
print('WARN: Failed to write entry to events file ' + print('WARN: Failed to write entry to events file ' +
tlEventsFilename + ' ' + str(e)) tlEventsFilename + ' ' + str(e))
return False return False
else: else:
with open(tlEventsFilename, 'w+') as tlEventsFile: try:
tlEventsFile.write(eventId + '\n') with open(tlEventsFilename, 'w+') as tlEventsFile:
tlEventsFile.write(eventId + '\n')
except OSError:
print('WARN: unable to write ' + tlEventsFilename)
# create a directory for the calendar year # create a directory for the calendar year
if not os.path.isdir(calendarPath + '/' + str(eventYear)): if not os.path.isdir(calendarPath + '/' + str(eventYear)):
@ -128,18 +130,24 @@ def saveEventPost(baseDir: str, handle: str, postId: str,
return False return False
# append the post Id to the file for the calendar month # append the post Id to the file for the calendar month
with open(calendarFilename, 'a+') as calendarFile: try:
calendarFile.write(postId + '\n') with open(calendarFilename, 'a+') as calendarFile:
calendarFile.write(postId + '\n')
except OSError:
print('WARN: unable to append ' + calendarFilename)
# create a file which will trigger a notification that # create a file which will trigger a notification that
# a new event has been added # a new event has been added
calendarNotificationFilename = \ calNotifyFilename = baseDir + '/accounts/' + handle + '/.newCalendar'
baseDir + '/accounts/' + handle + '/.newCalendar' notifyStr = \
with open(calendarNotificationFilename, 'w+') as calendarNotificationFile: '/calendar?year=' + str(eventYear) + '?month=' + \
notifyStr = \ str(eventMonthNumber) + '?day=' + str(eventDayOfMonth)
'/calendar?year=' + str(eventYear) + '?month=' + \ try:
str(eventMonthNumber) + '?day=' + str(eventDayOfMonth) with open(calNotifyFilename, 'w+') as calendarNotificationFile:
calendarNotificationFile.write(notifyStr) calendarNotificationFile.write(notifyStr)
except OSError:
print('WARN: unable to write ' + calNotifyFilename)
return False
return True return True
@ -244,9 +252,12 @@ def getTodaysEvents(baseDir: str, nickname: str, domain: str,
# if some posts have been deleted then regenerate the calendar file # if some posts have been deleted then regenerate the calendar file
if recreateEventsFile: if recreateEventsFile:
with open(calendarFilename, 'w+') as calendarFile: try:
for postId in calendarPostIds: with open(calendarFilename, 'w+') as calendarFile:
calendarFile.write(postId + '\n') for postId in calendarPostIds:
calendarFile.write(postId + '\n')
except OSError:
print('WARN: unable to write ' + calendarFilename)
return events return events
@ -360,9 +371,12 @@ def getThisWeeksEvents(baseDir: str, nickname: str, domain: str) -> {}:
# if some posts have been deleted then regenerate the calendar file # if some posts have been deleted then regenerate the calendar file
if recreateEventsFile: if recreateEventsFile:
with open(calendarFilename, 'w+') as calendarFile: try:
for postId in calendarPostIds: with open(calendarFilename, 'w+') as calendarFile:
calendarFile.write(postId + '\n') for postId in calendarPostIds:
calendarFile.write(postId + '\n')
except OSError:
print('WARN: unable to write ' + calendarFilename)
return events return events
@ -424,9 +438,12 @@ def getCalendarEvents(baseDir: str, nickname: str, domain: str,
# if some posts have been deleted then regenerate the calendar file # if some posts have been deleted then regenerate the calendar file
if recreateEventsFile: if recreateEventsFile:
with open(calendarFilename, 'w+') as calendarFile: try:
for postId in calendarPostIds: with open(calendarFilename, 'w+') as calendarFile:
calendarFile.write(postId + '\n') for postId in calendarPostIds:
calendarFile.write(postId + '\n')
except OSError:
print('WARN: unable to write ' + calendarFilename)
return events return events
@ -449,7 +466,10 @@ def removeCalendarEvent(baseDir: str, nickname: str, domain: str,
lines = f.readlines() lines = f.readlines()
if not lines: if not lines:
return return
with open(calendarFilename, 'w+') as f: try:
for line in lines: with open(calendarFilename, 'w+') as f:
if messageId not in line: for line in lines:
f.write(line) if messageId not in line:
f.write(line)
except OSError:
print('WARN: unable to write ' + calendarFilename)

112
inbox.py
View File

@ -141,9 +141,8 @@ def _storeLastPostId(baseDir: str, nickname: str, domain: str,
try: try:
with open(actorFilename, 'w+') as fp: with open(actorFilename, 'w+') as fp:
fp.write(postId) fp.write(postId)
except BaseException: except OSError:
print('EX: Unable to write last post id to ' + actorFilename) print('EX: Unable to write last post id to ' + actorFilename)
pass
def _updateCachedHashtagSwarm(baseDir: str, nickname: str, domain: str, def _updateCachedHashtagSwarm(baseDir: str, nickname: str, domain: str,
@ -185,10 +184,9 @@ def _updateCachedHashtagSwarm(baseDir: str, nickname: str, domain: str,
with open(cachedHashtagSwarmFilename, 'w+') as fp: with open(cachedHashtagSwarmFilename, 'w+') as fp:
fp.write(newSwarmStr) fp.write(newSwarmStr)
return True return True
except BaseException: except OSError:
print('EX: unable to write cached hashtag swarm ' + print('EX: unable to write cached hashtag swarm ' +
cachedHashtagSwarmFilename) cachedHashtagSwarmFilename)
pass
return False return False
@ -238,8 +236,11 @@ def storeHashTags(baseDir: str, nickname: str, domain: str,
tagline = str(daysSinceEpoch) + ' ' + nickname + ' ' + postUrl + '\n' tagline = str(daysSinceEpoch) + ' ' + nickname + ' ' + postUrl + '\n'
hashtagsCtr += 1 hashtagsCtr += 1
if not os.path.isfile(tagsFilename): if not os.path.isfile(tagsFilename):
with open(tagsFilename, 'w+') as tagsFile: try:
tagsFile.write(tagline) with open(tagsFilename, 'w+') as tagsFile:
tagsFile.write(tagline)
except OSError:
print('WARN: unable to write ' + tagsFilename)
else: else:
if postUrl not in open(tagsFilename).read(): if postUrl not in open(tagsFilename).read():
try: try:
@ -248,7 +249,7 @@ def storeHashTags(baseDir: str, nickname: str, domain: str,
if tagline not in content: if tagline not in content:
tagsFile.seek(0, 0) tagsFile.seek(0, 0)
tagsFile.write(tagline + content) tagsFile.write(tagline + content)
except Exception as e: except OSError as e:
print('WARN: Failed to write entry to tags file ' + print('WARN: Failed to write entry to tags file ' +
tagsFilename + ' ' + str(e)) tagsFilename + ' ' + str(e))
removeOldHashtags(baseDir, 3) removeOldHashtags(baseDir, 3)
@ -1980,8 +1981,12 @@ def _receiveAnnounce(recentPostsCache: {},
postJsonObject, personCache, postJsonObject, personCache,
translate, lookupActor, translate, lookupActor,
themeName) themeName)
with open(postFilename + '.tts', 'w+') as ttsFile: try:
ttsFile.write('\n') with open(postFilename + '.tts', 'w+') as ttsFile:
ttsFile.write('\n')
except OSError:
print('WARN: unable to write recent post ' +
postFilename)
if debug: if debug:
print('DEBUG: Obtaining actor for announce post ' + print('DEBUG: Obtaining actor for announce post ' +
@ -2145,11 +2150,17 @@ def populateReplies(baseDir: str, httpPrefix: str, domain: str,
if numLines > maxReplies: if numLines > maxReplies:
return False return False
if messageId not in open(postRepliesFilename).read(): if messageId not in open(postRepliesFilename).read():
with open(postRepliesFilename, 'a+') as repliesFile: try:
repliesFile.write(messageId + '\n') with open(postRepliesFilename, 'a+') as repliesFile:
repliesFile.write(messageId + '\n')
except OSError:
print('WARN: unable to append ' + postRepliesFilename)
else: else:
with open(postRepliesFilename, 'w+') as repliesFile: try:
repliesFile.write(messageId + '\n') with open(postRepliesFilename, 'w+') as repliesFile:
repliesFile.write(messageId + '\n')
except OSError:
print('WARN: unable to write ' + postRepliesFilename)
return True return True
@ -2322,8 +2333,11 @@ def _dmNotify(baseDir: str, handle: str, url: str) -> None:
return return
dmFile = accountDir + '/.newDM' dmFile = accountDir + '/.newDM'
if not os.path.isfile(dmFile): if not os.path.isfile(dmFile):
with open(dmFile, 'w+') as fp: try:
fp.write(url) with open(dmFile, 'w+') as fp:
fp.write(url)
except OSError:
print('WARN: unable to write ' + dmFile)
def _alreadyLiked(baseDir: str, nickname: str, domain: str, def _alreadyLiked(baseDir: str, nickname: str, domain: str,
@ -2438,17 +2452,16 @@ def _likeNotify(baseDir: str, domain: str, onionDomain: str,
try: try:
with open(prevLikeFile, 'w+') as fp: with open(prevLikeFile, 'w+') as fp:
fp.write(likeStr) fp.write(likeStr)
except BaseException: except OSError:
print('EX: ERROR: unable to save previous like notification ' + print('EX: ERROR: unable to save previous like notification ' +
prevLikeFile) prevLikeFile)
pass
try: try:
with open(likeFile, 'w+') as fp: with open(likeFile, 'w+') as fp:
fp.write(likeStr) fp.write(likeStr)
except BaseException: except OSError:
print('EX: ERROR: unable to write like notification file ' + print('EX: ERROR: unable to write like notification file ' +
likeFile) likeFile)
pass
def _reactionNotify(baseDir: str, domain: str, onionDomain: str, def _reactionNotify(baseDir: str, domain: str, onionDomain: str,
@ -2503,17 +2516,16 @@ def _reactionNotify(baseDir: str, domain: str, onionDomain: str,
try: try:
with open(prevReactionFile, 'w+') as fp: with open(prevReactionFile, 'w+') as fp:
fp.write(reactionStr) fp.write(reactionStr)
except BaseException: except OSError:
print('EX: ERROR: unable to save previous reaction notification ' + print('EX: ERROR: unable to save previous reaction notification ' +
prevReactionFile) prevReactionFile)
pass
try: try:
with open(reactionFile, 'w+') as fp: with open(reactionFile, 'w+') as fp:
fp.write(reactionStr) fp.write(reactionStr)
except BaseException: except OSError:
print('EX: ERROR: unable to write reaction notification file ' + print('EX: ERROR: unable to write reaction notification file ' +
reactionFile) reactionFile)
pass
def _notifyPostArrival(baseDir: str, handle: str, url: str) -> None: def _notifyPostArrival(baseDir: str, handle: str, url: str) -> None:
@ -2531,8 +2543,11 @@ def _notifyPostArrival(baseDir: str, handle: str, url: str) -> None:
existingNotificationMessage = fp.read() existingNotificationMessage = fp.read()
if url in existingNotificationMessage: if url in existingNotificationMessage:
return return
with open(notifyFile, 'w+') as fp: try:
fp.write(url) with open(notifyFile, 'w+') as fp:
fp.write(url)
except OSError:
print('WARN: unable to write ' + notifyFile)
def _replyNotify(baseDir: str, handle: str, url: str) -> None: def _replyNotify(baseDir: str, handle: str, url: str) -> None:
@ -2543,8 +2558,11 @@ def _replyNotify(baseDir: str, handle: str, url: str) -> None:
return return
replyFile = accountDir + '/.newReply' replyFile = accountDir + '/.newReply'
if not os.path.isfile(replyFile): if not os.path.isfile(replyFile):
with open(replyFile, 'w+') as fp: try:
fp.write(url) with open(replyFile, 'w+') as fp:
fp.write(url)
except OSError:
print('WARN: unable to write ' + replyFile)
def _gitPatchNotify(baseDir: str, handle: str, def _gitPatchNotify(baseDir: str, handle: str,
@ -2558,8 +2576,11 @@ def _gitPatchNotify(baseDir: str, handle: str,
patchFile = accountDir + '/.newPatch' patchFile = accountDir + '/.newPatch'
subject = subject.replace('[PATCH]', '').strip() subject = subject.replace('[PATCH]', '').strip()
handle = '@' + fromNickname + '@' + fromDomain handle = '@' + fromNickname + '@' + fromDomain
with open(patchFile, 'w+') as fp: try:
fp.write('git ' + handle + ' ' + subject) with open(patchFile, 'w+') as fp:
fp.write('git ' + handle + ' ' + subject)
except OSError:
print('WARN: unable to write ' + patchFile)
def _groupHandle(baseDir: str, handle: str) -> bool: def _groupHandle(baseDir: str, handle: str) -> bool:
@ -2709,14 +2730,14 @@ def inboxUpdateIndex(boxname: str, baseDir: str, handle: str,
indexFile.write(destinationFilename + '\n' + content) indexFile.write(destinationFilename + '\n' + content)
written = True written = True
return True return True
except Exception as e: except OSError as e:
print('WARN: Failed to write entry to index ' + str(e)) print('WARN: Failed to write entry to index ' + str(e))
else: else:
try: try:
with open(indexFilename, 'w+') as indexFile: with open(indexFilename, 'w+') as indexFile:
indexFile.write(destinationFilename + '\n') indexFile.write(destinationFilename + '\n')
written = True written = True
except Exception as e: except OSError as e:
print('WARN: Failed to write initial entry to index ' + str(e)) print('WARN: Failed to write initial entry to index ' + str(e))
return written return written
@ -2749,8 +2770,11 @@ def _updateLastSeen(baseDir: str, handle: str, actor: str) -> None:
if int(daysSinceEpochFile) == daysSinceEpoch: if int(daysSinceEpochFile) == daysSinceEpoch:
# value hasn't changed, so we can save writing anything to file # value hasn't changed, so we can save writing anything to file
return return
with open(lastSeenFilename, 'w+') as lastSeenFile: try:
lastSeenFile.write(str(daysSinceEpoch)) with open(lastSeenFilename, 'w+') as lastSeenFile:
lastSeenFile.write(str(daysSinceEpoch))
except OSError:
print('WARN: unable to write ' + lastSeenFilename)
def _bounceDM(senderPostId: str, session, httpPrefix: str, def _bounceDM(senderPostId: str, session, httpPrefix: str,
@ -3485,8 +3509,12 @@ def _inboxAfterInitial(recentPostsCache: {}, maxRecentPosts: int,
# This enables you to ignore a threat that's getting boring # This enables you to ignore a threat that's getting boring
if isReplyToMutedPost: if isReplyToMutedPost:
print('MUTE REPLY: ' + destinationFilename) print('MUTE REPLY: ' + destinationFilename)
with open(destinationFilename + '.muted', 'w+') as muteFile: destinationFilenameMuted = destinationFilename + '.muted'
muteFile.write('\n') try:
with open(destinationFilenameMuted, 'w+') as muteFile:
muteFile.write('\n')
except OSError:
print('WARN: unable to write ' + destinationFilenameMuted)
# update the indexes for different timelines # update the indexes for different timelines
for boxname in updateIndexList: for boxname in updateIndexList:
@ -3773,8 +3801,11 @@ def _checkJsonSignature(baseDir: str, queueJson: {}) -> (bool, bool):
alreadyUnknown = True alreadyUnknown = True
if not alreadyUnknown: if not alreadyUnknown:
with open(unknownContextsFile, 'a+') as unknownFile: try:
unknownFile.write(unknownContext + '\n') with open(unknownContextsFile, 'a+') as unknownFile:
unknownFile.write(unknownContext + '\n')
except OSError:
print('WARN: unable to append ' + unknownContextsFile)
else: else:
print('Unrecognized jsonld signature type: ' + jwebsigType) print('Unrecognized jsonld signature type: ' + jwebsigType)
@ -3788,8 +3819,11 @@ def _checkJsonSignature(baseDir: str, queueJson: {}) -> (bool, bool):
alreadyUnknown = True alreadyUnknown = True
if not alreadyUnknown: if not alreadyUnknown:
with open(unknownSignaturesFile, 'a+') as unknownFile: try:
unknownFile.write(jwebsigType + '\n') with open(unknownSignaturesFile, 'a+') as unknownFile:
unknownFile.write(jwebsigType + '\n')
except OSError:
print('WARN: unable to append ' + unknownSignaturesFile)
return hasJsonSignature, jwebsigType return hasJsonSignature, jwebsigType

View File

@ -46,8 +46,11 @@ def manualDenyFollowRequest(session, baseDir: str,
removeFromFollowRequests(baseDir, nickname, domain, denyHandle, debug) removeFromFollowRequests(baseDir, nickname, domain, denyHandle, debug)
# Store rejected follows # Store rejected follows
with open(rejectedFollowsFilename, 'a+') as rejectsFile: try:
rejectsFile.write(denyHandle + '\n') with open(rejectedFollowsFilename, 'a+') as rejectsFile:
rejectsFile.write(denyHandle + '\n')
except OSError:
print('WARN: unable to append ' + rejectedFollowsFilename)
denyNickname = denyHandle.split('@')[0] denyNickname = denyHandle.split('@')[0]
denyDomain = \ denyDomain = \
@ -104,11 +107,17 @@ def _approveFollowerHandle(accountDir: str, approveHandle: str) -> None:
approvedFilename = accountDir + '/approved.txt' approvedFilename = accountDir + '/approved.txt'
if os.path.isfile(approvedFilename): if os.path.isfile(approvedFilename):
if approveHandle not in open(approvedFilename).read(): if approveHandle not in open(approvedFilename).read():
with open(approvedFilename, 'a+') as approvedFile: try:
approvedFile.write(approveHandle + '\n') with open(approvedFilename, 'a+') as approvedFile:
approvedFile.write(approveHandle + '\n')
except OSError:
print('WARN: unable to append ' + approvedFilename)
else: else:
with open(approvedFilename, 'w+') as approvedFile: try:
approvedFile.write(approveHandle + '\n') with open(approvedFilename, 'w+') as approvedFile:
approvedFile.write(approveHandle + '\n')
except OSError:
print('WARN: unable to write ' + approvedFilename)
def manualApproveFollowRequest(session, baseDir: str, def manualApproveFollowRequest(session, baseDir: str,
@ -239,8 +248,11 @@ def manualApproveFollowRequest(session, baseDir: str,
else: else:
print('Manual follow accept: first follower accepted for ' + print('Manual follow accept: first follower accepted for ' +
handle + ' is ' + approveHandleFull) handle + ' is ' + approveHandleFull)
with open(followersFilename, 'w+') as followersFile: try:
followersFile.write(approveHandleFull + '\n') with open(followersFilename, 'w+') as followersFile:
followersFile.write(approveHandleFull + '\n')
except OSError:
print('WARN: unable to write ' + followersFilename)
# only update the follow requests file if the follow is confirmed to be # only update the follow requests file if the follow is confirmed to be
# in followers.txt # in followers.txt

View File

@ -126,9 +126,8 @@ def _spoofMetaData(baseDir: str, nickname: str, domain: str,
try: try:
with open(decoySeedFilename, 'w+') as fp: with open(decoySeedFilename, 'w+') as fp:
fp.write(str(decoySeed)) fp.write(str(decoySeed))
except BaseException: except OSError:
print('EX: unable to write ' + decoySeedFilename) print('EX: unable to write ' + decoySeedFilename)
pass
if os.path.isfile('/usr/bin/exiftool'): if os.path.isfile('/usr/bin/exiftool'):
print('Spoofing metadata in ' + outputFilename + ' using exiftool') print('Spoofing metadata in ' + outputFilename + ' using exiftool')
@ -290,10 +289,9 @@ def _updateEtag(mediaFilename: str) -> None:
try: try:
with open(mediaFilename + '.etag', 'w+') as etagFile: with open(mediaFilename + '.etag', 'w+') as etagFile:
etagFile.write(etag) etagFile.write(etag)
except BaseException: except OSError:
print('EX: _updateEtag unable to write ' + print('EX: _updateEtag unable to write ' +
str(mediaFilename) + '.etag') str(mediaFilename) + '.etag')
pass
def attachMedia(baseDir: str, httpPrefix: str, def attachMedia(baseDir: str, httpPrefix: str,

View File

@ -57,15 +57,21 @@ def _updateFeedsOutboxIndex(baseDir: str, domain: str, postId: str) -> None:
print('WARN: Failed to write entry to feeds posts index ' + print('WARN: Failed to write entry to feeds posts index ' +
indexFilename + ' ' + str(e)) indexFilename + ' ' + str(e))
else: else:
with open(indexFilename, 'w+') as feedsFile: try:
feedsFile.write(postId + '\n') with open(indexFilename, 'w+') as feedsFile:
feedsFile.write(postId + '\n')
except OSError:
print('WARN: unable to write ' + indexFilename)
def _saveArrivedTime(baseDir: str, postFilename: str, arrived: str) -> None: def _saveArrivedTime(baseDir: str, postFilename: str, arrived: str) -> None:
"""Saves the time when an rss post arrived to a file """Saves the time when an rss post arrived to a file
""" """
with open(postFilename + '.arrived', 'w+') as arrivedFile: try:
arrivedFile.write(arrived) with open(postFilename + '.arrived', 'w+') as arrivedFile:
arrivedFile.write(arrived)
except OSError:
print('WARN: unable to write ' + postFilename + '.arrived')
def _removeControlCharacters(content: str) -> str: def _removeControlCharacters(content: str) -> str:
@ -483,8 +489,11 @@ def _createNewsMirror(baseDir: str, domain: str,
for removePostId in removals: for removePostId in removals:
indexContent = \ indexContent = \
indexContent.replace(removePostId + '\n', '') indexContent.replace(removePostId + '\n', '')
with open(mirrorIndexFilename, 'w+') as indexFile: try:
indexFile.write(indexContent) with open(mirrorIndexFilename, 'w+') as indexFile:
indexFile.write(indexContent)
except OSError:
print('WARN: unable to write ' + mirrorIndexFilename)
mirrorArticleDir = mirrorDir + '/' + postIdNumber mirrorArticleDir = mirrorDir + '/' + postIdNumber
if os.path.isdir(mirrorArticleDir): if os.path.isdir(mirrorArticleDir):
@ -509,11 +518,17 @@ def _createNewsMirror(baseDir: str, domain: str,
# append the post Id number to the index file # append the post Id number to the index file
if os.path.isfile(mirrorIndexFilename): if os.path.isfile(mirrorIndexFilename):
with open(mirrorIndexFilename, 'a+') as indexFile: try:
indexFile.write(postIdNumber + '\n') with open(mirrorIndexFilename, 'a+') as indexFile:
indexFile.write(postIdNumber + '\n')
except OSError:
print('WARN: unable to append ' + mirrorIndexFilename)
else: else:
with open(mirrorIndexFilename, 'w+') as indexFile: try:
indexFile.write(postIdNumber + '\n') with open(mirrorIndexFilename, 'w+') as indexFile:
indexFile.write(postIdNumber + '\n')
except OSError:
print('WARN: unable to write ' + mirrorIndexFilename)
return True return True

103
person.py
View File

@ -507,16 +507,22 @@ def _createPersonBase(baseDir: str, nickname: str, domain: str, port: int,
if not os.path.isdir(baseDir + privateKeysSubdir): if not os.path.isdir(baseDir + privateKeysSubdir):
os.mkdir(baseDir + privateKeysSubdir) os.mkdir(baseDir + privateKeysSubdir)
filename = baseDir + privateKeysSubdir + '/' + handle + '.key' filename = baseDir + privateKeysSubdir + '/' + handle + '.key'
with open(filename, 'w+') as text_file: try:
print(privateKeyPem, file=text_file) with open(filename, 'w+') as text_file:
print(privateKeyPem, file=text_file)
except OSError:
print('WARN: unable to save ' + filename)
# save the public key # save the public key
publicKeysSubdir = '/keys/public' publicKeysSubdir = '/keys/public'
if not os.path.isdir(baseDir + publicKeysSubdir): if not os.path.isdir(baseDir + publicKeysSubdir):
os.mkdir(baseDir + publicKeysSubdir) os.mkdir(baseDir + publicKeysSubdir)
filename = baseDir + publicKeysSubdir + '/' + handle + '.pem' filename = baseDir + publicKeysSubdir + '/' + handle + '.pem'
with open(filename, 'w+') as text_file: try:
print(publicKeyPem, file=text_file) with open(filename, 'w+') as text_file:
print(publicKeyPem, file=text_file)
except OSError:
print('WARN: unable to save 2 ' + filename)
if password: if password:
password = removeLineEndings(password) password = removeLineEndings(password)
@ -625,22 +631,31 @@ def createPerson(baseDir: str, nickname: str, domain: str, port: int,
if manualFollowerApproval: if manualFollowerApproval:
followDMsFilename = acctDir(baseDir, nickname, domain) + '/.followDMs' followDMsFilename = acctDir(baseDir, nickname, domain) + '/.followDMs'
with open(followDMsFilename, 'w+') as fFile: try:
fFile.write('\n') with open(followDMsFilename, 'w+') as fFile:
fFile.write('\n')
except OSError:
print('WARN: unable to write ' + followDMsFilename)
# notify when posts are liked # notify when posts are liked
if nickname != 'news': if nickname != 'news':
notifyLikesFilename = \ notifyLikesFilename = \
acctDir(baseDir, nickname, domain) + '/.notifyLikes' acctDir(baseDir, nickname, domain) + '/.notifyLikes'
with open(notifyLikesFilename, 'w+') as nFile: try:
nFile.write('\n') with open(notifyLikesFilename, 'w+') as nFile:
nFile.write('\n')
except OSError:
print('WARN: unable to write ' + notifyLikesFilename)
# notify when posts have emoji reactions # notify when posts have emoji reactions
if nickname != 'news': if nickname != 'news':
notifyReactionsFilename = \ notifyReactionsFilename = \
acctDir(baseDir, nickname, domain) + '/.notifyReactions' acctDir(baseDir, nickname, domain) + '/.notifyReactions'
with open(notifyReactionsFilename, 'w+') as nFile: try:
nFile.write('\n') with open(notifyReactionsFilename, 'w+') as nFile:
nFile.write('\n')
except OSError:
print('WARN: unable to write ' + notifyReactionsFilename)
theme = getConfigParam(baseDir, 'theme') theme = getConfigParam(baseDir, 'theme')
if not theme: if not theme:
@ -1016,10 +1031,14 @@ def reenableAccount(baseDir: str, nickname: str) -> None:
lines = [] lines = []
with open(suspendedFilename, 'r') as f: with open(suspendedFilename, 'r') as f:
lines = f.readlines() lines = f.readlines()
with open(suspendedFilename, 'w+') as suspendedFile: try:
for suspended in lines: with open(suspendedFilename, 'w+') as suspendedFile:
if suspended.strip('\n').strip('\r') != nickname: for suspended in lines:
suspendedFile.write(suspended) if suspended.strip('\n').strip('\r') != nickname:
suspendedFile.write(suspended)
except OSError as e:
print('WARN: unable to save ' + suspendedFilename +
' ' + str(e))
def suspendAccount(baseDir: str, nickname: str, domain: str) -> None: def suspendAccount(baseDir: str, nickname: str, domain: str) -> None:
@ -1061,11 +1080,17 @@ def suspendAccount(baseDir: str, nickname: str, domain: str) -> None:
for suspended in lines: for suspended in lines:
if suspended.strip('\n').strip('\r') == nickname: if suspended.strip('\n').strip('\r') == nickname:
return return
with open(suspendedFilename, 'a+') as suspendedFile: try:
suspendedFile.write(nickname + '\n') with open(suspendedFilename, 'a+') as suspendedFile:
suspendedFile.write(nickname + '\n')
except OSError:
print('WARN: unable to append ' + suspendedFilename)
else: else:
with open(suspendedFilename, 'w+') as suspendedFile: try:
suspendedFile.write(nickname + '\n') with open(suspendedFilename, 'w+') as suspendedFile:
suspendedFile.write(nickname + '\n')
except OSError:
print('WARN: unable to write ' + suspendedFilename)
def canRemovePost(baseDir: str, nickname: str, def canRemovePost(baseDir: str, nickname: str,
@ -1122,10 +1147,13 @@ def _removeTagsForNickname(baseDir: str, nickname: str,
lines = [] lines = []
with open(tagFilename, 'r') as f: with open(tagFilename, 'r') as f:
lines = f.readlines() lines = f.readlines()
with open(tagFilename, 'w+') as tagFile: try:
for tagline in lines: with open(tagFilename, 'w+') as tagFile:
if matchStr not in tagline: for tagline in lines:
tagFile.write(tagline) if matchStr not in tagline:
tagFile.write(tagline)
except OSError:
print('WARN: unable to write ' + tagFilename)
def removeAccount(baseDir: str, nickname: str, def removeAccount(baseDir: str, nickname: str,
@ -1290,8 +1318,11 @@ def isPersonSnoozed(baseDir: str, nickname: str, domain: str,
with open(snoozedFilename, 'r') as snoozedFile: with open(snoozedFilename, 'r') as snoozedFile:
content = snoozedFile.read().replace(replaceStr, '') content = snoozedFile.read().replace(replaceStr, '')
if content: if content:
with open(snoozedFilename, 'w+') as writeSnoozedFile: try:
writeSnoozedFile.write(content) with open(snoozedFilename, 'w+') as writeSnoozedFile:
writeSnoozedFile.write(content)
except OSError:
print('WARN: unable to write ' + snoozedFilename)
if snoozeActor + ' ' in open(snoozedFilename).read(): if snoozeActor + ' ' in open(snoozedFilename).read():
return True return True
@ -1310,9 +1341,12 @@ def personSnooze(baseDir: str, nickname: str, domain: str,
if os.path.isfile(snoozedFilename): if os.path.isfile(snoozedFilename):
if snoozeActor + ' ' in open(snoozedFilename).read(): if snoozeActor + ' ' in open(snoozedFilename).read():
return return
with open(snoozedFilename, 'a+') as snoozedFile: try:
snoozedFile.write(snoozeActor + ' ' + with open(snoozedFilename, 'a+') as snoozedFile:
str(int(time.time())) + '\n') snoozedFile.write(snoozeActor + ' ' +
str(int(time.time())) + '\n')
except OSError:
print('WARN: unable to append ' + snoozedFilename)
def personUnsnooze(baseDir: str, nickname: str, domain: str, def personUnsnooze(baseDir: str, nickname: str, domain: str,
@ -1339,8 +1373,11 @@ def personUnsnooze(baseDir: str, nickname: str, domain: str,
with open(snoozedFilename, 'r') as snoozedFile: with open(snoozedFilename, 'r') as snoozedFile:
content = snoozedFile.read().replace(replaceStr, '') content = snoozedFile.read().replace(replaceStr, '')
if content: if content:
with open(snoozedFilename, 'w+') as writeSnoozedFile: try:
writeSnoozedFile.write(content) with open(snoozedFilename, 'w+') as writeSnoozedFile:
writeSnoozedFile.write(content)
except OSError:
print('WARN: unable to write ' + snoozedFilename)
def setPersonNotes(baseDir: str, nickname: str, domain: str, def setPersonNotes(baseDir: str, nickname: str, domain: str,
@ -1355,8 +1392,12 @@ def setPersonNotes(baseDir: str, nickname: str, domain: str,
if not os.path.isdir(notesDir): if not os.path.isdir(notesDir):
os.mkdir(notesDir) os.mkdir(notesDir)
notesFilename = notesDir + '/' + handle + '.txt' notesFilename = notesDir + '/' + handle + '.txt'
with open(notesFilename, 'w+') as notesFile: try:
notesFile.write(notes) with open(notesFilename, 'w+') as notesFile:
notesFile.write(notes)
except OSError:
print('WARN: unable to write ' + notesFilename)
return False
return True return True

View File

@ -41,17 +41,29 @@ def setPetName(baseDir: str, nickname: str, domain: str,
else: else:
newPetnamesStr += entry newPetnamesStr += entry
# save the updated petnames file # save the updated petnames file
with open(petnamesFilename, 'w+') as petnamesFile: try:
petnamesFile.write(newPetnamesStr) with open(petnamesFilename, 'w+') as petnamesFile:
petnamesFile.write(newPetnamesStr)
except OSError:
print('WARN: unable to save ' + petnamesFilename)
return False
return True return True
# entry does not exist in the petnames file # entry does not exist in the petnames file
with open(petnamesFilename, 'a+') as petnamesFile: try:
petnamesFile.write(entry) with open(petnamesFilename, 'a+') as petnamesFile:
petnamesFile.write(entry)
except OSError:
print('WARN: unable to append ' + petnamesFilename)
return False
return True return True
# first entry # first entry
with open(petnamesFilename, 'w+') as petnamesFile: try:
petnamesFile.write(entry) with open(petnamesFilename, 'w+') as petnamesFile:
petnamesFile.write(entry)
except OSError:
print('WARN: unable to write ' + petnamesFilename)
return False
return True return True

View File

@ -1573,8 +1573,11 @@ def pinPost(baseDir: str, nickname: str, domain: str,
""" """
accountDir = acctDir(baseDir, nickname, domain) accountDir = acctDir(baseDir, nickname, domain)
pinnedFilename = accountDir + '/pinToProfile.txt' pinnedFilename = accountDir + '/pinToProfile.txt'
with open(pinnedFilename, 'w+') as pinFile: try:
pinFile.write(pinnedContent) with open(pinnedFilename, 'w+') as pinFile:
pinFile.write(pinnedContent)
except OSError:
print('WARN: unable to write ' + pinnedFilename)
def undoPinnedPost(baseDir: str, nickname: str, domain: str) -> None: def undoPinnedPost(baseDir: str, nickname: str, domain: str) -> None: