Skip to content

Commit 7071169

Browse files
authored
Merge pull request #112 from EmixamPP/py-format
Python >= 3.10
2 parents 6f133d9 + 0c4cca0 commit 7071169

10 files changed

Lines changed: 218 additions & 170 deletions

File tree

sources/boot_service/base_boot_service.py

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,68 @@
1-
from abc import ABCMeta, abstractmethod
2-
from typing import List
3-
import subprocess
41
import logging
52
import os
3+
import subprocess
4+
from abc import ABCMeta, abstractmethod
65

76
from globals import UDEV_RULE_PATH, get_index, get_kernels
87

98

109
class BaseBootService(metaclass=ABCMeta):
1110
"""Manage the boot service of linux-enable-ir-emitter"""
1211

13-
def __init__(self, devices: List[str]) -> None:
14-
"""Create a boot service for run the drivers
12+
def __init__(self, devices: list[str]) -> None:
13+
"""Create a boot service for run the drivers.
1514
1615
Args:
17-
devices : devices for which a driver will be run
16+
devices (list[str]): devices for which a driver will be run.
1817
"""
19-
self.devices = devices
18+
self.devices: list[str] = devices
2019

2120
@abstractmethod
2221
def _enable(self) -> int:
23-
"""Enable the service
22+
"""Enable the service.
2423
2524
Returns:
26-
0: the service have been enabled successfully
27-
other value: Error with the boot service.
25+
int: 0 if the service have been enabled successfully.
26+
Otherwise, error with the boot service.
2827
"""
2928

