Skip to content

Commit

Permalink
context menu for python plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Eichhorn committed Apr 11, 2017
1 parent d8f522c commit d8eca09
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 52 deletions.
11 changes: 9 additions & 2 deletions JsonRPC/wox.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ class Wox(object):
def __init__(self):
rpc_request = json.loads(sys.argv[1])
# proxy is not working now
self.proxy = rpc_request.get("proxy",{})
self.proxy = rpc_request.get("proxy",{})
request_method_name = rpc_request.get("method")
request_parameters = rpc_request.get("parameters")
methods = inspect.getmembers(self, predicate=inspect.ismethod)

request_method = dict(methods)[request_method_name]
results = request_method(*request_parameters)
if request_method_name == "query":

if request_method_name == "query" or request_method_name == "context_menu":
print(json.dumps({"result": results}))

def query(self,query):
Expand All @@ -28,6 +29,12 @@ def query(self,query):
"""
return []

def context_menu(self, data):
"""
optional context menu entries for a result
"""
return []

def debug(self,msg):
"""
alert msg
Expand Down
12 changes: 11 additions & 1 deletion Plugins/HelloWorldPython/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,17 @@ def query(self, query):
results = []
results.append({
"Title": "Hello World",
"SubTitle": "Query: {}".format(query),
"SubTitle": "Query: {}".format(query),
"IcoPath":"Images/app.ico",
"ContextData": "ctxData"
})
return results

def context_menu(self, data):
results = []
results.append({
"Title": "Context menu entry",
"SubTitle": "Data: {}".format(data),
"IcoPath":"Images/app.ico"
})
return results
Expand Down
2 changes: 1 addition & 1 deletion Scripts/post_build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function Copy-Resources ($path, $config) {
$target = "$output\$config"
Copy-Item -Recurse -Force $project\Themes\* $target\Themes\
Copy-Item -Recurse -Force $project\Images\* $target\Images\
Copy-Item -Recurse -Force $path\Plugins\HelloWorldPython $target\Plugins\HelloWorldPython
Copy-Item -Recurse -Force $path\Plugins\HelloWorldPython\* $target\Plugins\HelloWorldPython
Copy-Item -Recurse -Force $path\JsonRPC $target\JsonRPC
Copy-Item -Force $path\packages\squirrel*\tools\Squirrel.exe $output\Update.exe
}
Expand Down
11 changes: 11 additions & 0 deletions Wox.Core/Plugin/ExecutablePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,16 @@ protected override string ExecuteCallback(JsonRPCRequestModel rpcRequest)
_startInfo.Arguments = $"\"{rpcRequest}\"";
return Execute(_startInfo);
}

protected override string ExecuteContextMenu(Result selectedResult) {
JsonRPCServerRequestModel request = new JsonRPCServerRequestModel {
Method = "contextmenu",
Parameters = new object[] { selectedResult.ContextData },
};

_startInfo.Arguments = $"\"{request}\"";

return Execute(_startInfo);
}
}
}
24 changes: 13 additions & 11 deletions Wox.Core/Plugin/JsonPRCModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public override string ToString()
string rpc = string.Empty;
if (Parameters != null && Parameters.Length > 0)
{
string parameters = Parameters.Aggregate("[", (current, o) => current + (GetParamterByType(o) + ","));
string parameters = Parameters.Aggregate("[", (current, o) => current + (GetParameterByType(o) + ","));
parameters = parameters.Substring(0, parameters.Length - 1) + "]";
rpc = string.Format(@"{{\""method\"":\""{0}\"",\""parameters\"":{1}", Method, parameters);
}
Expand All @@ -69,25 +69,27 @@ public override string ToString()

}

