Tidying to reduce file reads

main
Bob Mottram 2020-08-29 20:54:30 +01:00
parent 7ef0a275f0
commit 3179a37975
4 changed files with 21 additions and 25 deletions

View File

@ -45,8 +45,9 @@ def getPersonFromCache(baseDir: str, personUrl: str, personCache: {},
# does the person exist as a cached file? # does the person exist as a cached file?
cacheFilename = baseDir + '/cache/actors/' + \ cacheFilename = baseDir + '/cache/actors/' + \
personUrl.replace('/', '#')+'.json' personUrl.replace('/', '#')+'.json'
if os.path.isfile(getFileCaseInsensitive(cacheFilename)): actorFilename = getFileCaseInsensitive(cacheFilename)
personJson = loadJson(getFileCaseInsensitive(cacheFilename)) if actorFilename:
personJson = loadJson(actorFilename)
if personJson: if personJson:
storePersonInCache(baseDir, personUrl, personJson, storePersonInCache(baseDir, personUrl, personJson,
personCache, False) personCache, False)

View File

@ -7516,7 +7516,8 @@ class PubServer(BaseHTTPRequestHandler):
if fields.get('removeTwitter'): if fields.get('removeTwitter'):
if fields['removeTwitter'] == 'on': if fields['removeTwitter'] == 'on':
removeTwitterActive = True removeTwitterActive = True
with open(removeTwitterFilename, 'w+') as rFile: with open(removeTwitterFilename,
'w+') as rFile:
rFile.write('\n') rFile.write('\n')
if not removeTwitterActive: if not removeTwitterActive:
if os.path.isfile(removeTwitterFilename): if os.path.isfile(removeTwitterFilename):

View File

@ -907,21 +907,19 @@ def searchBoxPosts(baseDir: str, nickname: str, domain: str,
def getFileCaseInsensitive(path: str) -> str: def getFileCaseInsensitive(path: str) -> str:
"""Returns a case specific filename given a case insensitive version of it """Returns a case specific filename given a case insensitive version of it
""" """
# does the given file exist? If so then we don't need
# to do a directory search
if os.path.isfile(path): if os.path.isfile(path):
return path return path
if path != path.lower(): if path != path.lower():
if os.path.isfile(path.lower()): if os.path.isfile(path.lower()):
return path.lower() return path.lower()
directory, filename = os.path.split(path) # directory, filename = os.path.split(path)
directory, filename = (directory or '.'), filename.lower() # directory, filename = (directory or '.'), filename.lower()
for f in os.listdir(directory): # for f in os.listdir(directory):
if f.lower() == filename: # if f.lower() == filename:
newpath = os.path.join(directory, f) # newpath = os.path.join(directory, f)
if os.path.isfile(newpath): # if os.path.isfile(newpath):
return newpath # return newpath
return path return None
def undoLikesCollectionEntry(recentPostsCache: {}, def undoLikesCollectionEntry(recentPostsCache: {},

View File

@ -27,7 +27,6 @@ from matrix import getMatrixAddress
from donate import getDonationUrl from donate import getDonationUrl
from utils import removeIdEnding from utils import removeIdEnding
from utils import getProtocolPrefixes from utils import getProtocolPrefixes
from utils import getFileCaseInsensitive
from utils import searchBoxPosts from utils import searchBoxPosts
from utils import isEventPost from utils import isEventPost
from utils import isBlogPost from utils import isBlogPost
@ -304,16 +303,13 @@ def getPersonAvatarUrl(baseDir: str, personUrl: str, personCache: {},
# get from locally stored image # get from locally stored image
actorStr = personJson['id'].replace('/', '-') actorStr = personJson['id'].replace('/', '-')
avatarImagePath = baseDir + '/cache/avatars/' + actorStr avatarImagePath = baseDir + '/cache/avatars/' + actorStr
if os.path.isfile(getFileCaseInsensitive(avatarImagePath + '.png')):
return '/avatars/' + actorStr + '.png' imageExtension = ('png', 'jpg', 'jpeg', 'gif', 'webp')
elif os.path.isfile(getFileCaseInsensitive(avatarImagePath + '.jpg')): for ext in imageExtension:
return '/avatars/' + actorStr + '.jpg' if os.path.isfile(avatarImagePath + '.' + ext):
elif os.path.isfile(getFileCaseInsensitive(avatarImagePath + '.gif')): return '/avatars/' + actorStr + '.' + ext
return '/avatars/' + actorStr + '.gif' elif os.path.isfile(avatarImagePath.lower() + '.' + ext):
elif os.path.isfile(getFileCaseInsensitive(avatarImagePath + '.webp')): return '/avatars/' + actorStr.lower() + '.' + ext
return '/avatars/' + actorStr + '.webp'
elif os.path.isfile(getFileCaseInsensitive(avatarImagePath)):
return '/avatars/' + actorStr
if personJson.get('icon'): if personJson.get('icon'):
if personJson['icon'].get('url'): if personJson['icon'].get('url'):
@ -6649,7 +6645,7 @@ def htmlCalendar(translate: {},
for weekOfMonth in range(1, 7): for weekOfMonth in range(1, 7):
if dayOfMonth == daysInMonth: if dayOfMonth == daysInMonth:
continue continue
calendarStr += ' <tr>\n' calendarStr += ' <tr>\n'
for dayNumber in range(1, 8): for dayNumber in range(1, 8):
if (weekOfMonth > 1 and dayOfMonth < daysInMonth) or \ if (weekOfMonth > 1 and dayOfMonth < daysInMonth) or \
(weekOfMonth == 1 and dayNumber >= dow): (weekOfMonth == 1 and dayNumber >= dow):