Skip to content

Commit

Permalink
refactors path extraction to Frontend class
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfgangFahl committed Nov 4, 2023
1 parent a05de2b commit 30e993a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 56 deletions.
29 changes: 0 additions & 29 deletions frontend/webserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,35 +55,6 @@ async def wikis(client:Client):
if not self.login.authenticated():
return RedirectResponse('/login')
return await self.wikis()

@staticmethod
def extract_site_and_path(path):
"""
Splits the given path into the site component and the remaining path.
This static method assumes that the 'site' is the first element of the
path when split by "/", and the 'path' is the rest of the string after
the site.
Parameters:
path (str): The complete path to split.
Returns:
tuple: A tuple where the first element is the site and the second
element is the subsequent path.
"""
# Check if the path is empty or does not contain a "/"
if not path or "/" not in path:
return "", path

# Split the path into parts using the "/" as a separator
parts = path.split("/")

# The first part is the site, the rest is joined back into a path
site = parts[0]
remaining_path = "/".join(parts[1:])

return site, remaining_path

def enableSites(self, siteNames):
'''
Expand Down
37 changes: 33 additions & 4 deletions frontend/wikicms.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,49 @@ def log(self,msg):
'''
if self.debug:
print(msg,flush=True)

@staticmethod
def extract_site_and_path(path):
"""
Splits the given path into the site component and the remaining path.
This static method assumes that the 'site' is the first element of the
path when split by "/", and the 'path' is the rest of the string after
the site.
Parameters:
path (str): The complete path to split.
Returns:
tuple: A tuple where the first element is the site and the second
element is the subsequent path.
"""
# Check if the path is empty or does not contain a "/"
if not path or "/" not in path:
return "", path

# Split the path into parts using the "/" as a separator
parts = path.split("/")

# The first part is the site, the rest is joined back into a path
site = parts[0]
remaining_path = "/"+"/".join(parts[1:])

return site, remaining_path

def open(self,appWrap=None):
def open(self,ws=None):
'''
open the frontend
Args:
appWrap(appWrap): optional fb4 Application Wrapper
ws: optional Nicegui webserver
'''
self.appWrap=appWrap
self.ws=ws
if self.wiki is None:
self.wiki=WikiClient.ofWikiId(self.site.wikiId)
self.wiki.login()
self.smwclient=SMWClient(self.wiki.getSite())
self.site.open(appWrap)
self.site.open(ws)

def errMsg(self,ex):
if self.debug:
Expand Down
23 changes: 1 addition & 22 deletions tests/test_webserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,28 +68,7 @@ def testConfig(self):
print(path)
self.assertTrue("/tmp" in path)

def test_extract_site_and_path(self):
"""
Test splitting the path into site and path.
"""
# Test paths and their expected results.
paths = ['admin/', 'or/test']
expected_results = [('admin', '/'), ('or', '/test')]

for index, test_path in enumerate(paths):
# Extract site and path using the Webserver method.
site, path = CmsWebServer.extract_site_and_path(test_path)

# If debugging is enabled, print the results.
if getattr(self, 'debug', False):
print(f"Site: {site}, Path: {path}")

# Get the expected site and path.
expected_site, expected_path = expected_results[index]

# Assert that the results match the expectations.
self.assertEqual(expected_site, site)
self.assertEqual(expected_path, path)


def testWebServer(self):
'''
Expand Down
26 changes: 25 additions & 1 deletion tests/test_wikicms.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from wikibot3rd.wikiuser import WikiUser
#from wikibot.smw import SMWClient
from ngwidgets.basetest import Basetest
from frontend.wikicms import Frontend

class TestWikiCMS(Basetest):
'''
Expand Down Expand Up @@ -70,4 +71,27 @@ def testWikiCMS(self):
if self.debug:
print(text)
self.assertTrue("OpenResearch" in text)
pass
pass

def test_extract_site_and_path(self):
"""
Test splitting the path into site and path.
"""
# Test paths and their expected results.
paths = ['admin/', 'or/test']
expected_results = [('admin', '/'), ('or', '/test')]

for index, test_path in enumerate(paths):
# Extract site and path using the Webserver method.
site, path = Frontend.extract_site_and_path(test_path)

# If debugging is enabled, print the results.
if getattr(self, 'debug', False):
print(f"Site: {site}, Path: {path}")

# Get the expected site and path.
expected_site, expected_path = expected_results[index]

# Assert that the results match the expectations.
self.assertEqual(expected_site, site)
self.assertEqual(expected_path, path)

0 comments on commit 30e993a

Please sign in to comment.