Sunil Mohan Adapa 38ece87c6c
*: Utilize newer 3.10 syntax for type hints
Tests:

- mypy does not show any errors.

- Installing ejabberd app works. Privileged actions run fine.

- Unit tests work.

- No additional testing was done as type annotations don't have any effect at
runtime.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2023-09-25 20:03:34 -04:00

58 lines
1.5 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-or-later
"""Configure Privacy App."""
import pathlib
import augeas
from plinth.actions import privileged
CONFIG_FILE = pathlib.Path('/etc/popularity-contest.d/freedombox.conf')
@privileged
def setup():
"""Create initial popcon configuration."""
CONFIG_FILE.parent.mkdir(exist_ok=True)
CONFIG_FILE.touch()
aug = _load_augeas()
aug.set('ENCRYPT', 'yes')
aug.save()
# Set the vendor to 'FreedomBox' with 'Debian' as parent
default_link = pathlib.Path('/etc/dpkg/origins/default')
debian_link = pathlib.Path('/etc/dpkg/origins/debian')
if default_link.is_symlink() and default_link.resolve() == debian_link:
default_link.unlink()
default_link.symlink_to('freedombox')
@privileged
def set_configuration(enable_popcon: bool | None = None):
"""Update popcon configuration."""
aug = _load_augeas()
if enable_popcon:
aug.set('PARTICIPATE', 'yes')
else:
aug.set('PARTICIPATE', 'no')
aug.save()
def get_configuration() -> dict[str, bool]:
"""Return if popcon participation is enabled."""
aug = _load_augeas()
value = aug.get('PARTICIPATE')
return {'enable_popcon': (value == 'yes')}
def _load_augeas():
"""Initialize Augeas."""
aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD +
augeas.Augeas.NO_MODL_AUTOLOAD)
aug.transform('Shellvars', str(CONFIG_FILE))
aug.set('/augeas/context', '/files' + str(CONFIG_FILE))
aug.load()
return aug