Exception handling for loading json files

main2
Bob Mottram 2019-09-17 13:14:36 +01:00
parent 2210837a33
commit e53b6c5ff6
4 changed files with 196 additions and 55 deletions

View File

@ -1357,8 +1357,14 @@ class PubServer(BaseHTTPRequestHandler):
self.server.httpPrefix+':##'+self.server.domainFull+'#users#'+nickname+'#statuses#'+statusNumber+'.json' self.server.httpPrefix+':##'+self.server.domainFull+'#users#'+nickname+'#statuses#'+statusNumber+'.json'
if os.path.isfile(postFilename): if os.path.isfile(postFilename):
postJsonObject={} postJsonObject={}
with open(postFilename, 'r') as fp: loadedPost=False
postJsonObject=commentjson.load(fp) try:
with open(postFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
loadedPost=True
except Exception as e:
print(e)
if loadedPost:
# Only authorized viewers get to see likes on posts # Only authorized viewers get to see likes on posts
# Otherwize marketers could gain more social graph info # Otherwize marketers could gain more social graph info
if not authorized: if not authorized:
@ -1490,8 +1496,14 @@ class PubServer(BaseHTTPRequestHandler):
nickname=postSections[0] nickname=postSections[0]
actorFilename=self.server.baseDir+'/accounts/'+nickname+'@'+self.server.domain+'.json' actorFilename=self.server.baseDir+'/accounts/'+nickname+'@'+self.server.domain+'.json'
if os.path.isfile(actorFilename): if os.path.isfile(actorFilename):
with open(actorFilename, 'r') as fp: loadedActor=False
actorJson=commentjson.load(fp) try:
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
loadedActor=True
except Exception as e:
print(e)
if loadedActor:
if actorJson.get('roles'): if actorJson.get('roles'):
if self._requestHTTP(): if self._requestHTTP():
getPerson = \ getPerson = \
@ -1527,8 +1539,14 @@ class PubServer(BaseHTTPRequestHandler):
nickname=postSections[0] nickname=postSections[0]
actorFilename=self.server.baseDir+'/accounts/'+nickname+'@'+self.server.domain+'.json' actorFilename=self.server.baseDir+'/accounts/'+nickname+'@'+self.server.domain+'.json'
if os.path.isfile(actorFilename): if os.path.isfile(actorFilename):
with open(actorFilename, 'r') as fp: loadedActor=False
actorJson=commentjson.load(fp) try:
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
loadedActor=True
except Exception as e:
print(e)
if loadedActor:
if actorJson.get('skills'): if actorJson.get('skills'):
if self._requestHTTP(): if self._requestHTTP():
getPerson = \ getPerson = \
@ -2552,8 +2570,14 @@ class PubServer(BaseHTTPRequestHandler):
actorFilename=self.server.baseDir+'/accounts/'+nickname+'@'+self.server.domain+'.json' actorFilename=self.server.baseDir+'/accounts/'+nickname+'@'+self.server.domain+'.json'
if os.path.isfile(actorFilename): if os.path.isfile(actorFilename):
with open(actorFilename, 'r') as fp: loadedActor=False
actorJson=commentjson.load(fp) try:
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
loadedActor=True
except Exception as e:
print(e)
if loadedActor:
actorChanged=False actorChanged=False
skillCtr=1 skillCtr=1
newSkills={} newSkills={}
@ -3626,8 +3650,13 @@ def runDaemon(projectVersion, \
systemLanguage='en' systemLanguage='en'
translationsFile=baseDir+'/translations/'+systemLanguage+'.json' translationsFile=baseDir+'/translations/'+systemLanguage+'.json'
print('System language: '+systemLanguage) print('System language: '+systemLanguage)
with open(translationsFile, 'r') as fp:
httpd.translate=commentjson.load(fp) try:
with open(translationsFile, 'r') as fp:
httpd.translate=commentjson.load(fp)
except Exception as e:
print('ERROR while loading translations '+translationsFile)
print(e)
httpd.outboxThread={} httpd.outboxThread={}
httpd.projectVersion=projectVersion httpd.projectVersion=projectVersion

View File

@ -306,8 +306,12 @@ def inboxCheckCapabilities(baseDir :str,nickname :str,domain :str, \
queue.pop(0) queue.pop(0)
return False return False
with open(ocapFilename, 'r') as fp: try:
oc=commentjson.load(fp) with open(ocapFilename, 'r') as fp:
oc=commentjson.load(fp)
except Exception as e:
print(e)
return False
if not oc.get('id'): if not oc.get('id'):
if debug: if debug:
@ -363,8 +367,14 @@ def inboxPostRecipientsAdd(baseDir :str,httpPrefix :str,toList :[], \
ocapFilename=baseDir+'/accounts/'+handle+'/ocap/accept/'+actor.replace('/','#')+'.json' ocapFilename=baseDir+'/accounts/'+handle+'/ocap/accept/'+actor.replace('/','#')+'.json'
if os.path.isfile(ocapFilename): if os.path.isfile(ocapFilename):
# read the granted capabilities and obtain the id # read the granted capabilities and obtain the id
with open(ocapFilename, 'r') as fp: loadedOcap=False
ocapJson=commentjson.load(fp) try:
with open(ocapFilename, 'r') as fp:
ocapJson=commentjson.load(fp)
loadedOcap=True
except Exception as e:
print(e)
if loadedOcap:
if ocapJson.get('id'): if ocapJson.get('id'):
# append with the capabilities id # append with the capabilities id
recipientsDict[handle]=ocapJson['id'] recipientsDict[handle]=ocapJson['id']
@ -643,8 +653,14 @@ def personReceiveUpdate(baseDir: str, \
return False return False
else: else:
if os.path.isfile(actorFilename): if os.path.isfile(actorFilename):
with open(actorFilename, 'r') as fp: loadedActor=False
existingPersonJson=commentjson.load(fp) try:
with open(actorFilename, 'r') as fp:
existingPersonJson=commentjson.load(fp)
loadedActor=True
except Exception as e:
print(e)
if loadedActor:
if existingPersonJson['publicKey']['publicKeyPem']!=personJson['publicKey']['publicKeyPem']: if existingPersonJson['publicKey']['publicKeyPem']!=personJson['publicKey']['publicKeyPem']:
if debug: if debug:
print('WARN: Public key does not match cached actor when updating') print('WARN: Public key does not match cached actor when updating')
@ -997,8 +1013,15 @@ def receiveUndoAnnounce(session,handle: str,baseDir: str, \
return True return True
if debug: if debug:
print('DEBUG: announced/repeated post to be undone found in inbox') print('DEBUG: announced/repeated post to be undone found in inbox')
with open(postFilename, 'r') as fp:
postJsonObject=commentjson.load(fp) loadedPost=False
try:
with open(postFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
loadedPost=True
except Exception as e:
print(e)
if loadedPost:
if not postJsonObject.get('type'): if not postJsonObject.get('type'):
if postJsonObject['type']!='Announce': if postJsonObject['type']!='Announce':
if debug: if debug:

View File

@ -508,11 +508,13 @@ def createPostBase(baseDir: str,nickname: str, domain: str, port: int, \
ocapFilename=getOcapFilename(baseDir,nickname,domain,toUrl,'granted') ocapFilename=getOcapFilename(baseDir,nickname,domain,toUrl,'granted')
if ocapFilename: if ocapFilename:
if os.path.isfile(ocapFilename): if os.path.isfile(ocapFilename):
with open(ocapFilename, 'r') as fp: try:
oc=commentjson.load(fp) with open(ocapFilename, 'r') as fp:
if oc.get('id'): oc=commentjson.load(fp)
capabilityIdList=[oc['id']] if oc.get('id'):
capabilityIdList=[oc['id']]
except Exception as e:
print(e)
newPost = { newPost = {
"@context": "https://www.w3.org/ns/activitystreams", "@context": "https://www.w3.org/ns/activitystreams",
'id': newPostId+'/activity', 'id': newPostId+'/activity',
@ -1634,9 +1636,13 @@ def createModeration(baseDir: str,nickname: str,domain: str,port: int,httpPrefix
for postUrl in pageLines: for postUrl in pageLines:
postFilename=boxDir+'/'+postUrl.replace('/','#')+'.json' postFilename=boxDir+'/'+postUrl.replace('/','#')+'.json'
if os.path.isfile(postFilename): if os.path.isfile(postFilename):
with open(postFilename, 'r') as fp: try:
postJsonObject=commentjson.load(fp) with open(postFilename, 'r') as fp:
boxItems['orderedItems'].append(postJsonObject) postJsonObject=commentjson.load(fp)
boxItems['orderedItems'].append(postJsonObject)
except Exception as e:
print(e)
if headerOnly: if headerOnly:
return boxHeader return boxHeader
return boxItems return boxItems
@ -1739,8 +1745,14 @@ def createBoxBase(baseDir: str,boxname: str, \
if statusNumber: if statusNumber:
sharedInboxFilename=os.path.join(sharedBoxDir, postFilename) sharedInboxFilename=os.path.join(sharedBoxDir, postFilename)
# get the actor from the shared post # get the actor from the shared post
with open(sharedInboxFilename, 'r') as fp: loadedPost=False
postJsonObject=commentjson.load(fp) try:
with open(sharedInboxFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
loadedPost=True
except Exception as e:
print(e)
if loadedPost:
actorNickname=getNicknameFromActor(postJsonObject['actor']) actorNickname=getNicknameFromActor(postJsonObject['actor'])
actorDomain,actorPort=getDomainFromActor(postJsonObject['actor']) actorDomain,actorPort=getDomainFromActor(postJsonObject['actor'])
if actorNickname and actorDomain: if actorNickname and actorDomain:
@ -1756,8 +1768,14 @@ def createBoxBase(baseDir: str,boxname: str, \
ocapFilename=baseDir+'/accounts/'+handle+'/ocap/granted/'+postJsonObject['actor'].replace('/','#')+'.json' ocapFilename=baseDir+'/accounts/'+handle+'/ocap/granted/'+postJsonObject['actor'].replace('/','#')+'.json'
if os.path.isfile(ocapFilename): if os.path.isfile(ocapFilename):
# read the capabilities id # read the capabilities id
with open(ocapFilename, 'r') as fp: loadedOcap=False
ocapJson=commentjson.load(fp) try:
with open(ocapFilename, 'r') as fp:
ocapJson=commentjson.load(fp)
loadedOcap=True
except Exception as e:
print(e)
if loadedOcap:
if ocapJson.get('id'): if ocapJson.get('id'):
if ocapJson['id'] in capsList: if ocapJson['id'] in capsList:
postsInBoxDict[statusNumber]=sharedInboxFilename postsInBoxDict[statusNumber]=sharedInboxFilename
@ -2040,8 +2058,14 @@ def populateRepliesJson(baseDir: str,nickname: str,domain: str,postRepliesFilena
if os.path.isfile(searchFilename): if os.path.isfile(searchFilename):
if authorized or \ if authorized or \
'https://www.w3.org/ns/activitystreams#Public' in open(searchFilename).read(): 'https://www.w3.org/ns/activitystreams#Public' in open(searchFilename).read():
with open(searchFilename, 'r') as fp: loadedPost=False
postJsonObject=commentjson.load(fp) try:
with open(searchFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
loadedPost=True
except Exception as e:
print(e)
if loadedPost:
if postJsonObject['object'].get('cc'): if postJsonObject['object'].get('cc'):
if authorized or \ if authorized or \
('https://www.w3.org/ns/activitystreams#Public' in postJsonObject['object']['to'] or \ ('https://www.w3.org/ns/activitystreams#Public' in postJsonObject['object']['to'] or \
@ -2065,8 +2089,14 @@ def populateRepliesJson(baseDir: str,nickname: str,domain: str,postRepliesFilena
if authorized or \ if authorized or \
'https://www.w3.org/ns/activitystreams#Public' in open(searchFilename).read(): 'https://www.w3.org/ns/activitystreams#Public' in open(searchFilename).read():
# get the json of the reply and append it to the collection # get the json of the reply and append it to the collection
with open(searchFilename, 'r') as fp: loadedPost=False
postJsonObject=commentjson.load(fp) try:
with open(searchFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
loadedPost=True
except Exception as e:
print(e)
if loadedPost:
if postJsonObject['object'].get('cc'): if postJsonObject['object'].get('cc'):
if authorized or \ if authorized or \
('https://www.w3.org/ns/activitystreams#Public' in postJsonObject['object']['to'] or \ ('https://www.w3.org/ns/activitystreams#Public' in postJsonObject['object']['to'] or \

View File

@ -144,8 +144,14 @@ def htmlSearchEmoji(translate: {},baseDir: str,searchStr: str) -> str:
emojiForm+=htmlFooter() emojiForm+=htmlFooter()
return emojiForm return emojiForm
with open(emojiLookupFilename, 'r') as fp: loadedEmoji=False
emojiJson=commentjson.load(fp) try:
with open(emojiLookupFilename, 'r') as fp:
emojiJson=commentjson.load(fp)
loadedEmoji=True
except Exception as e:
print(e)
if loadedEmoji:
results={} results={}
for emojiName,filename in emojiJson.items(): for emojiName,filename in emojiJson.items():
if searchStr in emojiName: if searchStr in emojiName:
@ -206,8 +212,16 @@ def htmlSearchSharedItems(translate: {}, \
sharesFilename=baseDir+'/accounts/'+handle+'/shares.json' sharesFilename=baseDir+'/accounts/'+handle+'/shares.json'
if not os.path.isfile(sharesFilename): if not os.path.isfile(sharesFilename):
continue continue
with open(sharesFilename, 'r') as fp:
sharesJson=commentjson.load(fp) sharesJson=None
try:
with open(sharesFilename, 'r') as fp:
sharesJson=commentjson.load(fp)
except Exception as e:
print(e)
if not sharesJson:
continue
for name,sharedItem in sharesJson.items(): for name,sharedItem in sharesJson.items():
matched=True matched=True
for searchSubstr in searchStrLowerList: for searchSubstr in searchStrLowerList:
@ -360,8 +374,14 @@ def htmlHashtagSearch(translate: {}, \
if not postFilename: if not postFilename:
index-=1 index-=1
continue continue
with open(postFilename, 'r') as fp: loadedPost=False
postJsonObject=commentjson.load(fp) try:
with open(postFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
loadedPost=True
except Exception as e:
print(e)
if loadedPost:
if not isPublicPost(postJsonObject): if not isPublicPost(postJsonObject):
index-=1 index-=1
continue continue
@ -401,8 +421,14 @@ def htmlSkillsSearch(translate: {},baseDir: str, \
if f.startswith('inbox@'): if f.startswith('inbox@'):
continue continue
actorFilename = os.path.join(subdir, f) actorFilename = os.path.join(subdir, f)
with open(actorFilename, 'r') as fp: loadedActor=False
actorJson=commentjson.load(fp) try:
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
loadedActor=True
except Exception as e:
print(e)
if loadedActor:
if actorJson.get('id') and \ if actorJson.get('id') and \
actorJson.get('skills') and \ actorJson.get('skills') and \
actorJson.get('name') and \ actorJson.get('name') and \
@ -430,8 +456,14 @@ def htmlSkillsSearch(translate: {},baseDir: str, \
if f.startswith('inbox@'): if f.startswith('inbox@'):
continue continue
actorFilename = os.path.join(subdir, f) actorFilename = os.path.join(subdir, f)
with open(actorFilename, 'r') as fp: loadedActor=False
cachedActorJson=commentjson.load(fp) try:
with open(actorFilename, 'r') as fp:
cachedActorJson=commentjson.load(fp)
loadedActor=True
except Exception as e:
print(e)
if loadedActor:
if cachedActorJson.get('actor'): if cachedActorJson.get('actor'):
actorJson=cachedActorJson['actor'] actorJson=cachedActorJson['actor']
if actorJson.get('id') and \ if actorJson.get('id') and \
@ -504,8 +536,14 @@ def htmlEditProfile(translate: {},baseDir: str,path: str,domain: str,port: int)
displayNickname=nickname displayNickname=nickname
bioStr='' bioStr=''
manuallyApprovesFollowers='' manuallyApprovesFollowers=''
with open(actorFilename, 'r') as fp: loadedActor=False
actorJson=commentjson.load(fp) try:
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
loadedActor=True
except Exception as e:
print(e)
if loadedActor:
if actorJson.get('name'): if actorJson.get('name'):
displayNickname=actorJson['name'] displayNickname=actorJson['name']
if actorJson.get('summary'): if actorJson.get('summary'):
@ -1501,8 +1539,14 @@ def followerApprovalActive(baseDir: str,nickname: str,domain: str) -> bool:
manuallyApprovesFollowers=False manuallyApprovesFollowers=False
actorFilename=baseDir+'/accounts/'+nickname+'@'+domain+'.json' actorFilename=baseDir+'/accounts/'+nickname+'@'+domain+'.json'
if os.path.isfile(actorFilename): if os.path.isfile(actorFilename):
with open(actorFilename, 'r') as fp: loadedActor=False
actorJson=commentjson.load(fp) try:
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
loadedActor=True
except Exception as e:
print(e)
if loadedActor:
if actorJson.get('manuallyApprovesFollowers'): if actorJson.get('manuallyApprovesFollowers'):
manuallyApprovesFollowers=actorJson['manuallyApprovesFollowers'] manuallyApprovesFollowers=actorJson['manuallyApprovesFollowers']
return manuallyApprovesFollowers return manuallyApprovesFollowers
@ -1576,9 +1620,12 @@ def individualPostAsHtml(iconsDir: str,translate: {}, \
if not os.path.isfile(announceFilename+'.reject'): if not os.path.isfile(announceFilename+'.reject'):
if os.path.isfile(announceFilename): if os.path.isfile(announceFilename):
print('Reading cached Announce content for '+postJsonObject['object']) print('Reading cached Announce content for '+postJsonObject['object'])
with open(announceFilename, 'r') as fp: try:
postJsonObject=commentjson.load(fp) with open(announceFilename, 'r') as fp:
isAnnounced=True postJsonObject=commentjson.load(fp)
isAnnounced=True
except Exception as e:
print(e)
else: else:
print('Downloading Announce content for '+postJsonObject['object']) print('Downloading Announce content for '+postJsonObject['object'])
asHeader={'Accept': 'application/activity+json; profile="https://www.w3.org/ns/activitystreams"'} asHeader={'Accept': 'application/activity+json; profile="https://www.w3.org/ns/activitystreams"'}
@ -2202,8 +2249,13 @@ def htmlRemoveSharedItem(translate: {},baseDir: str,actor: str,shareName: str) -
if not os.path.isfile(sharesFile): if not os.path.isfile(sharesFile):
return None return None
sharesJson=None sharesJson=None
with open(sharesFile, 'r') as fp:
sharesJson=commentjson.load(fp) try:
with open(sharesFile, 'r') as fp:
sharesJson=commentjson.load(fp)
except Exception as e:
print(e)
if not sharesJson: if not sharesJson:
return None return None
if not sharesJson.get(shareName): if not sharesJson.get(shareName):
@ -2258,8 +2310,15 @@ def htmlDeletePost(translate,pageNumber: int, \
postFilename=locatePost(baseDir,nickname,domain,messageId) postFilename=locatePost(baseDir,nickname,domain,messageId)
if not postFilename: if not postFilename:
return None return None
with open(postFilename, 'r') as fp:
postJsonObject=commentjson.load(fp) postJsonObject=None
try:
with open(postFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
except Exception as e:
print(e)
if not postJsonObject:
return None
if os.path.isfile(baseDir+'/img/delete-background.png'): if os.path.isfile(baseDir+'/img/delete-background.png'):
if not os.path.isfile(baseDir+'/accounts/delete-background.png'): if not os.path.isfile(baseDir+'/accounts/delete-background.png'):