You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by sp...@apache.org on 2016/08/26 20:25:20 UTC
[13/18] tinkerpop git commit: separated gremlin preferences node from
groovysh preferences node
separated gremlin preferences node from groovysh preferences node
Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/04bc602b
Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/04bc602b
Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/04bc602b
Branch: refs/heads/master
Commit: 04bc602bea652c28d1e1f050ff89f95dbc544e14
Parents: bdded27
Author: Robert Dale <ro...@gmail.com>
Authored: Mon Aug 22 09:36:19 2016 -0400
Committer: Robert Dale <ro...@gmail.com>
Committed: Mon Aug 22 09:36:19 2016 -0400
----------------------------------------------------------------------
.../tinkerpop/gremlin/console/Console.groovy | 35 ++++---
.../gremlin/console/Preferences.groovy | 102 +++++++++++++++++++
2 files changed, 124 insertions(+), 13 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/04bc602b/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/Console.groovy
----------------------------------------------------------------------
diff --git a/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/Console.groovy b/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/Console.groovy
index 896cdf5..a9854db 100644
--- a/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/Console.groovy
+++ b/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/Console.groovy
@@ -72,7 +72,7 @@ class Console {
public static final String PREFERENCE_ITERATION_MAX = "max-iteration"
private static final int DEFAULT_ITERATION_MAX = 100
private static int maxIteration = DEFAULT_ITERATION_MAX
-
+
public static final String PREF_GREMLIN_COLOR = "gremlin.color"
def gremlinColor = { Preferences.get(PREF_GREMLIN_COLOR, "reset") }
@@ -102,13 +102,13 @@ class Console {
public static final String PREF_RESULT_PROMPT_COLOR = "result.prompt.color"
def resultPromptColor = { Preferences.get(PREF_RESULT_PROMPT_COLOR, "reset") }
-
+
public static final String PREF_EMPTY_RESULT_IND = "empty.result.indicator"
def emptyResult = { Preferences.get(PREF_EMPTY_RESULT_IND, "null") }
public static final String PREF_INPUT_PROMPT = "input.prompt"
def inputPrompt = { Preferences.get(PREF_INPUT_PROMPT, "gremlin>") }
-
+
public static final String PREF_RESULT_PROMPT = "result.prompt"
static def resultPrompt = { Preferences.get(PREF_RESULT_PROMPT, "==>") }
@@ -127,7 +127,7 @@ class Console {
*/
@Deprecated
public Console(final String initScriptFile) {
- this(new IO(System.in, System.out, System.err), initScriptFile.size() != null ? [initScriptFile] : null, true)
+ this(new IO(System.in, System.out, System.err), initScriptFile.size() != null ? [initScriptFile]: null, true)
}
public Console(final IO io, final List<String> scriptAndArgs, final boolean interactive) {
@@ -143,12 +143,17 @@ class Console {
maxIteration = Preferences.get(PREFERENCE_ITERATION_MAX, DEFAULT_ITERATION_MAX.toString()).toInteger()
Preferences.addChangeListener(new PreferenceChangeListener() {
- @Override
- void preferenceChange(PreferenceChangeEvent evt) {
- if (evt.key == PREFERENCE_ITERATION_MAX)
- maxIteration = Integer.parseInt(evt.newValue)
- }
- })
+ @Override
+ void preferenceChange(PreferenceChangeEvent evt) {
+ if (evt.key == PREFERENCE_ITERATION_MAX && null != evt.newValue)
+ try {
+ maxIteration = Integer.parseInt(evt.newValue)
+ } catch (NumberFormatException e) {
+ io.out.println(ansiRender(errorColor,"Unable to convert '${evt.newValue}' to integer. Using default ${DEFAULT_ITERATION_MAX}"))
+ maxIteration = DEFAULT_ITERATION_MAX
+ }
+ }
+ })
final Mediator mediator = new Mediator(this)
@@ -310,10 +315,10 @@ class Console {
}
}
}
-
+
def colorizeResult = { object ->
if (object instanceof Vertex) {
- return ansiRender(vertexColor, object.toString())
+ return ansiRender(vertexColor, object.toString())
} else if (object instanceof Edge) {
return ansiRender(edgeColor, object.toString())
} else if (object instanceof Iterable) {
@@ -433,10 +438,14 @@ class Console {
if (!interactive) System.exit(1)
}
}
-
+
def ansiRender = { color, text -> Ansi.ansi().render(String.format("@|%s %s|@", color.call(), text)).toString() }
public static void main(final String[] args) {
+
+// ExpandoMetaClass.enableGlobally()
+ org.apache.tinkerpop.gremlin.console.Preferences.expandoMagic()
+
// need to do some up front processing to try to support "bin/gremlin.sh init.groovy" until this deprecated
// feature can be removed. ultimately this should be removed when a breaking change can go in
IO io = new IO(System.in, System.out, System.err)
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/04bc602b/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/Preferences.groovy
----------------------------------------------------------------------
diff --git a/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/Preferences.groovy b/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/Preferences.groovy
new file mode 100644
index 0000000..699d602
--- /dev/null
+++ b/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/Preferences.groovy
@@ -0,0 +1,102 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tinkerpop.gremlin.console;
+
+import java.util.prefs.PreferenceChangeEvent
+import java.util.prefs.PreferenceChangeListener
+
+import org.codehaus.groovy.tools.shell.IO
+
+public class Preferences {
+
+ private static final java.util.prefs.Preferences STORE = java.util.prefs.Preferences.userRoot().node("/org/apache/tinkerpop/gremlin/console");
+
+ public static void expandoMagic() {
+ org.codehaus.groovy.tools.shell.util.Preferences.getMetaClass().'static'.getShowLastResult = {
+ return STORE.getBoolean(org.codehaus.groovy.tools.shell.util.Preferences.SHOW_LAST_RESULT_KEY, true);
+ }
+
+ org.codehaus.groovy.tools.shell.util.Preferences.getMetaClass().'static'.getSanitizeStackTrace = {
+ return STORE.getBoolean(org.codehaus.groovy.tools.shell.util.Preferences.SANITIZE_STACK_TRACE_KEY, true);
+ }
+
+ org.codehaus.groovy.tools.shell.util.Preferences.getMetaClass().'static'.getEditor = {
+ return STORE.get(org.codehaus.groovy.tools.shell.util.Preferences.EDITOR_KEY, System.getenv("EDITOR"));
+ }
+
+ org.codehaus.groovy.tools.shell.util.Preferences.getMetaClass().'static'.getParserFlavor = {
+ return STORE.get(org.codehaus.groovy.tools.shell.util.Preferences.PARSER_FLAVOR_KEY, org.codehaus.groovy.tools.shell.util.Preferences.PARSER_RIGID);
+ }
+
+ //
+ // Store Access
+ //
+
+ org.codehaus.groovy.tools.shell.util.Preferences.getMetaClass().'static'.keys = {
+ return STORE.keys();
+ }
+
+ org.codehaus.groovy.tools.shell.util.Preferences.getMetaClass().'static'.get = { String name, String defaultValue ->
+ return STORE.get(name, defaultValue);
+ }
+
+ org.codehaus.groovy.tools.shell.util.Preferences.getMetaClass().'static'.get = { String name ->
+ return get(name, null);
+ }
+
+ org.codehaus.groovy.tools.shell.util.Preferences.getMetaClass().'static'.put = { String name, String value ->
+ STORE.put(name, value);
+ }
+
+ org.codehaus.groovy.tools.shell.util.Preferences.getMetaClass().'static'.clear = { STORE.clear(); }
+
+ org.codehaus.groovy.tools.shell.util.Preferences.getMetaClass().'static'.addChangeListener = { PreferenceChangeListener listener ->
+ STORE.addPreferenceChangeListener(listener);
+ }
+
+ // reinstall change handler
+ String tmp = STORE.get(org.codehaus.groovy.tools.shell.util.Preferences.VERBOSITY_KEY, IO.Verbosity.INFO.name);
+ try {
+ org.codehaus.groovy.tools.shell.util.Preferences.verbosity = IO.Verbosity.forName(tmp);
+ }
+ catch (IllegalArgumentException e) {
+ org.codehaus.groovy.tools.shell.util.Preferences.verbosity = IO.Verbosity.INFO;
+ STORE.remove(org.codehaus.groovy.tools.shell.util.Preferences.VERBOSITY_KEY);
+ }
+
+ org.codehaus.groovy.tools.shell.util.Preferences.addChangeListener(new PreferenceChangeListener() {
+ public void preferenceChange(final PreferenceChangeEvent event) {
+ if (event.getKey().equals(org.codehaus.groovy.tools.shell.util.Preferences.VERBOSITY_KEY)) {
+ String name = event.getNewValue();
+
+ if (name == null) {
+ name = IO.Verbosity.INFO.name;
+ }
+
+ try {
+ org.codehaus.groovy.tools.shell.util.Preferences.verbosity = IO.Verbosity.forName(name);
+ }
+ catch (Exception e) {
+ event.getNode().put(event.getKey(), org.codehaus.groovy.tools.shell.util.Preferences.verbosity.name);
+ }
+ }
+ }
+ });
+ }
+}