Exception handling

merge-requests/30/head
Bob Mottram 2021-11-26 14:35:26 +00:00
parent cb0675cf5e
commit 8a623417bf
5 changed files with 514 additions and 253 deletions

211
daemon.py
View File

@ -4746,7 +4746,7 @@ class PubServer(BaseHTTPRequestHandler):
with open(cityFilename, 'w+') as fp: with open(cityFilename, 'w+') as fp:
fp.write(fields['cityDropdown']) fp.write(fields['cityDropdown'])
except OSError: except OSError:
print('EX: unable to write ' + cityFilename) print('EX: unable to write city ' + cityFilename)
# change displayed name # change displayed name
if fields.get('displayNickname'): if fields.get('displayNickname'):
@ -5390,14 +5390,21 @@ class PubServer(BaseHTTPRequestHandler):
if ',' in fields['moderators']: if ',' in fields['moderators']:
# if the list was given as comma separated # if the list was given as comma separated
mods = fields['moderators'].split(',') mods = fields['moderators'].split(',')
with open(moderatorsFile, 'w+') as modFile: try:
with open(moderatorsFile,
'w+') as modFile:
for modNick in mods: for modNick in mods:
modNick = modNick.strip() modNick = modNick.strip()
modDir = baseDir + \ modDir = baseDir + \
'/accounts/' + modNick + \ '/accounts/' + modNick + \
'@' + domain '@' + domain
if os.path.isdir(modDir): if os.path.isdir(modDir):
modFile.write(modNick + '\n') modFile.write(modNick +
'\n')
except OSError:
print('EX: ' +
'unable to write moderators ' +
moderatorsFile)
for modNick in mods: for modNick in mods:
modNick = modNick.strip() modNick = modNick.strip()
@ -5411,7 +5418,9 @@ class PubServer(BaseHTTPRequestHandler):
else: else:
# nicknames on separate lines # nicknames on separate lines
mods = fields['moderators'].split('\n') mods = fields['moderators'].split('\n')
with open(moderatorsFile, 'w+') as modFile: try:
with open(moderatorsFile,
'w+') as modFile:
for modNick in mods: for modNick in mods:
modNick = modNick.strip() modNick = modNick.strip()
modDir = \ modDir = \
@ -5419,7 +5428,12 @@ class PubServer(BaseHTTPRequestHandler):
'/accounts/' + modNick + \ '/accounts/' + modNick + \
'@' + domain '@' + domain
if os.path.isdir(modDir): if os.path.isdir(modDir):
modFile.write(modNick + '\n') modFile.write(modNick +
'\n')
except OSError:
print('EX: ' +
'unable to write moderators 2 ' +
moderatorsFile)
for modNick in mods: for modNick in mods:
modNick = modNick.strip() modNick = modNick.strip()
@ -5444,6 +5458,7 @@ class PubServer(BaseHTTPRequestHandler):
if ',' in fields['editors']: if ',' in fields['editors']:
# if the list was given as comma separated # if the list was given as comma separated
eds = fields['editors'].split(',') eds = fields['editors'].split(',')
try:
with open(editorsFile, 'w+') as edFile: with open(editorsFile, 'w+') as edFile:
for edNick in eds: for edNick in eds:
edNick = edNick.strip() edNick = edNick.strip()
@ -5452,6 +5467,9 @@ class PubServer(BaseHTTPRequestHandler):
'@' + domain '@' + domain
if os.path.isdir(edDir): if os.path.isdir(edDir):
edFile.write(edNick + '\n') edFile.write(edNick + '\n')
except OSError as e:
print('EX: unable to write editors ' +
editorsFile + ' ' + str(e))
for edNick in eds: for edNick in eds:
edNick = edNick.strip() edNick = edNick.strip()
@ -5465,7 +5483,9 @@ class PubServer(BaseHTTPRequestHandler):
else: else:
# nicknames on separate lines # nicknames on separate lines
eds = fields['editors'].split('\n') eds = fields['editors'].split('\n')
with open(editorsFile, 'w+') as edFile: try:
with open(editorsFile,
'w+') as edFile:
for edNick in eds: for edNick in eds:
edNick = edNick.strip() edNick = edNick.strip()
edDir = \ edDir = \
@ -5474,6 +5494,9 @@ class PubServer(BaseHTTPRequestHandler):
'@' + domain '@' + domain
if os.path.isdir(edDir): if os.path.isdir(edDir):
edFile.write(edNick + '\n') edFile.write(edNick + '\n')
except OSError as e:
print('EX: unable to write editors ' +
editorsFile + ' ' + str(e))
for edNick in eds: for edNick in eds:
edNick = edNick.strip() edNick = edNick.strip()
@ -5498,7 +5521,9 @@ class PubServer(BaseHTTPRequestHandler):
if ',' in fields['counselors']: if ',' in fields['counselors']:
# if the list was given as comma separated # if the list was given as comma separated
eds = fields['counselors'].split(',') eds = fields['counselors'].split(',')
with open(counselorsFile, 'w+') as edFile: try:
with open(counselorsFile,
'w+') as edFile:
for edNick in eds: for edNick in eds:
edNick = edNick.strip() edNick = edNick.strip()
edDir = baseDir + \ edDir = baseDir + \
@ -5506,6 +5531,10 @@ class PubServer(BaseHTTPRequestHandler):
'@' + domain '@' + domain
if os.path.isdir(edDir): if os.path.isdir(edDir):
edFile.write(edNick + '\n') edFile.write(edNick + '\n')
except OSError as e:
print('EX: ' +
'unable to write counselors ' +
counselorsFile + ' ' + str(e))
for edNick in eds: for edNick in eds:
edNick = edNick.strip() edNick = edNick.strip()
@ -5519,7 +5548,9 @@ class PubServer(BaseHTTPRequestHandler):
else: else:
# nicknames on separate lines # nicknames on separate lines
eds = fields['counselors'].split('\n') eds = fields['counselors'].split('\n')
with open(counselorsFile, 'w+') as edFile: try:
with open(counselorsFile,
'w+') as edFile:
for edNick in eds: for edNick in eds:
edNick = edNick.strip() edNick = edNick.strip()
edDir = \ edDir = \
@ -5528,6 +5559,10 @@ class PubServer(BaseHTTPRequestHandler):
'@' + domain '@' + domain
if os.path.isdir(edDir): if os.path.isdir(edDir):
edFile.write(edNick + '\n') edFile.write(edNick + '\n')
except OSError as e:
print('EX: ' +
'unable to write counselors ' +
counselorsFile + ' ' + str(e))
for edNick in eds: for edNick in eds:
edNick = edNick.strip() edNick = edNick.strip()
@ -5552,6 +5587,7 @@ class PubServer(BaseHTTPRequestHandler):
if ',' in fields['artists']: if ',' in fields['artists']:
# if the list was given as comma separated # if the list was given as comma separated
eds = fields['artists'].split(',') eds = fields['artists'].split(',')
try:
with open(artistsFile, 'w+') as edFile: with open(artistsFile, 'w+') as edFile:
for edNick in eds: for edNick in eds:
edNick = edNick.strip() edNick = edNick.strip()
@ -5560,6 +5596,9 @@ class PubServer(BaseHTTPRequestHandler):
'@' + domain '@' + domain
if os.path.isdir(edDir): if os.path.isdir(edDir):
edFile.write(edNick + '\n') edFile.write(edNick + '\n')
except OSError as e:
print('EX: unable to write artists ' +
artistsFile + ' ' + str(e))
for edNick in eds: for edNick in eds:
edNick = edNick.strip() edNick = edNick.strip()
@ -5573,6 +5612,7 @@ class PubServer(BaseHTTPRequestHandler):
else: else:
# nicknames on separate lines # nicknames on separate lines
eds = fields['artists'].split('\n') eds = fields['artists'].split('\n')
try:
with open(artistsFile, 'w+') as edFile: with open(artistsFile, 'w+') as edFile:
for edNick in eds: for edNick in eds:
edNick = edNick.strip() edNick = edNick.strip()
@ -5582,6 +5622,9 @@ class PubServer(BaseHTTPRequestHandler):
'@' + domain '@' + domain
if os.path.isdir(edDir): if os.path.isdir(edDir):
edFile.write(edNick + '\n') edFile.write(edNick + '\n')
except OSError as e:
print('EX: unable to write artists ' +
artistsFile + ' ' + str(e))
for edNick in eds: for edNick in eds:
edNick = edNick.strip() edNick = edNick.strip()
@ -5681,16 +5724,25 @@ class PubServer(BaseHTTPRequestHandler):
if onFinalWelcomeScreen: if onFinalWelcomeScreen:
# initial default setting created via # initial default setting created via
# the welcome screen # the welcome screen
try:
with open(followDMsFilename, 'w+') as fFile: with open(followDMsFilename, 'w+') as fFile:
fFile.write('\n') fFile.write('\n')
except OSError:
print('EX: unable to write follow DMs ' +
followDMsFilename)
actorChanged = True actorChanged = True
else: else:
followDMsActive = False followDMsActive = False
if fields.get('followDMs'): if fields.get('followDMs'):
if fields['followDMs'] == 'on': if fields['followDMs'] == 'on':
followDMsActive = True followDMsActive = True
with open(followDMsFilename, 'w+') as fFile: try:
with open(followDMsFilename,
'w+') as fFile:
fFile.write('\n') fFile.write('\n')
except OSError:
print('EX: unable to write follow DMs 2 ' +
followDMsFilename)
if not followDMsActive: if not followDMsActive:
if os.path.isfile(followDMsFilename): if os.path.isfile(followDMsFilename):
try: try:
@ -5708,9 +5760,13 @@ class PubServer(BaseHTTPRequestHandler):
if fields.get('removeTwitter'): if fields.get('removeTwitter'):
if fields['removeTwitter'] == 'on': if fields['removeTwitter'] == 'on':
removeTwitterActive = True removeTwitterActive = True
try:
with open(removeTwitterFilename, with open(removeTwitterFilename,
'w+') as rFile: 'w+') as rFile:
rFile.write('\n') rFile.write('\n')
except OSError:
print('EX: unable to write remove twitter ' +
removeTwitterFilename)
if not removeTwitterActive: if not removeTwitterActive:
if os.path.isfile(removeTwitterFilename): if os.path.isfile(removeTwitterFilename):
try: try:
@ -5731,8 +5787,12 @@ class PubServer(BaseHTTPRequestHandler):
if fields.get('hideLikeButton'): if fields.get('hideLikeButton'):
if fields['hideLikeButton'] == 'on': if fields['hideLikeButton'] == 'on':
hideLikeButtonActive = True hideLikeButtonActive = True
try:
with open(hideLikeButtonFile, 'w+') as rFile: with open(hideLikeButtonFile, 'w+') as rFile:
rFile.write('\n') rFile.write('\n')
except OSError:
print('EX: unable to write hide like ' +
hideLikeButtonFile)
# remove notify likes selection # remove notify likes selection
if os.path.isfile(notifyLikesFilename): if os.path.isfile(notifyLikesFilename):
try: try:
@ -5761,8 +5821,13 @@ class PubServer(BaseHTTPRequestHandler):
if fields.get('hideReactionButton'): if fields.get('hideReactionButton'):
if fields['hideReactionButton'] == 'on': if fields['hideReactionButton'] == 'on':
hideReactionButtonActive = True hideReactionButtonActive = True
with open(hideReactionButtonFile, 'w+') as rFile: try:
with open(hideReactionButtonFile,
'w+') as rFile:
rFile.write('\n') rFile.write('\n')
except OSError:
print('EX: unable to write hide reaction ' +
hideReactionButtonFile)
# remove notify Reaction selection # remove notify Reaction selection
if os.path.isfile(notifyReactionsFilename): if os.path.isfile(notifyReactionsFilename):
try: try:
@ -5783,8 +5848,12 @@ class PubServer(BaseHTTPRequestHandler):
# notify about new Likes # notify about new Likes
if onFinalWelcomeScreen: if onFinalWelcomeScreen:
# default setting from welcome screen # default setting from welcome screen
try:
with open(notifyLikesFilename, 'w+') as rFile: with open(notifyLikesFilename, 'w+') as rFile:
rFile.write('\n') rFile.write('\n')
except OSError:
print('EX: unable to write notify likes ' +
notifyLikesFilename)
actorChanged = True actorChanged = True
else: else:
notifyLikesActive = False notifyLikesActive = False
@ -5792,8 +5861,13 @@ class PubServer(BaseHTTPRequestHandler):
if fields['notifyLikes'] == 'on' and \ if fields['notifyLikes'] == 'on' and \
not hideLikeButtonActive: not hideLikeButtonActive:
notifyLikesActive = True notifyLikesActive = True
with open(notifyLikesFilename, 'w+') as rFile: try:
with open(notifyLikesFilename,
'w+') as rFile:
rFile.write('\n') rFile.write('\n')
except OSError:
print('EX: unable to write notify likes ' +
notifyLikesFilename)
if not notifyLikesActive: if not notifyLikesActive:
if os.path.isfile(notifyLikesFilename): if os.path.isfile(notifyLikesFilename):
try: try:
@ -5808,8 +5882,12 @@ class PubServer(BaseHTTPRequestHandler):
'/.notifyReactions' '/.notifyReactions'
if onFinalWelcomeScreen: if onFinalWelcomeScreen:
# default setting from welcome screen # default setting from welcome screen
try:
with open(notifyReactionsFilename, 'w+') as rFile: with open(notifyReactionsFilename, 'w+') as rFile:
rFile.write('\n') rFile.write('\n')
except OSError:
print('EX: unable to write notify reactions ' +
notifyReactionsFilename)
actorChanged = True actorChanged = True
else: else:
notifyReactionsActive = False notifyReactionsActive = False
@ -5817,9 +5895,14 @@ class PubServer(BaseHTTPRequestHandler):
if fields['notifyReactions'] == 'on' and \ if fields['notifyReactions'] == 'on' and \
not hideReactionButtonActive: not hideReactionButtonActive:
notifyReactionsActive = True notifyReactionsActive = True
try:
with open(notifyReactionsFilename, with open(notifyReactionsFilename,
'w+') as rFile: 'w+') as rFile:
rFile.write('\n') rFile.write('\n')
except OSError:
print('EX: unable to write ' +
'notify reactions ' +
notifyReactionsFilename)
if not notifyReactionsActive: if not notifyReactionsActive:
if os.path.isfile(notifyReactionsFilename): if os.path.isfile(notifyReactionsFilename):
try: try:
@ -5882,8 +5965,12 @@ class PubServer(BaseHTTPRequestHandler):
acctDir(baseDir, nickname, domain) + \ acctDir(baseDir, nickname, domain) + \
'/filters.txt' '/filters.txt'
if fields.get('filteredWords'): if fields.get('filteredWords'):
try:
with open(filterFilename, 'w+') as filterfile: with open(filterFilename, 'w+') as filterfile:
filterfile.write(fields['filteredWords']) filterfile.write(fields['filteredWords'])
except OSError:
print('EX: unable to write filter ' +
filterFilename)
else: else:
if os.path.isfile(filterFilename): if os.path.isfile(filterFilename):
try: try:
@ -5898,8 +5985,12 @@ class PubServer(BaseHTTPRequestHandler):
acctDir(baseDir, nickname, domain) + \ acctDir(baseDir, nickname, domain) + \
'/replacewords.txt' '/replacewords.txt'
if fields.get('switchWords'): if fields.get('switchWords'):
try:
with open(switchFilename, 'w+') as switchfile: with open(switchFilename, 'w+') as switchfile:
switchfile.write(fields['switchWords']) switchfile.write(fields['switchWords'])
except OSError:
print('EX: unable to write switches ' +
switchFilename)
else: else:
if os.path.isfile(switchFilename): if os.path.isfile(switchFilename):
try: try:
@ -5914,8 +6005,12 @@ class PubServer(BaseHTTPRequestHandler):
acctDir(baseDir, nickname, domain) + \ acctDir(baseDir, nickname, domain) + \
'/autotags.txt' '/autotags.txt'
if fields.get('autoTags'): if fields.get('autoTags'):
try:
with open(autoTagsFilename, 'w+') as autoTagsFile: with open(autoTagsFilename, 'w+') as autoTagsFile:
autoTagsFile.write(fields['autoTags']) autoTagsFile.write(fields['autoTags'])
except OSError:
print('EX: unable to write auto tags ' +
autoTagsFilename)
else: else:
if os.path.isfile(autoTagsFilename): if os.path.isfile(autoTagsFilename):
try: try:
@ -5930,8 +6025,12 @@ class PubServer(BaseHTTPRequestHandler):
acctDir(baseDir, nickname, domain) + \ acctDir(baseDir, nickname, domain) + \
'/autocw.txt' '/autocw.txt'
if fields.get('autoCW'): if fields.get('autoCW'):
try:
with open(autoCWFilename, 'w+') as autoCWFile: with open(autoCWFilename, 'w+') as autoCWFile:
autoCWFile.write(fields['autoCW']) autoCWFile.write(fields['autoCW'])
except OSError:
print('EX: unable to write auto CW ' +
autoCWFilename)
else: else:
if os.path.isfile(autoCWFilename): if os.path.isfile(autoCWFilename):
try: try:
@ -5946,8 +6045,12 @@ class PubServer(BaseHTTPRequestHandler):
acctDir(baseDir, nickname, domain) + \ acctDir(baseDir, nickname, domain) + \
'/blocking.txt' '/blocking.txt'
if fields.get('blocked'): if fields.get('blocked'):
try:
with open(blockedFilename, 'w+') as blockedfile: with open(blockedFilename, 'w+') as blockedfile:
blockedfile.write(fields['blocked']) blockedfile.write(fields['blocked'])
except OSError:
print('EX: unable to write blocked accounts ' +
blockedFilename)
else: else:
if os.path.isfile(blockedFilename): if os.path.isfile(blockedFilename):
try: try:
@ -5964,8 +6067,13 @@ class PubServer(BaseHTTPRequestHandler):
acctDir(baseDir, nickname, domain) + \ acctDir(baseDir, nickname, domain) + \
'/dmAllowedinstances.txt' '/dmAllowedinstances.txt'
if fields.get('dmAllowedInstances'): if fields.get('dmAllowedInstances'):
with open(dmAllowedInstancesFilename, 'w+') as aFile: try:
with open(dmAllowedInstancesFilename,
'w+') as aFile:
aFile.write(fields['dmAllowedInstances']) aFile.write(fields['dmAllowedInstances'])
except OSError:
print('EX: unable to write allowed DM instances ' +
dmAllowedInstancesFilename)
else: else:
if os.path.isfile(dmAllowedInstancesFilename): if os.path.isfile(dmAllowedInstancesFilename):
try: try:
@ -5981,8 +6089,12 @@ class PubServer(BaseHTTPRequestHandler):
acctDir(baseDir, nickname, domain) + \ acctDir(baseDir, nickname, domain) + \
'/allowedinstances.txt' '/allowedinstances.txt'
if fields.get('allowedInstances'): if fields.get('allowedInstances'):
try:
with open(allowedInstancesFilename, 'w+') as aFile: with open(allowedInstancesFilename, 'w+') as aFile:
aFile.write(fields['allowedInstances']) aFile.write(fields['allowedInstances'])
except OSError:
print('EX: unable to write allowed instances ' +
allowedInstancesFilename)
else: else:
if os.path.isfile(allowedInstancesFilename): if os.path.isfile(allowedInstancesFilename):
try: try:
@ -6036,8 +6148,13 @@ class PubServer(BaseHTTPRequestHandler):
baseDir + '/accounts/peertube.txt' baseDir + '/accounts/peertube.txt'
if fields.get('ptInstances'): if fields.get('ptInstances'):
self.server.peertubeInstances.clear() self.server.peertubeInstances.clear()
with open(peertubeInstancesFile, 'w+') as aFile: try:
with open(peertubeInstancesFile,
'w+') as aFile:
aFile.write(fields['ptInstances']) aFile.write(fields['ptInstances'])
except OSError:
print('EX: unable to write peertube ' +
peertubeInstancesFile)
ptInstancesList = \ ptInstancesList = \
fields['ptInstances'].split('\n') fields['ptInstances'].split('\n')
if ptInstancesList: if ptInstancesList:
@ -6063,8 +6180,12 @@ class PubServer(BaseHTTPRequestHandler):
acctDir(baseDir, nickname, domain) + \ acctDir(baseDir, nickname, domain) + \
'/gitprojects.txt' '/gitprojects.txt'
if fields.get('gitProjects'): if fields.get('gitProjects'):
try:
with open(gitProjectsFilename, 'w+') as aFile: with open(gitProjectsFilename, 'w+') as aFile:
aFile.write(fields['gitProjects'].lower()) aFile.write(fields['gitProjects'].lower())
except OSError:
print('EX: unable to write git ' +
gitProjectsFilename)
else: else:
if os.path.isfile(gitProjectsFilename): if os.path.isfile(gitProjectsFilename):
try: try:
@ -6314,8 +6435,13 @@ class PubServer(BaseHTTPRequestHandler):
return return
else: else:
if os.path.isfile(faviconFilename): if os.path.isfile(faviconFilename):
favBinary = None
try:
with open(faviconFilename, 'rb') as favFile: with open(faviconFilename, 'rb') as favFile:
favBinary = favFile.read() favBinary = favFile.read()
except OSError:
print('EX: unable to read favicon ' + faviconFilename)
if favBinary:
self._set_headers_etag(faviconFilename, self._set_headers_etag(faviconFilename,
favType, favType,
favBinary, None, favBinary, None,
@ -6360,8 +6486,13 @@ class PubServer(BaseHTTPRequestHandler):
filename = path.split('/exports/', 1)[1] filename = path.split('/exports/', 1)[1]
filename = baseDir + '/exports/' + filename filename = baseDir + '/exports/' + filename
if os.path.isfile(filename): if os.path.isfile(filename):
exportBinary = None
try:
with open(filename, 'rb') as fp: with open(filename, 'rb') as fp:
exportBinary = fp.read() exportBinary = fp.read()
except OSError:
print('EX: unable to read theme export ' + filename)
if exportBinary:
exportType = 'application/zip' exportType = 'application/zip'
self._set_headers_etag(filename, exportType, self._set_headers_etag(filename, exportType,
exportBinary, None, exportBinary, None,
@ -6409,8 +6540,13 @@ class PubServer(BaseHTTPRequestHandler):
return return
else: else:
if os.path.isfile(fontFilename): if os.path.isfile(fontFilename):
fontBinary = None
try:
with open(fontFilename, 'rb') as fontFile: with open(fontFilename, 'rb') as fontFile:
fontBinary = fontFile.read() fontBinary = fontFile.read()
except OSError:
print('EX: unable to load font ' + fontFilename)
if fontBinary:
self._set_headers_etag(fontFilename, self._set_headers_etag(fontFilename,
fontType, fontType,
fontBinary, None, fontBinary, None,
@ -6833,8 +6969,13 @@ class PubServer(BaseHTTPRequestHandler):
lastModifiedTimeStr = \ lastModifiedTimeStr = \
lastModifiedTime.strftime('%a, %d %b %Y %H:%M:%S GMT') lastModifiedTime.strftime('%a, %d %b %Y %H:%M:%S GMT')
mediaBinary = None
try:
with open(mediaFilename, 'rb') as avFile: with open(mediaFilename, 'rb') as avFile:
mediaBinary = avFile.read() mediaBinary = avFile.read()
except OSError:
print('EX: unable to read media binary ' + mediaFilename)
if mediaBinary:
self._set_headers_etag(mediaFilename, mediaFileType, self._set_headers_etag(mediaFilename, mediaFileType,
mediaBinary, None, mediaBinary, None,
None, True, None, True,
@ -6866,8 +7007,11 @@ class PubServer(BaseHTTPRequestHandler):
ontologyFileType = 'application/ld+json' ontologyFileType = 'application/ld+json'
if os.path.isfile(ontologyFilename): if os.path.isfile(ontologyFilename):
ontologyFile = None ontologyFile = None
try:
with open(ontologyFilename, 'r') as fp: with open(ontologyFilename, 'r') as fp:
ontologyFile = fp.read() ontologyFile = fp.read()
except OSError:
print('EX: unable to read ontology ' + ontologyFilename)
if ontologyFile: if ontologyFile:
ontologyFile = \ ontologyFile = \
ontologyFile.replace('static.datafoodconsortium.org', ontologyFile.replace('static.datafoodconsortium.org',
@ -6905,8 +7049,13 @@ class PubServer(BaseHTTPRequestHandler):
return return
mediaImageType = getImageMimeType(emojiFilename) mediaImageType = getImageMimeType(emojiFilename)
mediaBinary = None
try:
with open(emojiFilename, 'rb') as avFile: with open(emojiFilename, 'rb') as avFile:
mediaBinary = avFile.read() mediaBinary = avFile.read()
except OSError:
print('EX: unable to read emoji image ' + emojiFilename)
if mediaBinary:
self._set_headers_etag(emojiFilename, self._set_headers_etag(emojiFilename,
mediaImageType, mediaImageType,
mediaBinary, None, mediaBinary, None,
@ -6953,8 +7102,13 @@ class PubServer(BaseHTTPRequestHandler):
return return
else: else:
if os.path.isfile(mediaFilename): if os.path.isfile(mediaFilename):
mediaBinary = None
try:
with open(mediaFilename, 'rb') as avFile: with open(mediaFilename, 'rb') as avFile:
mediaBinary = avFile.read() mediaBinary = avFile.read()
except OSError:
print('EX: unable to read icon image ' + mediaFilename)
if mediaBinary:
mimeType = mediaFileMimeType(mediaFilename) mimeType = mediaFileMimeType(mediaFilename)
self._set_headers_etag(mediaFilename, self._set_headers_etag(mediaFilename,
mimeType, mimeType,
@ -6995,8 +7149,13 @@ class PubServer(BaseHTTPRequestHandler):
self._304() self._304()
return return
if os.path.isfile(mediaFilename): if os.path.isfile(mediaFilename):
mediaBinary = None
try:
with open(mediaFilename, 'rb') as avFile: with open(mediaFilename, 'rb') as avFile:
mediaBinary = avFile.read() mediaBinary = avFile.read()
except OSError:
print('EX: unable to read help image ' + mediaFilename)
if mediaBinary:
mimeType = mediaFileMimeType(mediaFilename) mimeType = mediaFileMimeType(mediaFilename)
self._set_headers_etag(mediaFilename, self._set_headers_etag(mediaFilename,
mimeType, mimeType,
@ -7020,8 +7179,13 @@ class PubServer(BaseHTTPRequestHandler):
# The file has not changed # The file has not changed
self._304() self._304()
return return
mediaBinary = None
try:
with open(mediaFilename, 'rb') as avFile: with open(mediaFilename, 'rb') as avFile:
mediaBinary = avFile.read() mediaBinary = avFile.read()
except OSError:
print('EX: unable to read cached avatar ' + mediaFilename)
if mediaBinary:
mimeType = mediaFileMimeType(mediaFilename) mimeType = mediaFileMimeType(mediaFilename)
self._set_headers_etag(mediaFilename, self._set_headers_etag(mediaFilename,
mimeType, mimeType,
@ -12419,8 +12583,13 @@ class PubServer(BaseHTTPRequestHandler):
return True return True
mediaFileType = getImageMimeType(mediaFilename) mediaFileType = getImageMimeType(mediaFilename)
mediaBinary = None
try:
with open(mediaFilename, 'rb') as avFile: with open(mediaFilename, 'rb') as avFile:
mediaBinary = avFile.read() mediaBinary = avFile.read()
except OSError:
print('EX: unable to read binary ' + mediaFilename)
if mediaBinary:
self._set_headers_etag(mediaFilename, self._set_headers_etag(mediaFilename,
mediaFileType, mediaFileType,
mediaBinary, None, mediaBinary, None,
@ -12489,8 +12658,13 @@ class PubServer(BaseHTTPRequestHandler):
lastModifiedTime.strftime('%a, %d %b %Y %H:%M:%S GMT') lastModifiedTime.strftime('%a, %d %b %Y %H:%M:%S GMT')
mediaImageType = getImageMimeType(avatarFile) mediaImageType = getImageMimeType(avatarFile)
mediaBinary = None
try:
with open(avatarFilename, 'rb') as avFile: with open(avatarFilename, 'rb') as avFile:
mediaBinary = avFile.read() mediaBinary = avFile.read()
except OSError:
print('EX: unable to read avatar ' + avatarFilename)
if mediaBinary:
self._set_headers_etag(avatarFilename, mediaImageType, self._set_headers_etag(avatarFilename, mediaImageType,
mediaBinary, None, mediaBinary, None,
refererDomain, True, refererDomain, True,
@ -15900,8 +16074,13 @@ class PubServer(BaseHTTPRequestHandler):
# check that the file exists # check that the file exists
filename = self.server.baseDir + self.path filename = self.server.baseDir + self.path
if os.path.isfile(filename): if os.path.isfile(filename):
content = None
try:
with open(filename, 'r', encoding='utf-8') as File: with open(filename, 'r', encoding='utf-8') as File:
content = File.read() content = File.read()
except OSError:
print('EX: unable to read file ' + filename)
if content:
contentJson = json.loads(content) contentJson = json.loads(content)
msg = json.dumps(contentJson, msg = json.dumps(contentJson,
ensure_ascii=False).encode('utf-8') ensure_ascii=False).encode('utf-8')
@ -15964,8 +16143,14 @@ class PubServer(BaseHTTPRequestHandler):
print('EX: do_HEAD unable to read ' + print('EX: do_HEAD unable to read ' +
mediaTagFilename) mediaTagFilename)
else: else:
mediaBinary = None
try:
with open(mediaFilename, 'rb') as avFile: with open(mediaFilename, 'rb') as avFile:
mediaBinary = avFile.read() mediaBinary = avFile.read()
except OSError:
print('EX: unable to read media binary ' +
mediaFilename)
if mediaBinary:
etag = md5(mediaBinary).hexdigest() # nosec etag = md5(mediaBinary).hexdigest() # nosec
try: try:
with open(mediaTagFilename, 'w+') as etagFile: with open(mediaTagFilename, 'w+') as etagFile:

View File

@ -18,8 +18,11 @@ def addFilter(baseDir: str, nickname: str, domain: str, words: str) -> bool:
if os.path.isfile(filtersFilename): if os.path.isfile(filtersFilename):
if words in open(filtersFilename).read(): if words in open(filtersFilename).read():
return False return False
try:
with open(filtersFilename, 'a+') as filtersFile: with open(filtersFilename, 'a+') as filtersFile:
filtersFile.write(words + '\n') filtersFile.write(words + '\n')
except OSError:
print('EX: unable to append filters ' + filtersFilename)
return True return True
@ -35,8 +38,11 @@ def addGlobalFilter(baseDir: str, words: str) -> bool:
if os.path.isfile(filtersFilename): if os.path.isfile(filtersFilename):
if words in open(filtersFilename).read(): if words in open(filtersFilename).read():
return False return False
try:
with open(filtersFilename, 'a+') as filtersFile: with open(filtersFilename, 'a+') as filtersFile:
filtersFile.write(words + '\n') filtersFile.write(words + '\n')
except OSError:
print('EX: unable to append filters ' + filtersFilename)
return True return True
@ -50,12 +56,15 @@ def removeFilter(baseDir: str, nickname: str, domain: str,
if words not in open(filtersFilename).read(): if words not in open(filtersFilename).read():
return False return False
newFiltersFilename = filtersFilename + '.new' newFiltersFilename = filtersFilename + '.new'
try:
with open(filtersFilename, 'r') as fp: with open(filtersFilename, 'r') as fp:
with open(newFiltersFilename, 'w+') as fpnew: with open(newFiltersFilename, 'w+') as fpnew:
for line in fp: for line in fp:
line = line.replace('\n', '') line = line.replace('\n', '')
if line != words: if line != words:
fpnew.write(line + '\n') fpnew.write(line + '\n')
except OSError as e:
print('EX: unable to remove filter ' + filtersFilename + ' ' + str(e))
if os.path.isfile(newFiltersFilename): if os.path.isfile(newFiltersFilename):
os.rename(newFiltersFilename, filtersFilename) os.rename(newFiltersFilename, filtersFilename)
return True return True
@ -71,12 +80,16 @@ def removeGlobalFilter(baseDir: str, words: str) -> bool:
if words not in open(filtersFilename).read(): if words not in open(filtersFilename).read():
return False return False
newFiltersFilename = filtersFilename + '.new' newFiltersFilename = filtersFilename + '.new'
try:
with open(filtersFilename, 'r') as fp: with open(filtersFilename, 'r') as fp:
with open(newFiltersFilename, 'w+') as fpnew: with open(newFiltersFilename, 'w+') as fpnew:
for line in fp: for line in fp:
line = line.replace('\n', '') line = line.replace('\n', '')
if line != words: if line != words:
fpnew.write(line + '\n') fpnew.write(line + '\n')
except OSError as e:
print('EX: unable to remove global filter ' +
filtersFilename + ' ' + str(e))
if os.path.isfile(newFiltersFilename): if os.path.isfile(newFiltersFilename):
os.rename(newFiltersFilename, filtersFilename) os.rename(newFiltersFilename, filtersFilename)
return True return True
@ -100,6 +113,7 @@ def _isFilteredBase(filename: str, content: str) -> bool:
if not os.path.isfile(filename): if not os.path.isfile(filename):
return False return False
try:
with open(filename, 'r') as fp: with open(filename, 'r') as fp:
for line in fp: for line in fp:
filterStr = line.replace('\n', '').replace('\r', '') filterStr = line.replace('\n', '').replace('\r', '')
@ -116,6 +130,8 @@ def _isFilteredBase(filename: str, content: str) -> bool:
if word not in content: if word not in content:
return False return False
return True return True
except OSError as e:
print('EX: _isFilteredBase ' + filename + ' ' + str(e))
return False return False

View File

@ -58,8 +58,12 @@ def createInitialLastSeen(baseDir: str, httpPrefix: str) -> None:
lastSeenDir = accountDir + '/lastseen' lastSeenDir = accountDir + '/lastseen'
if not os.path.isdir(lastSeenDir): if not os.path.isdir(lastSeenDir):
os.mkdir(lastSeenDir) os.mkdir(lastSeenDir)
followingHandles = []
try:
with open(followingFilename, 'r') as fp: with open(followingFilename, 'r') as fp:
followingHandles = fp.readlines() followingHandles = fp.readlines()
except OSError:
print('EX: createInitialLastSeen ' + followingFilename)
for handle in followingHandles: for handle in followingHandles:
if '#' in handle: if '#' in handle:
continue continue
@ -74,8 +78,12 @@ def createInitialLastSeen(baseDir: str, httpPrefix: str) -> None:
lastSeenFilename = \ lastSeenFilename = \
lastSeenDir + '/' + actor.replace('/', '#') + '.txt' lastSeenDir + '/' + actor.replace('/', '#') + '.txt'
if not os.path.isfile(lastSeenFilename): if not os.path.isfile(lastSeenFilename):
try:
with open(lastSeenFilename, 'w+') as fp: with open(lastSeenFilename, 'w+') as fp:
fp.write(str(100)) fp.write(str(100))
except OSError:
print('EX: createInitialLastSeen 2 ' +
lastSeenFilename)
break break
@ -124,6 +132,7 @@ def _removeFromFollowBase(baseDir: str,
break break
if not actorFound: if not actorFound:
return return
try:
with open(approveFollowsFilename + '.new', 'w+') as approvefilenew: with open(approveFollowsFilename + '.new', 'w+') as approvefilenew:
with open(approveFollowsFilename, 'r') as approvefile: with open(approveFollowsFilename, 'r') as approvefile:
if not acceptDenyActor: if not acceptDenyActor:
@ -134,6 +143,9 @@ def _removeFromFollowBase(baseDir: str,
for approveHandle in approvefile: for approveHandle in approvefile:
if acceptDenyActor not in approveHandle: if acceptDenyActor not in approveHandle:
approvefilenew.write(approveHandle) approvefilenew.write(approveHandle)
except OSError as e:
print('EX: _removeFromFollowBase ' +
approveFollowsFilename + ' ' + str(e))
os.rename(approveFollowsFilename + '.new', approveFollowsFilename) os.rename(approveFollowsFilename + '.new', approveFollowsFilename)
@ -218,8 +230,11 @@ def getFollowerDomains(baseDir: str, nickname: str, domain: str) -> []:
return [] return []
lines = [] lines = []
try:
with open(followersFile, 'r') as fpFollowers: with open(followersFile, 'r') as fpFollowers:
lines = fpFollowers.readlines() lines = fpFollowers.readlines()
except OSError:
print('EX: getFollowerDomains ' + followersFile)
domainsList = [] domainsList = []
for handle in lines: for handle in lines:
@ -251,8 +266,11 @@ def isFollowerOfPerson(baseDir: str, nickname: str, domain: str,
alreadyFollowing = False alreadyFollowing = False
followersStr = '' followersStr = ''
try:
with open(followersFile, 'r') as fpFollowers: with open(followersFile, 'r') as fpFollowers:
followersStr = fpFollowers.read() followersStr = fpFollowers.read()
except OSError:
print('EX: isFollowerOfPerson ' + followersFile)
if handle in followersStr: if handle in followersStr:
alreadyFollowing = True alreadyFollowing = True
@ -294,8 +312,13 @@ def unfollowAccount(baseDir: str, nickname: str, domain: str,
print('DEBUG: handle to unfollow ' + handleToUnfollow + print('DEBUG: handle to unfollow ' + handleToUnfollow +
' is not in ' + filename) ' is not in ' + filename)
return return
lines = []
try:
with open(filename, 'r') as f: with open(filename, 'r') as f:
lines = f.readlines() lines = f.readlines()
except OSError:
print('EX: unfollowAccount ' + filename)
if lines:
try: try:
with open(filename, 'w+') as f: with open(filename, 'w+') as f:
for line in lines: for line in lines:
@ -312,8 +335,11 @@ def unfollowAccount(baseDir: str, nickname: str, domain: str,
if os.path.isfile(unfollowedFilename): if os.path.isfile(unfollowedFilename):
if handleToUnfollowLower not in \ if handleToUnfollowLower not in \
open(unfollowedFilename).read().lower(): open(unfollowedFilename).read().lower():
try:
with open(unfollowedFilename, 'a+') as f: with open(unfollowedFilename, 'a+') as f:
f.write(handleToUnfollow + '\n') f.write(handleToUnfollow + '\n')
except OSError:
print('EX: unable to append ' + unfollowedFilename)
else: else:
try: try:
with open(unfollowedFilename, 'w+') as f: with open(unfollowedFilename, 'w+') as f:
@ -371,8 +397,13 @@ def _getNoOfFollows(baseDir: str, nickname: str, domain: str,
if not os.path.isfile(filename): if not os.path.isfile(filename):
return 0 return 0
ctr = 0 ctr = 0
lines = []
try:
with open(filename, 'r') as f: with open(filename, 'r') as f:
lines = f.readlines() lines = f.readlines()
except OSError:
print('EX: _getNoOfFollows ' + filename)
if lines:
for line in lines: for line in lines:
if '#' in line: if '#' in line:
continue continue
@ -483,8 +514,12 @@ def getFollowingFeed(baseDir: str, domain: str, port: int, path: str,
currPage = 1 currPage = 1
pageCtr = 0 pageCtr = 0
totalCtr = 0 totalCtr = 0
lines = []
try:
with open(filename, 'r') as f: with open(filename, 'r') as f:
lines = f.readlines() lines = f.readlines()
except OSError:
print('EX: getFollowingFeed ' + filename)
for line in lines: for line in lines:
if '#' not in line: if '#' not in line:
if '@' in line and not line.startswith('http'): if '@' in line and not line.startswith('http'):
@ -568,8 +603,13 @@ def _noOfFollowRequests(baseDir: str,
if not os.path.isfile(approveFollowsFilename): if not os.path.isfile(approveFollowsFilename):
return 0 return 0
ctr = 0 ctr = 0
lines = []
try:
with open(approveFollowsFilename, 'r') as f: with open(approveFollowsFilename, 'r') as f:
lines = f.readlines() lines = f.readlines()
except OSError:
print('EX: _noOfFollowRequests ' + approveFollowsFilename)
if lines:
if followType == "onion": if followType == "onion":
for fileLine in lines: for fileLine in lines:
if '.onion' in fileLine: if '.onion' in fileLine:
@ -607,8 +647,11 @@ def _storeFollowRequest(baseDir: str,
alreadyFollowing = False alreadyFollowing = False
followersStr = '' followersStr = ''
try:
with open(followersFilename, 'r') as fpFollowers: with open(followersFilename, 'r') as fpFollowers:
followersStr = fpFollowers.read() followersStr = fpFollowers.read()
except OSError:
print('EX: _storeFollowRequest ' + followersFilename)
if approveHandle in followersStr: if approveHandle in followersStr:
alreadyFollowing = True alreadyFollowing = True
@ -649,8 +692,11 @@ def _storeFollowRequest(baseDir: str,
if os.path.isfile(approveFollowsFilename): if os.path.isfile(approveFollowsFilename):
if approveHandle not in open(approveFollowsFilename).read(): if approveHandle not in open(approveFollowsFilename).read():
try:
with open(approveFollowsFilename, 'a+') as fp: with open(approveFollowsFilename, 'a+') as fp:
fp.write(approveHandleStored + '\n') fp.write(approveHandleStored + '\n')
except OSError:
print('EX: _storeFollowRequest 2 ' + approveFollowsFilename)
else: else:
if debug: if debug:
print('DEBUG: ' + approveHandleStored + print('DEBUG: ' + approveHandleStored +
@ -660,7 +706,7 @@ def _storeFollowRequest(baseDir: str,
with open(approveFollowsFilename, 'w+') as fp: with open(approveFollowsFilename, 'w+') as fp:
fp.write(approveHandleStored + '\n') fp.write(approveHandleStored + '\n')
except OSError: except OSError:
print('EX: unable to write ' + approveFollowsFilename) print('EX: _storeFollowRequest 3 ' + 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
@ -1053,11 +1099,14 @@ def sendFollowRequest(session, baseDir: str,
if os.path.isfile(unfollowedFilename): if os.path.isfile(unfollowedFilename):
if followHandle in open(unfollowedFilename).read(): if followHandle in open(unfollowedFilename).read():
unfollowedFile = None unfollowedFile = None
try:
with open(unfollowedFilename, 'r') as fp: with open(unfollowedFilename, 'r') as fp:
unfollowedFile = fp.read() unfollowedFile = fp.read()
except OSError:
print('EX: sendFollowRequest ' + unfollowedFilename)
if unfollowedFile:
unfollowedFile = \ unfollowedFile = \
unfollowedFile.replace(followHandle + '\n', '') unfollowedFile.replace(followHandle + '\n', '')
if unfollowedFile:
try: try:
with open(unfollowedFilename, 'w+') as fp: with open(unfollowedFilename, 'w+') as fp:
fp.write(unfollowedFile) fp.write(unfollowedFile)

View File

@ -44,13 +44,18 @@ def receivingCalendarEvents(baseDir: str, nickname: str, domain: str,
if not os.path.isfile(followingFilename): if not os.path.isfile(followingFilename):
return False return False
# create a new calendar file from the following file # create a new calendar file from the following file
followingHandles = None
try:
with open(followingFilename, 'r') as followingFile: with open(followingFilename, 'r') as followingFile:
followingHandles = followingFile.read() followingHandles = followingFile.read()
except OSError:
print('EX: receivingCalendarEvents ' + followingFilename)
if followingHandles:
try: try:
with open(calendarFilename, 'w+') as fp: with open(calendarFilename, 'w+') as fp:
fp.write(followingHandles) fp.write(followingHandles)
except OSError: except OSError:
print('EX: unable to write ' + calendarFilename) print('EX: receivingCalendarEvents 2 ' + calendarFilename)
return handle + '\n' in open(calendarFilename).read() return handle + '\n' in open(calendarFilename).read()
@ -83,14 +88,20 @@ def _receiveCalendarEvents(baseDir: str, nickname: str, domain: str,
followingHandles = '' followingHandles = ''
if os.path.isfile(calendarFilename): if os.path.isfile(calendarFilename):
print('Calendar file exists') print('Calendar file exists')
try:
with open(calendarFilename, 'r') as calendarFile: with open(calendarFilename, 'r') as calendarFile:
followingHandles = calendarFile.read() followingHandles = calendarFile.read()
except OSError:
print('EX: _receiveCalendarEvents ' + calendarFilename)
else: else:
# create a new calendar file from the following file # create a new calendar file from the following file
print('Creating calendar file ' + calendarFilename) print('Creating calendar file ' + calendarFilename)
followingHandles = '' followingHandles = ''
try:
with open(followingFilename, 'r') as followingFile: with open(followingFilename, 'r') as followingFile:
followingHandles = followingFile.read() followingHandles = followingFile.read()
except OSError:
print('EX: _receiveCalendarEvents 2 ' + calendarFilename)
if add: if add:
try: try:
with open(calendarFilename, 'w+') as fp: with open(calendarFilename, 'w+') as fp:
@ -110,7 +121,7 @@ def _receiveCalendarEvents(baseDir: str, nickname: str, domain: str,
with open(calendarFilename, 'w+') as fp: with open(calendarFilename, 'w+') as fp:
fp.write(followingHandles) fp.write(followingHandles)
except OSError: except OSError:
print('EX: unable to write ' + calendarFilename) print('EX: _receiveCalendarEvents 3 ' + 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
@ -121,7 +132,7 @@ def _receiveCalendarEvents(baseDir: str, nickname: str, domain: str,
with open(calendarFilename, 'w+') as fp: with open(calendarFilename, 'w+') as fp:
fp.write(followingHandles) fp.write(followingHandles)
except OSError: except OSError:
print('EX: unable to write ' + calendarFilename) print('EX: _receiveCalendarEvents 4 ' + calendarFilename)
def addPersonToCalendar(baseDir: str, nickname: str, domain: str, def addPersonToCalendar(baseDir: str, nickname: str, domain: str,

2
git.py
View File

@ -217,5 +217,5 @@ def receiveGitPatch(baseDir: str, nickname: str, domain: str,
patchFile.write(patchStr) patchFile.write(patchStr)
return True return True
except OSError as e: except OSError as e:
print('EX: unable to write patch ' + patchFilename + ' ' + str(e)) print('EX: receiveGitPatch ' + patchFilename + ' ' + str(e))
return False return False