source: trunk/src/org/expeditee/actions/Python.java@ 1078

Last change on this file since 1078 was 1078, checked in by davidb, 8 years ago

Refactoring to allow support for additional scriptable languages. In this case, Python being added in through Jythohn Java library

File size: 4.5 KB
Line 
1/**
2 * Python.java
3 * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package org.expeditee.actions;
20
21import java.awt.Color;
22import java.util.Collection;
23import java.util.LinkedList;
24import java.util.List;
25
26import javax.script.Bindings;
27import javax.script.Invocable;
28import javax.script.ScriptContext;
29import javax.script.ScriptEngine;
30import javax.script.ScriptEngineManager;
31import javax.script.ScriptException;
32import javax.script.SimpleScriptContext;
33
34import org.expeditee.gui.Frame;
35import org.expeditee.gui.FrameIO;
36import org.expeditee.gui.FrameUtils;
37import org.expeditee.gui.MessageBay;
38import org.expeditee.io.flowlayout.XGroupItem;
39import org.expeditee.items.Item;
40import org.expeditee.items.ItemUtils;
41import org.expeditee.items.Text;
42
43/**
44 * Javascript parser.
45 * Works differently to the other Javascript class in that it
46 * parses a frame as a whole rather than parsing individual text items as separate statements
47 *
48 * @author jts21
49 * @author davidb
50 */
51public class Python extends ScriptBase {
52
53 protected void init() {
54 ERROR_FRAMESET = "PythonErrors";
55 scriptEngineManager = new ScriptEngineManager();
56 scriptEngine = scriptEngineManager.getEngineByMimeType("application/python");
57 scriptEngine.put("invocable", (Invocable) scriptEngine);
58 }
59
60 public static void printPythonFrame(Item item) {
61 if(item.getChild() == null) {
62 // if the user clicked on the action without an item on their cursor
63 if(item.hasAction()) {
64 boolean isThis = false;
65 for(String s : item.getAction()) {
66 if(s.equalsIgnoreCase("printPythonFrame")) {
67 isThis = true;
68 break;
69 }
70 }
71 if(isThis) {
72 System.out.println(new Python(item.getParentOrCurrentFrame(), true));
73 return;
74 }
75 }
76 MessageBay.warningMessage("Requires either an item with a link to a frame, or no item (will run the current frame)");
77 } else {
78 System.out.println(new Python(item.getChild(), true));
79 }
80 }
81
82 public static void runPythonFrame(Item item) throws Exception {
83 if(item.getChild() == null) {
84 // if the user clicked on the action without an item on their cursor
85 if(item.hasAction()) {
86 boolean isThis = false;
87 for(String s : item.getAction()) {
88 if(s.equalsIgnoreCase("runPythonFrame")) {
89 isThis = true;
90 break;
91 }
92 }
93 if(isThis) {
94 Python.runFrameFollow(item.getParentOrCurrentFrame());
95 return;
96 }
97 }
98 MessageBay.warningMessage("Requires either an item with a link to a frame, or no item (will run the current frame)");
99 } else {
100 Python.runFrameFollow(item.getChild());
101 }
102 }
103
104
105 private static synchronized void runFrameFollow(Frame frame) throws Exception {
106 Python python = new Python(frame, true);
107 python.runFrame(frame,true);
108 }
109
110 private Python(Frame frame, boolean followLinks) {
111 super(frame,followLinks);
112 }
113
114
115 protected void processItems(List<Item> overlapping_y_ordered_items, boolean followLinks, int depth) {
116
117 // Loop through the items looking for code and links to new frames
118 for(Item i : overlapping_y_ordered_items) {
119 if(followLinks && i.hasLink()) {
120 Frame child = i.getChild();
121 if(child != null && !seen.contains(child)) {
122 this.parseFrame(child, true, depth);
123 }
124 }
125 if(i instanceof Text && !i.isAnnotation()) {
126 String text = ((Text)i).getText();
127 if (i == org.expeditee.io.flowlayout.XGroupItem.GROUPSEP_START) {
128 text = null;
129 depth++;
130 }
131 else if (i == org.expeditee.io.flowlayout.XGroupItem.GROUPSEP_END) {
132 text = null;
133 depth--;
134 }
135
136 if (text != null) {
137 int lineNumber = 0;
138 for(String line : text.trim().split("[\\n\\r]+")) {
139 if (depth>0) {
140 sb.append(String.format("%1$" + (4*depth) + "s", "")); // indent with 'depth' x 4 spaces
141 }
142 sb.append(line).append("\n");
143 lines.add(new CodeLine((Text)i, lineNumber++, line));
144 }
145 }
146 }
147 }
148
149
150 }
151
152}
Note: See TracBrowser for help on using the repository browser.