Snake case

main
Bob Mottram 2021-12-27 22:32:59 +00:00
parent b6ac0d6b24
commit 83880bd0b4
3 changed files with 72 additions and 72 deletions

View File

@ -64,7 +64,7 @@ from utils import save_json
from utils import get_config_param from utils import get_config_param
from utils import locateNewsVotes from utils import locateNewsVotes
from utils import locateNewsArrival from utils import locateNewsArrival
from utils import votesOnNewswireItem from utils import votes_on_newswire_item
from utils import remove_html from utils import remove_html
from utils import dangerous_markup from utils import dangerous_markup
from utils import acct_dir from utils import acct_dir
@ -3640,14 +3640,14 @@ def _passedNewswireVoting(newswire_votes_threshold: int,
if not votesJson: if not votesJson:
return True return True
if not positive_voting: if not positive_voting:
if votesOnNewswireItem(votesJson) >= \ if votes_on_newswire_item(votesJson) >= \
newswire_votes_threshold: newswire_votes_threshold:
# Too many veto votes. # Too many veto votes.
# Continue without incrementing # Continue without incrementing
# the posts counter # the posts counter
return False return False
else: else:
if votesOnNewswireItem < \ if votes_on_newswire_item < \
newswire_votes_threshold: newswire_votes_threshold:
# Not enough votes. # Not enough votes.
# Continue without incrementing # Continue without incrementing

132
utils.py
View File

@ -1028,33 +1028,32 @@ def get_nickname_from_actor(actor: str) -> str:
""" """
if actor.startswith('@'): if actor.startswith('@'):
actor = actor[1:] actor = actor[1:]
usersPaths = get_user_paths() users_paths = get_user_paths()
for possiblePath in usersPaths: for possible_path in users_paths:
if possiblePath in actor: if possible_path in actor:
nickStr = actor.split(possiblePath)[1].replace('@', '') nick_str = actor.split(possible_path)[1].replace('@', '')
if '/' not in nickStr: if '/' not in nick_str:
return nickStr return nick_str
else: return nick_str.split('/')[0]
return nickStr.split('/')[0]
if '/@' in actor: if '/@' in actor:
# https://domain/@nick # https://domain/@nick
nickStr = actor.split('/@')[1] nick_str = actor.split('/@')[1]
if '/' in nickStr: if '/' in nick_str:
nickStr = nickStr.split('/')[0] nick_str = nick_str.split('/')[0]
return nickStr return nick_str
elif '@' in actor: if '@' in actor:
nickStr = actor.split('@')[0] nick_str = actor.split('@')[0]
return nickStr return nick_str
elif '://' in actor: if '://' in actor:
domain = actor.split('://')[1] domain = actor.split('://')[1]
if '/' in domain: if '/' in domain:
domain = domain.split('/')[0] domain = domain.split('/')[0]
if '://' + domain + '/' not in actor: if '://' + domain + '/' not in actor:
return None return None
nickStr = actor.split('://' + domain + '/')[1] nick_str = actor.split('://' + domain + '/')[1]
if '/' in nickStr or '.' in nickStr: if '/' in nick_str or '.' in nick_str:
return None return None
return nickStr return nick_str
return None return None
@ -1080,10 +1079,10 @@ def get_domain_from_actor(actor: str) -> (str, int):
actor = actor[1:] actor = actor[1:]
port = None port = None
prefixes = get_protocol_prefixes() prefixes = get_protocol_prefixes()
usersPaths = get_user_paths() users_paths = get_user_paths()
for possiblePath in usersPaths: for possible_path in users_paths:
if possiblePath in actor: if possible_path in actor:
domain = actor.split(possiblePath)[0] domain = actor.split(possible_path)[0]
for prefix in prefixes: for prefix in prefixes:
domain = domain.replace(prefix, '') domain = domain.replace(prefix, '')
break break
@ -1111,28 +1110,28 @@ def _set_default_pet_name(base_dir: str, nickname: str, domain: str,
This helps especially when using onion or i2p address This helps especially when using onion or i2p address
""" """
domain = remove_domain_port(domain) domain = remove_domain_port(domain)
userPath = acct_dir(base_dir, nickname, domain) user_path = acct_dir(base_dir, nickname, domain)
petnamesFilename = userPath + '/petnames.txt' petnames_filename = user_path + '/petnames.txt'
petnameLookupEntry = follow_nickname + ' ' + \ petname_lookup_entry = follow_nickname + ' ' + \
follow_nickname + '@' + follow_domain + '\n' follow_nickname + '@' + follow_domain + '\n'
if not os.path.isfile(petnamesFilename): if not os.path.isfile(petnames_filename):
# if there is no existing petnames lookup file # if there is no existing petnames lookup file
with open(petnamesFilename, 'w+') as petnamesFile: with open(petnames_filename, 'w+') as petnames_file:
petnamesFile.write(petnameLookupEntry) petnames_file.write(petname_lookup_entry)
return return
with open(petnamesFilename, 'r') as petnamesFile: with open(petnames_filename, 'r') as petnames_file:
petnamesStr = petnamesFile.read() petnames_str = petnames_file.read()
if petnamesStr: if petnames_str:
petnamesList = petnamesStr.split('\n') petnames_list = petnames_str.split('\n')
for pet in petnamesList: for pet in petnames_list:
if pet.startswith(follow_nickname + ' '): if pet.startswith(follow_nickname + ' '):
# petname already exists # petname already exists
return return
# petname doesn't already exist # petname doesn't already exist
with open(petnamesFilename, 'a+') as petnames_file: with open(petnames_filename, 'a+') as petnames_file:
petnames_file.write(petnameLookupEntry) petnames_file.write(petname_lookup_entry)
def follow_person(base_dir: str, nickname: str, domain: str, def follow_person(base_dir: str, nickname: str, domain: str,
@ -1142,8 +1141,8 @@ def follow_person(base_dir: str, nickname: str, domain: str,
follow_file: str = 'following.txt') -> bool: follow_file: str = 'following.txt') -> bool:
"""Adds a person to the follow list """Adds a person to the follow list
""" """
follow_domainStrLower = follow_domain.lower().replace('\n', '') follow_domain_str_lower = follow_domain.lower().replace('\n', '')
if not domain_permitted(follow_domainStrLower, if not domain_permitted(follow_domain_str_lower,
federation_list): federation_list):
if debug: if debug:
print('DEBUG: follow of domain ' + print('DEBUG: follow of domain ' +
@ -1153,8 +1152,8 @@ def follow_person(base_dir: str, nickname: str, domain: str,
print('DEBUG: follow of domain ' + follow_domain) print('DEBUG: follow of domain ' + follow_domain)
if ':' in domain: if ':' in domain:
domainOnly = remove_domain_port(domain) domain_only = remove_domain_port(domain)
handle = nickname + '@' + domainOnly handle = nickname + '@' + domain_only
else: else:
handle = nickname + '@' + domain handle = nickname + '@' + domain
@ -1163,36 +1162,36 @@ def follow_person(base_dir: str, nickname: str, domain: str,
return False return False
if ':' in follow_domain: if ':' in follow_domain:
follow_domainOnly = remove_domain_port(follow_domain) follow_domain_only = remove_domain_port(follow_domain)
handleToFollow = follow_nickname + '@' + follow_domainOnly handle_to_follow = follow_nickname + '@' + follow_domain_only
else: else:
handleToFollow = follow_nickname + '@' + follow_domain handle_to_follow = follow_nickname + '@' + follow_domain
if group_account: if group_account:
handleToFollow = '!' + handleToFollow handle_to_follow = '!' + handle_to_follow
# was this person previously unfollowed? # was this person previously unfollowed?
unfollowedFilename = base_dir + '/accounts/' + handle + '/unfollowed.txt' unfollowed_filename = base_dir + '/accounts/' + handle + '/unfollowed.txt'
if os.path.isfile(unfollowedFilename): if os.path.isfile(unfollowed_filename):
if handleToFollow in open(unfollowedFilename).read(): if handle_to_follow in open(unfollowed_filename).read():
# remove them from the unfollowed file # remove them from the unfollowed file
newLines = '' new_lines = ''
with open(unfollowedFilename, 'r') as f: with open(unfollowed_filename, 'r') as unfoll_file:
lines = f.readlines() lines = unfoll_file.readlines()
for line in lines: for line in lines:
if handleToFollow not in line: if handle_to_follow not in line:
newLines += line new_lines += line
with open(unfollowedFilename, 'w+') as f: with open(unfollowed_filename, 'w+') as unfoll_file:
f.write(newLines) unfoll_file.write(new_lines)
if not os.path.isdir(base_dir + '/accounts'): if not os.path.isdir(base_dir + '/accounts'):
os.mkdir(base_dir + '/accounts') os.mkdir(base_dir + '/accounts')
handleToFollow = follow_nickname + '@' + follow_domain handle_to_follow = follow_nickname + '@' + follow_domain
if group_account: if group_account:
handleToFollow = '!' + handleToFollow handle_to_follow = '!' + handle_to_follow
filename = base_dir + '/accounts/' + handle + '/' + follow_file filename = base_dir + '/accounts/' + handle + '/' + follow_file
if os.path.isfile(filename): if os.path.isfile(filename):
if handleToFollow in open(filename).read(): if handle_to_follow in open(filename).read():
if debug: if debug:
print('DEBUG: follow already exists') print('DEBUG: follow already exists')
return True return True
@ -1200,9 +1199,9 @@ def follow_person(base_dir: str, nickname: str, domain: str,
try: try:
with open(filename, 'r+') as foll_file: with open(filename, 'r+') as foll_file:
content = foll_file.read() content = foll_file.read()
if handleToFollow + '\n' not in content: if handle_to_follow + '\n' not in content:
foll_file.seek(0, 0) foll_file.seek(0, 0)
foll_file.write(handleToFollow + '\n' + content) foll_file.write(handle_to_follow + '\n' + content)
print('DEBUG: follow added') print('DEBUG: follow added')
except OSError as ex: except OSError as ex:
print('WARN: Failed to write entry to follow file ' + print('WARN: Failed to write entry to follow file ' +
@ -1211,10 +1210,11 @@ def follow_person(base_dir: str, nickname: str, domain: str,
# first follow # first follow
if debug: if debug:
print('DEBUG: ' + handle + print('DEBUG: ' + handle +
' creating new following file to follow ' + handleToFollow + ' creating new following file to follow ' +
handle_to_follow +
', filename is ' + filename) ', filename is ' + filename)
with open(filename, 'w+') as fp: with open(filename, 'w+') as foll_file:
fp.write(handleToFollow + '\n') foll_file.write(handle_to_follow + '\n')
if follow_file.endswith('following.txt'): if follow_file.endswith('following.txt'):
# Default to adding new follows to the calendar. # Default to adding new follows to the calendar.
@ -1232,14 +1232,14 @@ def follow_person(base_dir: str, nickname: str, domain: str,
return True return True
def votesOnNewswireItem(status: []) -> int: def votes_on_newswire_item(status: []) -> int:
"""Returns the number of votes on a newswire item """Returns the number of votes on a newswire item
""" """
totalVotes = 0 total_votes = 0
for line in status: for line in status:
if 'vote:' in line: if 'vote:' in line:
totalVotes += 1 total_votes += 1
return totalVotes return total_votes
def locateNewsVotes(base_dir: str, domain: str, def locateNewsVotes(base_dir: str, domain: str,

View File

@ -16,7 +16,7 @@ from utils import get_base_content_from_post
from utils import remove_html from utils import remove_html
from utils import locate_post from utils import locate_post
from utils import load_json from utils import load_json
from utils import votesOnNewswireItem from utils import votes_on_newswire_item
from utils import get_nickname_from_actor from utils import get_nickname_from_actor
from utils import is_editor from utils import is_editor
from utils import get_config_param from utils import get_config_param
@ -268,7 +268,7 @@ def _htmlNewswire(base_dir: str, newswire: {}, nickname: str, moderator: bool,
totalVotesStr = '' totalVotesStr = ''
totalVotes = 0 totalVotes = 0
if moderator: if moderator:
totalVotes = votesOnNewswireItem(item[2]) totalVotes = votes_on_newswire_item(item[2])
totalVotesStr = \ totalVotesStr = \
_votesIndicator(totalVotes, positive_voting) _votesIndicator(totalVotes, positive_voting)
@ -295,7 +295,7 @@ def _htmlNewswire(base_dir: str, newswire: {}, nickname: str, moderator: bool,
totalVotes = 0 totalVotes = 0
if moderator: if moderator:
if moderatedItem: if moderatedItem:
totalVotes = votesOnNewswireItem(item[2]) totalVotes = votes_on_newswire_item(item[2])
# show a number of ticks or crosses for how many # show a number of ticks or crosses for how many
# votes for or against # votes for or against
totalVotesStr = \ totalVotesStr = \