Exceptions when file handling

merge-requests/30/head
Bob Mottram 2021-11-25 17:01:01 +00:00
parent 2837966da2
commit 8c4c1782ad
1 changed files with 53 additions and 23 deletions

View File

@ -49,8 +49,12 @@ def addGlobalBlock(baseDir: str,
if blockHandle in open(blockingFilename).read(): if blockHandle in open(blockingFilename).read():
return False return False
# block an account handle or domain # block an account handle or domain
with open(blockingFilename, 'a+') as blockFile: try:
blockFile.write(blockHandle + '\n') with open(blockingFilename, 'a+') as blockFile:
blockFile.write(blockHandle + '\n')
except OSError:
print('WARN: unable to save blocked handle ' + blockHandle)
return False
else: else:
blockHashtag = blockNickname blockHashtag = blockNickname
# is the hashtag already blocked? # is the hashtag already blocked?
@ -58,8 +62,12 @@ def addGlobalBlock(baseDir: str,
if blockHashtag + '\n' in open(blockingFilename).read(): if blockHashtag + '\n' in open(blockingFilename).read():
return False return False
# block a hashtag # block a hashtag
with open(blockingFilename, 'a+') as blockFile: try:
blockFile.write(blockHashtag + '\n') with open(blockingFilename, 'a+') as blockFile:
blockFile.write(blockHashtag + '\n')
except OSError:
print('WARN: unable to save blocked hashtag ' + blockHashtag)
return False
return True return True
@ -83,25 +91,47 @@ def addBlock(baseDir: str, nickname: str, domain: str,
if os.path.isfile(followingFilename): if os.path.isfile(followingFilename):
if blockHandle + '\n' in open(followingFilename).read(): if blockHandle + '\n' in open(followingFilename).read():
followingStr = '' followingStr = ''
with open(followingFilename, 'r') as followingFile: try:
followingStr = followingFile.read() with open(followingFilename, 'r') as followingFile:
followingStr = followingStr.replace(blockHandle + '\n', '') followingStr = followingFile.read()
with open(followingFilename, 'w+') as followingFile: followingStr = followingStr.replace(blockHandle + '\n', '')
followingFile.write(followingStr) except OSError:
print('WARN: Unable to read following ' + followingFilename)
return False
try:
with open(followingFilename, 'w+') as followingFile:
followingFile.write(followingStr)
except OSError:
print('WARN: Unable to write following ' + followingStr)
return False
# if they are a follower then remove them # if they are a follower then remove them
followersFilename = acctDir(baseDir, nickname, domain) + '/followers.txt' followersFilename = acctDir(baseDir, nickname, domain) + '/followers.txt'
if os.path.isfile(followersFilename): if os.path.isfile(followersFilename):
if blockHandle + '\n' in open(followersFilename).read(): if blockHandle + '\n' in open(followersFilename).read():
followersStr = '' followersStr = ''
with open(followersFilename, 'r') as followersFile: try:
followersStr = followersFile.read() with open(followersFilename, 'r') as followersFile:
followersStr = followersStr.replace(blockHandle + '\n', '') followersStr = followersFile.read()
with open(followersFilename, 'w+') as followersFile: followersStr = followersStr.replace(blockHandle + '\n', '')
followersFile.write(followersStr) except OSError:
print('WARN: Unable to read followers ' + followersFilename)
return False
with open(blockingFilename, 'a+') as blockFile: try:
blockFile.write(blockHandle + '\n') with open(followersFilename, 'w+') as followersFile:
followersFile.write(followersStr)
except OSError:
print('WARN: Unable to write followers ' + followersStr)
return False
try:
with open(blockingFilename, 'a+') as blockFile:
blockFile.write(blockHandle + '\n')
except OSError:
print('WARN: unable to append block handle ' + blockHandle)
return False
return True return True
@ -516,7 +546,7 @@ def mutePost(baseDir: str, nickname: str, domain: str, port: int,
try: try:
os.remove(cachedPostFilename) os.remove(cachedPostFilename)
print('MUTE: cached post removed ' + cachedPostFilename) print('MUTE: cached post removed ' + cachedPostFilename)
except BaseException: except OSError:
print('EX: MUTE cached post not removed ' + print('EX: MUTE cached post not removed ' +
cachedPostFilename) cachedPostFilename)
pass pass
@ -555,7 +585,7 @@ def mutePost(baseDir: str, nickname: str, domain: str, port: int,
os.remove(cachedPostFilename) os.remove(cachedPostFilename)
print('MUTE: cached referenced post removed ' + print('MUTE: cached referenced post removed ' +
cachedPostFilename) cachedPostFilename)
except BaseException: except OSError:
print('EX: ' + print('EX: ' +
'MUTE cached referenced post not removed ' + 'MUTE cached referenced post not removed ' +
cachedPostFilename) cachedPostFilename)
@ -587,7 +617,7 @@ def unmutePost(baseDir: str, nickname: str, domain: str, port: int,
if os.path.isfile(muteFilename): if os.path.isfile(muteFilename):
try: try:
os.remove(muteFilename) os.remove(muteFilename)
except BaseException: except OSError:
if debug: if debug:
print('EX: unmutePost mute filename not deleted ' + print('EX: unmutePost mute filename not deleted ' +
str(muteFilename)) str(muteFilename))
@ -638,7 +668,7 @@ def unmutePost(baseDir: str, nickname: str, domain: str, port: int,
if os.path.isfile(cachedPostFilename): if os.path.isfile(cachedPostFilename):
try: try:
os.remove(cachedPostFilename) os.remove(cachedPostFilename)
except BaseException: except OSError:
if debug: if debug:
print('EX: unmutePost cached post not deleted ' + print('EX: unmutePost cached post not deleted ' +
str(cachedPostFilename)) str(cachedPostFilename))
@ -671,7 +701,7 @@ def unmutePost(baseDir: str, nickname: str, domain: str, port: int,
os.remove(cachedPostFilename) os.remove(cachedPostFilename)
print('MUTE: cached referenced post removed ' + print('MUTE: cached referenced post removed ' +
cachedPostFilename) cachedPostFilename)
except BaseException: except OSError:
if debug: if debug:
print('EX: ' + print('EX: ' +
'unmutePost cached ref post not removed ' + 'unmutePost cached ref post not removed ' +
@ -818,7 +848,7 @@ def setBrochMode(baseDir: str, domainFull: str, enabled: bool) -> None:
if os.path.isfile(allowFilename): if os.path.isfile(allowFilename):
try: try:
os.remove(allowFilename) os.remove(allowFilename)
except BaseException: except OSError:
print('EX: setBrochMode allow file not deleted ' + print('EX: setBrochMode allow file not deleted ' +
str(allowFilename)) str(allowFilename))
pass pass
@ -885,7 +915,7 @@ def brochModeLapses(baseDir: str, lapseDays: int) -> bool:
try: try:
os.remove(allowFilename) os.remove(allowFilename)
removed = True removed = True
except BaseException: except OSError:
print('EX: brochModeLapses allow file not deleted ' + print('EX: brochModeLapses allow file not deleted ' +
str(allowFilename)) str(allowFilename))
pass pass