private string GetParamterByType(object paramter)
private string GetParameterByType(object parameter)
{

if (paramter is string)
if (parameter is null) {
return "null";
}
if (parameter is string)
{
return string.Format(@"\""{0}\""", RepalceEscapes(paramter.ToString()));
return string.Format(@"\""{0}\""", ReplaceEscapes(parameter.ToString()));
}
if (paramter is int || paramter is float || paramter is double)
if (parameter is int || parameter is float || parameter is double)
{
return string.Format(@"{0}", paramter);
return string.Format(@"{0}", parameter);
}
if (paramter is bool)
if (parameter is bool)
{
return string.Format(@"{0}", paramter.ToString().ToLower());
return string.Format(@"{0}", parameter.ToString().ToLower());
}
return paramter.ToString();
return parameter.ToString();
}

private string RepalceEscapes(string str)
private string ReplaceEscapes(string str)
{
return str.Replace(@"\", @"\\") //Escapes in ProcessStartInfo
.Replace(@"\", @"\\") //Escapes itself when passed to client
Expand Down
89 changes: 52 additions & 37 deletions Wox.Core/Plugin/JsonRPCPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Wox.Core.Plugin
/// Represent the plugin that using JsonPRC
/// every JsonRPC plugin should has its own plugin instance
/// </summary>
internal abstract class JsonRPCPlugin : IPlugin
internal abstract class JsonRPCPlugin : IPlugin, IContextMenu
{
protected PluginInitContext context;
public const string JsonRPC = "JsonRPC";
Expand All @@ -29,56 +29,71 @@ internal abstract class JsonRPCPlugin : IPlugin

protected abstract string ExecuteQuery(Query query);
protected abstract string ExecuteCallback(JsonRPCRequestModel rpcRequest);
protected abstract string ExecuteContextMenu(Result selectedResult);

public List<Result> Query(Query query)
{
string output = ExecuteQuery(query);
if (!String.IsNullOrEmpty(output))
{
try
{
List<Result> results = new List<Result>();
try {
return DeserializeResult(output);
}
catch (Exception e) {
Log.Exception($"|JsonRPCPlugin.Query|Exception when query <{query}>", e);
return null;
}
}

JsonRPCQueryResponseModel queryResponseModel = JsonConvert.DeserializeObject<JsonRPCQueryResponseModel>(output);
if (queryResponseModel.Result == null) return null;
public List<Result> LoadContextMenus(Result selectedResult) {
string output = ExecuteContextMenu(selectedResult);
try {
return DeserializeResult(output);
}
catch (Exception e) {
Log.Exception($"|JsonRPCPlugin.LoadContextMenus|Exception on result <{selectedResult}>", e);
return null;
}
}

private List<Result> DeserializeResult(string output) {
if (!String.IsNullOrEmpty(output)) {
List<Result> results = new List<Result>();

foreach (JsonRPCResult result in queryResponseModel.Result)
JsonRPCQueryResponseModel queryResponseModel = JsonConvert.DeserializeObject<JsonRPCQueryResponseModel>(output);
if (queryResponseModel.Result == null) return null;

foreach (JsonRPCResult result in queryResponseModel.Result)
{
JsonRPCResult result1 = result;
result.Action = c =>
{
JsonRPCResult result1 = result;
result.Action = c =>
{
if (result1.JsonRPCAction == null) return false;
if (result1.JsonRPCAction == null) return false;

if (!String.IsNullOrEmpty(result1.JsonRPCAction.Method))
if (!String.IsNullOrEmpty(result1.JsonRPCAction.Method))
{
if (result1.JsonRPCAction.Method.StartsWith("Wox."))
{
if (result1.JsonRPCAction.Method.StartsWith("Wox."))
{
ExecuteWoxAPI(result1.JsonRPCAction.Method.Substring(4), result1.JsonRPCAction.Parameters);
}
else
ExecuteWoxAPI(result1.JsonRPCAction.Method.Substring(4), result1.JsonRPCAction.Parameters);
}
else
{
string actionReponse = ExecuteCallback(result1.JsonRPCAction);
JsonRPCRequestModel jsonRpcRequestModel = JsonConvert.DeserializeObject<JsonRPCRequestModel>(actionReponse);
if (jsonRpcRequestModel != null
&& !String.IsNullOrEmpty(jsonRpcRequestModel.Method)
&& jsonRpcRequestModel.Method.StartsWith("Wox."))
{
string actionReponse = ExecuteCallback(result1.JsonRPCAction);
JsonRPCRequestModel jsonRpcRequestModel = JsonConvert.DeserializeObject<JsonRPCRequestModel>(actionReponse);
if (jsonRpcRequestModel != null
&& !String.IsNullOrEmpty(jsonRpcRequestModel.Method)
&& jsonRpcRequestModel.Method.StartsWith("Wox."))
{
ExecuteWoxAPI(jsonRpcRequestModel.Method.Substring(4), jsonRpcRequestModel.Parameters);
}
ExecuteWoxAPI(jsonRpcRequestModel.Method.Substring(4), jsonRpcRequestModel.Parameters);
}
}
return !result1.JsonRPCAction.DontHideAfterAction;
};
results.Add(result);
}
return results;
}
catch (Exception e)
{
Log.Exception($"|JsonRPCPlugin.Query|Exception when query <{query}>", e);
}
return !result1.JsonRPCAction.DontHideAfterAction;
};
results.Add(result);
}
return results;
} else {
return null;
}
return null;
}

private void ExecuteWoxAPI(string method, object[] parameters)
Expand Down
11 changes: 11 additions & 0 deletions Wox.Core/Plugin/PythonPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,16 @@ protected override string ExecuteCallback(JsonRPCRequestModel rpcRequest)
_startInfo.WorkingDirectory = context.CurrentPluginMetadata.PluginDirectory;
return Execute(_startInfo);
}

protected override string ExecuteContextMenu(Result selectedResult) {
JsonRPCServerRequestModel request = new JsonRPCServerRequestModel {
Method = "context_menu",
Parameters = new object[] { selectedResult.ContextData },
};
_startInfo.Arguments = $"-B \"{context.CurrentPluginMetadata.ExecuteFilePath}\" \"{request}\"";
_startInfo.WorkingDirectory = context.CurrentPluginMetadata.PluginDirectory;

return Execute(_startInfo);
}
}
}

0 comments on commit d8eca09

Please sign in to comment.