config: rename functions (improve readability)

Idem for apache component. See !1952.

Signed-off-by: Fioddor Superconcentrado <fioddor@gmail.com>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Fioddor Superconcentrado 2020-12-11 18:45:22 +01:00 committed by James Valleroy
parent 337e8c28dd
commit ec870a1766
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
4 changed files with 26 additions and 24 deletions

View File

@ -70,17 +70,17 @@ def setup(helper, old_version=None):
# (U)ser (W)eb (S)ites # (U)ser (W)eb (S)ites
def uws_usr2dir(user): def uws_directory_of_user(user):
"""Returns the directory of the given user's website.""" """Returns the directory of the given user's website."""
return '/home/{}/public_html'.format(user) return '/home/{}/public_html'.format(user)
def uws_usr2url(user): def uws_url_of_user(user):
"""Returns the url path of the given user's website.""" """Returns the url path of the given user's website."""
return '/~{}/'.format(user) return '/~{}/'.format(user)
def uws_dir2usr(directory): def user_of_uws_directory(directory):
"""Returns the user of a given user website directory.""" """Returns the user of a given user website directory."""
if directory.startswith('/home/'): if directory.startswith('/home/'):
pos_ini = 6 pos_ini = 6
@ -97,7 +97,7 @@ def uws_dir2usr(directory):
return user if is_valid_user_name(user) else None return user if is_valid_user_name(user) else None
def uws_url2usr(url): def user_of_uws_url(url):
"""Returns the user of a given user website url path.""" """Returns the user of a given user website url path."""
MISSING = -1 MISSING = -1
@ -113,20 +113,20 @@ def uws_url2usr(url):
return user if is_valid_user_name(user) else None return user if is_valid_user_name(user) else None
def uws_url2dir(url): def uws_directory_of_url(url):
"""Returns the directory of the user's website for the given url path. """Returns the directory of the user's website for the given url path.
Note: It doesn't return the full OS file path to the url path! Note: It doesn't return the full OS file path to the url path!
""" """
return uws_usr2dir(uws_url2usr(url)) return uws_directory_of_user(user_of_uws_url(url))
def uws_dir2url(directory): def uws_url_of_directory(directory):
"""Returns the url base path of the user's website for the given OS path. """Returns the url base path of the user's website for the given OS path.
Note: It doesn't return the url path for the file! Note: It doesn't return the url path for the file!
""" """
return uws_usr2url(uws_dir2usr(directory)) return uws_url_of_user(user_of_uws_directory(directory))
def get_users_with_website(): def get_users_with_website():
@ -140,6 +140,7 @@ def get_users_with_website():
] ]
return { return {
name: uws_usr2url(name) name: uws_url_of_user(name)
for name in lst_sub_dirs('/home') if os.path.isdir(uws_usr2dir(name)) for name in lst_sub_dirs('/home')
if os.path.isdir(uws_directory_of_user(name))
} }

View File

@ -3,28 +3,29 @@
Test module for (U)ser (Web) (S)ites. Test module for (U)ser (Web) (S)ites.
""" """
from plinth.modules.apache import (uws_usr2dir, uws_dir2url, uws_url2usr, from plinth.modules.apache import (uws_directory_of_user, uws_url_of_user,
uws_usr2url, uws_url2dir, uws_dir2usr) uws_directory_of_url, uws_url_of_directory,
user_of_uws_directory, user_of_uws_url)
def test_uws_namings(): def test_uws_namings():
"""Test name solvers for user, url and directory of UWS.""" """Test name solvers for user, url and directory of UWS."""
assert '/home/usr/public_html' == uws_usr2dir('usr') assert '/home/usr/public_html' == uws_directory_of_user('usr')
assert '/~usr/' == uws_usr2url('usr') assert '/~usr/' == uws_url_of_user('usr')
f = uws_dir2usr f = user_of_uws_directory
assert f('/home/usr/lacks/the/UWS/directory') is None assert f('/home/usr/lacks/the/UWS/directory') is None
assert 'usr' == f('/home/usr/public_html/is/a/normal/UWS/file') assert 'usr' == f('/home/usr/public_html/is/a/normal/UWS/file')
assert 'usr' == f('/home/usr/public_html/is/a/normal/UWS/path/') assert 'usr' == f('/home/usr/public_html/is/a/normal/UWS/path/')
assert '€.;#@|' == f('/home/€.;#@|/public_html/is/stange/but/valid/') assert '€.;#@|' == f('/home/€.;#@|/public_html/is/stange/but/valid/')
f = uws_url2usr f = user_of_uws_url
assert f('/usr/is/not/a/valid/UWS/url/due/to/missing/tilde') is None assert f('/usr/is/not/a/valid/UWS/url/due/to/missing/tilde') is None
assert 'usr' == f('whatever/~usr/is/considered/a/valid/UWS/path') assert 'usr' == f('whatever/~usr/is/considered/a/valid/UWS/path')
assert 'usr' == f('~usr') assert 'usr' == f('~usr')
assert 'usr' == f('~usr/') assert 'usr' == f('~usr/')
assert 'usr' == f('/~usr/') assert 'usr' == f('/~usr/')
assert '/home/usr/public_html' == uws_url2dir('~usr/any/file') assert '/home/usr/public_html' == uws_directory_of_url('~usr/any/file')
assert '/~usr/' == uws_dir2url('/home/usr/public_html/path/to/file') assert '/~usr/' == uws_url_of_directory('/home/usr/public_html/any/file')

