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'
if os.path.isfile(postFilename):
postJsonObject={}
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:
# Only authorized viewers get to see likes on posts
# Otherwize marketers could gain more social graph info
if not authorized:
@ -1490,8 +1496,14 @@ class PubServer(BaseHTTPRequestHandler):
nickname=postSections[0]
actorFilename=self.server.baseDir+'/accounts/'+nickname+'@'+self.server.domain+'.json'
if os.path.isfile(actorFilename):
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
loadedActor=False
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 self._requestHTTP():
getPerson = \
@ -1527,8 +1539,14 @@ class PubServer(BaseHTTPRequestHandler):
nickname=postSections[0]
actorFilename=self.server.baseDir+'/accounts/'+nickname+'@'+self.server.domain+'.json'
if os.path.isfile(actorFilename):
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
loadedActor=False
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 self._requestHTTP():
getPerson = \
@ -2552,8 +2570,14 @@ class PubServer(BaseHTTPRequestHandler):
actorFilename=self.server.baseDir+'/accounts/'+nickname+'@'+self.server.domain+'.json'
if os.path.isfile(actorFilename):
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
loadedActor=False
try:
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
loadedActor=True
except Exception as e:
print(e)
if loadedActor:
actorChanged=False
skillCtr=1
newSkills={}
@ -3626,8 +3650,13 @@ def runDaemon(projectVersion, \
systemLanguage='en'
translationsFile=baseDir+'/translations/'+systemLanguage+'.json'
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.projectVersion=projectVersion

View File

@ -306,8 +306,12 @@ def inboxCheckCapabilities(baseDir :str,nickname :str,domain :str, \
queue.pop(0)
return False
with open(ocapFilename, 'r') as fp:
oc=commentjson.load(fp)
try:
with open(ocapFilename, 'r') as fp:
oc=commentjson.load(fp)
except Exception as e:
print(e)
return False
if not oc.get('id'):
if debug:
@ -363,8 +367,14 @@ def inboxPostRecipientsAdd(baseDir :str,httpPrefix :str,toList :[], \
ocapFilename=baseDir+'/accounts/'+handle+'/ocap/accept/'+actor.replace('/','#')+'.json'
if os.path.isfile(ocapFilename):
# read the granted capabilities and obtain the id
with open(ocapFilename, 'r') as fp:
ocapJson=commentjson.load(fp)
loadedOcap=False
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'):
# append with the capabilities id
recipientsDict[handle]=ocapJson['id']
@ -643,8 +653,14 @@ def personReceiveUpdate(baseDir: str, \
return False
else:
if os.path.isfile(actorFilename):
with open(actorFilename, 'r') as fp:
existingPersonJson=commentjson.load(fp)
loadedActor=False
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 debug:
print('WARN: Public key does not match cached actor when updating')
@ -997,8 +1013,15 @@ def receiveUndoAnnounce(session,handle: str,baseDir: str, \
return True
if debug:
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 postJsonObject['type']!='Announce':
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')
if ocapFilename:
if os.path.isfile(ocapFilename):
with open(ocapFilename, 'r') as fp:
oc=commentjson.load(fp)
if oc.get('id'):
capabilityIdList=[oc['id']]
try:
with open(ocapFilename, 'r') as fp:
oc=commentjson.load(fp)
if oc.get('id'):
capabilityIdList=[oc['id']]
except Exception as e:
print(e)
newPost = {
"@context": "https://www.w3.org/ns/activitystreams",
'id': newPostId+'/activity',
@ -1634,9 +1636,13 @@ def createModeration(baseDir: str,nickname: str,domain: str,port: int,httpPrefix
for postUrl in pageLines:
postFilename=boxDir+'/'+postUrl.replace('/','#')+'.json'
if os.path.isfile(postFilename):
with open(postFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
boxItems['orderedItems'].append(postJsonObject)
try:
with open(postFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
boxItems['orderedItems'].append(postJsonObject)
except Exception as e:
print(e)
if headerOnly:
return boxHeader
return boxItems
@ -1739,8 +1745,14 @@ def createBoxBase(baseDir: str,boxname: str, \
if statusNumber:
sharedInboxFilename=os.path.join(sharedBoxDir, postFilename)
# get the actor from the shared post
with open(sharedInboxFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
loadedPost=False
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'])
actorDomain,actorPort=getDomainFromActor(postJsonObject['actor'])
if actorNickname and actorDomain:
@ -1756,8 +1768,14 @@ def createBoxBase(baseDir: str,boxname: str, \
ocapFilename=baseDir+'/accounts/'+handle+'/ocap/granted/'+postJsonObject['actor'].replace('/','#')+'.json'
if os.path.isfile(ocapFilename):
# read the capabilities id
with open(ocapFilename, 'r') as fp:
ocapJson=commentjson.load(fp)
loadedOcap=False
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['id'] in capsList:
postsInBoxDict[statusNumber]=sharedInboxFilename
@ -2040,8 +2058,14 @@ def populateRepliesJson(baseDir: str,nickname: str,domain: str,postRepliesFilena
if os.path.isfile(searchFilename):
if authorized or \
'https://www.w3.org/ns/activitystreams#Public' in open(searchFilename).read():
with open(searchFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
loadedPost=False
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 authorized 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 \
'https://www.w3.org/ns/activitystreams#Public' in open(searchFilename).read():
# get the json of the reply and append it to the collection
with open(searchFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
loadedPost=False
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 authorized 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()
return emojiForm
with open(emojiLookupFilename, 'r') as fp:
emojiJson=commentjson.load(fp)
loadedEmoji=False
try:
with open(emojiLookupFilename, 'r') as fp:
emojiJson=commentjson.load(fp)
loadedEmoji=True
except Exception as e:
print(e)
if loadedEmoji:
results={}
for emojiName,filename in emojiJson.items():
if searchStr in emojiName:
@ -206,8 +212,16 @@ def htmlSearchSharedItems(translate: {}, \
sharesFilename=baseDir+'/accounts/'+handle+'/shares.json'
if not os.path.isfile(sharesFilename):
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():
matched=True
for searchSubstr in searchStrLowerList:
@ -360,8 +374,14 @@ def htmlHashtagSearch(translate: {}, \
if not postFilename:
index-=1
continue
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 isPublicPost(postJsonObject):
index-=1
continue
@ -401,8 +421,14 @@ def htmlSkillsSearch(translate: {},baseDir: str, \
if f.startswith('inbox@'):
continue
actorFilename = os.path.join(subdir, f)
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
loadedActor=False
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 \
actorJson.get('skills') and \
actorJson.get('name') and \
@ -430,8 +456,14 @@ def htmlSkillsSearch(translate: {},baseDir: str, \
if f.startswith('inbox@'):
continue
actorFilename = os.path.join(subdir, f)
with open(actorFilename, 'r') as fp:
cachedActorJson=commentjson.load(fp)
loadedActor=False
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'):
actorJson=cachedActorJson['actor']
if actorJson.get('id') and \
@ -504,8 +536,14 @@ def htmlEditProfile(translate: {},baseDir: str,path: str,domain: str,port: int)
displayNickname=nickname
bioStr=''
manuallyApprovesFollowers=''
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
loadedActor=False
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'):
displayNickname=actorJson['name']
if actorJson.get('summary'):
@ -1501,8 +1539,14 @@ def followerApprovalActive(baseDir: str,nickname: str,domain: str) -> bool:
manuallyApprovesFollowers=False
actorFilename=baseDir+'/accounts/'+nickname+'@'+domain+'.json'
if os.path.isfile(actorFilename):
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
loadedActor=False
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'):
manuallyApprovesFollowers=actorJson['manuallyApprovesFollowers']
return manuallyApprovesFollowers
@ -1576,9 +1620,12 @@ def individualPostAsHtml(iconsDir: str,translate: {}, \
if not os.path.isfile(announceFilename+'.reject'):
if os.path.isfile(announceFilename):
print('Reading cached Announce content for '+postJsonObject['object'])
with open(announceFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
isAnnounced=True
try:
with open(announceFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
isAnnounced=True
except Exception as e:
print(e)
else:
print('Downloading Announce content for '+postJsonObject['object'])
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):
return 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:
return None
if not sharesJson.get(shareName):
@ -2258,8 +2310,15 @@ def htmlDeletePost(translate,pageNumber: int, \
postFilename=locatePost(baseDir,nickname,domain,messageId)
if not postFilename:
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 not os.path.isfile(baseDir+'/accounts/delete-background.png'):