From 3179a37975062d59d6943ae3c1dcd6d59b6b0082 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sat, 29 Aug 2020 20:54:30 +0100 Subject: [PATCH] Tidying to reduce file reads --- cache.py | 5 +++-- daemon.py | 3 ++- utils.py | 18 ++++++++---------- webinterface.py | 20 ++++++++------------ 4 files changed, 21 insertions(+), 25 deletions(-) diff --git a/cache.py b/cache.py index 0ac180f80..08d7386c1 100644 --- a/cache.py +++ b/cache.py @@ -45,8 +45,9 @@ def getPersonFromCache(baseDir: str, personUrl: str, personCache: {}, # does the person exist as a cached file? cacheFilename = baseDir + '/cache/actors/' + \ personUrl.replace('/', '#')+'.json' - if os.path.isfile(getFileCaseInsensitive(cacheFilename)): - personJson = loadJson(getFileCaseInsensitive(cacheFilename)) + actorFilename = getFileCaseInsensitive(cacheFilename) + if actorFilename: + personJson = loadJson(actorFilename) if personJson: storePersonInCache(baseDir, personUrl, personJson, personCache, False) diff --git a/daemon.py b/daemon.py index 3b322a57e..bebb1ff27 100644 --- a/daemon.py +++ b/daemon.py @@ -7516,7 +7516,8 @@ class PubServer(BaseHTTPRequestHandler): if fields.get('removeTwitter'): if fields['removeTwitter'] == 'on': removeTwitterActive = True - with open(removeTwitterFilename, 'w+') as rFile: + with open(removeTwitterFilename, + 'w+') as rFile: rFile.write('\n') if not removeTwitterActive: if os.path.isfile(removeTwitterFilename): diff --git a/utils.py b/utils.py index f143bebbd..1a5e1b708 100644 --- a/utils.py +++ b/utils.py @@ -907,21 +907,19 @@ def searchBoxPosts(baseDir: str, nickname: str, domain: str, def getFileCaseInsensitive(path: str) -> str: """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): return path if path != path.lower(): if os.path.isfile(path.lower()): return path.lower() - directory, filename = os.path.split(path) - directory, filename = (directory or '.'), filename.lower() - for f in os.listdir(directory): - if f.lower() == filename: - newpath = os.path.join(directory, f) - if os.path.isfile(newpath): - return newpath - return path + # directory, filename = os.path.split(path) + # directory, filename = (directory or '.'), filename.lower() + # for f in os.listdir(directory): + # if f.lower() == filename: + # newpath = os.path.join(directory, f) + # if os.path.isfile(newpath): + # return newpath + return None def undoLikesCollectionEntry(recentPostsCache: {}, diff --git a/webinterface.py b/webinterface.py index 778565009..ad6691c2e 100644 --- a/webinterface.py +++ b/webinterface.py @@ -27,7 +27,6 @@ from matrix import getMatrixAddress from donate import getDonationUrl from utils import removeIdEnding from utils import getProtocolPrefixes -from utils import getFileCaseInsensitive from utils import searchBoxPosts from utils import isEventPost from utils import isBlogPost @@ -304,16 +303,13 @@ def getPersonAvatarUrl(baseDir: str, personUrl: str, personCache: {}, # get from locally stored image actorStr = personJson['id'].replace('/', '-') avatarImagePath = baseDir + '/cache/avatars/' + actorStr - if os.path.isfile(getFileCaseInsensitive(avatarImagePath + '.png')): - return '/avatars/' + actorStr + '.png' - elif os.path.isfile(getFileCaseInsensitive(avatarImagePath + '.jpg')): - return '/avatars/' + actorStr + '.jpg' - elif os.path.isfile(getFileCaseInsensitive(avatarImagePath + '.gif')): - return '/avatars/' + actorStr + '.gif' - elif os.path.isfile(getFileCaseInsensitive(avatarImagePath + '.webp')): - return '/avatars/' + actorStr + '.webp' - elif os.path.isfile(getFileCaseInsensitive(avatarImagePath)): - return '/avatars/' + actorStr + + imageExtension = ('png', 'jpg', 'jpeg', 'gif', 'webp') + for ext in imageExtension: + if os.path.isfile(avatarImagePath + '.' + ext): + return '/avatars/' + actorStr + '.' + ext + elif os.path.isfile(avatarImagePath.lower() + '.' + ext): + return '/avatars/' + actorStr.lower() + '.' + ext if personJson.get('icon'): if personJson['icon'].get('url'): @@ -6649,7 +6645,7 @@ def htmlCalendar(translate: {}, for weekOfMonth in range(1, 7): if dayOfMonth == daysInMonth: continue - calendarStr += ' \n' + calendarStr += ' \n' for dayNumber in range(1, 8): if (weekOfMonth > 1 and dayOfMonth < daysInMonth) or \ (weekOfMonth == 1 and dayNumber >= dow):