Added type hints.
This commit is contained in:
parent
d46be0bc0b
commit
501f28e0ee
3 changed files with 38 additions and 34 deletions
|
@ -1,11 +1,16 @@
|
||||||
|
|
||||||
"""Sets the system mode."""
|
"""Sets the system mode."""
|
||||||
|
|
||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser, Namespace
|
||||||
from subprocess import DEVNULL, CalledProcessError, check_call, run
|
from subprocess import DEVNULL
|
||||||
|
from subprocess import CalledProcessError
|
||||||
|
from subprocess import CompletedProcess
|
||||||
|
from subprocess import check_call
|
||||||
|
from subprocess import run
|
||||||
from sys import stderr
|
from sys import stderr
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
from tabletmode.config import load_configuration
|
from tabletmode.config import load_config
|
||||||
|
|
||||||
|
|
||||||
DESCRIPTION = 'Sets or toggles the system mode.'
|
DESCRIPTION = 'Sets or toggles the system mode.'
|
||||||
|
@ -13,7 +18,7 @@ LAPTOP_MODE_SERVICE = 'laptop-mode.service'
|
||||||
TABLET_MODE_SERVICE = 'tablet-mode.service'
|
TABLET_MODE_SERVICE = 'tablet-mode.service'
|
||||||
|
|
||||||
|
|
||||||
def get_arguments():
|
def get_args() -> Namespace:
|
||||||
"""Returns the CLI arguments."""
|
"""Returns the CLI arguments."""
|
||||||
|
|
||||||
parser = ArgumentParser(description=DESCRIPTION)
|
parser = ArgumentParser(description=DESCRIPTION)
|
||||||
|
@ -28,13 +33,11 @@ def get_arguments():
|
||||||
return parser.parse_args()
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
def systemctl(action, unit, *, root=False):
|
def systemctl(action: str, unit: str, *, root: bool = False) -> bool:
|
||||||
"""Runs systemctl."""
|
"""Runs systemctl."""
|
||||||
|
|
||||||
command = ['/usr/bin/sudo'] if root else []
|
command = ['/usr/bin/sudo'] if root else []
|
||||||
command.append('systemctl')
|
command += ['systemctl', action, unit]
|
||||||
command.append(action)
|
|
||||||
command.append(unit)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
check_call(command, stdout=DEVNULL) # Return 0 on success.
|
check_call(command, stdout=DEVNULL) # Return 0 on success.
|
||||||
|
@ -44,7 +47,7 @@ def systemctl(action, unit, *, root=False):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def notify_send(summary, body=None):
|
def notify_send(summary: str, body: Optional[str] = None) -> CompletedProcess:
|
||||||
"""Sends the respective message."""
|
"""Sends the respective message."""
|
||||||
|
|
||||||
command = ['/usr/bin/notify-send', summary]
|
command = ['/usr/bin/notify-send', summary]
|
||||||
|
@ -55,19 +58,19 @@ def notify_send(summary, body=None):
|
||||||
return run(command, stdout=DEVNULL, check=False)
|
return run(command, stdout=DEVNULL, check=False)
|
||||||
|
|
||||||
|
|
||||||
def notify_laptop_mode():
|
def notify_laptop_mode() -> CompletedProcess:
|
||||||
"""Notifies about laptop mode."""
|
"""Notifies about laptop mode."""
|
||||||
|
|
||||||
return notify_send('Laptop mode.', 'The system is now in laptop mode.')
|
return notify_send('Laptop mode.', 'The system is now in laptop mode.')
|
||||||
|
|
||||||
|
|
||||||
def notify_tablet_mode():
|
def notify_tablet_mode() -> CompletedProcess:
|
||||||
"""Notifies about tablet mode."""
|
"""Notifies about tablet mode."""
|
||||||
|
|
||||||
return notify_send('Tablet mode.', 'The system is now in tablet mode.')
|
return notify_send('Tablet mode.', 'The system is now in tablet mode.')
|
||||||
|
|
||||||
|
|
||||||
def default_mode(notify=False):
|
def default_mode(notify: bool = False) -> None:
|
||||||
"""Restores all blocked input devices."""
|
"""Restores all blocked input devices."""
|
||||||
|
|
||||||
systemctl('stop', LAPTOP_MODE_SERVICE, root=True)
|
systemctl('stop', LAPTOP_MODE_SERVICE, root=True)
|
||||||
|
@ -77,7 +80,7 @@ def default_mode(notify=False):
|
||||||
notify_send('Default mode.', 'The system is now in default mode.')
|
notify_send('Default mode.', 'The system is now in default mode.')
|
||||||
|
|
||||||
|
|
||||||
def laptop_mode(notify=False):
|
def laptop_mode(notify: bool = False) -> None:
|
||||||
"""Starts the laptop mode."""
|
"""Starts the laptop mode."""
|
||||||
|
|
||||||
systemctl('stop', TABLET_MODE_SERVICE, root=True)
|
systemctl('stop', TABLET_MODE_SERVICE, root=True)
|
||||||
|
@ -87,7 +90,7 @@ def laptop_mode(notify=False):
|
||||||
notify_laptop_mode()
|
notify_laptop_mode()
|
||||||
|
|
||||||
|
|
||||||
def tablet_mode(notify=False):
|
def tablet_mode(notify: bool = False) -> None:
|
||||||
"""Starts the tablet mode."""
|
"""Starts the tablet mode."""
|
||||||
|
|
||||||
systemctl('stop', LAPTOP_MODE_SERVICE, root=True)
|
systemctl('stop', LAPTOP_MODE_SERVICE, root=True)
|
||||||
|
@ -97,7 +100,7 @@ def tablet_mode(notify=False):
|
||||||
notify_tablet_mode()
|
notify_tablet_mode()
|
||||||
|
|
||||||
|
|
||||||
def toggle_mode(notify=False):
|
def toggle_mode(notify: bool = False) -> None:
|
||||||
"""Toggles between laptop and tablet mode."""
|
"""Toggles between laptop and tablet mode."""
|
||||||
|
|
||||||
if systemctl('status', TABLET_MODE_SERVICE):
|
if systemctl('status', TABLET_MODE_SERVICE):
|
||||||
|
@ -106,20 +109,20 @@ def toggle_mode(notify=False):
|
||||||
tablet_mode(notify=notify)
|
tablet_mode(notify=notify)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main() -> None:
|
||||||
"""Runs the main program."""
|
"""Runs the main program."""
|
||||||
|
|
||||||
arguments = get_arguments()
|
args = get_args()
|
||||||
configuration = load_configuration()
|
config = load_config()
|
||||||
notify = configuration.get('notify', False) or arguments.notify
|
notify = config.get('notify', False) or args.notify
|
||||||
|
|
||||||
if arguments.mode == 'toggle':
|
if args.mode == 'toggle':
|
||||||
toggle_mode(notify=notify)
|
toggle_mode(notify=notify)
|
||||||
elif arguments.mode == 'default':
|
elif args.mode == 'default':
|
||||||
default_mode(notify=notify)
|
default_mode(notify=notify)
|
||||||
elif arguments.mode == 'laptop':
|
elif args.mode == 'laptop':
|
||||||
laptop_mode(notify=notify)
|
laptop_mode(notify=notify)
|
||||||
elif arguments.mode == 'tablet':
|
elif args.mode == 'tablet':
|
||||||
tablet_mode(notify=notify)
|
tablet_mode(notify=notify)
|
||||||
else:
|
else:
|
||||||
print('Must specify a mode.', file=stderr, flush=True)
|
print('Must specify a mode.', file=stderr, flush=True)
|
||||||
|
|
|
@ -5,14 +5,14 @@ from logging import getLogger
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
__all__ = ['load_configuration']
|
__all__ = ['load_config']
|
||||||
|
|
||||||
|
|
||||||
CONFIG_FILE = Path('/etc/tablet-mode.json')
|
CONFIG_FILE = Path('/etc/tablet-mode.json')
|
||||||
LOGGER = getLogger('tabletmode')
|
LOGGER = getLogger('tabletmode')
|
||||||
|
|
||||||
|
|
||||||
def load_configuration():
|
def load_config() -> dict:
|
||||||
"""Returns the configuration."""
|
"""Returns the configuration."""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
|
|
||||||
"""System mode daemon."""
|
"""System mode daemon."""
|
||||||
|
|
||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser, Namespace
|
||||||
from logging import DEBUG, INFO, basicConfig, getLogger
|
from logging import DEBUG, INFO, basicConfig, getLogger
|
||||||
from subprocess import Popen
|
from subprocess import Popen
|
||||||
|
from typing import Iterable
|
||||||
|
|
||||||
from tabletmode.config import load_configuration
|
from tabletmode.config import load_config
|
||||||
|
|
||||||
|
|
||||||
DESCRIPTION = 'Setup system for laptop or tablet mode.'
|
DESCRIPTION = 'Setup system for laptop or tablet mode.'
|
||||||
|
@ -14,7 +15,7 @@ LOG_FORMAT = '[%(levelname)s] %(name)s: %(message)s'
|
||||||
LOGGER = getLogger('sysmoded')
|
LOGGER = getLogger('sysmoded')
|
||||||
|
|
||||||
|
|
||||||
def get_arguments():
|
def get_args() -> Namespace:
|
||||||
"""Parses the CLI arguments."""
|
"""Parses the CLI arguments."""
|
||||||
|
|
||||||
parser = ArgumentParser(description=DESCRIPTION)
|
parser = ArgumentParser(description=DESCRIPTION)
|
||||||
|
@ -27,13 +28,13 @@ def get_arguments():
|
||||||
return parser.parse_args()
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
def disable_device(device):
|
def disable_device(device: str) -> Popen:
|
||||||
"""Disables the respective device via evtest."""
|
"""Disables the respective device via evtest."""
|
||||||
|
|
||||||
return Popen((EVTEST, '--grab', device))
|
return Popen((EVTEST, '--grab', device))
|
||||||
|
|
||||||
|
|
||||||
def disable_devices(devices):
|
def disable_devices(devices: Iterable[str]) -> None:
|
||||||
"""Disables the given devices."""
|
"""Disables the given devices."""
|
||||||
|
|
||||||
subprocesses = []
|
subprocesses = []
|
||||||
|
@ -46,11 +47,11 @@ def disable_devices(devices):
|
||||||
subprocess.wait()
|
subprocess.wait()
|
||||||
|
|
||||||
|
|
||||||
def get_devices(mode):
|
def get_devices(mode: str) -> Iterable[str]:
|
||||||
"""Reads the device from the config file."""
|
"""Reads the device from the config file."""
|
||||||
|
|
||||||
configuration = load_configuration()
|
config = load_config()
|
||||||
devices = configuration.get(mode) or ()
|
devices = config.get(mode) or ()
|
||||||
|
|
||||||
if not devices:
|
if not devices:
|
||||||
LOGGER.info('No devices configured to disable.')
|
LOGGER.info('No devices configured to disable.')
|
||||||
|
@ -61,7 +62,7 @@ def get_devices(mode):
|
||||||
def main():
|
def main():
|
||||||
"""Runs the main program."""
|
"""Runs the main program."""
|
||||||
|
|
||||||
arguments = get_arguments()
|
arguments = get_args()
|
||||||
level = DEBUG if arguments.verbose else INFO
|
level = DEBUG if arguments.verbose else INFO
|
||||||
basicConfig(level=level, format=LOG_FORMAT)
|
basicConfig(level=level, format=LOG_FORMAT)
|
||||||
devices = get_devices(arguments.mode)
|
devices = get_devices(arguments.mode)
|
||||||
|
|
Loading…
Reference in a new issue