[sikuli] 240/385: implemented the <delete unused images> in Java
Gilles Filippini
pini at moszumanska.debian.org
Sun Jun 29 19:26:18 UTC 2014
This is an automated email from the git hooks/post-receive script.
pini pushed a commit to tag upstream/1.1.0_beta1
in repository sikuli.
commit a48022cffda4d56d5a2fa1794940f31ae19485c1
Author: Raimund Hocke <info at its-me-raiman.de>
Date: Thu Feb 20 17:05:07 2014 +0100
implemented the <delete unused images> in Java
---
.../main/java/org/sikuli/basics/FileManager.java | 51 +++++++++++++++------
IDE/pom.xml | 5 +++
IDE/src/main/java/org/sikuli/ide/EditorPane.java | 52 +++++++++++++++++++---
.../main/java/org/sikuli/syntaxhighlight/Run.java | 6 +--
.../org/sikuli/syntaxhighlight/grammar/Lexer.java | 7 ++-
.../{contrib => LexerContrib}/ClojureLexer.jso | 0
.../{contrib => LexerContrib}/CssLexer.jso | 0
.../{contrib => LexerContrib}/DefaultStyle.jso | 0
.../{contrib => LexerContrib}/HtmlLexer.jso | 0
.../{contrib => LexerContrib}/JavaLexer.jso | 0
.../{contrib => LexerContrib}/JavascriptLexer.jso | 0
.../{contrib => LexerContrib}/PythonLexer.jso | 0
.../{contrib => LexerContrib}/XmlLexer.jso | 0
.../sikuli/scriptrunner/JythonScriptRunner.java | 10 -----
.../src/main/resources/scripts/clean-dot-sikuli.py | 29 ------------
15 files changed, 93 insertions(+), 67 deletions(-)
diff --git a/Basics/src/main/java/org/sikuli/basics/FileManager.java b/Basics/src/main/java/org/sikuli/basics/FileManager.java
index ccaa796..39d3293 100644
--- a/Basics/src/main/java/org/sikuli/basics/FileManager.java
+++ b/Basics/src/main/java/org/sikuli/basics/FileManager.java
@@ -34,6 +34,7 @@ import java.security.CodeSource;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
+import java.util.List;
import java.util.Random;
import java.util.ServiceLoader;
import java.util.jar.JarOutputStream;
@@ -57,13 +58,13 @@ public class FileManager {
private static void log0(int level, String message, Object... args) {
Debug.logx(level, "", me + ": " + message, args);
}
- //</editor-fold>
-
+ //</editor-fold>
+
static final int DOWNLOAD_BUFFER_SIZE = 153600;
static IResourceLoader nativeLoader = null;
private static MultiFrame _progress = null;
private static final String EXECUTABLE = "#executable";
-
+
/**
* System.load() the given library module <br>
* from standard places (folder libs or SikuliX/libs) in the following order<br>
@@ -102,7 +103,7 @@ public class FileManager {
conn.disconnect();
}
}
-
+
public static Proxy getProxy() {
Proxy proxy = Settings.proxy;
if (!Settings.proxyChecked) {
@@ -129,7 +130,7 @@ public class FileManager {
}
return proxy;
}
-
+
public static boolean setProxy(String pName, String pPort) {
InetAddress a = null;
String host = null;
@@ -164,7 +165,7 @@ public class FileManager {
}
return false;
}
-
+
/**
* download a file at the given url to a local folder
*
@@ -210,7 +211,7 @@ public class FileManager {
FileOutputStream writer = new FileOutputStream(fullpath);
InputStream reader;
if (getProxy() != null) {
- reader = url.openConnection(getProxy()).getInputStream();
+ reader = url.openConnection(getProxy()).getInputStream();
} else {
reader = url.openConnection().getInputStream();
}
@@ -342,7 +343,7 @@ public class FileManager {
log0(lvl, "tempdir delete: %s", path);
}
}
-
+
public static boolean deleteFileOrFolder(String path, fileFilter filter) {
File entry = new File(path);
File f;
@@ -381,7 +382,7 @@ public class FileManager {
}
return true;
}
-
+
public static boolean deleteFileOrFolder(String path) {
return deleteFileOrFolder(path, null);
}
@@ -868,6 +869,28 @@ public class FileManager {
}
}
+ public static void deleteNotUsedImages(String bundle, List<String> usedImages) {
+ File scriptFolder = new File(bundle);
+ if (!scriptFolder.isDirectory()) {
+ return;
+ }
+ String path;
+ for (File image : scriptFolder.listFiles(new FilenameFilter() {
+ @Override
+ public boolean accept(File dir, String name) {
+ if ((name.endsWith(".png") || name.endsWith(".jpg"))) {
+ return true;
+ }
+ return false;
+ }
+ })) {
+ if (!usedImages.contains(image.getName())) {
+ Debug.log(3, "FileManager: delete not used: %s", image);
+ image.delete();
+ }
+ }
+ }
+
private static class FileFilterScript implements FilenameFilter {
private String _check;
public FileFilterScript(String check) {
@@ -902,7 +925,7 @@ public class FileManager {
}
return nativeLoader;
}
-
+
public static String getJarParentFolder() {
CodeSource src = FileManager.class.getProtectionDomain().getCodeSource();
String jarParentPath = "--- not known ---";
@@ -917,7 +940,7 @@ public class FileManager {
}
return RunningFromJar + jarParentPath;
}
-
+
public static String getJarName() {
CodeSource src = FileManager.class.getProtectionDomain().getCodeSource();
if (src.getLocation() != null) {
@@ -925,7 +948,7 @@ public class FileManager {
}
return "";
}
-
+
public static boolean writeStringToFile(String text, String path) {
PrintStream out = null;
try {
@@ -1099,7 +1122,7 @@ public class FileManager {
addToJarWriteFile(jar, dir, prefix);
}
}
-
+
private static void addToJarWriteFile(JarOutputStream jar, File file, String prefix) throws IOException {
if (file.getName().startsWith(".")) {
return;
@@ -1134,7 +1157,7 @@ public class FileManager {
}
out.flush();
}
-
+
public static boolean pathEquals(String path1, String path2) {
return (new File(path1)).equals(new File(path2));
}
diff --git a/IDE/pom.xml b/IDE/pom.xml
index 50a9637..155678d 100755
--- a/IDE/pom.xml
+++ b/IDE/pom.xml
@@ -58,6 +58,11 @@
<artifactId>mac_widgets</artifactId>
<version>0.9.5</version>
</dependency>
+ <dependency>
+ <groupId>org.sikuli</groupId>
+ <artifactId>Jygments4SikuliX</artifactId>
+ <version>1.1.0</version>
+ </dependency>
</dependencies>
<build>
diff --git a/IDE/src/main/java/org/sikuli/ide/EditorPane.java b/IDE/src/main/java/org/sikuli/ide/EditorPane.java
index 4ccd56d..be39c2b 100755
--- a/IDE/src/main/java/org/sikuli/ide/EditorPane.java
+++ b/IDE/src/main/java/org/sikuli/ide/EditorPane.java
@@ -11,6 +11,8 @@ import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.*;
import java.io.*;
+import java.util.ArrayList;
+import java.util.List;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
@@ -33,6 +35,9 @@ import org.sikuli.script.Location;
import org.sikuli.basics.SikuliX;
import org.sikuli.script.Image;
import org.sikuli.script.ImagePath;
+import org.sikuli.syntaxhighlight.ResolutionException;
+import org.sikuli.syntaxhighlight.grammar.Lexer;
+import org.sikuli.syntaxhighlight.grammar.Token;
public class EditorPane extends JTextPane implements KeyListener, CaretListener {
@@ -331,7 +336,7 @@ public class EditorPane extends JTextPane implements KeyListener, CaretListener
private void writeSrcFile() throws IOException {
Debug.log(3, "IDE: writeSrcFile: " + _editingFile.getName());
writeFile(_editingFile.getAbsolutePath());
- if (PreferencesUser.getInstance().getAtSaveMakeHTML()) {
+ if (PreferencesUser.getInstance().getAtSaveMakeHTML()) {
convertSrcToHtml(getSrcBundle());
} else {
String snameDir = new File(_editingFile.getAbsolutePath()).getParentFile().getName();
@@ -339,12 +344,21 @@ public class EditorPane extends JTextPane implements KeyListener, CaretListener
(new File(snameDir, sname)).delete();
}
//TODO bundle image clean in Java
- if (Settings.CPYTHON.equals(getSikuliContentType()) && PreferencesUser.getInstance().getAtSaveCleanBundle()) {
+ if (PreferencesUser.getInstance().getAtSaveCleanBundle()) {
cleanBundle(getSrcBundle());
}
setDirty(false);
}
+ private Lexer getLexer(File script) {
+ try {
+ return Lexer.getByName("python");
+ } catch (ResolutionException ex) {
+ return null;
+ }
+
+ }
+
public String exportAsZip() throws IOException, FileNotFoundException {
File file = new SikuliIDEFileChooser(SikuliIDE.getInstance()).export();
if (file == null) {
@@ -462,16 +476,40 @@ public class EditorPane extends JTextPane implements KeyListener, CaretListener
}
private void convertSrcToHtml(String bundle) {
- SikuliX.getScriptRunner("jython", null, null).doSomethingSpecial("convertSrcToHtml",
- new String[]{bundle});
+ if (null != SikuliX.getScriptRunner("jython", null, null)) {
+ SikuliX.getScriptRunner("jython", null, null).doSomethingSpecial("convertSrcToHtml",
+ new String[]{bundle});
+ }
}
private void cleanBundle(String bundle) {
- if (!PreferencesUser.getInstance().getAtSaveCleanBundle()) {
+ String scriptText = getText();
+ Lexer lexer = getLexer(_editingFile);
+ Iterable<Token> tokens = lexer.getTokens(scriptText);
+ List<String> usedImages = new ArrayList<String>();
+ boolean inString = false;
+ String current;
+ for (Token t : tokens) {
+ current = t.getValue();
+ if (!inString) {
+ if ("'\"".contains(current)) {
+ inString = true;
+ }
+ continue;
+ }
+ if ("'\"".contains(current)) {
+ inString = false;
+ continue;
+ }
+ if (current.endsWith(".png") || current.endsWith("jpg")) {
+ Debug.log(3,"IDE: save: used image: %s", current);
+ usedImages.add(current);
+ }
+ }
+ if (usedImages.size() == 0) {
return;
}
- SikuliX.getScriptRunner("jython", null, null).doSomethingSpecial("cleanBundle",
- new String[]{bundle});
+ FileManager.deleteNotUsedImages(bundle, usedImages);
}
public File copyFileToBundle(String filename) {
diff --git a/Jygments4SikuliX/src/main/java/org/sikuli/syntaxhighlight/Run.java b/Jygments4SikuliX/src/main/java/org/sikuli/syntaxhighlight/Run.java
index c3e137c..ea1f392 100644
--- a/Jygments4SikuliX/src/main/java/org/sikuli/syntaxhighlight/Run.java
+++ b/Jygments4SikuliX/src/main/java/org/sikuli/syntaxhighlight/Run.java
@@ -9,14 +9,14 @@ import org.sikuli.syntaxhighlight.grammar.Lexer;
import org.sikuli.syntaxhighlight.grammar.Token;
public class Run {
-
+
private static void p(String text, Object... args) {
System.out.println(String.format(text, args));
- }
+ }
public static void main(String[] args) throws IOException, ResolutionException {
String file = System.getProperty("user.dir") + "/src/main/java/org/sikuli/syntaxhighlight/Util.java";
- String aLexer = "java";
+ String aLexer = "python";
Lexer lexer = Lexer.getByName(aLexer);
if (lexer != null) {
Formatter formatter = Formatter.getByName("html");
diff --git a/Jygments4SikuliX/src/main/java/org/sikuli/syntaxhighlight/grammar/Lexer.java b/Jygments4SikuliX/src/main/java/org/sikuli/syntaxhighlight/grammar/Lexer.java
index abd5396..f29c097 100644
--- a/Jygments4SikuliX/src/main/java/org/sikuli/syntaxhighlight/grammar/Lexer.java
+++ b/Jygments4SikuliX/src/main/java/org/sikuli/syntaxhighlight/grammar/Lexer.java
@@ -60,12 +60,11 @@ public class Lexer extends Grammar
else
{
// Try contrib package
- String pack = Jygments.class.getPackage().getName();
- lexer = getByFullName( pack, "contrib", name );
+ lexer = getByFullName( "LexerContrib", "", name );
if( lexer == null )
{
// Try this package
- pack = Lexer.class.getPackage().getName();
+ String pack = Lexer.class.getPackage().getName();
lexer = getByFullName( pack, "", name );
}
return lexer;
@@ -75,7 +74,7 @@ public class Lexer extends Grammar
public static Lexer getByFullName( String name ) throws ResolutionException {
return getByFullName("", "", name);
}
-
+
@SuppressWarnings("unchecked")
public static Lexer getByFullName( String pack, String sub, String name ) throws ResolutionException
{
diff --git a/Jygments4SikuliX/src/main/resources/contrib/ClojureLexer.jso b/Jygments4SikuliX/src/main/resources/LexerContrib/ClojureLexer.jso
similarity index 100%
rename from Jygments4SikuliX/src/main/resources/contrib/ClojureLexer.jso
rename to Jygments4SikuliX/src/main/resources/LexerContrib/ClojureLexer.jso
diff --git a/Jygments4SikuliX/src/main/resources/contrib/CssLexer.jso b/Jygments4SikuliX/src/main/resources/LexerContrib/CssLexer.jso
similarity index 100%
rename from Jygments4SikuliX/src/main/resources/contrib/CssLexer.jso
rename to Jygments4SikuliX/src/main/resources/LexerContrib/CssLexer.jso
diff --git a/Jygments4SikuliX/src/main/resources/contrib/DefaultStyle.jso b/Jygments4SikuliX/src/main/resources/LexerContrib/DefaultStyle.jso
similarity index 100%
rename from Jygments4SikuliX/src/main/resources/contrib/DefaultStyle.jso
rename to Jygments4SikuliX/src/main/resources/LexerContrib/DefaultStyle.jso
diff --git a/Jygments4SikuliX/src/main/resources/contrib/HtmlLexer.jso b/Jygments4SikuliX/src/main/resources/LexerContrib/HtmlLexer.jso
similarity index 100%
rename from Jygments4SikuliX/src/main/resources/contrib/HtmlLexer.jso
rename to Jygments4SikuliX/src/main/resources/LexerContrib/HtmlLexer.jso
diff --git a/Jygments4SikuliX/src/main/resources/contrib/JavaLexer.jso b/Jygments4SikuliX/src/main/resources/LexerContrib/JavaLexer.jso
similarity index 100%
rename from Jygments4SikuliX/src/main/resources/contrib/JavaLexer.jso
rename to Jygments4SikuliX/src/main/resources/LexerContrib/JavaLexer.jso
diff --git a/Jygments4SikuliX/src/main/resources/contrib/JavascriptLexer.jso b/Jygments4SikuliX/src/main/resources/LexerContrib/JavascriptLexer.jso
similarity index 100%
rename from Jygments4SikuliX/src/main/resources/contrib/JavascriptLexer.jso
rename to Jygments4SikuliX/src/main/resources/LexerContrib/JavascriptLexer.jso
diff --git a/Jygments4SikuliX/src/main/resources/contrib/PythonLexer.jso b/Jygments4SikuliX/src/main/resources/LexerContrib/PythonLexer.jso
similarity index 100%
rename from Jygments4SikuliX/src/main/resources/contrib/PythonLexer.jso
rename to Jygments4SikuliX/src/main/resources/LexerContrib/PythonLexer.jso
diff --git a/Jygments4SikuliX/src/main/resources/contrib/XmlLexer.jso b/Jygments4SikuliX/src/main/resources/LexerContrib/XmlLexer.jso
similarity index 100%
rename from Jygments4SikuliX/src/main/resources/contrib/XmlLexer.jso
rename to Jygments4SikuliX/src/main/resources/LexerContrib/XmlLexer.jso
diff --git a/Jython/src/main/java/org/sikuli/scriptrunner/JythonScriptRunner.java b/Jython/src/main/java/org/sikuli/scriptrunner/JythonScriptRunner.java
index 484c0e6..11825e1 100644
--- a/Jython/src/main/java/org/sikuli/scriptrunner/JythonScriptRunner.java
+++ b/Jython/src/main/java/org/sikuli/scriptrunner/JythonScriptRunner.java
@@ -509,9 +509,6 @@ public class JythonScriptRunner implements IScriptRunner {
} else if ("convertSrcToHtml".equals(action)) {
convertSrcToHtml((String) args[0]);
return true;
- } else if ("cleanBundle".equals(action)) {
- cleanBundle((String) args[0]);
- return true;
} else if ("createRegionForWith".equals(action)) {
args[0] = createRegionForWith(args[0]);
return true;
@@ -637,13 +634,6 @@ public class JythonScriptRunner implements IScriptRunner {
py.exec(pyConverter);
}
- private void cleanBundle(String bundle) {
- PythonInterpreter py = new PythonInterpreter();
- log(lvl, "Clear source bundle " + bundle);
- py.set("bundle_path", bundle);
- py.exec(pyBundleCleaner);
- }
-
private Object createRegionForWith(Object reg) {
return null;
}
diff --git a/Jython/src/main/resources/scripts/clean-dot-sikuli.py b/Jython/src/main/resources/scripts/clean-dot-sikuli.py
deleted file mode 100755
index cd7305f..0000000
--- a/Jython/src/main/resources/scripts/clean-dot-sikuli.py
+++ /dev/null
@@ -1,29 +0,0 @@
-#TODO to be implemneted in Java (-> FileManager.java)
-import os
-import re
-import glob
-import sys
-
-def grep(string,list):
- expr = re.compile(string)
- return filter(expr.search,list)
-
-if locals().has_key('bundle_path'):
- path = bundle_path
-#path = sys.argv[1]
-
-f_py = glob.glob(path + "/*.py")
-pngFilter = re.compile("\"([^\"]+\.png)\"", re.I)
-goodFiles = []
-
-for py in f_py:
- src = open(py, "r")
- for line in src:
- m = pngFilter.findall(line)
- if m:
- goodFiles += m
- src.close()
-for png in glob.glob(path + "/*.png"):
- if not os.path.basename(png) in goodFiles:
- os.remove(png)
-
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/sikuli.git
More information about the pkg-java-commits
mailing list