3029
@abstractmethod
3130
def _disable(self) -> int:
32-
"""Disable the service
31+
"""Disable the service.
3332
3433
Returns:
35-
0: the service have been disabled successfully
36-
other value: The boot service does not exists.
34+
int: 0 if the service have been disabled successfully.
35+
Otherwise, error with the boot service.
3736
"""
3837

3938
@abstractmethod
4039
def status(self) -> int:
4140
"""Print the service status
41+
4242
Returns:
43-
0: the service works fine
44-
other value: error with the boot service
43+
int: 0 if the service works fine.
44+
Otherwise, error with the boot service.
4545
"""
4646

4747
def enable(self) -> int:
48+
"""Enable the service.
49+
50+
Returns:
51+
int: 0 if the service have been enabled successfully.
52+
Otherwise, error with the boot service.
53+
"""
4854
self._create_udev()
4955

50-
exit_code = subprocess.run(
51-
["udevadm", "control", "--reload-rules"], capture_output=True
52-
).returncode
53-
exit_code += subprocess.run(
54-
["udevadm", "trigger"], capture_output=True
55-
).returncode
56+
exit_code = subprocess.call(
57+
["udevadm", "control", "--reload-rules"],
58+
stdout=subprocess.DEVNULL,
59+
stderr=subprocess.DEVNULL,
60+
)
61+
exit_code += subprocess.call(
62+
["udevadm", "trigger"],
63+
stdout=subprocess.DEVNULL,
64+
stderr=subprocess.DEVNULL,
65+
)
5666

5767
if exit_code:
5868
logging.error("Error with the udev boot service.")
@@ -61,6 +71,12 @@ def enable(self) -> int:
6171
return exit_code
6272

6373
def disable(self) -> int:
74+
"""Disable the service.
75+
76+
Returns:
77+
int: 0 if the service have been disabled successfully.
78+
Otherwise, error with the boot service.
79+
"""
6480
try:
6581
os.remove(UDEV_RULE_PATH)
6682
exit_code = 0

sources/boot_service/openrc/openrc.py

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,48 @@
11
import logging
22
import subprocess
33

4-
from globals import BOOT_SERVICE_NAME
54
from boot_service import BaseBootService
5+
from globals import BOOT_SERVICE_NAME
66

77

88
class Openrc(BaseBootService):
99
def _enable(self) -> int:
10-
"""Enable the service
11-
12-
Returns:
13-
0: the service have been enabled successfully
14-
other value: Error with the boot service.
15-
"""
16-
exit_code = subprocess.run(
17-
["rc-update", "add", BOOT_SERVICE_NAME, "default"], capture_output=True
18-
).returncode
19-
exit_code += subprocess.run(
20-
["rc-service", BOOT_SERVICE_NAME, "start"], capture_output=True
21-
).returncode
10+
exit_code = subprocess.call(
11+
["rc-update", "add", BOOT_SERVICE_NAME, "default"],
12+
stdout=subprocess.DEVNULL,
13+
stderr=subprocess.DEVNULL,
14+
)
15+
exit_code += subprocess.call(
16+
["rc-service", BOOT_SERVICE_NAME, "start"],
17+
stdout=subprocess.DEVNULL,
18+
stderr=subprocess.DEVNULL,
19+
)
2220

2321
if exit_code:
2422
logging.error("Error with the openrc boot service.")
2523

2624
return exit_code
2725

2826
def _disable(self) -> int:
29-
"""Disable the service
30-
31-
Returns:
32-
0: the service have been disabled successfully
33-
other value: The boot service does not exists.
34-
"""
35-
exit_code = subprocess.run(
36-
["rc-update", "del", BOOT_SERVICE_NAME, "default"], capture_output=True
37-
).returncode
27+
exit_code = subprocess.call(
28+
["rc-update", "del", BOOT_SERVICE_NAME, "default"],
29+
stdout=subprocess.DEVNULL,
30+
stderr=subprocess.DEVNULL,
31+
)
3832

3933
if exit_code:
4034
logging.error("The openrc boot service does not exists.")
4135

4236
return exit_code
4337

4438
def status(self) -> int:
45-
"""Print the service status
46-
Returns:
47-
0: the service works fine
48-
other value: error with the boot service
49-
"""
5039
exec = subprocess.run(
51-
["rc-status", "-a"], capture_output=True
40+
["rc-status", "-a"],
41+
capture_output=True,
42+
text=True,
5243
)
5344

54-
output = exec.stdout.decode("utf-8").strip()
55-
for line in output.split("\n"):
45+
for line in exec.stdout.split("\n"):
5646
if BOOT_SERVICE_NAME in line:
5747
return 0
5848

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,45 @@
11
import logging
22
import subprocess
33

4-
from globals import BOOT_SERVICE_NAME
54
from boot_service import BaseBootService
5+
from globals import BOOT_SERVICE_NAME
66

77

88
class Systemd(BaseBootService):
99
def _enable(self) -> int:
10-
"""Enable the service
11-
12-
Returns:
13-
0: the service have been enabled successfully
14-
other value: Error with the boot service.
15-
"""
16-
exit_code = subprocess.run(
17-
["systemctl", "enable", "--now", BOOT_SERVICE_NAME], capture_output=True
18-
).returncode
10+
exit_code = subprocess.call(
11+
["systemctl", "enable", "--now", BOOT_SERVICE_NAME],
12+
stdout=subprocess.DEVNULL,
13+
stderr=subprocess.DEVNULL,
14+
)
1915

2016
if exit_code:
2117
logging.error("Error with the systemd boot service.")
2218

2319
return exit_code
2420

2521
def _disable(self) -> int:
26-
"""Disable the service
27-
28-
Returns:
29-
0: the service have been disabled successfully
30-
other value: The boot service does not exists.
31-
"""
32-
exit_code = subprocess.run(
33-
["systemctl", "disable", BOOT_SERVICE_NAME], capture_output=True
34-
).returncode
22+
exit_code = subprocess.call(
23+
["systemctl", "disable", BOOT_SERVICE_NAME],
24+
stdout=subprocess.DEVNULL,
25+
stderr=subprocess.DEVNULL,
26+
)
3527

3628
if exit_code:
3729
logging.error("The systemd boot service does not exists.")
3830

3931
return exit_code
4032

4133
def status(self) -> int:
42-
"""Print the service status
43-
Returns:
44-
0: the service works fine
45-
other value: error with the boot service
46-
"""
4734
exec = subprocess.run(
48-
["systemctl", "status", BOOT_SERVICE_NAME], capture_output=True
35+
["systemctl", "status", BOOT_SERVICE_NAME],
36+
capture_output=True,
37+
text=True,
4938
)
5039

5140
if exec.returncode == 4:
5241
logging.error("The systemd boot service does not exists.")
5342
else:
54-
print(exec.stdout.decode("utf-8").strip())
43+
print(exec.stdout.strip())
5544

5645
return exec.returncode

sources/command/boot.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
import logging
21
from typing import NoReturn
32

4-
from globals import ExitCode, get_devices, get_boot_service_constructor
3+
import logging
4+
5+
from globals import ExitCode, get_boot_service_constructor, get_devices
56

67

78
def boot(boot_status: str) -> NoReturn:
89
"""Enable or disable the boot service which
9-
activates the ir emitter for all configured device
10+
activates the ir emitter for all configured device,
11+
and exit.
1012
1113
args:
12-
boot_status: "enable" or "disable" or "status"
14+
boot_status (str): "enable" or "disable" or "status".
1315
1416
Raises:
15-
Exception: boot status arg can only be equal to
16-
enable, disable or status
17+
Exception (AssertionError): boot_status not in the proposition.
1718
"""
1819
assert boot_status in ["enable", "disable", "status"]
1920

sources/command/configure.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
1-
import logging
21
from typing import NoReturn
2+
3+
import logging
34
import subprocess
45

5-
from globals import ExitCode, BIN_DRIVER_GENERATOR_PATH, SAVE_DRIVER_FOLDER_PATH
66
from command import boot
7+
from globals import (BIN_DRIVER_GENERATOR_PATH, SAVE_DRIVER_FOLDER_PATH,
8+
ExitCode)
79

810

911
def configure(device: str, emitters: int, neg_answer_limit: int) -> NoReturn:
10-
"""Find a driver for the infrared camera
12+
"""Find a driver for the infrared camera and exit.
1113
1214
Args:
13-
device: path to the infrared camera
14-
emitters: number of emitters on the device
15-
neg_answer_limit: after k negative answer the pattern will be skiped. Use 256 for unlimited
15+
device str: path to the infrared camera.
16+
emitters (int): number of emitters on the device.
17+
neg_answer_limit (int): after k negative answer the pattern will be skiped. Use 256 for unlimited.
1618
"""
1719
logging.info("Ensure to not use the camera during the execution.")
1820
logging.info("Warning to do not kill the process !")
1921

