Skip to content

Commit

Permalink
Final changes to release 0.1. Testing desktop app is unfinished at th…
Browse files Browse the repository at this point in the history
…os point.
  • Loading branch information
livegrios committed Jun 24, 2024
1 parent 9b5dd1c commit 8450817
Show file tree
Hide file tree
Showing 16 changed files with 134 additions and 80 deletions.
35 changes: 18 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ and retrieve all the messages that are produced by your Python program.
#### 1. Your Python Environment
Consider that you have your standard Python environment installed at: ```C:\Python311```
#### 2. Your Python Script
For instance, consider that you have a Python script called ```print_numbers.py```
For instance, consider that you have a Python script called ```print_numbers_v1.py```
which is intented to print the first 10 numbers starting at 1:
```python
# print_numbers.py
# [C:/Users/Markdown/print_numbers_v1.py]

for i in range(1, 11):
print(i)
```
And imagine that your python file is stored in ```C:/Users/Markdown/print_numbers.py```.
And imagine that your python file is stored in ```C:/Users/Markdown/print_numbers_v1.py```.

From you Java app, you can invoke the Python environment to execute your script and
print the outputs produced by Python in whis way:
Expand All @@ -31,9 +31,9 @@ public class Test
{
try
{
PythonEnvironment pyenv = new PythonEnvironment("C:/Python311_Dist01",
"C:/Python311_Dist01/python.exe");
PythonScript ps = new PythonScript("C:/Users/Markdown/print_numbers.py", "", "");
PythonEnvironment pyenv = new PythonEnvironment("C:/Python311",
"C:/Python311/python.exe");
PythonScript ps = new PythonScript("C:/Users/Markdown/print_numbers_v1.py", "", "");
PythonListener listener = new PythonListener()
{
@Override
Expand Down Expand Up @@ -84,36 +84,37 @@ with the next content:
```
You can create the ```PythonEnvironment``` object as:
```
PythonEnvironment pyenv = PythonEnvironment.fromFile("C:/Users/Markdown/Documents/python_env.json");
PythonEnvironment pyenv = PythonEnvironment.fromFile("C:/Users/Markdown/python_env.json");
```

Consequently, the script configuration file (```C:/Users/Markdown/Documents/script01.json```) content can be as follows:
Consequently, the script configuration file (```C:/Users/Markdown/script01.json```) content can be as follows:
```json
{
"shortName" : "Example01",
"description" : "Script which prints the first 10 int numbers.",
"filePath" : "C:/Users/Markdown/print_numbers.py",
"action" : "",
"parameters" : []
"arguments" : []
}
```

And, the PythonScript object can be created as following:
```java
PythonScript ps = PythonScript.fromFile("C:/Users/Markdown/Documents/script01.json");
PythonScript ps = PythonScript.fromFile("C:/Users/Markdown/script01.json");
```

### Example 2
In this example, you have a second Python Script which prints the first N integer numbers,
where N is a value passed as an argument to the script. Your Python script is as follows:
where N is a value passed as an argument to the script. In this example, the Python script
code is as follows:
```python
# [C:/Users/Markdown/print_numbers_dynamic.py]
# [C:/Users/Markdown/print_numbers_v2.py]
import sys

try:

max_value = int(sys.argv[1])
print('Parameter Received [max_value=%d]' % max_value)
print('Argument Received [max_value=%d]' % max_value)
for i in range(1, max_value + 1):
print(i)

Expand All @@ -125,14 +126,14 @@ except RuntimeWarning as rw:
print(str(rw))
```

And this is your script configuration file stored in ```[C:/Users/Markdown/Documents/script01.json]```:
And this is your script configuration file stored in ```[C:/Users/Markdown/Documents/script02.json]```:
```json
{
"shortName" : "Example02",
"description" : "Script which prints the first N int numbers specified by the max_value parameter.",
"description" : "Script which prints the first N int numbers specified by the max_value argument.",
"filePath" : "E:/VSPROY/VS2022/UTL/jpyll/python_samples/print_numbers_v2.py",
"action" : "",
"parameters" : [
"arguments" : [
{
"name" : "max_value",
"type" : "Int",
Expand All @@ -152,7 +153,7 @@ public class Test
try
{
PythonEnvironment pyenv = PythonEnvironment.fromFile("C:/Users/Markdown/Documents/python_env.json");
PythonScript ps = PythonScript.fromFile("C:/Users/Markdown/Documents/script01.json");
PythonScript ps = PythonScript.fromFile("C:/Users/Markdown/Documents/script02.json");
PythonListener listener = new PythonListener()
{
@Override
Expand Down
6 changes: 3 additions & 3 deletions jpyll/src/org/livegrios/jpyll/PythonProcess.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.livegrios.jpyll.model.PythonParameter;
import org.livegrios.jpyll.model.PythonArgument;
import org.livegrios.jpyll.model.PythonEnvironment;
import org.livegrios.jpyll.model.PythonScript;

Expand Down Expand Up @@ -62,13 +62,13 @@ public static PythonProcess build(PythonEnvironment pyenv, PythonScript pythonSc
@Override
public void run()
{
cmd = new String[2 + pythonScript.getParameters().size()];
cmd = new String[2 + pythonScript.getArguments().size()];
int i = 2;
try
{
cmd[0] = pyenv.getPythonBinPath();
cmd[1] = pythonScript.getFilePath();
for (PythonParameter par : pythonScript.getParameters())
for (PythonArgument par : pythonScript.getArguments())
cmd[i++] =par.getValue().toString();
Runtime runtime = Runtime.getRuntime();
process = runtime.exec(cmd);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* This class abstracts a Python script parameter information.
* @author LiveGrios
*/
public class PythonParameter
public class PythonArgument
{
public static enum Type
{
Expand All @@ -45,7 +45,7 @@ public static enum Type
/**
* The default constructor.
*/
public PythonParameter(){}
public PythonArgument(){}

/**
* Create a <code>Parameter</code> object specifying its <i>name</i> and
Expand All @@ -56,7 +56,7 @@ public PythonParameter(){}
* integer (Type.Int), double precision (Type.Float),
* string (Type.String) and boolean (Type.Boolean).
*/
public PythonParameter(String name, Type type)
public PythonArgument(String name, Type type)
{
this.name = name;
this.type = type;
Expand All @@ -74,7 +74,7 @@ public PythonParameter(String name, Type type)
* such as numbers (integer and floating point), String and
* boolean.
*/
public PythonParameter(String name, Type type, Object value)
public PythonArgument(String name, Type type, Object value)
{
this.name = name;
this.type = type;
Expand Down
30 changes: 15 additions & 15 deletions jpyll/src/org/livegrios/jpyll/model/PythonScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ public class PythonScript
String description;
String filePath;
String action;
List<PythonParameter> parameters;
List<PythonArgument> arguments;
/**
* The default constructor.
*/
public PythonScript()
{
parameters = new ArrayList<>();
arguments = new ArrayList<>();
}

/**
Expand All @@ -65,7 +65,7 @@ public PythonScript(String filePath, String description, String action)
this.filePath = filePath;
this.description = description;
this.action = action;
this.parameters = new ArrayList<>();
this.arguments = new ArrayList<>();
}

/**
Expand Down Expand Up @@ -142,30 +142,30 @@ public void setAction(String action)
}

/**
* Return the list of parameters needed by the script.
* @return The list of parameters.
* Return the list of arguments needed by the script.
* @return The list of arguments.
*/
public List<PythonParameter> getParameters()
public List<PythonArgument> getArguments()
{
return parameters;
return arguments;
}

/**
* Remove all the preiously defined script parameters.
* Remove all the preiously defined script arguments.
*/
public void clearParameters()
public void clearArguments()
{
parameters.clear();
arguments.clear();
}

/**
* Add a parameter to the script.
* @param p The script <code>Parameter</code> to be added.
* Add arguments to the script.
* @param p The script <code>Argument</code> to be added.
*/
public void addParameter(PythonParameter... p)
public void addArgument(PythonArgument... p)
{
for (PythonParameter par : p)
parameters.add(par);
for (PythonArgument par : p)
arguments.add(par);
}

public String toJSON()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@
import com.google.gson.GsonBuilder;
import java.util.List;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import org.fxmisc.richtext.CodeArea;
import org.livegrios.jpyll.model.PythonEnvironment;
import org.livegrios.jpyll.model.PythonParameter;
import org.livegrios.jpyll.model.PythonArgument;
import org.livegrios.jpyll.model.PythonScript;

/**
Expand All @@ -23,7 +20,7 @@ public class ControllerPythonScript
static Gson gson = new GsonBuilder().setPrettyPrinting().create();

Main main;
ObservableList<PythonParameter> parameters;
ObservableList<PythonArgument> parameters;
TableAdapterPythonParameters tableAdapterPythonParameters;

PythonEnvironment pyenv;
Expand Down Expand Up @@ -88,7 +85,7 @@ public void refreshCodeArea(CodeArea codeArea, Object entity)
codeArea.append(gson.toJson(entity), Application.STYLESHEET_MODENA);
}

public List<PythonParameter> getParameters()
public List<PythonArgument> getParameters()
{
return main.tblvScriptParameters.getItems();
}
Expand All @@ -97,20 +94,20 @@ public void addNewParameter()
{
String name = main.txtParameterName.getText().trim();
int typeIndex = main.cmbPythonParameterType.getSelectionModel().getSelectedIndex();
PythonParameter.Type type = null;
PythonArgument.Type type = null;
String value = main.txtParameterValue.getText();
PythonParameter pp = null;
PythonArgument pp = null;

if (name.isEmpty())
name = "Unamed";

if (typeIndex < 0)
typeIndex = 0;

type = PythonParameter.Type.values()[typeIndex];
type = PythonArgument.Type.values()[typeIndex];

pp = new PythonParameter(name, type, value);
pyscript.addParameter(pp);
pp = new PythonArgument(name, type, value);
pyscript.addArgument(pp);
parameters.add(pp);
refreshCodeArea(main.codeAreaPythonScript, pyscript);
}
Expand Down
6 changes: 3 additions & 3 deletions jpyll_test/src/org/livegrios/jpyll_test/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.livegrios.jpyll.JPythonLinker;
import org.livegrios.jpyll.PythonListener;
import org.livegrios.jpyll.model.PythonEnvironment;
import org.livegrios.jpyll.model.PythonParameter;
import org.livegrios.jpyll.model.PythonArgument;
import org.livegrios.jpyll.model.PythonScript;

/**
Expand Down Expand Up @@ -50,7 +50,7 @@ public class Main extends Application
@FXML TextField txtParameterName;
@FXML TextField txtParameterValue;

@FXML TableView<PythonParameter> tblvScriptParameters;
@FXML TableView<PythonArgument> tblvScriptParameters;

@FXML ComboBox cmbPythonParameterType;

Expand Down Expand Up @@ -120,7 +120,7 @@ private void initComponents()

controllerPythonScript = new ControllerPythonScript(this);

cmbPythonParameterType.setItems(FXCollections.observableArrayList(PythonParameter.Type.values()));
cmbPythonParameterType.setItems(FXCollections.observableArrayList(PythonArgument.Type.values()));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.util.Callback;
import org.livegrios.jpyll.model.PythonParameter;
import org.livegrios.jpyll.model.PythonArgument;

/**
*
Expand All @@ -21,14 +21,14 @@ public class TableAdapterPythonParameters
public static Font FONT_DEFAULT = Font.font("Monospaced", FontWeight.NORMAL, 16);
public static Font FONT_TYPE = Font.font("Monospaced", FontWeight.BOLD, 16);

TableColumn<PythonParameter, String> tcParamName;
TableColumn<PythonParameter, PythonParameter.Type> tcParamType;
TableColumn<PythonParameter, Object> tcParamValue;
TableColumn<PythonArgument, String> tcParamName;
TableColumn<PythonArgument, PythonArgument.Type> tcParamType;
TableColumn<PythonArgument, Object> tcParamValue;

TableView<PythonParameter> tableView;
ObservableList<PythonParameter> data;
TableView<PythonArgument> tableView;
ObservableList<PythonArgument> data;

public TableAdapterPythonParameters(TableView<PythonParameter> tableView, ObservableList<PythonParameter> data)
public TableAdapterPythonParameters(TableView<PythonArgument> tableView, ObservableList<PythonArgument> data)
{
this.tableView = tableView;
this.data = data;
Expand All @@ -41,9 +41,9 @@ public void adapt()
tcParamValue = new TableColumn<>("Value");

tcParamName.setPrefWidth(95);
tcParamName.setCellFactory((TableColumn<PythonParameter, String> param) ->
tcParamName.setCellFactory((TableColumn<PythonArgument, String> param) ->
{
TableCell<PythonParameter, String> cell = new TableCell<PythonParameter, String>()
TableCell<PythonArgument, String> cell = new TableCell<PythonArgument, String>()
{
@Override
protected void updateItem(String value, boolean empty)
Expand All @@ -64,12 +64,12 @@ protected void updateItem(String value, boolean empty)
});

tcParamType.setPrefWidth(95);
tcParamType.setCellFactory((TableColumn<PythonParameter, PythonParameter.Type> param) ->
tcParamType.setCellFactory((TableColumn<PythonArgument, PythonArgument.Type> param) ->
{
TableCell<PythonParameter, PythonParameter.Type> cell = new TableCell<PythonParameter, PythonParameter.Type>()
TableCell<PythonArgument, PythonArgument.Type> cell = new TableCell<PythonArgument, PythonArgument.Type>()
{
@Override
protected void updateItem(PythonParameter.Type value, boolean empty)
protected void updateItem(PythonArgument.Type value, boolean empty)
{
super.updateItem(value, empty); //To change body of generated methods, choose Tools | Templates.

Expand All @@ -89,9 +89,9 @@ protected void updateItem(PythonParameter.Type value, boolean empty)


tcParamValue.setPrefWidth(95);
tcParamValue.setCellFactory((TableColumn<PythonParameter, Object> param) ->
tcParamValue.setCellFactory((TableColumn<PythonArgument, Object> param) ->
{
TableCell<PythonParameter, Object> cell = new TableCell<PythonParameter, Object>()
TableCell<PythonArgument, Object> cell = new TableCell<PythonArgument, Object>()
{
@Override
protected void updateItem(Object value, boolean empty)
Expand Down
Loading

0 comments on commit 8450817

Please sign in to comment.