Revert "Storage functions"

This reverts commit feb4286031.
main
Bob Mottram 2021-06-21 23:52:50 +01:00
parent cd008c3013
commit 550a993711
11 changed files with 74 additions and 39 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -118,8 +118,11 @@ def saveEventPost(baseDir: str, handle: str, postId: str,
return False
# append the post Id to the file for the calendar month
if not storeValue(calendarFilename, postId, 'append'):
calendarFile = open(calendarFilename, 'a+')
if not calendarFile:
return False
calendarFile.write(postId + '\n')
calendarFile.close()
# create a file which will trigger a notification that
# a new event has been added
@ -242,10 +245,9 @@ 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+')
if calendarFile:
for postId in calendarPostIds:
calendarFile.write(postId + '\n')
calendarFile.close()
for postId in calendarPostIds:
calendarFile.write(postId + '\n')
calendarFile.close()
return events
@ -360,10 +362,9 @@ 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+')
if calendarFile:
for postId in calendarPostIds:
calendarFile.write(postId + '\n')
calendarFile.close()
for postId in calendarPostIds:
calendarFile.write(postId + '\n')
calendarFile.close()
return events
@ -426,10 +427,9 @@ 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+')
if calendarFile:
for postId in calendarPostIds:
calendarFile.write(postId + '\n')
calendarFile.close()
for postId in calendarPostIds:
calendarFile.write(postId + '\n')
calendarFile.close()
return events

View File

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

View File

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

View File

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

View File

@ -10,7 +10,6 @@ import threading
import sys
import time
import datetime
from storage import storeValue
class threadWithTrace(threading.Thread):
@ -140,8 +139,10 @@ def removeDormantThreads(baseDir: str, threadsList: [], debug: bool,
if debug:
sendLogFilename = baseDir + '/send.csv'
storeStr = \
currTime.strftime("%Y-%m-%dT%H:%M:%SZ") + \
',' + str(noOfActiveThreads) + \
',' + str(len(threadsList))
storeValue(sendLogFilename, storeStr, 'append')
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

View File

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