View File

@ -12,7 +12,7 @@ from django.utils.translation import ugettext_lazy as _
from plinth import actions from plinth import actions
from plinth import app as app_module from plinth import app as app_module
from plinth import frontpage, menu from plinth import frontpage, menu
from plinth.modules.apache import (uws_url2usr, uws_usr2url, from plinth.modules.apache import (user_of_uws_url, uws_url_of_user,
get_users_with_website) get_users_with_website)
from plinth.modules.names.components import DomainType from plinth.modules.names.components import DomainType
from plinth.signals import domain_added from plinth.signals import domain_added
@ -93,7 +93,7 @@ def home_page_url2scid(url):
return 'apache-default' return 'apache-default'
if url and url.startswith('/~'): if url and url.startswith('/~'):
return 'uws-{}'.format(uws_url2usr(url)) return 'uws-{}'.format(user_of_uws_url(url))
shortcuts = frontpage.Shortcut.list() shortcuts = frontpage.Shortcut.list()
for shortcut in shortcuts: for shortcut in shortcuts:
@ -114,7 +114,7 @@ def _home_page_scid2url(shortcut_id):
elif shortcut_id.startswith('uws-'): elif shortcut_id.startswith('uws-'):
user = shortcut_id[4:] user = shortcut_id[4:]
if user in get_users_with_website(): if user in get_users_with_website():
url = uws_usr2url(user) url = uws_url_of_user(user)
else: else:
url = None url = None
else: else:

View File

@ -9,7 +9,7 @@ import os
from unittest.mock import (patch, MagicMock) from unittest.mock import (patch, MagicMock)
from plinth import __main__ as plinth_main from plinth import __main__ as plinth_main
from plinth.modules.apache import uws_usr2dir from plinth.modules.apache import uws_directory_of_user
from plinth.modules.config import (home_page_url2scid, get_home_page, from plinth.modules.config import (home_page_url2scid, get_home_page,
_home_page_scid2url, change_home_page) _home_page_scid2url, change_home_page)
from plinth.modules.config.forms import ConfigurationForm from plinth.modules.config.forms import ConfigurationForm
@ -92,7 +92,7 @@ def test_homepage_mapping_skip_ci():
"""Special tests for homepage functions.""" """Special tests for homepage functions."""
try: try:
UWS_DIRECTORY = uws_usr2dir(os.getlogin()) UWS_DIRECTORY = uws_directory_of_user(os.getlogin())
except OSError: except OSError:
reason = "Needs access to ~/ directory. " \ reason = "Needs access to ~/ directory. " \
+ "CI sandboxed workspace doesn't provide it." + "CI sandboxed workspace doesn't provide it."
@ -143,7 +143,7 @@ def test_homepage_field():
Currently they share the same test case. Currently they share the same test case.
""" """
try: try:
UWS_DIRECTORY = uws_usr2dir(os.getlogin()) UWS_DIRECTORY = uws_directory_of_user(os.getlogin())
except OSError: except OSError:
reason = "Needs access to ~/ directory, etc. " \ reason = "Needs access to ~/ directory, etc. " \
+ "CI sandboxed workspace doesn't provide it." + "CI sandboxed workspace doesn't provide it."