Skip to content

Commit

Permalink
bin/: use python typing
Browse files Browse the repository at this point in the history
  • Loading branch information
Salamandar committed Dec 2, 2024
1 parent 4ba64ab commit ce4c23c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 19 deletions.
16 changes: 9 additions & 7 deletions bin/yunohost
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import sys
import yunohost


def _parse_cli_args():
def _parse_cli_args() -> tuple[argparse.ArgumentParser, argparse.Namespace, list[str]]:
"""Parse additional arguments for the cli"""
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument(
Expand Down Expand Up @@ -71,7 +71,7 @@ def _parse_cli_args():
elif opts.json:
opts.output_as = "json"

return (parser, opts, args)
return parser, opts, args


# Stupid PATH management because sometimes (e.g. some cron job) PATH is only /usr/bin:/bin ...
Expand All @@ -82,12 +82,10 @@ if os.environ["PATH"] != default_path:

# Main action ----------------------------------------------------------

if __name__ == "__main__":

def main() -> None:
if os.geteuid() != 0:
sys.stderr.write(
"\033[1;31mError:\033[0m yunohost command must be "
"run as root or with sudo.\n"
)
print("\033[1;31mError:\033[0m yunohost command must be run as root or with sudo.", file=sys.stderr)
sys.exit(1)

parser, opts, args = _parse_cli_args()
Expand All @@ -104,3 +102,7 @@ if __name__ == "__main__":
args=args,
parser=parser,
)


if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion bin/yunohost-api
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ DEFAULT_HOST = "localhost"
DEFAULT_PORT = 6787


def _parse_api_args():
def _parse_api_args() -> argparse.Namespace:
"""Parse main arguments for the api"""
parser = argparse.ArgumentParser(
add_help=False,
Expand Down
2 changes: 1 addition & 1 deletion bin/yunohost-portal-api
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ DEFAULT_HOST = "localhost"
DEFAULT_PORT = 6788


def _parse_api_args():
def _parse_api_args() -> argparse.Namespace:
"""Parse main arguments for the api"""
parser = argparse.ArgumentParser(
add_help=False,
Expand Down
8 changes: 4 additions & 4 deletions bin/yunomdns
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ def get_network_local_interfaces() -> Dict[str, Dict[str, List[str]]]:
# Listener class, to detect duplicates on the network
# Stores the list of servers in its list property
class Listener:
def __init__(self):
def __init__(self) -> None:
self.list = []

def remove_service(self, zeroconf, type, name):
def remove_service(self, zeroconf: Zeroconf, type: str, name: str) -> None:
info = zeroconf.get_service_info(type, name)
self.list.remove(info.server)

def update_service(self, zeroconf, type, name):
def update_service(self, zeroconf: Zeroconf, type: str, name: str) -> None:
pass

def add_service(self, zeroconf, type, name):
def add_service(self, zeroconf: Zeroconf, type: str, name: str) -> None:
info = zeroconf.get_service_info(type, name)
self.list.append(info.server[:-1])

Expand Down
12 changes: 6 additions & 6 deletions bin/yunopaste
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,23 @@ SERVER_URL = "https://paste.yunohost.org"
TIMEOUT = 3


def create_snippet(data):
def create_snippet(data: str) -> str:
try:
url = SERVER_URL + "/documents"
url = f"{SERVER_URL}/documents"
response = requests.post(url, data=data.encode("utf-8"), timeout=TIMEOUT)
response.raise_for_status()
dockey = json.loads(response.text)["key"]
return SERVER_URL + "/raw/" + dockey
return f"{SERVER_URL}/raw/{dockey}"
except requests.exceptions.RequestException as e:
print("\033[31mError: {}\033[0m".format(e))
print(f"\033[31mError: {e}\033[0m", file=sys.stderr)
sys.exit(1)


def main():
def main() -> None:
output = sys.stdin.read()

if not output:
print("\033[31mError: No input received from stdin.\033[0m")
print("\033[31mError: No input received from stdin.\033[0m", file=sys.stderr)
sys.exit(1)

url = create_snippet(output)
Expand Down

0 comments on commit ce4c23c

Please sign in to comment.