From 88ce1638164b828317892ddd1ebec4540d046788 Mon Sep 17 00:00:00 2001 From: TimoEg Date: Thu, 10 Oct 2024 17:59:03 +0200 Subject: [PATCH] fixed default __os__ --- README.md | 3 --- installation_instruction/__main__.py | 31 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 3a8ac59..e588ed2 100644 --- a/README.md +++ b/README.md @@ -114,9 +114,6 @@ description: pip: Standard python package manager. ``` -> [!CAUTION] -> Currently `__os__` automatic detection not working. - * For the package to set the default os to the running system, name the property `__os__`. ```yaml diff --git a/installation_instruction/__main__.py b/installation_instruction/__main__.py index c9df53a..6c6f509 100644 --- a/installation_instruction/__main__.py +++ b/installation_instruction/__main__.py @@ -27,6 +27,7 @@ import click import json import platformdirs +import platform from .__init__ import __version__, __description__, __repository__, __author__, __author_email__, __license__ from .get_flags_and_options_from_schema import _get_flags_and_options @@ -39,6 +40,29 @@ License: {__license__} Repository: {__repository__}""" +def _get_system(option_types): + """ + Returns the os from the list of possible os systems defined in the schema. + + :param option_types: list of system from the schema. + :type option_types: list + :return: os system from input list or None. + :rtype: string or None + """ + + system = platform.system() + system_names = { + 'Linux': 'linux', + 'Darwin': 'mac', + 'Windows': 'win', + } + + new_default = system_names.get(system,None) + for type in option_types: + if new_default in type.lower(): + return type + + return None class ConfigReadCommand(click.MultiCommand): """ @@ -65,6 +89,13 @@ def get_command(self, ctx, config_file: str) -> click.Command|None: _red_echo("Error (parsing options from schema): " + str(e)) exit(1) + #set new default value for __os__ Option + for option in options: + if '__os__' in option.name: + system_default = _get_system(option.type.choices) + if system_default: + option.default = system_default + def callback(**kwargs): inst = instruction.validate_and_render(kwargs) if inst[1]: