Skip to content
This repository has been archived by the owner on Jan 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #747 from Avaiga/feature/taipy#260-taipy-run-cli
Browse files Browse the repository at this point in the history
Feature/taipy#260 - Update _CoreCLI arguments to be reused in taipy run command
  • Loading branch information
trgiangdo authored Sep 6, 2023
2 parents 659e74d + 75d9ea9 commit 7072a7c
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 50 deletions.
115 changes: 73 additions & 42 deletions src/taipy/core/_core_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.

from typing import Dict

from taipy._cli._base_cli import _CLI

from .config import CoreSection
Expand All @@ -17,71 +19,100 @@
class _CoreCLI:
"""Command-line interface for Taipy Core application."""

@classmethod
def create_parser(cls):
core_parser = _CLI._add_groupparser("Taipy Core", "Optional arguments for Taipy Core service")

mode_group = core_parser.add_mutually_exclusive_group()
mode_group.add_argument(
"--development",
action="store_true",
help="""
__MODE_ARGS: Dict[str, Dict] = {
"--development": {
"action": "store_true",
"dest": "taipy_development",
"help": """
When execute Taipy application in `development` mode, all entities from the previous development version
will be deleted before running new Taipy application.
""",
)
mode_group.add_argument(
"--experiment",
nargs="?",
const="",
metavar="VERSION",
help="""
},
"--experiment": {
"dest": "taipy_experiment",
"nargs": "?",
"const": "",
"metavar": "VERSION",
"help": """
When execute Taipy application in `experiment` mode, the current Taipy application is saved to a new
version. If version name already exists, check for compatibility with current Python Config and run the
application. Without being specified, the version number will be a random string.
""",
)
mode_group.add_argument(
"--production",
nargs="?",
const="",
metavar="VERSION",
help="""
},
"--production": {
"dest": "taipy_production",
"nargs": "?",
"const": "",
"metavar": "VERSION",
"help": """
When execute in `production` mode, the current version is used in production. All production versions
should have the same configuration and share all entities. Without being specified, the latest version
is used.
""",
)
},
}

__FORCE_ARGS: Dict[str, Dict] = {
"--force": {
"dest": "taipy_force",
"action": "store_true",
"help": """
Force override the configuration of the version if existed and run the application. Default to False.
""",
},
"--no-force": {
"dest": "no_taipy_force",
"action": "store_true",
"help": "Stop the application if any Config conflict exists.",
},
}

@classmethod
def create_parser(cls):
core_parser = _CLI._add_groupparser("Taipy Core", "Optional arguments for Taipy Core service")

mode_group = core_parser.add_mutually_exclusive_group()
for mode_arg, mode_arg_dict in cls.__MODE_ARGS.items():
mode_group.add_argument(mode_arg, cls.__add_taipy_prefix(mode_arg), **mode_arg_dict)

force_group = core_parser.add_mutually_exclusive_group()
force_group.add_argument(
"--taipy-force",
action="store_true",
help="Force override the configuration of the version if existed and run the application."
" Default to False.",
)
force_group.add_argument(
"--no-taipy-force",
action="store_true",
help="Stop the application if any Config conflict exists.",
)
for force_arg, force_arg_dict in cls.__FORCE_ARGS.items():
force_group.add_argument(cls.__add_taipy_prefix(force_arg), **force_arg_dict)

@classmethod
def create_run_parser(cls):
run_parser = _CLI._add_subparser("run", help="Run a Taipy application.")
mode_group = run_parser.add_mutually_exclusive_group()
for mode_arg, mode_arg_dict in cls.__MODE_ARGS.items():
mode_group.add_argument(mode_arg, **mode_arg_dict)

force_group = run_parser.add_mutually_exclusive_group()
for force_arg, force_arg_dict in cls.__FORCE_ARGS.items():
force_group.add_argument(force_arg, **force_arg_dict)

@classmethod
def parse_arguments(cls):
args = _CLI._parse()
as_dict = {}
if args.development:
if args.taipy_development:
as_dict[CoreSection._MODE_KEY] = CoreSection._DEVELOPMENT_MODE
elif args.experiment is not None:
elif args.taipy_experiment is not None:
as_dict[CoreSection._MODE_KEY] = CoreSection._EXPERIMENT_MODE
as_dict[CoreSection._VERSION_NUMBER_KEY] = args.experiment
elif args.production is not None:
as_dict[CoreSection._VERSION_NUMBER_KEY] = args.taipy_experiment
elif args.taipy_production is not None:
as_dict[CoreSection._MODE_KEY] = CoreSection._PRODUCTION_MODE
as_dict[CoreSection._VERSION_NUMBER_KEY] = args.production
as_dict[CoreSection._VERSION_NUMBER_KEY] = args.taipy_production

if args.taipy_force:
as_dict[CoreSection._TAIPY_FORCE_KEY] = True
as_dict[CoreSection._FORCE_KEY] = True
elif args.no_taipy_force:
as_dict[CoreSection._TAIPY_FORCE_KEY] = False
as_dict[CoreSection._FORCE_KEY] = False

return as_dict

@classmethod
def __add_taipy_prefix(cls, key: str):
if key.startswith("--no-"):
return key[:5] + "taipy-" + key[5:]

return key[:2] + "taipy-" + key[2:]
16 changes: 8 additions & 8 deletions src/taipy/core/config/core_section.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ class CoreSection(UniqueSection):
_VERSION_NUMBER_KEY = "version_number"
_DEFAULT_VERSION_NUMBER = ""

_TAIPY_FORCE_KEY = "force"
_DEFAULT_TAIPY_FORCE = False
_FORCE_KEY = "force"
_DEFAULT_FORCE = False

def __init__(
self,
Expand All @@ -92,7 +92,7 @@ def __init__(
)
self.mode = mode or self._DEFAULT_MODE
self.version_number = version_number or self._DEFAULT_VERSION_NUMBER
self.force = force or self._DEFAULT_TAIPY_FORCE
self.force = force or self._DEFAULT_FORCE
super().__init__(**properties)

def __copy__(self):
Expand Down Expand Up @@ -167,7 +167,7 @@ def default_config(cls):
cls._DEFAULT_READ_ENTITY_RETRY,
cls._DEFAULT_MODE,
cls._DEFAULT_VERSION_NUMBER,
cls._DEFAULT_TAIPY_FORCE,
cls._DEFAULT_FORCE,
)

def _clean(self):
Expand All @@ -178,7 +178,7 @@ def _clean(self):
self._read_entity_retry = self._DEFAULT_READ_ENTITY_RETRY
self.mode = self._DEFAULT_MODE
self.version_number = self._DEFAULT_VERSION_NUMBER
self.force = self._DEFAULT_TAIPY_FORCE
self.force = self._DEFAULT_FORCE
self._properties.clear()

def _to_dict(self):
Expand All @@ -198,7 +198,7 @@ def _to_dict(self):
if self.version_number is not None:
as_dict[self._VERSION_NUMBER_KEY] = self.version_number
if self.force is not None:
as_dict[self._TAIPY_FORCE_KEY] = self.force
as_dict[self._FORCE_KEY] = self.force
as_dict.update(self._properties)
return as_dict

Expand All @@ -211,7 +211,7 @@ def _from_dict(cls, as_dict: Dict[str, Any], id=None, config: Optional[_Config]
read_entity_retry = as_dict.pop(cls._READ_ENTITY_RETRY_KEY, None)
mode = as_dict.pop(cls._MODE_KEY, None)
version_nb = as_dict.pop(cls._VERSION_NUMBER_KEY, None)
force = as_dict.pop(cls._TAIPY_FORCE_KEY, None)
force = as_dict.pop(cls._FORCE_KEY, None)
return CoreSection(
root_folder,
storage_folder,
Expand Down Expand Up @@ -254,7 +254,7 @@ def _update(self, as_dict: Dict[str, Any]):
if self.version_number != version_number:
self.version_number = version_number

force = _tpl._replace_templates(as_dict.pop(self._TAIPY_FORCE_KEY, self.force))
force = _tpl._replace_templates(as_dict.pop(self._FORCE_KEY, self.force))
if self.force != force:
self.force = force

Expand Down

0 comments on commit 7072a7c

Please sign in to comment.