From d4568ddbb58c936d293681bac014ef8f5f122ab3 Mon Sep 17 00:00:00 2001 From: Pierre-Antoine Rouby <pierre-antoine.rouby@inrae.fr> Date: Tue, 12 Sep 2023 12:58:19 +0200 Subject: [PATCH] Script: Add pamhyr2 script with exemple hello. --- src/Scripts/AScript.py | 34 ++++++++++++++++++++++++++++++++++ src/Scripts/Hello.py | 34 ++++++++++++++++++++++++++++++++++ src/pamhyr.py | 35 ++++++++++++++++++++++++++++++++--- 3 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 src/Scripts/AScript.py create mode 100644 src/Scripts/Hello.py diff --git a/src/Scripts/AScript.py b/src/Scripts/AScript.py new file mode 100644 index 00000000..ab8bf735 --- /dev/null +++ b/src/Scripts/AScript.py @@ -0,0 +1,34 @@ +# AScript.py -- Pamhyr abstract script class +# Copyright (C) 2023 INRAE +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +# -*- coding: utf-8 -*- + +from Model.Except import NotImplementedMethodeError + +class AScript(object): + name = "" + description = "" + + def __init__(self, args): + super(AScript, self).__init__() + + self._args = args.copy() + + def usage(self): + raise NotImplementedMethodeError(self, self.usage) + + def run(self): + raise NotImplementedMethodeError(self, self.run) diff --git a/src/Scripts/Hello.py b/src/Scripts/Hello.py new file mode 100644 index 00000000..dd20e431 --- /dev/null +++ b/src/Scripts/Hello.py @@ -0,0 +1,34 @@ +# help.py -- Pamhyr +# Copyright (C) 2023 INRAE +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +# -*- coding: utf-8 -*- + +import logging +from Scripts.AScript import AScript + +logger = logging.getLogger() + + +class ScriptHello(AScript): + name = "Hello" + description = "Display hello" + + def usage(self): + logger.info(f"Usage : {self._args[0]} hello") + + def run(self): + logger.info("Hello from Pamhyr2 script !") + return True diff --git a/src/pamhyr.py b/src/pamhyr.py index c2a7d938..61424fe8 100755 --- a/src/pamhyr.py +++ b/src/pamhyr.py @@ -34,10 +34,25 @@ from tools import ( from View.MainWindow import ApplicationWindow from Model.Study import Study +from Scripts.Hello import ScriptHello + from init import license, setup_lang logger = logging.getLogger() +scripts = { + "hello": ScriptHello, +} + +def usage(argv): + logger.error(f"{logger_color_red()}Invalid script name '{argv[1]}'{logger_color_reset()}") + logger.info(f"Usage: {argv[0]} <script> <args...>") + logger.info(f"") + logger.info(f"Valid scripts:") + for s in scripts: + logger.info(f"\t{s}\t\t{scripts[s].name}: {scripts[s].description}") + + def main(): conf = Config.load() app = QApplication(sys.argv) @@ -47,10 +62,24 @@ def main(): license() - application = ApplicationWindow(conf=conf) - application.show() + if len(sys.argv) > 1: + # Run a script + script = sys.argv[1] + + if script not in scripts: + usage(sys.argv) + sys.exit(-1) + + application = scripts[script](sys.argv) + ret = application.run() + if not ret: + application.usage() + else: + # No args, run Pamhyr2 interface + application = ApplicationWindow(conf=conf) + application.show() + ret = app.exec_() - ret = app.exec_() display_timers() sys.exit(ret) -- GitLab