From 5097b0b0411feedb6d66be8c9b0bb09c92b38ffe Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Wed, 16 Jun 2021 20:44:13 +0200 Subject: [PATCH] [DEV] some modifs --- out/eclipse/.gitignore | 1 + src/org/atriasoft/island/Actions.java | 127 ++++++++++++++++ .../island/model/ActionException.java | 10 ++ .../island/model/ActionInterface.java | 49 ++++++ tmpsrc/actions.java | 140 ------------------ 5 files changed, 187 insertions(+), 140 deletions(-) create mode 100644 out/eclipse/.gitignore create mode 100644 src/org/atriasoft/island/Actions.java create mode 100644 src/org/atriasoft/island/model/ActionException.java create mode 100644 src/org/atriasoft/island/model/ActionInterface.java delete mode 100644 tmpsrc/actions.java diff --git a/out/eclipse/.gitignore b/out/eclipse/.gitignore new file mode 100644 index 0000000..840e7d3 --- /dev/null +++ b/out/eclipse/.gitignore @@ -0,0 +1 @@ +/classes/ diff --git a/src/org/atriasoft/island/Actions.java b/src/org/atriasoft/island/Actions.java new file mode 100644 index 0000000..905a711 --- /dev/null +++ b/src/org/atriasoft/island/Actions.java @@ -0,0 +1,127 @@ +package org.atriasoft.island; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.atriasoft.island.internal.Log; +import org.atriasoft.island.model.ActionException; +import org.atriasoft.island.model.ActionInterface; + +public class Actions { + private static Map> actions = new HashMap>(); + public static void addAction(String name, Class klass) { + actions.put(name, klass); + } + /** + * Get the wall list of action availlable + * @return The list of action name + */ + public Set getListOfAction() { + return actions.keySet(); + } + + + ///** + // * Get a description of an action + // * @param action_name (string) Name of the action + // * @param function_name (string) Name of the fucntion to call + // * @param default_value (*) Renurned value of the call if function does not exist + // * @return (*) the getted value or the default_value + // */ + //public void get_function_value(action_name, function_name, default_value = None): + // global list_actions; + // for elem in list_actions: + // if elem["name"] == action_name: + // // finish the parsing + // sys.path.append(os.path.dirname(elem["path"])) + // the_action = __import__(__base_action_name + action_name) + // if function_name not in dir(the_action): + // return default_value + // method_to_call = getattr(the_action, function_name) + // return method_to_call() + // return default_value + + /** + * Get the global help value of a module + * @param action_name (string) Name of the action + * @return The first line of description + */ + private String getFullActionHelp(String action_name) { + Class actionClass = actions.get(action_name); + if (actionClass == null) { + return "Action does not exist"; + } + ActionInterface action = actionClass.getDeclaredConstructor().newInstance(); + if (action == null) { + return "Action creation error"; + } + return action.help(); + } + + private String getActionHelpExample(String action_name) { + Class actionClass = actions.get(action_name); + if (actionClass == null) { + return null; + } + ActionInterface action = actionClass.getDeclaredConstructor().newInstance(); + if (action == null) { + return null; + } + return action.helpExample(); + } + + /** + * Get the global help value of a module + * @param action_name (string) Name of the action + * @return The first line of description + */ + public String getActionHelp(String action_name) { + return getFullActionHelp(action_name).split("\n")[0]; + } + + public void usage(String action_name, List arguments) { + String help = getFullActionHelp(action_name); + Log.print("Description:"); + Log.print("\t" + help); + //TODO: ??? arguments.display(action_name); + String helpExample = getActionHelpExample(action_name); + if (helpExample != null) { + Log.print("Example:"); + for (String elem : helpExample.split("\n")) { + Log.print("\t" + elem); + } + } + System.exit(0); + } + + public void execute(String action_name, List arguments) { + Class actionClass = actions.get(action_name); + if (actionClass == null) { + throw new ActionException("Action does not registered"); + } + ActionInterface action = actionClass.getDeclaredConstructor().newInstance(); + if (action == null) { + throw new ActionException("Uneable to intanciate Action"); + } + Log.info("action: " + action_name); + Arguments my_under_args_parser = new Arguments(); + my_under_args_parser.add("h", "help", null, "Help of this action"); + action.addSpecificArguments(my_under_args_parser, action_name); + boolean have_unknow_argument = action.haveUnknowArgument(); + my_under_args = my_under_args_parser.parse(arguments, have_unknow_argument); + // search help if needed ==> permit to not duplicating code + for (elem : my_under_args) { + if (elem.get_option_name() == "help") { + usage(my_under_args_parser, action_name); + System.exit(0); + } + } + // now we can execute: + Log.info("execute: " + action_name); + for (elem : my_under_args) { + Log.debug(" " + elem.get_option_name() + "='" + elem.get_arg() + "'"); + } + action.execute(my_under_args); + } +} diff --git a/src/org/atriasoft/island/model/ActionException.java b/src/org/atriasoft/island/model/ActionException.java new file mode 100644 index 0000000..53bae3c --- /dev/null +++ b/src/org/atriasoft/island/model/ActionException.java @@ -0,0 +1,10 @@ +package org.atriasoft.island.model; + +public class ActionException extends Exception { + private static final long serialVersionUID = 1L; + + public ActionException(String message) { + super(message); + } + +} diff --git a/src/org/atriasoft/island/model/ActionInterface.java b/src/org/atriasoft/island/model/ActionInterface.java new file mode 100644 index 0000000..88528a2 --- /dev/null +++ b/src/org/atriasoft/island/model/ActionInterface.java @@ -0,0 +1,49 @@ +package org.atriasoft.island.model; + +public interface ActionInterface { + /** + * Get the global description of the current action + * @return the description string (fist line if reserved for the overview, all is for the specific display) + */ + String help(); + + /** + * @brief Add argument to the specific action + * @param[in,out] my_args (death.Arguments) Argument manager + * @param[in] section Name of the currect action + */ + default void addSpecificArguments(Arguments myArgs, String section) { + + } + + /** + * Set the option argument are not able to check if the argument are correct or not + * @return Have parameter without arguments + */ + default boolean haveUnknowArgument() { + return false; + } + + /** + * Add argument to the specific action + * @param myArgs (death.Arguments) Argument manager + * @param section Name of the current action + */ + void add_specific_arguments(Arguments myArgs, String section); + + /** + * Execute the action required. + * @return error value [0 .. 50] the <0 value is reserved system ==> else, what you want. + * None : No error (return program out 0) + * -10 : ACTION is not existing + * -11 : ACTION execution system error + * -12 : ACTION Wrong parameters + */ + public void execute(Arguments _arguments) throws ActionException; + + String helpExample(); + + + + +} diff --git a/tmpsrc/actions.java b/tmpsrc/actions.java deleted file mode 100644 index 34e4fa4..0000000 --- a/tmpsrc/actions.java +++ /dev/null @@ -1,140 +0,0 @@ -//!/usr/bin/python -// -*- coding: utf-8 -*- -//# -//# @author Edouard DUPIN -//# -//# @copyright 2012, Edouard DUPIN, all right reserved -//# -//# @license MPL v2.0 (see license file) -//# - -// Local import -from realog import debug -import os -import sys -from . import env -import death.Arguments as arguments - -list_actions = [] - -__base_action_name = env.get_system_base_name() + "Action_" - -public void init(files): - global list_actions; - debug.verbose("List of action for island: " + str(len(files))) - for elem_path in files : - debug.verbose("parse file : " + elem_path) - base_name = os.path.basename(elem_path) - if len(base_name) <= 3 + len(__base_action_name): - // reject it, too small - continue - base_name = base_name[:-3] - if base_name[:len(__base_action_name)] != __base_action_name: - // reject it, wrong start file - continue - name_action = base_name[len(__base_action_name):] - debug.debug(" '" + os.path.basename(elem_path)[:-3] + "' file=" + elem_path) - list_actions.append({ - "name":name_action, - "path":elem_path, - }) - -//# -//# @brief Get the wall list of action availlable -//# @return ([string]) the list of action name -//# -public void get_list_of_action(): - global list_actions; - out = [] - for elem in list_actions: - out.append(elem["name"]) - return out - -//# -//# @brief Get a description of an action -//# @param[in] action_name (string) Name of the action -//# @param[in] function_name (string) Name of the fucntion to call -//# @param[in] default_value (*) Renurned value of the call if function does not exist -//# @return (*) the getted value or the default_value -//# -public void get_function_value(action_name, function_name, default_value = None): - global list_actions; - for elem in list_actions: - if elem["name"] == action_name: - // finish the parsing - sys.path.append(os.path.dirname(elem["path"])) - the_action = __import__(__base_action_name + action_name) - if function_name not in dir(the_action): - return default_value - method_to_call = getattr(the_action, function_name) - return method_to_call() - return default_value - -//# -//# @brief Get the global help value of a module -//# @param[in] action_name (string) Name of the action -//# @return The first line of description -//# -public void get_action_help(action_name): - value = get_function_value(action_name, "help", "---") - return value.split("\n")[0] - - -public void usage(arguments, action_name): - color = debug.get_color_set() - // generic argument displayed for specific action: - //print("Specific argument for the command: '" + action_name + "'" ) - //print(" " + get_desc(action_name)) - value = get_function_value(action_name, "help") - debug.info("Description:") - debug.info("\t" + str(value)) - arguments.display(action_name) - value = get_function_value(action_name, "help_example") - if value != None: - debug.info("Example:") - for elem in value.split("\n"): - debug.info("\t" + value) - exit(0) - -public void execute(action_name, argument_start_id): - global list_actions; - // TODO: Move here the check if action is availlable - - for elem in list_actions: - if elem["name"] != action_name: - continue - debug.info("action: " + str(elem)); - // finish the parsing - sys.path.append(os.path.dirname(elem["path"])) - the_action = __import__(__base_action_name + action_name) - my_under_args_parser = arguments.Arguments() - my_under_args_parser.add("h", "help", desc="Help of this action") - - if "add_specific_arguments" in dir(the_action): - the_action.add_specific_arguments(my_under_args_parser, elem["name"]) - have_unknow_argument = false - if "have_unknow_argument" in dir(the_action): - have_unknow_argument = the_action.have_unknow_argument() - my_under_args = my_under_args_parser.parse(argument_start_id, have_unknow_argument) - // search help if needed ==> permit to not duplicating code - for elem in my_under_args: - if elem.get_option_name() == "help": - usage(my_under_args_parser, action_name) - return 0 - // now we can execute: - if "execute" not in dir(the_action): - debug.error("execute is not implmented for this action ... '" + str(action_name) + "'") - return -11 - debug.info("execute: " + action_name) - for elem in my_under_args: - debug.debug(" " + str(elem.get_option_name()) + "='" + str(elem.get_arg()) + "'") - ret = the_action.execute(my_under_args) - if ret == None: - return 0 - if ret < 0: - debug.info(" ==========================") - debug.info(" == Some error occured ==") - debug.info(" ==========================") - return ret - debug.error("Can not do the action...") - return -10