flake8 format

main
Bob Mottram 2020-04-04 11:12:34 +01:00
parent da3900c9f1
commit 3334cc0466
1 changed files with 46 additions and 37 deletions

View File

@ -1,17 +1,19 @@
__filename__="question.py" __filename__ = "question.py"
__author__="Bob Mottram" __author__ = "Bob Mottram"
__license__="AGPL3+" __license__ = "AGPL3+"
__version__="1.1.0" __version__ = "1.1.0"
__maintainer__="Bob Mottram" __maintainer__ = "Bob Mottram"
__email__="bob@freedombone.net" __email__ = "bob@freedombone.net"
__status__="Production" __status__ = "Production"
import os import os
from utils import locatePost from utils import locatePost
from utils import loadJson from utils import loadJson
from utils import saveJson from utils import saveJson
def questionUpdateVotes(baseDir: str,nickname: str,domain: str,replyJson: {}) -> {}:
def questionUpdateVotes(baseDir: str, nickname: str, domain: str,
replyJson: {}) -> {}:
""" For a given reply update the votes on a question """ For a given reply update the votes on a question
Returns the question json object if the vote totals were changed Returns the question json object if the vote totals were changed
""" """
@ -25,11 +27,11 @@ def questionUpdateVotes(baseDir: str,nickname: str,domain: str,replyJson: {}) ->
return None return None
if not replyJson['object'].get('name'): if not replyJson['object'].get('name'):
return None return None
inReplyTo=replyJson['object']['inReplyTo'] inReplyTo = replyJson['object']['inReplyTo']
questionPostFilename=locatePost(baseDir,nickname,domain,inReplyTo) questionPostFilename = locatePost(baseDir, nickname, domain, inReplyTo)
if not questionPostFilename: if not questionPostFilename:
return None return None
questionJson=loadJson(questionPostFilename) questionJson = loadJson(questionPostFilename)
if not questionJson: if not questionJson:
return None return None
if not questionJson.get('object'): if not questionJson.get('object'):
@ -38,7 +40,7 @@ def questionUpdateVotes(baseDir: str,nickname: str,domain: str,replyJson: {}) ->
return None return None
if not questionJson['object'].get('type'): if not questionJson['object'].get('type'):
return None return None
if questionJson['type']!='Question': if questionJson['type'] != 'Question':
return None return None
if not questionJson['object'].get('oneOf'): if not questionJson['object'].get('oneOf'):
return None return None
@ -46,45 +48,51 @@ def questionUpdateVotes(baseDir: str,nickname: str,domain: str,replyJson: {}) ->
return None return None
if not questionJson['object'].get('content'): if not questionJson['object'].get('content'):
return None return None
replyVote=replyJson['object']['name'] replyVote = replyJson['object']['name']
# does the reply name field match any possible question option? # does the reply name field match any possible question option?
foundAnswer=None foundAnswer = None
for possibleAnswer in questionJson['object']['oneOf']: for possibleAnswer in questionJson['object']['oneOf']:
if not possibleAnswer.get('name'): if not possibleAnswer.get('name'):
continue continue
if possibleAnswer['name'] == replyVote: if possibleAnswer['name'] == replyVote:
foundAnswer=possibleAnswer foundAnswer = possibleAnswer
break break
if not foundAnswer: if not foundAnswer:
return None return None
# update the voters file # update the voters file
votersFileSeparator=';;;' votersFileSeparator = ';;;'
votersFilename=questionPostFilename.replace('.json','.voters') votersFilename = questionPostFilename.replace('.json', '.voters')
if not os.path.isfile(votersFilename): if not os.path.isfile(votersFilename):
# create a new voters file # create a new voters file
votersFile=open(votersFilename, "w") votersFile = open(votersFilename, "w")
if votersFile: if votersFile:
votersFile.write(replyJson['actor']+votersFileSeparator+foundAnswer+'\n') votersFile.write(replyJson['actor'] +
votersFileSeparator +
foundAnswer + '\n')
votersFile.close() votersFile.close()
else: else:
if replyJson['actor'] not in open(votersFilename).read(): if replyJson['actor'] not in open(votersFilename).read():
# append to the voters file # append to the voters file
votersFile=open(votersFilename, "a+") votersFile = open(votersFilename, "a+")
if votersFile: if votersFile:
votersFile.write(replyJson['actor']+votersFileSeparator+foundAnswer+'\n') votersFile.write(replyJson['actor'] +
votersFileSeparator +
foundAnswer + '\n')
votersFile.close() votersFile.close()
else: else:
# change an entry in the voters file # change an entry in the voters file
with open(votersFilename, "r") as votersFile: with open(votersFilename, "r") as votersFile:
lines=votersFile.readlines() lines = votersFile.readlines()
newlines=[] newlines = []
saveVotersFile=False saveVotersFile = False
for voteLine in lines: for voteLine in lines:
if voteLine.startswith(replyJson['actor']+votersFileSeparator): if voteLine.startswith(replyJson['actor'] +
newVoteLine=replyJson['actor']+votersFileSeparator+foundAnswer+'\n' votersFileSeparator):
if voteLine==newVoteLine: newVoteLine = replyJson['actor'] + \
votersFileSeparator + foundAnswer + '\n'
if voteLine == newVoteLine:
break break
saveVotersFile=True saveVotersFile = True
newlines.append(newVoteLine) newlines.append(newVoteLine)
else: else:
newlines.append(voteLine) newlines.append(voteLine)
@ -95,21 +103,22 @@ def questionUpdateVotes(baseDir: str,nickname: str,domain: str,replyJson: {}) ->
else: else:
return None return None
# update the vote counts # update the vote counts
questionTotalsChanged=False questionTotalsChanged = False
for possibleAnswer in questionJson['object']['oneOf']: for possibleAnswer in questionJson['object']['oneOf']:
if not possibleAnswer.get('name'): if not possibleAnswer.get('name'):
continue continue
totalItems=0 totalItems = 0
with open(votersFilename, "r") as votersFile: with open(votersFilename, "r") as votersFile:
lines=votersFile.readlines() lines = votersFile.readlines()
for voteLine in lines: for voteLine in lines:
if voteLine.endswith(votersFileSeparator+possibleAnswer['name']+'\n'): if voteLine.endswith(votersFileSeparator +
totalItems+=1 possibleAnswer['name'] + '\n'):
if possibleAnswer['replies']['totalItems']!=totalItems: totalItems += 1
possibleAnswer['replies']['totalItems']=totalItems if possibleAnswer['replies']['totalItems'] != totalItems:
questionTotalsChanged=True possibleAnswer['replies']['totalItems'] = totalItems
questionTotalsChanged = True
if not questionTotalsChanged: if not questionTotalsChanged:
return None return None
# save the question with altered totals # save the question with altered totals
saveJson(questionJson,questionPostFilename) saveJson(questionJson, questionPostFilename)
return questionJson return questionJson