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)
else:
# append to password file
with open(passwordFile, 'a+') as passfile:
passfile.write(storeStr + '\n')
storeValue(passwordFile, storeStr, 'append')
else:
storeValue(passwordFile, storeStr, 'write')
return True

View File

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

View File

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

View File

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

View File

@ -276,8 +276,7 @@ def unfollowAccount(baseDir: str, nickname: str, domain: str,
if os.path.isfile(unfollowedFilename):
if handleToUnfollowLower not in \
open(unfollowedFilename).read().lower():
with open(unfollowedFilename, "a+") as f:
f.write(handleToUnfollow + '\n')
storeValue(unfollowedFilename, handleToUnfollow, 'append')
else:
storeValue(unfollowedFilename, handleToUnfollow, 'write')
@ -599,8 +598,7 @@ def _storeFollowRequest(baseDir: str,
if os.path.isfile(approveFollowsFilename):
if approveHandle not in open(approveFollowsFilename).read():
with open(approveFollowsFilename, 'a+') as fp:
fp.write(approveHandleStored + '\n')
storeValue(approveFollowsFilename, approveHandleStored, 'append')
else:
if debug:
print('DEBUG: ' + approveHandleStored +

View File

@ -118,11 +118,8 @@ def saveEventPost(baseDir: str, handle: str, postId: str,
return False
# append the post Id to the file for the calendar month
calendarFile = open(calendarFilename, 'a+')
if not calendarFile:
if not storeValue(calendarFilename, postId, 'append'):
return False
calendarFile.write(postId + '\n')
calendarFile.close()
# create a file which will trigger a notification that
# 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 recreateEventsFile:
calendarFile = open(calendarFilename, 'w+')
for postId in calendarPostIds:
calendarFile.write(postId + '\n')
calendarFile.close()
if calendarFile:
for postId in calendarPostIds:
calendarFile.write(postId + '\n')
calendarFile.close()
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 recreateEventsFile:
calendarFile = open(calendarFilename, 'w+')
for postId in calendarPostIds:
calendarFile.write(postId + '\n')
calendarFile.close()
if calendarFile:
for postId in calendarPostIds:
calendarFile.write(postId + '\n')
calendarFile.close()
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 recreateEventsFile:
calendarFile = open(calendarFilename, 'w+')
for postId in calendarPostIds:
calendarFile.write(postId + '\n')
calendarFile.close()
if calendarFile:
for postId in calendarPostIds:
calendarFile.write(postId + '\n')
calendarFile.close()
return events

View File

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

View File

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

View File

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

View File

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

View File

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