diff --git a/blog.py b/blog.py index 5acefa981..0822aa64a 100644 --- a/blog.py +++ b/blog.py @@ -28,7 +28,6 @@ from utils import firstParagraphFromString from posts import createBlogsTimeline from newswire import rss2Header from newswire import rss2Footer -from storage import readWholeFile def _noOfBlogReplies(baseDir: str, httpPrefix: str, translate: {}, @@ -745,9 +744,8 @@ def htmlEditBlog(mediaInstance: bool, translate: {}, editBlogText = '' + translate['Write your post text below.'] + '' if os.path.isfile(baseDir + '/accounts/newpost.txt'): - newPostStr = readWholeFile(baseDir + '/accounts/newpost.txt') - if newPostStr: - editBlogText = '

' + newPostStr + '

' + with open(baseDir + '/accounts/newpost.txt', 'r') as file: + editBlogText = '

' + file.read() + '

' cssFilename = baseDir + '/epicyon-profile.css' if os.path.isfile(baseDir + '/epicyon.css'): diff --git a/bookmarks.py b/bookmarks.py index f5f55fe96..3858d22e7 100644 --- a/bookmarks.py +++ b/bookmarks.py @@ -25,7 +25,6 @@ from utils import saveJson from posts import getPersonBox from session import postJson from storage import storeValue -from storage import readWholeFile def undoBookmarksCollectionEntry(recentPostsCache: {}, @@ -60,9 +59,10 @@ def undoBookmarksCollectionEntry(recentPostsCache: {}, bookmarkIndex = bookmarkIndex.replace('\n', '').replace('\r', '') if bookmarkIndex not in open(bookmarksIndexFilename).read(): return - indexStr = readWholeFile(bookmarksIndexFilename) - indexStr = indexStr.replace(bookmarkIndex + '\n', '') - storeValue(bookmarksIndexFilename, indexStr, 'writeonly') + indexStr = '' + with open(bookmarksIndexFilename, 'r') as indexFile: + indexStr = indexFile.read().replace(bookmarkIndex + '\n', '') + storeValue(bookmarksIndexFilename, indexStr, 'writeonly') if not postJsonObject.get('type'): return diff --git a/categories.py b/categories.py index 32247ccf2..67c65ba15 100644 --- a/categories.py +++ b/categories.py @@ -10,7 +10,6 @@ __module_group__ = "RSS Feeds" import os import datetime from storage import storeValue -from storage import readWholeFile def getHashtagCategory(baseDir: str, hashtag: str) -> str: @@ -25,9 +24,10 @@ def getHashtagCategory(baseDir: str, hashtag: str) -> str: if not os.path.isfile(categoryFilename): return '' - categoryStr = readWholeFile(categoryFilename) - if categoryStr: - return categoryStr + with open(categoryFilename, 'r') as fp: + categoryStr = fp.read() + if categoryStr: + return categoryStr return '' @@ -53,10 +53,12 @@ def getHashtagCategories(baseDir: str, hashtag = f.split('.')[0] if len(hashtag) > maxTagLength: continue - categoryStr = readWholeFile(categoryFilename) - if not categoryStr: - continue - if categoryStr: + with open(categoryFilename, 'r') as fp: + categoryStr = fp.read() + + if not categoryStr: + continue + if category: # only return a dictionary for a specific category if categoryStr != category: diff --git a/city.py b/city.py index 12d5ba570..1726966d3 100644 --- a/city.py +++ b/city.py @@ -12,7 +12,6 @@ import datetime import random import math from random import randint -from storage import readWholeFile # states which the simulated city dweller can be in PERSON_SLEEP = 0 @@ -295,7 +294,8 @@ def getSpoofedCity(city: str, baseDir: str, nickname: str, domain: str) -> str: cityFilename = baseDir + '/accounts/' + \ nickname + '@' + domain + '/city.txt' if os.path.isfile(cityFilename): - city = readWholeFile(cityFilename).replace('\n', '') + with open(cityFilename, 'r') as fp: + city = fp.read().replace('\n', '') return city diff --git a/content.py b/content.py index 89821b983..22ba06f0b 100644 --- a/content.py +++ b/content.py @@ -19,7 +19,6 @@ from utils import dangerousMarkup from utils import isPGPEncrypted from utils import containsPGPPublicKey from petnames import getPetName -from storage import readWholeFile def removeHtmlTag(htmlStr: str, tag: str) -> str: @@ -170,9 +169,8 @@ def dangerousCSS(filename: str, allowLocalNetworkAccess: bool) -> bool: if not os.path.isfile(filename): return False - content = readWholeFile(filename) - if content: - content = content.lower() + with open(filename, 'r') as fp: + content = fp.read().lower() cssMatches = ('behavior:', ':expression', '?php', '.php', 'google', 'regexp', 'localhost', diff --git a/daemon.py b/daemon.py index 6ca766959..32ee60d6e 100644 --- a/daemon.py +++ b/daemon.py @@ -301,7 +301,6 @@ from speaker import getSSMLbox from city import getSpoofedCity import os from storage import storeValue -from storage import readWholeFile # maximum number of posts to list in outbox feed @@ -666,7 +665,11 @@ class PubServer(BaseHTTPRequestHandler): # self.send_header('Cache-Control', 'public, max-age=86400') etag = None if os.path.isfile(mediaFilename + '.etag'): - etag = readWholeFile(mediaFilename + '.etag') + try: + with open(mediaFilename + '.etag', 'r') as etagFile: + etag = etagFile.read() + except BaseException: + pass if not etag: etag = sha1(data).hexdigest() # nosec storeValue(mediaFilename + '.etag', etag, 'writeonly') @@ -687,7 +690,12 @@ class PubServer(BaseHTTPRequestHandler): oldEtag = self.headers['If-None-Match'] if os.path.isfile(mediaFilename + '.etag'): # load the etag from file - currEtag = readWholeFile(mediaFilename) + currEtag = '' + try: + with open(mediaFilename, 'r') as etagFile: + currEtag = etagFile.read() + except BaseException: + pass if oldEtag == currEtag: # The file has not changed return True @@ -1524,7 +1532,12 @@ class PubServer(BaseHTTPRequestHandler): loginNickname + '@' + domain + '/.salt' salt = createPassword(32) if os.path.isfile(saltFilename): - salt = readWholeFile(saltFilename) + try: + with open(saltFilename, 'r') as fp: + salt = fp.read() + except Exception as e: + print('WARN: Unable to read salt for ' + + loginNickname + ' ' + str(e)) else: storeValue(saltFilename, salt, 'writeonly') @@ -13095,7 +13108,11 @@ class PubServer(BaseHTTPRequestHandler): fileLength = os.path.getsize(mediaFilename) mediaTagFilename = mediaFilename + '.etag' if os.path.isfile(mediaTagFilename): - etag = readWholeFile(mediaTagFilename) + try: + with open(mediaTagFilename, 'r') as etagFile: + etag = etagFile.read() + except BaseException: + pass else: with open(mediaFilename, 'rb') as avFile: mediaBinary = avFile.read() @@ -14839,7 +14856,13 @@ def loadTokens(baseDir: str, tokensDict: {}, tokensLookup: {}) -> None: if not os.path.isfile(tokenFilename): continue nickname = handle.split('@')[0] - token = readWholeFile(tokenFilename) + token = None + try: + with open(tokenFilename, 'r') as fp: + token = fp.read() + except Exception as e: + print('WARN: Unable to read token for ' + + nickname + ' ' + str(e)) if not token: continue tokensDict[nickname] = token diff --git a/desktop_client.py b/desktop_client.py index d1dc12fbd..ee7fccf83 100644 --- a/desktop_client.py +++ b/desktop_client.py @@ -57,7 +57,6 @@ from bookmarks import sendUndoBookmarkViaServer from delete import sendDeleteViaServer from person import getActorJson from storage import storeValue -from storage import readWholeFile def _desktopHelp() -> None: @@ -285,9 +284,10 @@ def _desktopShowBanner() -> None: bannerFilename = 'theme/' + bannerTheme + '/banner.txt' if not os.path.isfile(bannerFilename): return - banner = readWholeFile(bannerFilename) - if banner: - print(banner + '\n') + with open(bannerFilename, 'r') as bannerFile: + banner = bannerFile.read() + if banner: + print(banner + '\n') def _desktopWaitForCmd(timeout: int, debug: bool) -> str: diff --git a/follow.py b/follow.py index 1387c2c8a..9eb158491 100644 --- a/follow.py +++ b/follow.py @@ -31,8 +31,6 @@ from auth import createBasicAuthHeader from session import getJson from session import postJson from storage import storeValue -from storage import readWholeFile -from storage import readFileLines def createInitialLastSeen(baseDir: str, httpPrefix: str) -> None: @@ -51,8 +49,8 @@ def createInitialLastSeen(baseDir: str, httpPrefix: str) -> None: lastSeenDir = accountDir + '/lastseen' if not os.path.isdir(lastSeenDir): os.mkdir(lastSeenDir) - followingHandles = readFileLines(followingFilename) - if followingHandles: + with open(followingFilename, 'r') as fp: + followingHandles = fp.readlines() for handle in followingHandles: if '#' in handle: continue @@ -216,7 +214,9 @@ def isFollowerOfPerson(baseDir: str, nickname: str, domain: str, alreadyFollowing = False - followersStr = readWholeFile(followersFile) + followersStr = '' + with open(followersFile, 'r') as fpFollowers: + followersStr = fpFollowers.read() if handle in followersStr: alreadyFollowing = True @@ -556,7 +556,9 @@ def _storeFollowRequest(baseDir: str, if os.path.isfile(followersFilename): alreadyFollowing = False - followersStr = readWholeFile(followersFilename) + followersStr = '' + with open(followersFilename, 'r') as fpFollowers: + followersStr = fpFollowers.read() if approveHandle in followersStr: alreadyFollowing = True diff --git a/followingCalendar.py b/followingCalendar.py index 24cd9da12..3b4665dbe 100644 --- a/followingCalendar.py +++ b/followingCalendar.py @@ -8,7 +8,6 @@ __status__ = "Production" __module_group__ = "Calendar" import os -from storage import readWholeFile from storage import storeValue @@ -30,8 +29,9 @@ def receivingCalendarEvents(baseDir: str, nickname: str, domain: str, if not os.path.isfile(followingFilename): return False # create a new calendar file from the following file - followingHandles = readWholeFile(followingFilename) - storeValue(calendarFilename, followingHandles, 'writeonly') + with open(followingFilename, 'r') as followingFile: + followingHandles = followingFile.read() + storeValue(calendarFilename, followingHandles, 'writeonly') return handle + '\n' in open(calendarFilename).read() @@ -66,11 +66,14 @@ def _receiveCalendarEvents(baseDir: str, nickname: str, domain: str, followingHandles = '' if os.path.isfile(calendarFilename): print('Calendar file exists') - followingHandles = readWholeFile(calendarFilename) + with open(calendarFilename, 'r') as calendarFile: + followingHandles = calendarFile.read() else: # create a new calendar file from the following file print('Creating calendar file ' + calendarFilename) - followingHandles = readWholeFile(followingFilename) + followingHandles = '' + with open(followingFilename, 'r') as followingFile: + followingHandles = followingFile.read() if add: storeValue(calendarFilename, followingHandles + handle, 'write') diff --git a/happening.py b/happening.py index efb5c7c3f..1bc8685a5 100644 --- a/happening.py +++ b/happening.py @@ -17,7 +17,6 @@ from utils import loadJson from utils import saveJson from utils import locatePost from storage import storeValue -from storage import readWholeFile def _validUuid(testUuid: str, version=4): @@ -36,9 +35,9 @@ def _removeEventFromTimeline(eventId: str, tlEventsFilename: str) -> None: """ if eventId + '\n' not in open(tlEventsFilename).read(): return - eventsTimeline = readWholeFile(tlEventsFilename) - eventsTimeline = eventsTimeline.replace(eventId + '\n', '') - storeValue(tlEventsFilename, eventsTimeline, 'writeonly') + with open(tlEventsFilename, 'r') as fp: + eventsTimeline = fp.read().replace(eventId + '\n', '') + storeValue(tlEventsFilename, eventsTimeline, 'writeonly') def saveEventPost(baseDir: str, handle: str, postId: str, diff --git a/inbox.py b/inbox.py index 033f6b0d5..e80fc9071 100644 --- a/inbox.py +++ b/inbox.py @@ -84,7 +84,6 @@ from context import hasValidContext from speaker import updateSpeaker from announce import isSelfAnnounce from storage import storeValue -from storage import readWholeFile def storeHashTags(baseDir: str, nickname: str, postJsonObject: {}) -> None: @@ -1880,9 +1879,10 @@ def _likeNotify(baseDir: str, domain: str, onionDomain: str, # was there a previous like notification? if os.path.isfile(prevLikeFile): # is it the same as the current notification ? - prevLikeStr = readWholeFile(prevLikeFile) - if prevLikeStr == likeStr: - return + with open(prevLikeFile, 'r') as fp: + prevLikeStr = fp.read() + if prevLikeStr == likeStr: + return storeValue(prevLikeFile, likeStr, 'writeonly') storeValue(likeFile, likeStr, 'writeonly') @@ -2108,8 +2108,8 @@ def _updateLastSeen(baseDir: str, handle: str, actor: str) -> None: daysSinceEpoch = (currTime - datetime.datetime(1970, 1, 1)).days # has the value changed? if os.path.isfile(lastSeenFilename): - daysSinceEpochFile = readWholeFile(lastSeenFilename) - if daysSinceEpochFile: + with open(lastSeenFilename, 'r') as lastSeenFile: + daysSinceEpochFile = lastSeenFile.read() if int(daysSinceEpochFile) == daysSinceEpoch: # value hasn't changed, so we can save writing anything to file return diff --git a/manualapprove.py b/manualapprove.py index 9c7fdd558..7e97543b4 100644 --- a/manualapprove.py +++ b/manualapprove.py @@ -12,7 +12,6 @@ from follow import followedAccountAccepts from follow import followedAccountRejects from follow import removeFromFollowRequests from utils import loadJson -from storage import readWholeFile from storage import storeValue @@ -97,7 +96,9 @@ def manualApproveFollowRequest(session, baseDir: str, return # is the handle in the requests file? - approveFollowsStr = readWholeFile(approveFollowsFilename) + approveFollowsStr = '' + with open(approveFollowsFilename, 'r') as fpFollowers: + approveFollowsStr = fpFollowers.read() exists = False approveHandleFull = approveHandle if approveHandle in approveFollowsStr: diff --git a/media.py b/media.py index 0182222ba..5530f60f5 100644 --- a/media.py +++ b/media.py @@ -21,7 +21,6 @@ from shutil import copyfile from shutil import rmtree from shutil import move from city import spoofGeolocation -from storage import readWholeFile from storage import storeValue @@ -71,10 +70,8 @@ def _spoofMetaData(baseDir: str, nickname: str, domain: str, baseDir + '/accounts/' + nickname + '@' + domain + '/decoyseed' decoySeed = 63725 if os.path.isfile(decoySeedFilename): - decoySeedStr = readWholeFile(decoySeedFilename) - if decoySeedStr: - if decoySeedStr.isdigit(): - decoySeed = int(decoySeedStr) + with open(decoySeedFilename, 'r') as fp: + decoySeed = int(fp.read()) else: decoySeed = randint(10000, 10000000000000000) decoySeedStr = str(decoySeed) diff --git a/newsdaemon.py b/newsdaemon.py index 8c43b0b55..c816417dd 100644 --- a/newsdaemon.py +++ b/newsdaemon.py @@ -34,7 +34,6 @@ from utils import clearFromPostCaches from utils import dangerousMarkup from inbox import storeHashTags from session import createSession -from storage import readWholeFile from storage import storeValue @@ -399,8 +398,9 @@ def _createNewsMirror(baseDir: str, domain: str, # remove the corresponding index entries if removals: - indexContent = readWholeFile(mirrorIndexFilename) - if indexContent: + indexContent = '' + with open(mirrorIndexFilename, 'r') as indexFile: + indexContent = indexFile.read() for removePostId in removals: indexContent = \ indexContent.replace(removePostId + '\n', '') diff --git a/newswire.py b/newswire.py index 8a877d07f..51c773994 100644 --- a/newswire.py +++ b/newswire.py @@ -29,8 +29,6 @@ from utils import removeHtml from blocking import isBlockedDomain from blocking import isBlockedHashtag from filters import isFiltered -from storage import readWholeFile -from storage import readFileLines def _removeCDATA(text: str) -> str: @@ -221,8 +219,8 @@ def loadHashtagCategories(baseDir: str, language: str) -> None: if not os.path.isfile(hashtagCategoriesFilename): return - xmlStr = readWholeFile(hashtagCategoriesFilename) - if xmlStr: + with open(hashtagCategoriesFilename, 'r') as fp: + xmlStr = fp.read() _xml2StrToHashtagCategories(baseDir, xmlStr, 1024, True) @@ -1016,7 +1014,9 @@ def getDictFromNewswire(session, baseDir: str, domain: str, maxPostsPerSource = 5 # add rss feeds - rssFeed = readFileLines(subscriptionsFilename) + rssFeed = [] + with open(subscriptionsFilename, 'r') as fp: + rssFeed = fp.readlines() result = {} for url in rssFeed: url = url.strip() diff --git a/person.py b/person.py index eaca21988..f9052ff86 100644 --- a/person.py +++ b/person.py @@ -52,7 +52,6 @@ from session import createSession from session import getJson from webfinger import webfingerHandle from pprint import pprint -from storage import readWholeFile from storage import storeValue @@ -1122,7 +1121,9 @@ def isPersonSnoozed(baseDir: str, nickname: str, domain: str, replaceStr = line break if replaceStr: - content = readWholeFile(snoozedFilename).replace(replaceStr, '') + content = None + with open(snoozedFilename, 'r') as snoozedFile: + content = snoozedFile.read().replace(replaceStr, '') if content: storeValue(snoozedFilename, content, 'writeonly') @@ -1167,7 +1168,9 @@ def personUnsnooze(baseDir: str, nickname: str, domain: str, replaceStr = line break if replaceStr: - content = readWholeFile(snoozedFilename).replace(replaceStr, '') + content = None + with open(snoozedFilename, 'r') as snoozedFile: + content = snoozedFile.read().replace(replaceStr, '') if content: storeValue(snoozedFilename, content, 'writeonly') diff --git a/petnames.py b/petnames.py index 7d4ffd4f5..52ece3c3f 100644 --- a/petnames.py +++ b/petnames.py @@ -7,7 +7,6 @@ __email__ = "bob@freedombone.net" __status__ = "Production" import os -from storage import readWholeFile from storage import storeValue @@ -29,8 +28,8 @@ def setPetName(baseDir: str, nickname: str, domain: str, # does this entry already exist? if os.path.isfile(petnamesFilename): - petnamesStr = readWholeFile(petnamesFilename) - if petnamesStr: + with open(petnamesFilename, 'r') as petnamesFile: + petnamesStr = petnamesFile.read() if entry in petnamesStr: return True if ' ' + handle + '\n' in petnamesStr: @@ -66,8 +65,8 @@ def getPetName(baseDir: str, nickname: str, domain: str, if not os.path.isfile(petnamesFilename): return '' - petnamesStr = readWholeFile(petnamesFilename) - if petnamesStr: + with open(petnamesFilename, 'r') as petnamesFile: + petnamesStr = petnamesFile.read() if ' ' + handle + '\n' in petnamesStr: petnamesList = petnamesStr.split('\n') for pet in petnamesList: @@ -87,8 +86,8 @@ def _getPetNameHandle(baseDir: str, nickname: str, domain: str, if not os.path.isfile(petnamesFilename): return '' - petnamesStr = readWholeFile(petnamesFilename) - if petnamesStr: + with open(petnamesFilename, 'r') as petnamesFile: + petnamesStr = petnamesFile.read() if petname + ' ' in petnamesStr: petnamesList = petnamesStr.split('\n') for pet in petnamesList: diff --git a/posts.py b/posts.py index e7d0c6328..05291fedf 100644 --- a/posts.py +++ b/posts.py @@ -72,7 +72,6 @@ from git import convertPostToPatch from linked_data_sig import generateJsonSignature from petnames import resolvePetnames from storage import storeValue -from storage import readWholeFile def isModerator(baseDir: str, nickname: str) -> bool: @@ -3023,8 +3022,9 @@ def _addPostToTimeline(filePath: str, boxname: str, postsInBox: [], boxActor: str) -> bool: """ Reads a post from file and decides whether it is valid """ - postStr = readWholeFile(filePath) - if postStr: + with open(filePath, 'r') as postFile: + postStr = postFile.read() + if filePath.endswith('.json'): repliesFilename = filePath.replace('.json', '.replies') if os.path.isfile(repliesFilename): @@ -3715,7 +3715,9 @@ def getPublicPostDomainsBlocked(session, baseDir: str, return [] # read the blocked domains as a single string - blockedStr = readWholeFile(blockingFilename) + blockedStr = '' + with open(blockingFilename, 'r') as fp: + blockedStr = fp.read() blockedDomains = [] for domainName in postDomains: @@ -3764,7 +3766,8 @@ def checkDomains(session, baseDir: str, updateFollowerWarnings = False followerWarningStr = '' if os.path.isfile(followerWarningFilename): - followerWarningStr = readWholeFile(followerWarningFilename) + with open(followerWarningFilename, 'r') as fp: + followerWarningStr = fp.read() if singleCheck: # checks a single random non-mutual diff --git a/speaker.py b/speaker.py index c1d61042d..a04ef3709 100644 --- a/speaker.py +++ b/speaker.py @@ -22,8 +22,6 @@ from utils import loadJson from utils import saveJson from utils import isPGPEncrypted from content import htmlReplaceQuoteMarks -from storage import readFileLines -from storage import readWholeFile speakerRemoveChars = ('.\n', '. ', ',', ';', '?', '!') @@ -137,8 +135,8 @@ def _speakerPronounce(baseDir: str, sayText: str, translate: {}) -> str: ")": "," } if os.path.isfile(pronounceFilename): - pronounceList = readFileLines(pronounceFilename) - if pronounceList: + with open(pronounceFilename, 'r') as fp: + pronounceList = fp.readlines() for conversion in pronounceList: separator = None if '->' in conversion: @@ -496,8 +494,8 @@ def _postToSpeakerJson(baseDir: str, httpPrefix: str, accountsDir = baseDir + '/accounts/' + nickname + '@' + domainFull approveFollowsFilename = accountsDir + '/followrequests.txt' if os.path.isfile(approveFollowsFilename): - follows = readFileLines(approveFollowsFilename) - if follows: + with open(approveFollowsFilename, 'r') as fp: + follows = fp.readlines() if len(follows) > 0: followRequestsExist = True for i in range(len(follows)): @@ -514,7 +512,8 @@ def _postToSpeakerJson(baseDir: str, httpPrefix: str, likedBy = '' likeFilename = accountsDir + '/.newLike' if os.path.isfile(likeFilename): - likedBy = readWholeFile(likeFilename) + with open(likeFilename, 'r') as fp: + likedBy = fp.read() calendarFilename = accountsDir + '/.newCalendar' postCal = os.path.isfile(calendarFilename) shareFilename = accountsDir + '/.newShare' diff --git a/storage.py b/storage.py index d78d35648..0417796f0 100644 --- a/storage.py +++ b/storage.py @@ -54,19 +54,3 @@ def storeValue(filename: str, lineStr: str, storeType: str) -> bool: print('WARN: Unable to prepend to ' + filename + ' ' + str(e)) return False - - -def readWholeFile(filename: str) -> str: - """Returns the entire contents of a file - """ - with open(filename, 'r') as fp: - return fp.read() - return '' - - -def readFileLines(filename: str) -> []: - """Returns a list of lines from a file - """ - with open(filename, 'r') as fp: - return fp.readlines() - return [] diff --git a/theme.py b/theme.py index 600faadbf..5fa2bac01 100644 --- a/theme.py +++ b/theme.py @@ -17,7 +17,6 @@ from shutil import unpack_archive from shutil import rmtree from content import dangerousCSS from storage import storeValue -from storage import readWholeFile def importTheme(baseDir: str, filename: str) -> bool: @@ -36,9 +35,9 @@ def importTheme(baseDir: str, filename: str) -> bool: print('WARN: ' + themeFile + ' missing from imported theme') return False - newThemeName = readWholeFile(tempThemeDir + '/name.txt') - if newThemeName: - newThemeName = newThemeName.replace('\n', '').replace('\r', '') + newThemeName = None + with open(tempThemeDir + '/name.txt', 'r') as fp: + newThemeName = fp.read().replace('\n', '').replace('\r', '') if len(newThemeName) > 20: print('WARN: Imported theme name is too long') return False @@ -328,8 +327,8 @@ def _setThemeFromDict(baseDir: str, name: str, if not os.path.isfile(templateFilename): continue - css = readWholeFile(templateFilename) - if css: + with open(templateFilename, 'r') as cssfile: + css = cssfile.read() for paramName, paramValue in themeParams.items(): if paramName == 'newswire-publish-icon': if paramValue.lower() == 'true': @@ -386,8 +385,8 @@ def _setBackgroundFormat(baseDir: str, name: str, cssFilename = baseDir + '/' + backgroundType + '.css' if not os.path.isfile(cssFilename): return - css = readWholeFile(cssFilename) - if css: + with open(cssFilename, 'r') as cssfile: + css = cssfile.read() css = css.replace('background.jpg', 'background.' + extension) storeValue(cssFilename, css, 'writeonly') @@ -400,8 +399,8 @@ def enableGrayscale(baseDir: str) -> None: templateFilename = baseDir + '/' + filename if not os.path.isfile(templateFilename): continue - css = readWholeFile(templateFilename) - if css: + with open(templateFilename, 'r') as cssfile: + css = cssfile.read() if 'grayscale' not in css: css = \ css.replace('body, html {', @@ -421,8 +420,8 @@ def disableGrayscale(baseDir: str) -> None: templateFilename = baseDir + '/' + filename if not os.path.isfile(templateFilename): continue - css = readWholeFile(templateFilename) - if css: + with open(templateFilename, 'r') as cssfile: + css = cssfile.read() if 'grayscale' in css: css = \ css.replace('\n filter: grayscale(100%);', '') @@ -457,8 +456,8 @@ def _setCustomFont(baseDir: str): templateFilename = baseDir + '/' + filename if not os.path.isfile(templateFilename): continue - css = readWholeFile(templateFilename) - if css: + with open(templateFilename, 'r') as cssfile: + css = cssfile.read() css = \ setCSSparam(css, "*src", "url('./fonts/custom." + @@ -541,9 +540,10 @@ def getTextModeBanner(baseDir: str) -> str: """ textModeBannerFilename = baseDir + '/accounts/banner.txt' if os.path.isfile(textModeBannerFilename): - bannerStr = readWholeFile(textModeBannerFilename) - if bannerStr: - return bannerStr.replace('\n', '
') + with open(textModeBannerFilename, 'r') as fp: + bannerStr = fp.read() + if bannerStr: + return bannerStr.replace('\n', '
') return None @@ -554,9 +554,10 @@ def getTextModeLogo(baseDir: str) -> str: if not os.path.isfile(textModeLogoFilename): textModeLogoFilename = baseDir + '/img/logo.txt' - logoStr = readWholeFile(textModeLogoFilename) - if logoStr: - return logoStr.replace('\n', '
') + with open(textModeLogoFilename, 'r') as fp: + logoStr = fp.read() + if logoStr: + return logoStr.replace('\n', '
') return None diff --git a/utils.py b/utils.py index 41abdba78..85d24364e 100644 --- a/utils.py +++ b/utils.py @@ -19,7 +19,6 @@ from followingCalendar import addPersonToCalendar from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import hashes from storage import storeValue -from storage import readWholeFile # posts containing these strings will always get screened out, # both incoming and outgoing. @@ -164,8 +163,8 @@ def isDormant(baseDir: str, nickname: str, domain: str, actor: str, if not os.path.isfile(lastSeenFilename): return False - daysSinceEpochStr = readWholeFile(lastSeenFilename) - if daysSinceEpochStr: + with open(lastSeenFilename, 'r') as lastSeenFile: + daysSinceEpochStr = lastSeenFile.read() daysSinceEpoch = int(daysSinceEpochStr) currTime = datetime.datetime.utcnow() currDaysSinceEpoch = (currTime - datetime.datetime(1970, 1, 1)).days @@ -507,8 +506,8 @@ def loadJson(filename: str, delaySec=2, maxTries=5) -> {}: tries = 0 while tries < maxTries: try: - data = readWholeFile(filename) - if data: + with open(filename, 'r') as fp: + data = fp.read() jsonObject = json.loads(data) break except BaseException: @@ -528,13 +527,14 @@ def loadJsonOnionify(filename: str, domain: str, onionDomain: str, tries = 0 while tries < 5: try: - data = readWholeFile(filename) - if data: - data = data.replace(domain, onionDomain) - data = data.replace('https:', 'http:') - print('*****data: ' + data) - jsonObject = json.loads(data) - break + with open(filename, 'r') as fp: + data = fp.read() + if data: + data = data.replace(domain, onionDomain) + data = data.replace('https:', 'http:') + print('*****data: ' + data) + jsonObject = json.loads(data) + break except BaseException: print('WARN: loadJson exception') if delaySec > 0: @@ -942,13 +942,14 @@ def _setDefaultPetName(baseDir: str, nickname: str, domain: str, storeValue(petnamesFilename, petnameLookupEntry, 'writeonly') return - petnamesStr = readWholeFile(petnamesFilename) - if petnamesStr: - petnamesList = petnamesStr.split('\n') - for pet in petnamesList: - if pet.startswith(followNickname + ' '): - # petname already exists - return + with open(petnamesFilename, 'r') as petnamesFile: + petnamesStr = petnamesFile.read() + if petnamesStr: + petnamesList = petnamesStr.split('\n') + for pet in petnamesList: + if pet.startswith(followNickname + ' '): + # petname already exists + return # petname doesn't already exist storeValue(petnamesFilename, petnameLookupEntry, 'append') @@ -1093,12 +1094,13 @@ def locateNewsArrival(baseDir: str, domain: str, accountDir = baseDir + '/accounts/news@' + domain + '/' postFilename = accountDir + 'outbox/' + postUrl if os.path.isfile(postFilename): - arrival = readWholeFile(postFilename) - if arrival: - arrivalDate = \ - datetime.datetime.strptime(arrival, - "%Y-%m-%dT%H:%M:%SZ") - return arrivalDate + with open(postFilename, 'r') as arrivalFile: + arrival = arrivalFile.read() + if arrival: + arrivalDate = \ + datetime.datetime.strptime(arrival, + "%Y-%m-%dT%H:%M:%SZ") + return arrivalDate return None @@ -1486,8 +1488,8 @@ def noOfActiveAccountsMonthly(baseDir: str, months: int) -> bool: lastUsedFilename = \ baseDir + '/accounts/' + account + '/.lastUsed' if os.path.isfile(lastUsedFilename): - lastUsed = readWholeFile(lastUsedFilename) - if lastUsed: + with open(lastUsedFilename, 'r') as lastUsedFile: + lastUsed = lastUsedFile.read() if lastUsed.isdigit(): timeDiff = (currTime - int(lastUsed)) if timeDiff < monthSeconds: @@ -1643,8 +1645,8 @@ def getCSS(baseDir: str, cssFilename: str, cssCache: {}) -> str: # file hasn't changed, so return the version in the cache return cssCache[cssFilename][1] - css = readWholeFile(cssFilename) - if css: + with open(cssFilename, 'r') as fpCSS: + css = fpCSS.read() if cssCache.get(cssFilename): # alter the cache contents cssCache[cssFilename][0] = lastModified @@ -1754,8 +1756,9 @@ def _searchVirtualBoxPosts(baseDir: str, nickname: str, domain: str, postFilename = path + '/' + postFilename.strip() if not os.path.isfile(postFilename): continue - data = readWholeFile(postFilename).lower() - if data: + with open(postFilename, 'r') as postFile: + data = postFile.read().lower() + notFound = False for keyword in searchWords: if keyword not in data: @@ -1796,8 +1799,9 @@ def searchBoxPosts(baseDir: str, nickname: str, domain: str, for root, dirs, fnames in os.walk(path): for fname in fnames: filePath = os.path.join(root, fname) - data = readWholeFile(filePath).lower() - if data: + with open(filePath, 'r') as postFile: + data = postFile.read().lower() + notFound = False for keyword in searchWords: if keyword not in data: diff --git a/webapp_about.py b/webapp_about.py index a9f468007..233d052f7 100644 --- a/webapp_about.py +++ b/webapp_about.py @@ -13,7 +13,6 @@ from utils import getConfigParam from webapp_utils import htmlHeaderWithWebsiteMarkup from webapp_utils import htmlFooter from webapp_utils import markdownToHtml -from storage import readWholeFile def htmlAbout(cssCache: {}, baseDir: str, httpPrefix: str, @@ -33,9 +32,8 @@ def htmlAbout(cssCache: {}, baseDir: str, httpPrefix: str, aboutText = 'Information about this instance goes here.' if os.path.isfile(baseDir + '/accounts/about.md'): - aboutText = readWholeFile(baseDir + '/accounts/about.md') - if aboutText: - aboutText = markdownToHtml(aboutText) + with open(baseDir + '/accounts/about.md', 'r') as aboutFile: + aboutText = markdownToHtml(aboutFile.read()) aboutForm = '' cssFilename = baseDir + '/epicyon-profile.css' diff --git a/webapp_column_left.py b/webapp_column_left.py index 5fc2cfe82..5428650f0 100644 --- a/webapp_column_left.py +++ b/webapp_column_left.py @@ -18,7 +18,6 @@ from webapp_utils import headerButtonsFrontScreen from webapp_utils import htmlHeaderWithExternalStyle from webapp_utils import htmlFooter from webapp_utils import getBannerFile -from storage import readWholeFile def _linksExist(baseDir: str) -> bool: @@ -404,7 +403,8 @@ def htmlEditLinks(cssCache: {}, translate: {}, baseDir: str, path: str, linksFilename = baseDir + '/accounts/links.txt' linksStr = '' if os.path.isfile(linksFilename): - linksStr = readWholeFile(linksFilename) + with open(linksFilename, 'r') as fp: + linksStr = fp.read() editLinksForm += \ '
' @@ -426,7 +426,8 @@ def htmlEditLinks(cssCache: {}, translate: {}, baseDir: str, path: str, aboutFilename = baseDir + '/accounts/about.md' aboutStr = '' if os.path.isfile(aboutFilename): - aboutStr = readWholeFile(aboutFilename) + with open(aboutFilename, 'r') as fp: + aboutStr = fp.read() editLinksForm += \ '
' @@ -444,7 +445,8 @@ def htmlEditLinks(cssCache: {}, translate: {}, baseDir: str, path: str, TOSFilename = baseDir + '/accounts/tos.md' TOSStr = '' if os.path.isfile(TOSFilename): - TOSStr = readWholeFile(TOSFilename) + with open(TOSFilename, 'r') as fp: + TOSStr = fp.read() editLinksForm += \ '
' diff --git a/webapp_column_right.py b/webapp_column_right.py index 6a5664630..0d194c1b0 100644 --- a/webapp_column_right.py +++ b/webapp_column_right.py @@ -24,7 +24,6 @@ from webapp_utils import htmlFooter from webapp_utils import getBannerFile from webapp_utils import htmlPostSeparator from webapp_utils import headerButtonsFrontScreen -from storage import readWholeFile def _votesIndicator(totalVotes: int, positiveVoting: bool) -> str: @@ -577,7 +576,8 @@ def htmlEditNewswire(cssCache: {}, translate: {}, baseDir: str, path: str, newswireFilename = baseDir + '/accounts/newswire.txt' newswireStr = '' if os.path.isfile(newswireFilename): - newswireStr = readWholeFile(newswireFilename) + with open(newswireFilename, 'r') as fp: + newswireStr = fp.read() editNewswireForm += \ '
' @@ -595,7 +595,8 @@ def htmlEditNewswire(cssCache: {}, translate: {}, baseDir: str, path: str, filterFilename = \ baseDir + '/accounts/news@' + domain + '/filters.txt' if os.path.isfile(filterFilename): - filterStr = readWholeFile(filterFilename) + with open(filterFilename, 'r') as filterfile: + filterStr = filterfile.read() editNewswireForm += \ '