diff --git a/announce.py b/announce.py index 54b72c8d..c3468efe 100644 --- a/announce.py +++ b/announce.py @@ -114,11 +114,16 @@ def undoAnnounceCollectionEntry(postFilename: str,actor: str,debug: bool) -> Non del postJsonObject['object']['shares'] else: postJsonObject['object']['shares']['totalItems']=len(postJsonObject['object']['shares']['items']) - try: - with open(postFilename, 'w') as fp: - commentjson.dump(postJsonObject, fp, indent=4, sort_keys=False) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(postFilename, 'w') as fp: + commentjson.dump(postJsonObject, fp, indent=4, sort_keys=False) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 def updateAnnounceCollection(postFilename: str,actor: str,debug: bool) -> None: """Updates the announcements collection within a post @@ -134,7 +139,7 @@ def updateAnnounceCollection(postFilename: str,actor: str,debug: bool) -> None: break except Exception as e: print(e) - time.sleep(2) + time.sleep(1) tries+=1 if postJsonObject: if not postJsonObject.get('object'): @@ -178,11 +183,16 @@ def updateAnnounceCollection(postFilename: str,actor: str,debug: bool) -> None: if debug: print('DEBUG: saving post with shares (announcements) added') pprint(postJsonObject) - try: - with open(postFilename, 'w') as fp: - commentjson.dump(postJsonObject, fp, indent=4, sort_keys=False) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(postFilename, 'w') as fp: + commentjson.dump(postJsonObject, fp, indent=4, sort_keys=False) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 def announcedByPerson(postJsonObject: {}, nickname: str,domain: str) -> bool: """Returns True if the given post is announced by the given person @@ -245,11 +255,16 @@ def createAnnounce(session,baseDir: str,federationList: [], \ if saveToFile: outboxDir = createOutboxDir(nickname,domain,baseDir) filename=outboxDir+'/'+newAnnounceId.replace('/','#')+'.json' - try: - with open(filename, 'w') as fp: - commentjson.dump(newAnnounce, fp, indent=4, sort_keys=False) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(filename, 'w') as fp: + commentjson.dump(newAnnounce, fp, indent=4, sort_keys=False) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 announceNickname=None announceDomain=None diff --git a/availability.py b/availability.py index e91e07ca..2fe8acd8 100644 --- a/availability.py +++ b/availability.py @@ -36,15 +36,20 @@ def setAvailability(baseDir: str,nickname: str,domain: str, \ break except Exception as e: print(e) - time.sleep(2) + time.sleep(1) tries+=1 if actorJson: actorJson['availability']=status - try: - with open(actorFilename, 'w') as fp: - commentjson.dump(actorJson, fp, indent=4, sort_keys=False) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(actorFilename, 'w') as fp: + commentjson.dump(actorJson, fp, indent=4, sort_keys=False) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 return True def getAvailability(baseDir: str,nickname: str,domain: str) -> str: @@ -62,7 +67,7 @@ def getAvailability(baseDir: str,nickname: str,domain: str) -> str: break except Exception as e: print(e) - time.sleep(2) + time.sleep(1) tries+=1 if actorJson: if not actorJson.get('availability'): diff --git a/cache.py b/cache.py index 8613372b..69bbfef7 100644 --- a/cache.py +++ b/cache.py @@ -26,11 +26,16 @@ def storePersonInCache(baseDir: str,personUrl: str,personJson: {},personCache: { if os.path.isdir(baseDir+'/cache/actors'): cacheFilename=baseDir+'/cache/actors/'+personUrl.replace('/','#')+'.json' if not os.path.isfile(cacheFilename): - try: - with open(cacheFilename, 'w') as fp: - commentjson.dump(personJson, fp, indent=4, sort_keys=False) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(cacheFilename, 'w') as fp: + commentjson.dump(personJson, fp, indent=4, sort_keys=False) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 def getPersonFromCache(baseDir: str,personUrl: str,personCache: {}) -> {}: """Get an actor from the cache @@ -50,7 +55,7 @@ def getPersonFromCache(baseDir: str,personUrl: str,personCache: {}) -> {}: except Exception as e: print('ERROR: unable to load actor from cache '+cacheFilename) print(e) - time.sleep(2) + time.sleep(1) tries+=1 if personJson: storePersonInCache(baseDir,personUrl,personJson,personCache) diff --git a/capabilities.py b/capabilities.py index a3690f14..f79dce3b 100644 --- a/capabilities.py +++ b/capabilities.py @@ -127,11 +127,16 @@ def capabilitiesAccept(baseDir: str,httpPrefix: str, \ # if the capability already exists then load it from file if os.path.isfile(ocapFilename): - try: - with open(ocapFilename, 'r') as fp: - ocapAccept=commentjson.load(fp) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(ocapFilename, 'r') as fp: + ocapAccept=commentjson.load(fp) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 # otherwise create a new capability if not ocapAccept: acceptedActorNickname=getNicknameFromActor(acceptedActor) @@ -155,11 +160,16 @@ def capabilitiesAccept(baseDir: str,httpPrefix: str, \ ocapAccept['actor']=httpPrefix+"://"+fullDomain+'/users/'+nickname if saveToFile: - try: - with open(ocapFilename, 'w') as fp: - commentjson.dump(ocapAccept, fp, indent=4, sort_keys=False) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(ocapFilename, 'w') as fp: + commentjson.dump(ocapAccept, fp, indent=4, sort_keys=False) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 return ocapAccept def capabilitiesGrantedSave(baseDir :str,nickname :str,domain :str,ocap: {}) -> bool: @@ -171,11 +181,16 @@ def capabilitiesGrantedSave(baseDir :str,nickname :str,domain :str,ocap: {}) -> ocapFilename=getOcapFilename(baseDir,nickname,domain,ocap['actor'],'granted') if not ocapFilename: return False - try: - with open(ocapFilename, 'w') as fp: - commentjson.dump(ocap, fp, indent=4, sort_keys=False) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(ocapFilename, 'w') as fp: + commentjson.dump(ocap, fp, indent=4, sort_keys=False) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 return True def capabilitiesUpdate(baseDir: str,httpPrefix: str, \ @@ -217,11 +232,16 @@ def capabilitiesUpdate(baseDir: str,httpPrefix: str, \ } # read the existing capability - try: - with open(ocapFilename, 'r') as fp: - ocapJson=commentjson.load(fp) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(ocapFilename, 'r') as fp: + ocapJson=commentjson.load(fp) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 # set the new capabilities list. eg. ["inbox:write","objects:read"] ocapJson['capability']=updateCaps @@ -240,11 +260,16 @@ def capabilitiesUpdate(baseDir: str,httpPrefix: str, \ ocapUpdate['object']=ocapJson # save it again - try: - with open(ocapFilename, 'w') as fp: - commentjson.dump(ocapJson, fp, indent=4, sort_keys=False) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(ocapFilename, 'w') as fp: + commentjson.dump(ocapJson, fp, indent=4, sort_keys=False) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 return ocapUpdate @@ -267,20 +292,29 @@ def capabilitiesReceiveUpdate(baseDir :str, \ return False ocapJson=None - try: - with open(ocapFilename, 'r') as fp: - ocapJson=commentjson.load(fp) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(ocapFilename, 'r') as fp: + ocapJson=commentjson.load(fp) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 if ocapJson: ocapJson['id']=newCapabilitiesId ocapJson['capability']=capabilityList - try: - with open(ocapFilename, 'w') as fp: - commentjson.dump(ocapJson, fp, indent=4, sort_keys=False) - return True - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(ocapFilename, 'w') as fp: + commentjson.dump(ocapJson, fp, indent=4, sort_keys=False) + return True + except Exception as e: + print(e) + time.sleep(1) + tries+=1 return False diff --git a/config.py b/config.py index cd72025a..49b8d274 100644 --- a/config.py +++ b/config.py @@ -7,6 +7,7 @@ __email__ = "bob@freedombone.net" __status__ = "Production" import os +import time import json import commentjson @@ -18,39 +19,59 @@ def createConfig(baseDir: str) -> None: return configJson = { } - try: - with open(configFilename, 'w') as fp: - commentjson.dump(configJson, fp, indent=4, sort_keys=False) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(configFilename, 'w') as fp: + commentjson.dump(configJson, fp, indent=4, sort_keys=False) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 def setConfigParam(baseDir: str, variableName: str, variableValue) -> None: """Sets a configuration value """ createConfig(baseDir) configFilename=baseDir+'/config.json' - try: - with open(configFilename, 'r') as fp: - configJson=commentjson.load(fp) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(configFilename, 'r') as fp: + configJson=commentjson.load(fp) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 configJson[variableName]=variableValue - try: - with open(configFilename, 'w') as fp: - commentjson.dump(configJson, fp, indent=4, sort_keys=False) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(configFilename, 'w') as fp: + commentjson.dump(configJson, fp, indent=4, sort_keys=False) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 def getConfigParam(baseDir: str, variableName: str): """Gets a configuration value """ createConfig(baseDir) configFilename=baseDir+'/config.json' - try: - with open(configFilename, 'r') as fp: - configJson=commentjson.load(fp) - if configJson.get(variableName): - return configJson[variableName] - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(configFilename, 'r') as fp: + configJson=commentjson.load(fp) + if configJson.get(variableName): + return configJson[variableName] + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 return None diff --git a/content.py b/content.py index 9a1e1975..048ef6f6 100644 --- a/content.py +++ b/content.py @@ -322,7 +322,7 @@ def addHtmlTags(baseDir: str,httpPrefix: str, \ try: with open(baseDir+'/emoji/emoji.json', 'r') as fp: emojiDict=commentjson.load(fp) - break + break except Exception as e: print('Failed to load emoji: '+baseDir+'/emoji/emoji.json '+str(e)) time.sleep(1) diff --git a/daemon.py b/daemon.py index 6b347426..d2c9edb5 100644 --- a/daemon.py +++ b/daemon.py @@ -1468,12 +1468,17 @@ class PubServer(BaseHTTPRequestHandler): if os.path.isfile(postFilename): postJsonObject={} loadedPost=False - try: - with open(postFilename, 'r') as fp: - postJsonObject=commentjson.load(fp) - loadedPost=True - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(postFilename, 'r') as fp: + postJsonObject=commentjson.load(fp) + loadedPost=True + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 if loadedPost: # Only authorized viewers get to see likes on posts # Otherwize marketers could gain more social graph info @@ -1617,12 +1622,17 @@ class PubServer(BaseHTTPRequestHandler): actorFilename=self.server.baseDir+'/accounts/'+nickname+'@'+self.server.domain+'.json' if os.path.isfile(actorFilename): loadedActor=False - try: - with open(actorFilename, 'r') as fp: - actorJson=commentjson.load(fp) - loadedActor=True - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(actorFilename, 'r') as fp: + actorJson=commentjson.load(fp) + loadedActor=True + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 if loadedActor: if actorJson.get('roles'): if self._requestHTTP(): @@ -1663,12 +1673,17 @@ class PubServer(BaseHTTPRequestHandler): actorFilename=self.server.baseDir+'/accounts/'+nickname+'@'+self.server.domain+'.json' if os.path.isfile(actorFilename): loadedActor=False - try: - with open(actorFilename, 'r') as fp: - actorJson=commentjson.load(fp) - loadedActor=True - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(actorFilename, 'r') as fp: + actorJson=commentjson.load(fp) + loadedActor=True + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 if loadedActor: if actorJson.get('skills'): if self._requestHTTP(): @@ -1719,12 +1734,17 @@ class PubServer(BaseHTTPRequestHandler): if os.path.isfile(postFilename): postJsonObject={} readPost=False - try: - with open(postFilename, 'r') as fp: - postJsonObject=commentjson.load(fp) - readPost=True - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(postFilename, 'r') as fp: + postJsonObject=commentjson.load(fp) + readPost=True + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 if not readPost: self.send_response(429) self.end_headers() @@ -3057,19 +3077,29 @@ class PubServer(BaseHTTPRequestHandler): os.remove(allowedInstancesFilename) # save actor json file within accounts if actorChanged: - try: - with open(actorFilename, 'w') as fp: - commentjson.dump(actorJson, fp, indent=4, sort_keys=False) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(actorFilename, 'w') as fp: + commentjson.dump(actorJson, fp, indent=4, sort_keys=False) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 # also copy to the actors cache and personCache in memory storePersonInCache(self.server.baseDir,actorJson['id'],actorJson,self.server.personCache) actorCacheFilename=self.server.baseDir+'/cache/actors/'+actorJson['id'].replace('/','#')+'.json' - try: - with open(actorCacheFilename, 'w') as fp: - commentjson.dump(actorJson, fp, indent=4, sort_keys=False) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(actorCacheFilename, 'w') as fp: + commentjson.dump(actorJson, fp, indent=4, sort_keys=False) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 # send actor update to followers updateActorJson={ 'type': 'Update', diff --git a/follow.py b/follow.py index d03b76a4..c24d426f 100644 --- a/follow.py +++ b/follow.py @@ -411,12 +411,16 @@ def storeFollowRequest(baseDir: str, \ if not os.path.isdir(requestsDir): os.mkdir(requestsDir) followActivityfilename=requestsDir+'/'+approveHandle+'.follow' - try: - with open(followActivityfilename, 'w') as fp: - commentjson.dump(followJson, fp, indent=4, sort_keys=False) - return True - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(followActivityfilename, 'w') as fp: + commentjson.dump(followJson, fp, indent=4, sort_keys=False) + return True + except Exception as e: + print(e) + time.sleep(1) + tries+=1 return False def receiveFollowRequest(session,baseDir: str,httpPrefix: str, \ diff --git a/inbox.py b/inbox.py index 5fa0c2dd..1618b811 100644 --- a/inbox.py +++ b/inbox.py @@ -285,11 +285,16 @@ def savePostToInboxQueue(baseDir: str,httpPrefix: str, \ if debug: print('Inbox queue item created') pprint(newQueueItem) - try: - with open(filename, 'w') as fp: - commentjson.dump(newQueueItem, fp, indent=4, sort_keys=False) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(filename, 'w') as fp: + commentjson.dump(newQueueItem, fp, indent=4, sort_keys=False) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 return filename def inboxCheckCapabilities(baseDir :str,nickname :str,domain :str, \ @@ -691,12 +696,17 @@ def personReceiveUpdate(baseDir: str, \ # save to cache in memory storePersonInCache(baseDir,personJson['id'],personJson,personCache) # save to cache on file - try: - with open(actorFilename, 'w') as fp: - commentjson.dump(personJson, fp, indent=4, sort_keys=False) - print('actor updated for '+personJson['id']) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(actorFilename, 'w') as fp: + commentjson.dump(personJson, fp, indent=4, sort_keys=False) + print('actor updated for '+personJson['id']) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 # remove avatar if it exists so that it will be refreshed later # when a timeline is constructed @@ -1546,11 +1556,16 @@ def inboxAfterCapabilities(session,keyId: str,handle: str,messageJson: {}, \ obtainAvatarForReplyPost(session,baseDir,httpPrefix,domain,personCache,postJsonObject,debug) # save the post to file - try: - with open(destinationFilename, 'w+') as fp: - commentjson.dump(postJsonObject, fp, indent=4, sort_keys=False) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(destinationFilename, 'w+') as fp: + commentjson.dump(postJsonObject, fp, indent=4, sort_keys=False) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 inboxUpdateCalendar(baseDir,handle,postJsonObject) @@ -1903,12 +1918,17 @@ def runInboxQueue(projectVersion: str, \ if len(recipientsDictFollowers)>0: sharedInboxPostFilename=queueJson['destination'].replace(inboxHandle,inboxHandle) if not os.path.isfile(sharedInboxPostFilename): - try: - with open(sharedInboxPostFilename, 'w') as fp: - commentjson.dump(queueJson['post'],fp,indent=4, \ - sort_keys=False) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(sharedInboxPostFilename, 'w') as fp: + commentjson.dump(queueJson['post'],fp,indent=4, \ + sort_keys=False) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 # for posts addressed to specific accounts for handle,capsId in recipientsDict.items(): diff --git a/like.py b/like.py index 1986b4dd..ed671a8e 100644 --- a/like.py +++ b/like.py @@ -72,11 +72,16 @@ def undoLikesCollectionEntry(postFilename: str,objectUrl: str,actor: str,debug: del postJsonObject['object']['likes'] else: postJsonObject['object']['likes']['totalItems']=len(postJsonObject['likes']['items']) - try: - with open(postFilename, 'w') as fp: - commentjson.dump(postJsonObject, fp, indent=4, sort_keys=False) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(postFilename, 'w') as fp: + commentjson.dump(postJsonObject, fp, indent=4, sort_keys=False) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 def likedByPerson(postJsonObject: {}, nickname: str,domain: str) -> bool: """Returns True if the given post is liked by the given person @@ -159,11 +164,16 @@ def updateLikesCollection(postFilename: str,objectUrl: str, actor: str,debug: bo if debug: print('DEBUG: saving post with likes added') pprint(postJsonObject) - try: - with open(postFilename, 'w') as fp: - commentjson.dump(postJsonObject, fp, indent=4, sort_keys=False) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(postFilename, 'w') as fp: + commentjson.dump(postJsonObject, fp, indent=4, sort_keys=False) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 def like(session,baseDir: str,federationList: [],nickname: str,domain: str,port: int, \ ccList: [],httpPrefix: str,objectUrl: str,clientToServer: bool, \ diff --git a/manualapprove.py b/manualapprove.py index 35acac9a..dae262c1 100644 --- a/manualapprove.py +++ b/manualapprove.py @@ -9,7 +9,7 @@ __status__ = "Production" import os import json import commentjson - +import time from follow import followedAccountAccepts from follow import followedAccountRejects from follow import removeFromFollowRequests diff --git a/person.py b/person.py index 0325c73b..e985918d 100644 --- a/person.py +++ b/person.py @@ -106,11 +106,16 @@ def setProfileImage(baseDir: str,httpPrefix :str,nickname: str,domain: str, \ if personJson: personJson[iconFilenameBase]['mediaType']=mediaType personJson[iconFilenameBase]['url']=httpPrefix+'://'+fullDomain+'/users/'+nickname+'/'+iconFilename - try: - with open(personFilename, 'w') as fp: - commentjson.dump(personJson, fp, indent=4, sort_keys=False) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(personFilename, 'w') as fp: + commentjson.dump(personJson, fp, indent=4, sort_keys=False) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 cmd = '/usr/bin/convert '+imageFilename+' -size '+resolution+' -quality 50 '+profileFilename subprocess.call(cmd, shell=True) @@ -145,11 +150,16 @@ def setOrganizationScheme(baseDir: str,nickname: str,domain: str, \ if actorJson: actorJson['orgSchema']=schema - try: - with open(actorFilename, 'w') as fp: - commentjson.dump(actorJson, fp, indent=4, sort_keys=False) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(actorFilename, 'w') as fp: + commentjson.dump(actorJson, fp, indent=4, sort_keys=False) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 return True def accountExists(baseDir: str,nickname: str,domain: str) -> bool: @@ -269,11 +279,16 @@ def createPersonBase(baseDir: str,nickname: str,domain: str,port: int, \ if not os.path.isdir(baseDir+peopleSubdir+'/'+handle+'/queue'): os.mkdir(baseDir+peopleSubdir+'/'+handle+'/queue') filename=baseDir+peopleSubdir+'/'+handle+'.json' - try: - with open(filename, 'w') as fp: - commentjson.dump(newPerson, fp, indent=4, sort_keys=False) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(filename, 'w') as fp: + commentjson.dump(newPerson, fp, indent=4, sort_keys=False) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 # save to cache if not os.path.isdir(baseDir+'/cache'): @@ -281,11 +296,16 @@ def createPersonBase(baseDir: str,nickname: str,domain: str,port: int, \ if not os.path.isdir(baseDir+'/cache/actors'): os.mkdir(baseDir+'/cache/actors') cacheFilename=baseDir+'/cache/actors/'+newPerson['id'].replace('/','#')+'.json' - try: - with open(cacheFilename, 'w') as fp: - commentjson.dump(newPerson, fp, indent=4, sort_keys=False) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(cacheFilename, 'w') as fp: + commentjson.dump(newPerson, fp, indent=4, sort_keys=False) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 # save the private key privateKeysSubdir='/keys/private' @@ -442,7 +462,7 @@ def personLookup(domain: str,path: str,baseDir: str) -> {}: break except: print('WARN: Failed to load actor '+filename) - time.sleep(2) + time.sleep(1) tries+=1 if tries>=5: return None @@ -572,11 +592,16 @@ def setDisplayNickname(baseDir: str,nickname: str, domain: str, \ if not personJson: return False personJson['name']=displayName - try: - with open(filename, 'w') as fp: - commentjson.dump(personJson, fp, indent=4, sort_keys=False) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(filename, 'w') as fp: + commentjson.dump(personJson, fp, indent=4, sort_keys=False) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 return True def setBio(baseDir: str,nickname: str, domain: str, bio: str) -> bool: @@ -605,11 +630,16 @@ def setBio(baseDir: str,nickname: str, domain: str, bio: str) -> bool: return False personJson['summary']=bio - try: - with open(filename, 'w') as fp: - commentjson.dump(personJson, fp, indent=4, sort_keys=False) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(filename, 'w') as fp: + commentjson.dump(personJson, fp, indent=4, sort_keys=False) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 return True diff --git a/posts.py b/posts.py index 6c062d80..346d05b7 100644 --- a/posts.py +++ b/posts.py @@ -415,11 +415,16 @@ def savePostToBox(baseDir: str,httpPrefix: str,postId: str, \ boxDir = createPersonDir(nickname,domain,baseDir,boxname) filename=boxDir+'/'+postId.replace('/','#')+'.json' - try: - with open(filename, 'w') as fp: - commentjson.dump(postJsonObject, fp, indent=4, sort_keys=False) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(filename, 'w') as fp: + commentjson.dump(postJsonObject, fp, indent=4, sort_keys=False) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 return filename def updateHashtagsIndex(baseDir: str,tag: {},newPostId: str) -> None: @@ -2388,10 +2393,14 @@ def downloadAnnounce(session,baseDir: str,httpPrefix: str,nickname: str,domain: rejectAnnounce(announceFilename) return None postJsonObject=announcedJson - try: - with open(announceFilename, 'w') as fp: - commentjson.dump(postJsonObject, fp, indent=4, sort_keys=False) - return postJsonObject - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(announceFilename, 'w') as fp: + commentjson.dump(postJsonObject, fp, indent=4, sort_keys=False) + return postJsonObject + except Exception as e: + print(e) + time.sleep(1) + tries+=1 return None diff --git a/roles.py b/roles.py index d67ec689..40e71a02 100644 --- a/roles.py +++ b/roles.py @@ -9,6 +9,7 @@ __status__ = "Production" import json import commentjson import os +import time from webfinger import webfingerHandle from auth import createBasicAuthHeader from posts import getPersonBox @@ -29,21 +30,31 @@ def clearModeratorStatus(baseDir: str) -> None: filename=os.path.join(baseDir+'/accounts/', filename) if '"moderator"' in open(filename).read(): actorJson=None - try: - with open(filename, 'r') as fp: - actorJson=commentjson.load(fp) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(filename, 'r') as fp: + actorJson=commentjson.load(fp) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 if actorJson: if actorJson['roles'].get('instance'): if 'moderator' in actorJson['roles']['instance']: actorJson['roles']['instance'].remove('moderator') - try: - with open(filename, 'w') as fp: - commentjson.dump(actorJson, fp, indent=4, sort_keys=False) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(filename, 'w') as fp: + commentjson.dump(actorJson, fp, indent=4, sort_keys=False) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 def addModerator(baseDir: str,nickname: str,domain: str) -> None: """Adds a moderator nickname to the file @@ -98,11 +109,16 @@ def setRole(baseDir: str,nickname: str,domain: str, \ return False actorJson=None - try: - with open(actorFilename, 'r') as fp: - actorJson=commentjson.load(fp) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(actorFilename, 'r') as fp: + actorJson=commentjson.load(fp) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 if actorJson: if role: @@ -123,11 +139,16 @@ def setRole(baseDir: str,nickname: str,domain: str, \ # if the project contains no roles then remove it if len(actorJson['roles'][project])==0: del actorJson['roles'][project] - try: - with open(actorFilename, 'w') as fp: - commentjson.dump(actorJson, fp, indent=4, sort_keys=False) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(actorFilename, 'w') as fp: + commentjson.dump(actorJson, fp, indent=4, sort_keys=False) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 return True def getRoles(baseDir: str,nickname: str,domain: str, \ @@ -139,11 +160,16 @@ def getRoles(baseDir: str,nickname: str,domain: str, \ return False actorJson=None - try: - with open(actorFilename, 'r') as fp: - actorJson=commentjson.load(fp) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(actorFilename, 'r') as fp: + actorJson=commentjson.load(fp) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 if actorJson: if not actorJson.get('roles'): diff --git a/shares.py b/shares.py index 82702ffa..1ac0e942 100644 --- a/shares.py +++ b/shares.py @@ -26,11 +26,16 @@ def removeShare(baseDir: str,nickname: str,domain: str, \ """ sharesFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/shares.json' if os.path.isfile(sharesFilename): - try: - with open(sharesFilename, 'r') as fp: - sharesJson=commentjson.load(fp) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(sharesFilename, 'r') as fp: + sharesJson=commentjson.load(fp) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 itemID=displayName.replace(' ','') if sharesJson.get(itemID): @@ -45,11 +50,16 @@ def removeShare(baseDir: str,nickname: str,domain: str, \ os.remove(itemIDfile+'.gif') # remove the item itself del sharesJson[itemID] - try: - with open(sharesFilename, 'w') as fp: - commentjson.dump(sharesJson, fp, indent=4, sort_keys=False) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(sharesFilename, 'w') as fp: + commentjson.dump(sharesJson, fp, indent=4, sort_keys=False) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 def addShare(baseDir: str, \ httpPrefix: str,nickname: str,domain: str,port: int, \ @@ -66,11 +76,16 @@ def addShare(baseDir: str, \ sharesFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/shares.json' sharesJson={} if os.path.isfile(sharesFilename): - try: - with open(sharesFilename, 'r') as fp: - sharesJson=commentjson.load(fp) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(sharesFilename, 'r') as fp: + sharesJson=commentjson.load(fp) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 duration=duration.lower() durationSec=0 @@ -146,11 +161,16 @@ def addShare(baseDir: str, \ "expire": durationSec } - try: - with open(sharesFilename, 'w') as fp: - commentjson.dump(sharesJson, fp, indent=4, sort_keys=False) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(sharesFilename, 'w') as fp: + commentjson.dump(sharesJson, fp, indent=4, sort_keys=False) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 def expireShares(baseDir: str,nickname: str,domain: str) -> None: """Removes expired items from shares @@ -162,11 +182,16 @@ def expireShares(baseDir: str,nickname: str,domain: str) -> None: sharesFilename=baseDir+'/accounts/'+handle+'/shares.json' if os.path.isfile(sharesFilename): sharesJson=None - try: - with open(sharesFilename, 'r') as fp: - sharesJson=commentjson.load(fp) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(sharesFilename, 'r') as fp: + sharesJson=commentjson.load(fp) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 if sharesJson: currTime=int(time.time()) deleteItemID=[] @@ -184,11 +209,16 @@ def expireShares(baseDir: str,nickname: str,domain: str) -> None: os.remove(itemIDfile+'.jpg') if os.path.isfile(itemIDfile+'.gif'): os.remove(itemIDfile+'.gif') - try: - with open(sharesFilename, 'w') as fp: - commentjson.dump(sharesJson, fp, indent=4, sort_keys=False) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(sharesFilename, 'w') as fp: + commentjson.dump(sharesJson, fp, indent=4, sort_keys=False) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 def getSharesFeedForPerson(baseDir: str, \ domain: str,port: int, \ @@ -239,12 +269,17 @@ def getSharesFeedForPerson(baseDir: str, \ if headerOnly: noOfShares=0 if os.path.isfile(sharesFilename): - try: - with open(sharesFilename, 'r') as fp: - sharesJson=commentjson.load(fp) - noOfShares=len(sharesJson.items()) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(sharesFilename, 'r') as fp: + sharesJson=commentjson.load(fp) + noOfShares=len(sharesJson.items()) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 shares = { '@context': 'https://www.w3.org/ns/activitystreams', 'first': httpPrefix+'://'+domain+'/users/'+nickname+'/shares?page=1', @@ -273,11 +308,16 @@ def getSharesFeedForPerson(baseDir: str, \ totalCtr=0 sharesJson=None - try: - with open(sharesFilename, 'r') as fp: - sharesJson=commentjson.load(fp) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(sharesFilename, 'r') as fp: + sharesJson=commentjson.load(fp) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 if sharesJson: for itemID,item in sharesJson.items(): diff --git a/skills.py b/skills.py index fd787aeb..64d621f4 100644 --- a/skills.py +++ b/skills.py @@ -9,6 +9,7 @@ __status__ = "Production" import json import commentjson import os +import time from webfinger import webfingerHandle from auth import createBasicAuthHeader from posts import getPersonBox @@ -28,11 +29,16 @@ def setSkillLevel(baseDir: str,nickname: str,domain: str, \ return False actorJson=None - try: - with open(actorFilename, 'r') as fp: - actorJson=commentjson.load(fp) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(actorFilename, 'r') as fp: + actorJson=commentjson.load(fp) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 if actorJson: if not actorJson.get('skills'): @@ -41,11 +47,16 @@ def setSkillLevel(baseDir: str,nickname: str,domain: str, \ actorJson['skills'][skill]=skillLevelPercent else: del actorJson['skills'][skill] - try: - with open(actorFilename, 'w') as fp: - commentjson.dump(actorJson, fp, indent=4, sort_keys=False) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(actorFilename, 'w') as fp: + commentjson.dump(actorJson, fp, indent=4, sort_keys=False) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 return True def setSkills(baseDir: str,nickname: str,domain: str,skills: {}) -> None: @@ -54,19 +65,29 @@ def setSkills(baseDir: str,nickname: str,domain: str,skills: {}) -> None: return False actorJson=None - try: - with open(actorFilename, 'r') as fp: - actorJson=commentjson.load(fp) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(actorFilename, 'r') as fp: + actorJson=commentjson.load(fp) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 if actorJson: actorJson['skills']=skills - try: - with open(actorFilename, 'w') as fp: - commentjson.dump(actorJson, fp, indent=4, sort_keys=False) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(actorFilename, 'w') as fp: + commentjson.dump(actorJson, fp, indent=4, sort_keys=False) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 def getSkills(baseDir: str,nickname: str,domain: str) -> []: """Returns the skills for a given person @@ -76,11 +97,16 @@ def getSkills(baseDir: str,nickname: str,domain: str) -> []: return False actorJson=None - try: - with open(actorFilename, 'r') as fp: - actorJson=commentjson.load(fp) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(actorFilename, 'r') as fp: + actorJson=commentjson.load(fp) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 if actorJson: if not actorJson.get('skills'): diff --git a/webfinger.py b/webfinger.py index b4b869f5..5a5e6c17 100644 --- a/webfinger.py +++ b/webfinger.py @@ -92,19 +92,29 @@ def storeWebfingerEndpoint(nickname: str,domain: str,port: int,baseDir: str, \ if not os.path.isdir(baseDir+wfSubdir): os.mkdir(baseDir+wfSubdir) filename=baseDir+wfSubdir+'/'+handle.lower()+'.json' - try: - with open(filename, 'w') as fp: - commentjson.dump(wfJson, fp, indent=4, sort_keys=False) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(filename, 'w') as fp: + commentjson.dump(wfJson, fp, indent=4, sort_keys=False) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 if nickname=='inbox': handle=originalDomain+'@'+domain filename=baseDir+wfSubdir+'/'+handle.lower()+'.json' - try: - with open(filename, 'w') as fp: - commentjson.dump(wfJson, fp, indent=4, sort_keys=False) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(filename, 'w') as fp: + commentjson.dump(wfJson, fp, indent=4, sort_keys=False) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 return True def createWebfingerEndpoint(nickname: str,domain: str,port: int, \ diff --git a/webinterface.py b/webinterface.py index 2a8886f5..6a391605 100644 --- a/webinterface.py +++ b/webinterface.py @@ -150,12 +150,17 @@ def htmlSearchEmoji(translate: {},baseDir: str,searchStr: str) -> str: return emojiForm loadedEmoji=False - try: - with open(emojiLookupFilename, 'r') as fp: - emojiJson=commentjson.load(fp) - loadedEmoji=True - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(emojiLookupFilename, 'r') as fp: + emojiJson=commentjson.load(fp) + loadedEmoji=True + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 if loadedEmoji: results={} for emojiName,filename in emojiJson.items(): @@ -219,11 +224,16 @@ def htmlSearchSharedItems(translate: {}, \ continue sharesJson=None - try: - with open(sharesFilename, 'r') as fp: - sharesJson=commentjson.load(fp) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(sharesFilename, 'r') as fp: + sharesJson=commentjson.load(fp) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 if not sharesJson: continue @@ -380,12 +390,17 @@ def htmlHashtagSearch(translate: {}, \ index-=1 continue loadedPost=False - try: - with open(postFilename, 'r') as fp: - postJsonObject=commentjson.load(fp) - loadedPost=True - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(postFilename, 'r') as fp: + postJsonObject=commentjson.load(fp) + loadedPost=True + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 if loadedPost: if not isPublicPost(postJsonObject): index-=1 @@ -427,12 +442,17 @@ def htmlSkillsSearch(translate: {},baseDir: str, \ continue actorFilename = os.path.join(subdir, f) loadedActor=False - try: - with open(actorFilename, 'r') as fp: - actorJson=commentjson.load(fp) - loadedActor=True - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(actorFilename, 'r') as fp: + actorJson=commentjson.load(fp) + loadedActor=True + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 if loadedActor: if actorJson.get('id') and \ actorJson.get('skills') and \ @@ -462,12 +482,17 @@ def htmlSkillsSearch(translate: {},baseDir: str, \ continue actorFilename = os.path.join(subdir, f) loadedActor=False - try: - with open(actorFilename, 'r') as fp: - cachedActorJson=commentjson.load(fp) - loadedActor=True - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(actorFilename, 'r') as fp: + cachedActorJson=commentjson.load(fp) + loadedActor=True + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 if loadedActor: if cachedActorJson.get('actor'): actorJson=cachedActorJson['actor'] @@ -543,12 +568,17 @@ def htmlEditProfile(translate: {},baseDir: str,path: str,domain: str,port: int) bioStr='' manuallyApprovesFollowers='' loadedActor=False - try: - with open(actorFilename, 'r') as fp: - actorJson=commentjson.load(fp) - loadedActor=True - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(actorFilename, 'r') as fp: + actorJson=commentjson.load(fp) + loadedActor=True + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 if loadedActor: if actorJson.get('name'): displayNickname=actorJson['name'] @@ -1588,12 +1618,17 @@ def followerApprovalActive(baseDir: str,nickname: str,domain: str) -> bool: actorFilename=baseDir+'/accounts/'+nickname+'@'+domain+'.json' if os.path.isfile(actorFilename): loadedActor=False - try: - with open(actorFilename, 'r') as fp: - actorJson=commentjson.load(fp) - loadedActor=True - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(actorFilename, 'r') as fp: + actorJson=commentjson.load(fp) + loadedActor=True + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 if loadedActor: if actorJson.get('manuallyApprovesFollowers'): manuallyApprovesFollowers=actorJson['manuallyApprovesFollowers'] @@ -2323,12 +2358,17 @@ def htmlIndividualPost(translate: {}, \ if not postFilename: break loadedPost=False - try: - with open(postFilename, 'r') as fp: - postJsonObject=commentjson.load(fp) - loadedPost=True - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(postFilename, 'r') as fp: + postJsonObject=commentjson.load(fp) + loadedPost=True + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 if loadedPost: postStr= \ individualPostAsHtml(iconsDir,translate,None, \ @@ -2395,12 +2435,16 @@ def htmlRemoveSharedItem(translate: {},baseDir: str,actor: str,shareName: str) - if not os.path.isfile(sharesFile): return None sharesJson=None - - try: - with open(sharesFile, 'r') as fp: - sharesJson=commentjson.load(fp) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(sharesFile, 'r') as fp: + sharesJson=commentjson.load(fp) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 if not sharesJson: return None @@ -2458,11 +2502,16 @@ def htmlDeletePost(translate,pageNumber: int, \ return None postJsonObject=None - try: - with open(postFilename, 'r') as fp: - postJsonObject=commentjson.load(fp) - except Exception as e: - print(e) + tries=0 + while tries<5: + try: + with open(postFilename, 'r') as fp: + postJsonObject=commentjson.load(fp) + break + except Exception as e: + print(e) + time.sleep(1) + tries+=1 if not postJsonObject: return None @@ -2776,7 +2825,7 @@ def getCalendarEvents(baseDir: str,nickname: str,domain: str,year: int,monthNumb break except Exception as e: print(e) - time.sleep(2) + time.sleep(1) tries+=1 if postJsonObject: if postJsonObject.get('object'):