Snake case

main
Bob Mottram 2021-12-31 11:23:00 +00:00
parent a884e8a755
commit 2f8bcc9f04
1 changed files with 79 additions and 68 deletions

View File

@ -15,109 +15,120 @@ from utils import has_object_dict
def question_update_votes(base_dir: str, nickname: str, domain: str,
replyJson: {}) -> ({}, str):
reply_json: {}) -> ({}, str):
""" For a given reply update the votes on a question
Returns the question json object if the vote totals were changed
"""
if not has_object_dict(replyJson):
if not has_object_dict(reply_json):
return None, None
if not replyJson['object'].get('inReplyTo'):
if not reply_json['object'].get('inReplyTo'):
return None, None
if not replyJson['object']['inReplyTo']:
if not reply_json['object']['inReplyTo']:
return None, None
if not isinstance(replyJson['object']['inReplyTo'], str):
if not isinstance(reply_json['object']['inReplyTo'], str):
return None, None
if not replyJson['object'].get('name'):
if not reply_json['object'].get('name'):
return None, None
inReplyTo = replyJson['object']['inReplyTo']
questionPostFilename = locate_post(base_dir, nickname, domain, inReplyTo)
if not questionPostFilename:
in_reply_to = reply_json['object']['inReplyTo']
question_post_filename = \
locate_post(base_dir, nickname, domain, in_reply_to)
if not question_post_filename:
return None, None
questionJson = load_json(questionPostFilename)
if not questionJson:
question_json = load_json(question_post_filename)
if not question_json:
return None, None
if not has_object_dict(questionJson):
if not has_object_dict(question_json):
return None, None
if not questionJson['object'].get('type'):
if not question_json['object'].get('type'):
return None, None
if questionJson['type'] != 'Question':
if question_json['type'] != 'Question':
return None, None
if not questionJson['object'].get('oneOf'):
if not question_json['object'].get('oneOf'):
return None, None
if not isinstance(questionJson['object']['oneOf'], list):
if not isinstance(question_json['object']['oneOf'], list):
return None, None
if not questionJson['object'].get('content'):
if not question_json['object'].get('content'):
return None, None
replyVote = replyJson['object']['name']
reply_vote = reply_json['object']['name']
# does the reply name field match any possible question option?
foundAnswer = None, None
for possibleAnswer in questionJson['object']['oneOf']:
if not possibleAnswer.get('name'):
found_answer = None, None
for possible_answer in question_json['object']['oneOf']:
if not possible_answer.get('name'):
continue
if possibleAnswer['name'] == replyVote:
foundAnswer = possibleAnswer
if possible_answer['name'] == reply_vote:
found_answer = possible_answer
break
if not foundAnswer:
if not found_answer:
return None, None
# update the voters file
votersFileSeparator = ';;;'
votersFilename = questionPostFilename.replace('.json', '.voters')
if not os.path.isfile(votersFilename):
voters_file_separator = ';;;'
voters_filename = question_post_filename.replace('.json', '.voters')
if not os.path.isfile(voters_filename):
# create a new voters file
with open(votersFilename, 'w+') as votersFile:
votersFile.write(replyJson['actor'] +
votersFileSeparator +
foundAnswer + '\n')
try:
with open(voters_filename, 'w+') as voters_file:
voters_file.write(reply_json['actor'] +
voters_file_separator +
found_answer + '\n')
except OSError:
print('EX: unable to write voters file ' + voters_filename)
else:
if replyJson['actor'] not in open(votersFilename).read():
if reply_json['actor'] not in open(voters_filename).read():
# append to the voters file
with open(votersFilename, 'a+') as votersFile:
votersFile.write(replyJson['actor'] +
votersFileSeparator +
foundAnswer + '\n')
try:
with open(voters_filename, 'a+') as voters_file:
voters_file.write(reply_json['actor'] +
voters_file_separator +
found_answer + '\n')
except OSError:
print('EX: unable to append to voters file ' + voters_filename)
else:
# change an entry in the voters file
with open(votersFilename, 'r') as votersFile:
lines = votersFile.readlines()
with open(voters_filename, 'r') as voters_file:
lines = voters_file.readlines()
newlines = []
saveVotersFile = False
for voteLine in lines:
if voteLine.startswith(replyJson['actor'] +
votersFileSeparator):
newVoteLine = replyJson['actor'] + \
votersFileSeparator + foundAnswer + '\n'
if voteLine == newVoteLine:
save_voters_file = False
for vote_line in lines:
if vote_line.startswith(reply_json['actor'] +
voters_file_separator):
new_vote_line = reply_json['actor'] + \
voters_file_separator + found_answer + '\n'
if vote_line == new_vote_line:
break
saveVotersFile = True
newlines.append(newVoteLine)
save_voters_file = True
newlines.append(new_vote_line)
else:
newlines.append(voteLine)
if saveVotersFile:
with open(votersFilename, 'w+') as votersFile:
for voteLine in newlines:
votersFile.write(voteLine)
newlines.append(vote_line)
if save_voters_file:
try:
with open(voters_filename, 'w+') as voters_file:
for vote_line in newlines:
voters_file.write(vote_line)
except OSError:
print('EX: unable to write voters file2 ' +
voters_filename)
else:
return None, None
# update the vote counts
questionTotalsChanged = False
for possibleAnswer in questionJson['object']['oneOf']:
if not possibleAnswer.get('name'):
question_totals_changed = False
for possible_answer in question_json['object']['oneOf']:
if not possible_answer.get('name'):
continue
totalItems = 0
with open(votersFilename, 'r') as votersFile:
lines = votersFile.readlines()
for voteLine in lines:
if voteLine.endswith(votersFileSeparator +
possibleAnswer['name'] + '\n'):
totalItems += 1
if possibleAnswer['replies']['totalItems'] != totalItems:
possibleAnswer['replies']['totalItems'] = totalItems
questionTotalsChanged = True
if not questionTotalsChanged:
total_items = 0
with open(voters_filename, 'r') as voters_file:
lines = voters_file.readlines()
for vote_line in lines:
if vote_line.endswith(voters_file_separator +
possible_answer['name'] + '\n'):
total_items += 1
if possible_answer['replies']['totalItems'] != total_items:
possible_answer['replies']['totalItems'] = total_items
question_totals_changed = True
if not question_totals_changed:
return None, None
# save the question with altered totals
save_json(questionJson, questionPostFilename)
return questionJson, questionPostFilename
save_json(question_json, question_post_filename)
return question_json, question_post_filename
def is_question(postObjectJson: {}) -> bool: