[DEV] some modifs
This commit is contained in:
parent
85fb5f5c81
commit
5097b0b041
1
out/eclipse/.gitignore
vendored
Normal file
1
out/eclipse/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/classes/
|
127
src/org/atriasoft/island/Actions.java
Normal file
127
src/org/atriasoft/island/Actions.java
Normal file
@ -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<String, Class<ActionInterface>> actions = new HashMap<String,Class<ActionInterface>>();
|
||||
public static void addAction(String name, Class<ActionInterface> klass) {
|
||||
actions.put(name, klass);
|
||||
}
|
||||
/**
|
||||
* Get the wall list of action availlable
|
||||
* @return The list of action name
|
||||
*/
|
||||
public Set<String> 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<ActionInterface> 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<ActionInterface> 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<String> 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<String> arguments) {
|
||||
Class<ActionInterface> 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);
|
||||
}
|
||||
}
|
10
src/org/atriasoft/island/model/ActionException.java
Normal file
10
src/org/atriasoft/island/model/ActionException.java
Normal file
@ -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);
|
||||
}
|
||||
|
||||
}
|
49
src/org/atriasoft/island/model/ActionInterface.java
Normal file
49
src/org/atriasoft/island/model/ActionInterface.java
Normal file
@ -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();
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@ -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
|
Loading…
x
Reference in New Issue
Block a user