flake8 style

main
Bob Mottram 2020-04-01 19:39:27 +00:00
parent 805aef6a74
commit 2c6169fa64
1 changed files with 64 additions and 55 deletions

View File

@ -1,13 +1,11 @@
__filename__="availability.py" __filename__ = "availability.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 json
import time
import os import os
from webfinger import webfingerHandle from webfinger import webfingerHandle
from auth import createBasicAuthHeader from auth import createBasicAuthHeader
@ -18,42 +16,45 @@ from utils import getDomainFromActor
from utils import loadJson from utils import loadJson
from utils import saveJson from utils import saveJson
def setAvailability(baseDir: str,nickname: str,domain: str, \
def setAvailability(baseDir: str, nickname: str, domain: str,
status: str) -> bool: status: str) -> bool:
"""Set an availability status """Set an availability status
""" """
# avoid giant strings # avoid giant strings
if len(status)>128: if len(status) > 128:
return False return False
actorFilename=baseDir+'/accounts/'+nickname+'@'+domain+'.json' actorFilename = baseDir + '/accounts/' + nickname + '@' + domain + '.json'
if not os.path.isfile(actorFilename): if not os.path.isfile(actorFilename):
return False return False
actorJson=loadJson(actorFilename) actorJson = loadJson(actorFilename)
if actorJson: if actorJson:
actorJson['availability']=status actorJson['availability'] = status
saveJson(actorJson,actorFilename) saveJson(actorJson, actorFilename)
return True return True
def getAvailability(baseDir: str,nickname: str,domain: str) -> str:
def getAvailability(baseDir: str, nickname: str, domain: str) -> str:
"""Returns the availability for a given person """Returns the availability for a given person
""" """
actorFilename=baseDir+'/accounts/'+nickname+'@'+domain+'.json' actorFilename = baseDir + '/accounts/' + nickname + '@' + domain + '.json'
if not os.path.isfile(actorFilename): if not os.path.isfile(actorFilename):
return False return False
actorJson=loadJson(actorFilename) actorJson = loadJson(actorFilename)
if actorJson: if actorJson:
if not actorJson.get('availability'): if not actorJson.get('availability'):
return None return None
return actorJson['availability'] return actorJson['availability']
return None return None
def outboxAvailability(baseDir: str,nickname: str,messageJson: {}, \
def outboxAvailability(baseDir: str, nickname: str, messageJson: {},
debug: bool) -> bool: debug: bool) -> bool:
"""Handles receiving an availability update """Handles receiving an availability update
""" """
if not messageJson.get('type'): if not messageJson.get('type'):
return False return False
if not messageJson['type']=='Availability': if not messageJson['type'] == 'Availability':
return False return False
if not messageJson.get('actor'): if not messageJson.get('actor'):
return False return False
@ -62,37 +63,39 @@ def outboxAvailability(baseDir: str,nickname: str,messageJson: {}, \
if not isinstance(messageJson['object'], str): if not isinstance(messageJson['object'], str):
return False return False
actorNickname=getNicknameFromActor(messageJson['actor']) actorNickname = getNicknameFromActor(messageJson['actor'])
if actorNickname!=nickname: if actorNickname != nickname:
return False return False
domain,port=getDomainFromActor(messageJson['actor']) domain, port = getDomainFromActor(messageJson['actor'])
status=messageJson['object'].replace('"','') status = messageJson['object'].replace('"', '')
return setAvailability(baseDir,nickname,domain,status) return setAvailability(baseDir, nickname, domain, status)
def sendAvailabilityViaServer(baseDir: str,session, \
nickname: str,password: str, \ def sendAvailabilityViaServer(baseDir: str, session,
domain: str,port: int, \ nickname: str, password: str,
httpPrefix: str, \ domain: str, port: int,
status: str, \ httpPrefix: str,
cachedWebfingers: {},personCache: {}, \ status: str,
debug: bool,projectVersion: str) -> {}: cachedWebfingers: {}, personCache: {},
debug: bool, projectVersion: str) -> {}:
"""Sets the availability for a person via c2s """Sets the availability for a person via c2s
""" """
if not session: if not session:
print('WARN: No session for sendAvailabilityViaServer') print('WARN: No session for sendAvailabilityViaServer')
return 6 return 6
domainFull=domain domainFull = domain
if port: if port:
if port!=80 and port!=443: if port != 80 and port != 443:
if ':' not in domain: if ':' not in domain:
domainFull=domain+':'+str(port) domainFull = domain + ':' + str(port)
toUrl=httpPrefix+'://'+domainFull+'/users/'+nickname toUrl = httpPrefix + '://' + domainFull + '/users/' + nickname
ccUrl=httpPrefix+'://'+domainFull+'/users/'+nickname+'/followers' ccUrl = httpPrefix + '://' + domainFull + '/users/' + nickname + \
'/followers'
newAvailabilityJson={ newAvailabilityJson = {
'type': 'Availability', 'type': 'Availability',
'actor': httpPrefix+'://'+domainFull+'/users/'+nickname, 'actor': httpPrefix+'://'+domainFull+'/users/'+nickname,
'object': '"'+status+'"', 'object': '"'+status+'"',
@ -100,42 +103,48 @@ def sendAvailabilityViaServer(baseDir: str,session, \
'cc': [ccUrl] 'cc': [ccUrl]
} }
handle=httpPrefix+'://'+domainFull+'/@'+nickname handle = httpPrefix + '://' + domainFull + '/@' + nickname
# lookup the inbox for the To handle # lookup the inbox for the To handle
wfRequest= \ wfRequest = webfingerHandle(session, handle, httpPrefix,
webfingerHandle(session,handle,httpPrefix,cachedWebfingers, \ cachedWebfingers,
domain,projectVersion) domain, projectVersion)
if not wfRequest: if not wfRequest:
if debug: if debug:
print('DEBUG: announce webfinger failed for '+handle) print('DEBUG: announce webfinger failed for ' + handle)
return 1 return 1
postToBox='outbox' postToBox = 'outbox'
# get the actor inbox for the To handle # get the actor inbox for the To handle
inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition,avatarUrl,displayName= \ (inboxUrl, pubKeyId, pubKey,
getPersonBox(baseDir,session,wfRequest,personCache, \ fromPersonId, sharedInbox,
projectVersion,httpPrefix,nickname,domain,postToBox) capabilityAcquisition,
avatarUrl, displayName) = getPersonBox(baseDir, session, wfRequest,
personCache, projectVersion,
httpPrefix, nickname,
domain, postToBox)
if not inboxUrl: if not inboxUrl:
if debug: if debug:
print('DEBUG: No '+postToBox+' was found for '+handle) print('DEBUG: No ' + postToBox + ' was found for ' + handle)
return 3 return 3
if not fromPersonId: if not fromPersonId:
if debug: if debug:
print('DEBUG: No actor was found for '+handle) print('DEBUG: No actor was found for ' + handle)
return 4 return 4
authHeader=createBasicAuthHeader(Nickname,password) authHeader = createBasicAuthHeader(nickname, password)
headers={ headers = {
'host': domain, \ 'host': domain,
'Content-type': 'application/json', \ 'Content-type': 'application/json',
'Authorization': authHeader 'Authorization': authHeader
} }
postResult= \ postResult = postJson(session, newAvailabilityJson, [],
postJson(session,newAvailabilityJson,[],inboxUrl,headers,"inbox:write") inboxUrl, headers, "inbox:write")
if not postResult:
print('WARN: failed to post availability')
if debug: if debug:
print('DEBUG: c2s POST availability success') print('DEBUG: c2s POST availability success')