Skip to content

Commit a57ceb0

Browse files
committed
Use removeprefix
1 parent 35ff0fd commit a57ceb0

File tree

4 files changed

+12
-27
lines changed

4 files changed

+12
-27
lines changed

webware/Admin/Errors.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ def cellContents(self, _rowIndex, colIndex, value):
1818
if self._headings[colIndex] in ('pathname', 'error report filename'):
1919
path = self.application().serverSidePath()
2020
if value.startswith(path):
21-
value = value[len(path):]
22-
if value.startswith(sep):
23-
value = value[len(sep):]
21+
value = value.removeprefix(path).removeprefix(sep)
2422
link = f'View?filename={urlEncode(value)}'
2523
value = value.replace(sep, sep + '<wbr>')
2624
value = f'<a href="{link}">{value}</a>'

webware/Examples/Colors.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ def rgbToHexColor(r, g, b):
2424
def hexToRgbColor(h):
2525
"""Convert #RRGGBB to r, g, b."""
2626
h = h.strip()
27-
if h.startswith('#'):
28-
h = h[1:]
27+
h = h.removeprefix('#')
2928
h = h[:2], h[2:4], h[4:]
3029
return [int(x, 16) for x in h]
3130

webware/HTTPRequest.py

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,7 @@ def serverSidePath(self, path=None):
351351
server side directory to form a path relative to the object.
352352
"""
353353
if path:
354-
if path.startswith('/'):
355-
path = path[1:]
354+
path = path.removeprefix('/')
356355
return os.path.normpath(os.path.join(
357356
os.path.dirname(self._serverSidePath), path))
358357
return self._serverSidePath
@@ -367,8 +366,7 @@ def serverSideContextPath(self, path=None):
367366
if the request is in a subdirectory of the main context directory.
368367
"""
369368
if path:
370-
if path.startswith('/'):
371-
path = path[1:]
369+
path = path.removeprefix('/')
372370
return os.path.normpath(os.path.join(
373371
self._serverSideContextPath, path))
374372
return self._serverSideContextPath
@@ -385,15 +383,11 @@ def servletURI(self):
385383
"""Return servlet URI without any query strings or extra path info."""
386384
p = self._pathInfo
387385
if not self._extraURLPath:
388-
if p.endswith('/'):
389-
p = p[:-1]
390-
return p
386+
return p.removesuffix('/')
391387
i = p.rfind(self._extraURLPath)
392388
if i >= 0:
393389
p = p[:i]
394-
if p.endswith('/'):
395-
p = p[:-1]
396-
return p
390+
return p.removesuffix('/')
397391

398392
def uriWebwareRoot(self):
399393
"""Return relative URL path of the Webware root location."""
@@ -494,11 +488,9 @@ def siteRoot(self):
494488
of the servlet that you have forwarded to.
495489
"""
496490
url = self.originalURLPath()
497-
if url.startswith('/'):
498-
url = url[1:]
491+
url = url.removeprefix('/')
499492
contextName = self.contextName() + '/'
500-
if url.startswith(contextName):
501-
url = url[len(contextName):]
493+
url = url.removeprefix(contextName)
502494
numStepsBack = url.count('/')
503495
return '../' * numStepsBack
504496

@@ -512,11 +504,9 @@ def siteRootFromCurrentServlet(self):
512504
relative to the _current_ servlet, not the _original_ servlet.
513505
"""
514506
url = self.urlPath()
515-
if url.startswith('/'):
516-
url = url[1:]
507+
url = url.removeprefix('/')
517508
contextName = self.contextName() + '/'
518-
if url.startswith(contextName):
519-
url = url[len(contextName):]
509+
url = url.removeprefix(contextName)
520510
numStepsBackward = url.count('/')
521511
return '../' * numStepsBackward
522512

@@ -529,8 +519,7 @@ def servletPathFromSiteRoot(self):
529519
servlet in a database, for example.
530520
"""
531521
urlPath = self.urlPath()
532-
if urlPath.startswith('/'):
533-
urlPath = urlPath[1:]
522+
urlPath = urlPath.removeprefix('/')
534523
parts = urlPath.split('/')
535524
newParts = []
536525
for part in parts:

webware/Servlet.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,7 @@ def serverSidePath(self, path=None):
155155
if self._serverSidePath is None:
156156
self._serverSidePath = self._transaction.request().serverSidePath()
157157
if path:
158-
if path.startswith('/'):
159-
path = path[1:]
158+
path = path.removeprefix('/')
160159
return os.path.normpath(os.path.join(
161160
os.path.dirname(self._serverSidePath), path))
162161
return self._serverSidePath

0 commit comments

Comments
 (0)