2022
log_level = int(logging.getLogger().level == logging.DEBUG)
21-
exit_code = subprocess.call([BIN_DRIVER_GENERATOR_PATH, device, str(emitters), str(neg_answer_limit), SAVE_DRIVER_FOLDER_PATH, str(log_level)])
23+
exit_code = subprocess.call(
24+
[
25+
BIN_DRIVER_GENERATOR_PATH,
26+
device,
27+
str(emitters),
28+
str(neg_answer_limit),
29+
SAVE_DRIVER_FOLDER_PATH,
30+
str(log_level),
31+
]
32+
)
2233

2334
if exit_code != ExitCode.SUCCESS:
2435
logging.error("The configuration has failed.")
25-
logging.info("Do not hesitate to visit the GitHub ! https://github.com/EmixamPP/linux-enable-ir-emitter/wiki")
36+
logging.info(
37+
"Do not hesitate to visit the GitHub ! https://github.com/EmixamPP/linux-enable-ir-emitter/wiki"
38+
)
2639
else:
2740
logging.info("The driver has been successfully generated.")
2841
boot("enable")

sources/command/delete.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1-
import os
1+
from typing import NoReturn
2+
23
import logging
4+
import os
35

4-
from typing import NoReturn
56
from globals import ExitCode, get_drivers_path
67

78

8-
def delete(device: str) -> NoReturn:
9-
"""Remove the driver associated to a device,
10-
without causing error if the driver does not exists
9+
def delete(device: str | None) -> NoReturn:
10+
"""Remove the driver associated to a device,
11+
without causing error if the driver does not exists,
12+
and exit.
1113
1214
Args:
13-
device: path to the infrared camera
14-
None to execute all driver.
15+
device (str | None): path to the infrared camera. None to execute all driver.
1516
"""
1617
try:
1718
for driver in get_drivers_path(device):
1819
os.remove(driver)
1920
except FileNotFoundError:
20-
pass # no driver for this device, but there is no need to send error message
21+
pass # no driver for this device, but there is no need to send error message
2122

2223
logging.info("The drivers have been deleted.")
2324
exit(ExitCode.SUCCESS)

sources/command/run.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1+
from typing import NoReturn
2+
13
import logging
24
import subprocess
3-
from typing import NoReturn
45

5-
from globals import ExitCode, BIN_EXECUTE_DRIVER_PATH, get_drivers_path
6+
from globals import BIN_EXECUTE_DRIVER_PATH, ExitCode, get_drivers_path
67

78

8-
def run(device: str) -> NoReturn:
9-
"""Apply the driver associated to a device
9+
def run(device: str | None) -> NoReturn:
10+
"""Apply the driver associated to a device and exit.
1011
1112
Args:
12-
device: path to the infrared camera
13-
None to execute all driver.
13+
device (str | None): path to the infrared camera. None to execute all driver.
1414
"""
15+
1516
paths = get_drivers_path(device)
1617

1718
if len(paths) == 0:
1819
logging.critical("No driver for %s has been configured.", device)
19-
exit(ExitCode.FAILURE)
20+
exit(ExitCode.FAILURE)
2021

2122
general_exit_code = ExitCode.SUCCESS
2223
for driver in paths:

0 commit comments

Comments
 (0)