forked from WLAN-Pi/wlanpi-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Run Command and Constants refactor (WLAN-Pi#47)
Co-authored-by: Michael Ketchel <[email protected]>
- Loading branch information
1 parent
1c9085a
commit 45acd14
Showing
24 changed files
with
949 additions
and
290 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ autoflake | |
mypy | ||
flake8 | ||
pytest | ||
pytest-asyncio | ||
pytest-cov | ||
coverage-badge | ||
pytest-mock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Core config | ||
API_V1_STR: str = "/api/v1" | ||
PROJECT_NAME: str = "wlanpi-core" | ||
PROJECT_DESCRIPTION: str = ( | ||
"The wlanpi-core API provides endpoints for applications on the WLAN Pi to share data. 🚀" | ||
) | ||
|
||
# Linux programs | ||
IFCONFIG_FILE: str = "/sbin/ifconfig" | ||
IW_FILE: str = "/sbin/iw" | ||
IP_FILE: str = "/usr/sbin/ip" | ||
UFW_FILE: str = "/usr/sbin/ufw" | ||
ETHTOOL_FILE: str = "/sbin/ethtool" | ||
|
||
# Mode changer scripts | ||
MODE_FILE: str = "/etc/wlanpi-state" | ||
|
||
# Version file for WLAN Pi image | ||
WLANPI_IMAGE_FILE: str = "/etc/wlanpi-release" | ||
|
||
WCONSOLE_SWITCHER_FILE: str = "/opt/wlanpi-wconsole/wconsole_switcher" | ||
HOTSPOT_SWITCHER_FILE: str = "/opt/wlanpi-hotspot/hotspot_switcher" | ||
WIPERF_SWITCHER_FILE: str = "/opt/wlanpi-wiperf/wiperf_switcher" | ||
SERVER_SWITCHER_FILE: str = "/opt/wlanpi-server/server_switcher" | ||
BRIDGE_SWITCHER_FILE: str = "/opt/wlanpi-bridge/bridge_switcher" | ||
|
||
REG_DOMAIN_FILE: str = "/usr/bin/wlanpi-reg-domain" | ||
TIME_ZONE_FILE: str = "/usr/bin/wlanpi-timezone" | ||
|
||
# WPA Supplicant dbus service and interface | ||
WPAS_DBUS_SERVICE: str = "fi.w1.wpa_supplicant1" | ||
WPAS_DBUS_INTERFACE: str = "fi.w1.wpa_supplicant1" | ||
WPAS_DBUS_OPATH: str = "/fi/w1/wpa_supplicant1" | ||
WPAS_DBUS_INTERFACES_INTERFACE: str = "fi.w1.wpa_supplicant1.Interface" | ||
WPAS_DBUS_INTERFACES_OPATH: str = "/fi/w1/wpa_supplicant1/Interfaces" | ||
WPAS_DBUS_BSS_INTERFACE: str = "fi.w1.wpa_supplicant1.BSS" | ||
WPAS_DBUS_NETWORK_INTERFACE: str = "fi.w1.wpa_supplicant1.Network" | ||
|
||
# Core API configuration | ||
API_DEFAULT_TIMEOUT: int = 20 | ||
|
||
# VLAN model constants | ||
DEFAULT_VLAN_INTERFACE_FILE = "/etc/network/interfaces.d/vlans" | ||
DEFAULT_INTERFACE_FILE = "/etc/network/interfaces" | ||
|
||
# Service Constants | ||
BT_ADAPTER = "hci0" | ||
|
||
#### Paths below here are relative to script dir or /tmp fixed paths ### | ||
|
||
# Networkinfo data file names | ||
LLDPNEIGH_FILE: str = "/tmp/lldpneigh.txt" | ||
CDPNEIGH_FILE: str = "/tmp/cdpneigh.txt" | ||
IPCONFIG_FILE: str = "/opt/wlanpi-common/networkinfo/ipconfig.sh 2>/dev/null" | ||
REACHABILITY_FILE: str = "/opt/wlanpi-common/networkinfo/reachability.sh" | ||
PUBLICIP_CMD: str = "/opt/wlanpi-common/networkinfo/publicip.sh" | ||
PUBLICIP6_CMD: str = "/opt/wlanpi-common/networkinfo/publicip6.sh" | ||
BLINKER_FILE: str = "/opt/wlanpi-common/networkinfo/portblinker.sh" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,52 @@ | ||
import json | ||
import re | ||
from json import JSONDecodeError | ||
from re import RegexFlag | ||
from typing import Union | ||
|
||
|
||
class CommandResult: | ||
"""Returned by run_command""" | ||
|
||
def __init__(self, output: str, error: str, status_code: int): | ||
self.output = output | ||
self.error = error | ||
self.status_code = status_code | ||
self.success = self.status_code == 0 | ||
def __init__(self, stdout: str, stderr: str, return_code: int): | ||
self.stdout = stdout | ||
self.stderr = stderr | ||
self.return_code = return_code | ||
self.success = self.return_code == 0 | ||
|
||
def output_from_json(self) -> Union[dict, list, int, float, str, None]: | ||
try: | ||
return json.loads(self.output) | ||
return json.loads(self.stdout) | ||
except JSONDecodeError: | ||
return None | ||
|
||
def grep_stdout_for_string( | ||
self, string: str, negate: bool = False, split: bool = False | ||
) -> Union[str, list[str]]: | ||
if negate: | ||
filtered = list(filter(lambda x: string not in x, self.stdout.split("\n"))) | ||
else: | ||
filtered = list(filter(lambda x: string in x, self.stdout.split("\n"))) | ||
return filtered if split else "\n".join(filtered) | ||
|
||
def grep_stdout_for_pattern( | ||
self, | ||
pattern: Union[re.Pattern[str], str], | ||
flags: Union[int, RegexFlag] = 0, | ||
negate: bool = False, | ||
split: bool = False, | ||
) -> Union[str, list[str]]: | ||
if negate: | ||
filtered = list( | ||
filter( | ||
lambda x: not re.match(pattern, x, flags=flags), | ||
self.stdout.split("\n"), | ||
) | ||
) | ||
else: | ||
filtered = list( | ||
filter( | ||
lambda x: re.match(pattern, x, flags=flags), self.stdout.split("\n") | ||
) | ||
) | ||
return filtered if split else "\n".join(filtered) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
class RunCommandError(Exception): | ||
"""Raised when runcommand returns stderr""" | ||
|
||
def __init__(self, error_msg: str, status_code: int): | ||
def __init__(self, error_msg: str, return_code: int): | ||
super().__init__(error_msg) | ||
|
||
self.status_code = status_code | ||
self.return_code = return_code | ||
self.error_msg = error_msg |
Oops, something went wrong.