Storage functions

merge-requests/30/head
Bob Mottram 2021-06-21 22:21:43 +01:00
parent 9c18e7042e
commit feb4286031
11 changed files with 39 additions and 74 deletions

View File

@ -173,8 +173,7 @@ def storeBasicCredentials(baseDir: str, nickname: str, password: str) -> bool:
os.rename(passwordFile + '.new', passwordFile) os.rename(passwordFile + '.new', passwordFile)
else: else:
# append to password file # append to password file
with open(passwordFile, 'a+') as passfile: storeValue(passwordFile, storeStr, 'append')
passfile.write(storeStr + '\n')
else: else:
storeValue(passwordFile, storeStr, 'write') storeValue(passwordFile, storeStr, 'write')
return True return True

View File

@ -40,10 +40,7 @@ def addGlobalBlock(baseDir: str,
if blockHandle in open(blockingFilename).read(): if blockHandle in open(blockingFilename).read():
return False return False
# block an account handle or domain # block an account handle or domain
blockFile = open(blockingFilename, "a+") storeValue(blockingFilename, blockHandle, 'append')
if blockFile:
blockFile.write(blockHandle + '\n')
blockFile.close()
else: else:
blockHashtag = blockNickname blockHashtag = blockNickname
# is the hashtag already blocked? # is the hashtag already blocked?
@ -51,10 +48,7 @@ def addGlobalBlock(baseDir: str,
if blockHashtag + '\n' in open(blockingFilename).read(): if blockHashtag + '\n' in open(blockingFilename).read():
return False return False
# block a hashtag # block a hashtag
blockFile = open(blockingFilename, "a+") storeValue(blockingFilename, blockHashtag, 'append')
if blockFile:
blockFile.write(blockHashtag + '\n')
blockFile.close()
return True return True
@ -70,9 +64,7 @@ def addBlock(baseDir: str, nickname: str, domain: str,
if os.path.isfile(blockingFilename): if os.path.isfile(blockingFilename):
if blockHandle in open(blockingFilename).read(): if blockHandle in open(blockingFilename).read():
return False return False
blockFile = open(blockingFilename, "a+") storeValue(blockingFilename, blockHandle, 'append')
blockFile.write(blockHandle + '\n')
blockFile.close()
return True return True

View File

@ -432,10 +432,7 @@ class PubServer(BaseHTTPRequestHandler):
self.server.maxReplies, self.server.maxReplies,
self.server.debug) self.server.debug)
# record the vote # record the vote
votesFile = open(votesFilename, 'a+') storeValue(votesFilename, messageId, 'append')
if votesFile:
votesFile.write(messageId + '\n')
votesFile.close()
# ensure that the cached post is removed if it exists, # ensure that the cached post is removed if it exists,
# so that it then will be recreated # so that it then will be recreated

View File

@ -7,6 +7,7 @@ __email__ = "bob@freedombone.net"
__status__ = "Production" __status__ = "Production"
import os import os
from storage import storeValue
def addFilter(baseDir: str, nickname: str, domain: str, words: str) -> bool: def addFilter(baseDir: str, nickname: str, domain: str, words: str) -> bool:
@ -17,9 +18,7 @@ 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
filtersFile = open(filtersFilename, "a+") storeValue(filtersFilename, words, 'append')
filtersFile.write(words + '\n')
filtersFile.close()
return True return True
@ -35,9 +34,7 @@ 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
filtersFile = open(filtersFilename, "a+") storeValue(filtersFilename, words, 'append')
filtersFile.write(words + '\n')
filtersFile.close()
return True return True

View File

@ -276,8 +276,7 @@ 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():
with open(unfollowedFilename, "a+") as f: storeValue(unfollowedFilename, handleToUnfollow, 'append')
f.write(handleToUnfollow + '\n')
else: else:
storeValue(unfollowedFilename, handleToUnfollow, 'write') storeValue(unfollowedFilename, handleToUnfollow, 'write')
@ -599,8 +598,7 @@ 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():
with open(approveFollowsFilename, 'a+') as fp: storeValue(approveFollowsFilename, approveHandleStored, 'append')
fp.write(approveHandleStored + '\n')
else: else:
if debug: if debug:
print('DEBUG: ' + approveHandleStored + print('DEBUG: ' + approveHandleStored +

View File

@ -118,11 +118,8 @@ def saveEventPost(baseDir: str, handle: str, postId: str,
return False return False
# append the post Id to the file for the calendar month # append the post Id to the file for the calendar month
calendarFile = open(calendarFilename, 'a+') if not storeValue(calendarFilename, postId, 'append'):
if not calendarFile:
return False return False
calendarFile.write(postId + '\n')
calendarFile.close()
# create a file which will trigger a notification that # create a file which will trigger a notification that
# a new event has been added # a new event has been added
@ -245,9 +242,10 @@ def getTodaysEvents(baseDir: str, nickname: str, domain: str,
# if some posts have been deleted then regenerate the calendar file # if some posts have been deleted then regenerate the calendar file
if recreateEventsFile: if recreateEventsFile:
calendarFile = open(calendarFilename, 'w+') calendarFile = open(calendarFilename, 'w+')
for postId in calendarPostIds: if calendarFile:
calendarFile.write(postId + '\n') for postId in calendarPostIds:
calendarFile.close() calendarFile.write(postId + '\n')
calendarFile.close()
return events return events
@ -362,9 +360,10 @@ def getThisWeeksEvents(baseDir: str, nickname: str, domain: str) -> {}:
# if some posts have been deleted then regenerate the calendar file # if some posts have been deleted then regenerate the calendar file
if recreateEventsFile: if recreateEventsFile:
calendarFile = open(calendarFilename, 'w+') calendarFile = open(calendarFilename, 'w+')
for postId in calendarPostIds: if calendarFile:
calendarFile.write(postId + '\n') for postId in calendarPostIds:
calendarFile.close() calendarFile.write(postId + '\n')
calendarFile.close()
return events return events
@ -427,9 +426,10 @@ def getCalendarEvents(baseDir: str, nickname: str, domain: str,
# if some posts have been deleted then regenerate the calendar file # if some posts have been deleted then regenerate the calendar file
if recreateEventsFile: if recreateEventsFile:
calendarFile = open(calendarFilename, 'w+') calendarFile = open(calendarFilename, 'w+')
for postId in calendarPostIds: if calendarFile:
calendarFile.write(postId + '\n') for postId in calendarPostIds:
calendarFile.close() calendarFile.write(postId + '\n')
calendarFile.close()
return events return events

View File

@ -2816,10 +2816,7 @@ def _checkJsonSignature(baseDir: str, queueJson: {}) -> (bool, bool):
alreadyUnknown = True alreadyUnknown = True
if not alreadyUnknown: if not alreadyUnknown:
unknownFile = open(unknownContextsFile, "a+") storeValue(unknownContextsFile, unknownContext, 'append')
if unknownFile:
unknownFile.write(unknownContext + '\n')
unknownFile.close()
else: else:
print('Unrecognized jsonld signature type: ' + print('Unrecognized jsonld signature type: ' +
jwebsigType) jwebsigType)
@ -2834,10 +2831,7 @@ def _checkJsonSignature(baseDir: str, queueJson: {}) -> (bool, bool):
alreadyUnknown = True alreadyUnknown = True
if not alreadyUnknown: if not alreadyUnknown:
unknownFile = open(unknownSignaturesFile, "a+") storeValue(unknownSignaturesFile, jwebsigType, 'append')
if unknownFile:
unknownFile.write(jwebsigType + '\n')
unknownFile.close()
return hasJsonSignature, jwebsigType return hasJsonSignature, jwebsigType

View File

@ -12,6 +12,7 @@ from follow import followedAccountAccepts
from follow import followedAccountRejects from follow import followedAccountRejects
from follow import removeFromFollowRequests from follow import removeFromFollowRequests
from utils import loadJson from utils import loadJson
from storage import storeValue
def manualDenyFollowRequest(session, baseDir: str, def manualDenyFollowRequest(session, baseDir: str,
@ -41,9 +42,7 @@ def manualDenyFollowRequest(session, baseDir: str,
removeFromFollowRequests(baseDir, nickname, domain, denyHandle, debug) removeFromFollowRequests(baseDir, nickname, domain, denyHandle, debug)
# Store rejected follows # Store rejected follows
rejectsFile = open(rejectedFollowsFilename, "a+") storeValue(rejectedFollowsFilename, denyHandle, 'append')
rejectsFile.write(denyHandle + '\n')
rejectsFile.close()
denyNickname = denyHandle.split('@')[0] denyNickname = denyHandle.split('@')[0]
denyDomain = \ denyDomain = \
@ -70,13 +69,9 @@ def _approveFollowerHandle(accountDir: str, approveHandle: str) -> None:
approvedFilename = accountDir + '/approved.txt' approvedFilename = accountDir + '/approved.txt'
if os.path.isfile(approvedFilename): if os.path.isfile(approvedFilename):
if approveHandle not in open(approvedFilename).read(): if approveHandle not in open(approvedFilename).read():
approvedFile = open(approvedFilename, "a+") storeValue(approvedFilename, approveHandle, 'append')
approvedFile.write(approveHandle + '\n')
approvedFile.close()
else: else:
approvedFile = open(approvedFilename, "w+") storeValue(approvedFilename, approveHandle, 'write')
approvedFile.write(approveHandle + '\n')
approvedFile.close()
def manualApproveFollowRequest(session, baseDir: str, def manualApproveFollowRequest(session, baseDir: str,
@ -201,9 +196,7 @@ def manualApproveFollowRequest(session, baseDir: str,
else: else:
print('Manual follow accept: first follower accepted for ' + print('Manual follow accept: first follower accepted for ' +
handle + ' is ' + approveHandleFull) handle + ' is ' + approveHandleFull)
followersFile = open(followersFilename, "w+") storeValue(followersFilename, approveHandleFull, 'write')
followersFile.write(approveHandleFull + '\n')
followersFile.close()
# only update the follow requests file if the follow is confirmed to be # only update the follow requests file if the follow is confirmed to be
# in followers.txt # in followers.txt

View File

@ -1144,11 +1144,8 @@ def personSnooze(baseDir: str, nickname: str, domain: str,
if os.path.isfile(snoozedFilename): if os.path.isfile(snoozedFilename):
if snoozeActor + ' ' in open(snoozedFilename).read(): if snoozeActor + ' ' in open(snoozedFilename).read():
return return
snoozedFile = open(snoozedFilename, "a+") storeStr = snoozeActor + ' ' + str(int(time.time()))
if snoozedFile: storeValue(snoozedFilename, storeStr, 'append')
snoozedFile.write(snoozeActor + ' ' +
str(int(time.time())) + '\n')
snoozedFile.close()
def personUnsnooze(baseDir: str, nickname: str, domain: str, def personUnsnooze(baseDir: str, nickname: str, domain: str,

View File

@ -10,6 +10,7 @@ import threading
import sys import sys
import time import time
import datetime import datetime
from storage import storeValue
class threadWithTrace(threading.Thread): class threadWithTrace(threading.Thread):
@ -139,10 +140,8 @@ def removeDormantThreads(baseDir: str, threadsList: [], debug: bool,
if debug: if debug:
sendLogFilename = baseDir + '/send.csv' sendLogFilename = baseDir + '/send.csv'
try: storeStr = \
with open(sendLogFilename, "a+") as logFile: currTime.strftime("%Y-%m-%dT%H:%M:%SZ") + \
logFile.write(currTime.strftime("%Y-%m-%dT%H:%M:%SZ") + ',' + str(noOfActiveThreads) + \
',' + str(noOfActiveThreads) + ',' + str(len(threadsList))
',' + str(len(threadsList)) + '\n') storeValue(sendLogFilename, storeStr, 'append')
except BaseException:
pass

View File

@ -951,8 +951,7 @@ def _setDefaultPetName(baseDir: str, nickname: str, domain: str,
# petname already exists # petname already exists
return return
# petname doesn't already exist # petname doesn't already exist
with open(petnamesFilename, 'a+') as petnamesFile: storeValue(petnamesFilename, petnameLookupEntry, 'append')
petnamesFile.write(petnameLookupEntry)
def followPerson(baseDir: str, nickname: str, domain: str, def followPerson(baseDir: str, nickname: str, domain: str,