diff --git a/ipwb/replay.py b/ipwb/replay.py index 115cee24..b7a7a0e1 100755 --- a/ipwb/replay.py +++ b/ipwb/replay.py @@ -110,10 +110,10 @@ def upload_file(): # TODO: Check if semaphore lock exists, log it if so, wait for the lock # to be released, and create a new lock - print('Indexing file from uploaded WARC at {0} to {1}'.format( - warcPath, app.cdxjFilePath)) + print((f'Indexing file from uploaded WARC at' + f'{warcPath} to {app.cdxjFilePath}')) indexer.indexFileAt(warcPath, outfile=app.cdxjFilePath) - print('Index updated at {0}'.format(app.cdxjFilePath)) + print(f'Index updated at {app.cdxjFilePath}') # TODO: Release semaphore lock resp.location = request.referrer @@ -181,26 +181,25 @@ def showMementosForURIRs(urir): indexPath = ipwbUtils.getIPWBReplayIndexPath() - print('Getting CDXJ Lines with the URI-R {0} from {1}' - .format(urir, indexPath)) + print(f'Getting CDXJ lines with the URI-R {urir} from {indexPath}') cdxjLinesWithURIR = getCDXJLinesWithURIR(urir, indexPath) if len(cdxjLinesWithURIR) == 1: fields = cdxjLinesWithURIR[0].split(' ', 2) - redirectURI = '/memento/{1}/{0}'.format(unsurt(fields[0]), fields[1]) + redirectURI = f'/memento/{fields[1]}/{unsurt(fields[0])}' return redirect(redirectURI, code=302) msg = '' if cdxjLinesWithURIR: - msg += '
{0} capture(s) available:
{len(cdxjLinesWithURIR)} capture(s) available:
No captures found for {urir} at {datetime}.
' return Response(msg, status=404) @@ -247,7 +245,7 @@ def resolveMemento(urir, datetime): def compile_target_uri(url: str, query_string: bytes) -> str: """Append GET query string to the page path, to get full URI.""" if query_string: - return '{}?{}'.format(url, query_string.decode('utf-8')) + return f"{url}?{query_string.decode('utf-8')}" else: return url @@ -260,7 +258,7 @@ def showMemento(urir, datetime): try: datetime = ipwbUtils.padDigits14(datetime, validate=True) except ValueError as e: - msg = 'Expected a 4-14 digits valid datetime: {}'.format(datetime) + msg = f'Expected a 4-14 digits valid datetime: {datetime}' return Response(msg, status=400) resolvedMemento = resolveMemento(urir, datetime) @@ -270,7 +268,7 @@ def showMemento(urir, datetime): (newDatetime, linkHeader, uri) = resolvedMemento if newDatetime != datetime: - resp = redirect('/memento/{0}/{1}'.format(newDatetime, urir), code=302) + resp = redirect(f'/memento/{newDatetime}/{urir}', code=302) else: resp = show_uri(uri, newDatetime) @@ -300,7 +298,7 @@ def getCDXJLinesWithURIR(urir, indexPath): indexPath = getIndexFileFullPath(indexPath) - print('Getting CDXJ Lines with {0} in {1}'.format(urir, indexPath)) + print(f'Getting CDXJ lines with {urir} in {indexPath}') s = surt.surt(urir, path_strip_trailing_slash_unless_empty=False) cdxjLinesWithURIR = [] @@ -352,7 +350,7 @@ def queryTimeGate(urir): return resolvedMemento (newDatetime, linkHeader, uri) = resolvedMemento - resp = redirect('/memento/{0}/{1}'.format(newDatetime, urir), code=302) + resp = redirect(f'/memento/{newDatetime}/{urir}', code=302) resp.headers['Link'] = linkHeader resp.headers['Vary'] = 'Accept-Datetime' @@ -372,9 +370,7 @@ def showTimeMap(urir, format): hostAndPort = ipwbUtils.getIPWBReplayConfig() - tgURI = 'http://{0}:{1}/timegate/{2}'.format( - hostAndPort[0], - hostAndPort[1], urir) + tgURI = f'http://{hostAndPort[0]}:{hostAndPort[1]}/timegate/{urir}' tm = '' # Initialize for usage beyond below conditionals if format == 'link': @@ -399,13 +395,9 @@ def getLinkHeaderAbbreviatedTimeMap(urir, pivotDatetime): cdxjLinesWithURIR = getCDXJLinesWithURIR(urir, indexPath) hostAndPort = ipwbUtils.getIPWBReplayConfig() - tgURI = 'http://{0}:{1}/timegate/{2}'.format( - hostAndPort[0], - hostAndPort[1], urir) + tgURI = f'http://{hostAndPort[0]}:{hostAndPort[1]}/timegate/{urir}' - tmURI = 'http://{0}:{1}/timemap/link/{2}'.format( - hostAndPort[0], - hostAndPort[1], urir) + tmURI = f'http://{hostAndPort[0]}:{hostAndPort[1]}/timemap/link/{urir}' tm = generateLinkTimeMapFromCDXJLines(cdxjLinesWithURIR, s, tmURI, tgURI) # Fix base TM relation when viewing abbrev version in Link resp @@ -472,17 +464,17 @@ def generateLinkTimeMapFromCDXJLines(cdxjLines, original, tmself, tgURI): hostAndPort = urlunsplit(tmurl) + '/' # unsurted URI will never have a scheme, add one - originalURI = 'http://{0}'.format(unsurt(original)) + originalURI = f'http://{unsurt(original)}' - tmData = '<{0}>; rel="original",\n'.format(originalURI) - tmData += '<{0}>; rel="self timemap"; '.format(tmself) + tmData = f'<{originalURI}>; rel="original",\n' + tmData += f'<{tmself}>; rel="self timemap"; ' tmData += 'type="application/link-format",\n' cdxjTMURI = tmself.replace('/timemap/link/', '/timemap/cdxj/') - tmData += '<{0}>; rel="timemap"; '.format(cdxjTMURI) + tmData += f'<{cdxjTMURI}>; rel="timemap"; ' tmData += 'type="application/cdxj+ors",\n' - tmData += '<{0}>; rel="timegate"'.format(tgURI) + tmData += f'<{tgURI}>; rel="timegate"' for i, line in enumerate(cdxjLines): (surtURI, datetime, json) = line.split(' ', 2) @@ -497,9 +489,8 @@ def generateLinkTimeMapFromCDXJLines(cdxjLines, original, tmself, tgURI): elif len(cdxjLines) == 1: firstLastStr = 'first last ' - tmData += ',\n<{0}memento/{1}/{2}>; rel="{3}memento"; datetime="{4}"' \ - .format(hostAndPort, datetime, unsurt(surtURI), firstLastStr, - dtRFC1123) + tmData += (f',\n<{hostAndPort}memento/{datetime}/{unsurt(surtURI)}>; ' + f'rel="{firstLastStr}memento"; datetime="{dtRFC1123}"') return tmData + '\n' @@ -510,18 +501,18 @@ def generateCDXJTimeMapFromCDXJLines(cdxjLines, original, tmself, tgURI): tgURI = urlunsplit(getProxiedURIT(tgURI)) # unsurted URI will never have a scheme, add one - originalURI = 'http://{0}'.format(unsurt(original)) + originalURI = f'http://{unsurt(original)}' tmData = '!context ["http://tools.ietf.org/html/rfc7089"]\n' - tmData += '!id {{"uri": "{0}"}}\n'.format(tmself) + tmData += f'!id {{"uri": "{tmself}"}}\n' tmData += '!keys ["memento_datetime_YYYYMMDDhhmmss"]\n' - tmData += '!meta {{"original_uri": "{0}"}}\n'.format(originalURI) - tmData += '!meta {{"timegate_uri": "{0}"}}\n'.format(tgURI) + tmData += f'!meta {{"original_uri": "{originalURI}"}}\n' + tmData += f'!meta {{"timegate_uri": "{tgURI}"}}\n' linkTMURI = tmself.replace('/timemap/cdxj/', '/timemap/link/') - tmData += ('!meta {{"timemap_uri": {{' - '"link_format": "{0}", ' - '"cdxj_format": "{1}"' - '}}}}\n').format(linkTMURI, tmself) + tmData += (f'!meta {{"timemap_uri": {{' + f'"link_format": "{linkTMURI}", ' + f'"cdxj_format": "{tmself}"' + f'}}}}\n') hostAndPort = tmself[0:tmself.index('timemap/')] for i, line in enumerate(cdxjLines): @@ -537,12 +528,10 @@ def generateCDXJTimeMapFromCDXJLines(cdxjLines, original, tmself, tgURI): elif len(cdxjLines) == 1: firstLastStr = 'first last ' - tmData += ('{1} {{' - '"uri": "{0}memento/{1}/{2}", ' - '"rel": "{3}memento", ' - '"datetime"="{4}"}}\n').format( - hostAndPort, datetime, unsurt(surtURI), - firstLastStr, dtRFC1123) + tmData += (f'{datetime} {{' + f'"uri": "{hostAndPort}memento/{datetime}/{surtURI}", ' + f'"rel": "{firstLastStr}memento", ' + f'"datetime"="{dtRFC1123}"}}\n') return tmData @@ -604,8 +593,6 @@ def showLandingPage(): return render_template('index.html', summary=summary, uris=uris) -# TODO: Do we need this route? -@app.route('/No captures found for {urir}.
' - msg += ('').format(urir) + msg += (f'') return msg +@app.errorhandler(404) +def page_not_found(e): + return "Resource not found
", 404 + + def generateNoMementosInterface(path, datetime): msg = 'No captures found for {path} at {datetime}.
' linesWithSameURIR = getCDXJLinesWithURIR(path, None) - print('CDXJ lines with URI-R at {0}'.format(path)) + print(f'CDXJ lines with URI-R at {path}') print(linesWithSameURIR) # TODO: Use closest instead of conditioning on single entry # temporary fix for core functionality in #225 if len(linesWithSameURIR) == 1: fields = linesWithSameURIR[0].split(' ', 2) - redirectURI = '/{1}/{0}'.format(unsurt(fields[0]), fields[1]) + redirectURI = f'/{fields[1]}/{unsurt(fields[0])}' return redirect(redirectURI, code=302) urir = '' if linesWithSameURIR: - msg += '{0} capture(s) available:
{len(linesWithSameURIR)} capture(s) available:
TimeMaps: ' - msg += 'Link '.format(urir) - msg += 'CDXJ '.format(urir) + msg += f'Link ' + msg += f'CDXJ ' resp = Response(msg, status=404) linkHeader = getLinkHeaderAbbreviatedTimeMap(path, datetime) @@ -860,18 +851,18 @@ def generateDaemonStatusButton(): text = 'Running' buttonText = 'Stop' - statusPageHTML = ''.format(buttonText) + statusPageHTML = f'' statusPageHTML += ('