From c89918a89028df0c4a1c88a207a1ffbede5922c2 Mon Sep 17 00:00:00 2001 From: Christian Kratky Date: Wed, 8 Apr 2020 22:01:20 +0200 Subject: [PATCH] Update python runtime for MQTTnet.Server. --- Source/MQTTnet.Server/MQTTnet.Server.csproj | 4 +-- .../DataSharing/DataSharingService.cs | 10 +++--- .../Scripting/PythonIOStream.cs | 8 ++--- .../Scripting/PythonScriptHostService.cs | 34 +++++++++---------- .../Scripting/PythonScriptInstance.cs | 6 ++-- Source/MQTTnet.Server/Web/Startup.cs | 4 +-- Source/MQTTnet.Server/appsettings.json | 6 +--- 7 files changed, 34 insertions(+), 38 deletions(-) diff --git a/Source/MQTTnet.Server/MQTTnet.Server.csproj b/Source/MQTTnet.Server/MQTTnet.Server.csproj index 885ec2f..967086a 100644 --- a/Source/MQTTnet.Server/MQTTnet.Server.csproj +++ b/Source/MQTTnet.Server/MQTTnet.Server.csproj @@ -43,8 +43,8 @@ - - + + diff --git a/Source/MQTTnet.Server/Scripting/DataSharing/DataSharingService.cs b/Source/MQTTnet.Server/Scripting/DataSharing/DataSharingService.cs index 41b6854..f4f52d7 100644 --- a/Source/MQTTnet.Server/Scripting/DataSharing/DataSharingService.cs +++ b/Source/MQTTnet.Server/Scripting/DataSharing/DataSharingService.cs @@ -1,14 +1,14 @@ -using System; +using Microsoft.Extensions.Logging; +using System; using System.Collections.Generic; -using Microsoft.Extensions.Logging; namespace MQTTnet.Server.Scripting.DataSharing { public class DataSharingService { - private readonly Dictionary _storage = new Dictionary(); - private readonly PythonScriptHostService _pythonScriptHostService; - private readonly ILogger _logger; + readonly Dictionary _storage = new Dictionary(); + readonly PythonScriptHostService _pythonScriptHostService; + readonly ILogger _logger; public DataSharingService(PythonScriptHostService pythonScriptHostService, ILogger logger) { diff --git a/Source/MQTTnet.Server/Scripting/PythonIOStream.cs b/Source/MQTTnet.Server/Scripting/PythonIOStream.cs index b346e89..0755a59 100644 --- a/Source/MQTTnet.Server/Scripting/PythonIOStream.cs +++ b/Source/MQTTnet.Server/Scripting/PythonIOStream.cs @@ -1,14 +1,14 @@ -using System; +using Microsoft.Extensions.Logging; +using System; using System.IO; using System.Text; -using Microsoft.Extensions.Logging; namespace MQTTnet.Server.Scripting { public class PythonIOStream : Stream { - private readonly ILogger _logger; - private readonly Encoding _encoder = Encoding.UTF8; + readonly ILogger _logger; + readonly Encoding _encoder = Encoding.UTF8; public PythonIOStream(ILogger logger) { diff --git a/Source/MQTTnet.Server/Scripting/PythonScriptHostService.cs b/Source/MQTTnet.Server/Scripting/PythonScriptHostService.cs index c521d9d..f3607ff 100644 --- a/Source/MQTTnet.Server/Scripting/PythonScriptHostService.cs +++ b/Source/MQTTnet.Server/Scripting/PythonScriptHostService.cs @@ -1,4 +1,8 @@ -using System; +using Microsoft.Extensions.Logging; +using Microsoft.Scripting; +using Microsoft.Scripting.Hosting; +using MQTTnet.Server.Configuration; +using System; using System.Collections.Generic; using System.Dynamic; using System.IO; @@ -6,22 +10,18 @@ using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; -using Microsoft.Extensions.Logging; -using Microsoft.Scripting; -using Microsoft.Scripting.Hosting; -using MQTTnet.Server.Configuration; namespace MQTTnet.Server.Scripting { public class PythonScriptHostService { - private readonly IDictionary _proxyObjects = new ExpandoObject(); - private readonly List _scriptInstances = new List(); - private readonly string _scriptsPath; - private readonly ScriptingSettingsModel _scriptingSettings; - private readonly ILogger _logger; - private readonly ScriptEngine _scriptEngine; - + readonly IDictionary _proxyObjects = new ExpandoObject(); + readonly List _scriptInstances = new List(); + readonly string _scriptsPath; + readonly ScriptingSettingsModel _scriptingSettings; + readonly ILogger _logger; + readonly ScriptEngine _scriptEngine; + public PythonScriptHostService(ScriptingSettingsModel scriptingSettings, PythonIOStream pythonIOStream, ILogger logger) { _scriptingSettings = scriptingSettings ?? throw new ArgumentNullException(nameof(scriptingSettings)); @@ -139,7 +139,7 @@ namespace MQTTnet.Server.Scripting } } - private async Task TryInitializeScriptAsync(string path) + async Task TryInitializeScriptAsync(string path) { var uid = new FileInfo(path).Name.Replace(".py", string.Empty, StringComparison.OrdinalIgnoreCase); @@ -155,7 +155,7 @@ namespace MQTTnet.Server.Scripting { _scriptInstances.Add(scriptInstance); } - + _logger.LogInformation($"Initialized script '{uid}'."); } catch (Exception exception) @@ -164,7 +164,7 @@ namespace MQTTnet.Server.Scripting } } - private PythonScriptInstance CreateScriptInstance(string uid, string path, string code) + PythonScriptInstance CreateScriptInstance(string uid, string path, string code) { var scriptScope = _scriptEngine.CreateScope(); @@ -173,11 +173,11 @@ namespace MQTTnet.Server.Scripting scriptScope.SetVariable("mqtt_net_server", _proxyObjects); compiledCode.Execute(scriptScope); - + return new PythonScriptInstance(uid, path, scriptScope); } - private void AddSearchPaths(ScriptEngine scriptEngine) + void AddSearchPaths(ScriptEngine scriptEngine) { if (_scriptingSettings.IncludePaths?.Any() != true) { diff --git a/Source/MQTTnet.Server/Scripting/PythonScriptInstance.cs b/Source/MQTTnet.Server/Scripting/PythonScriptInstance.cs index 5d28f1e..e64a168 100644 --- a/Source/MQTTnet.Server/Scripting/PythonScriptInstance.cs +++ b/Source/MQTTnet.Server/Scripting/PythonScriptInstance.cs @@ -1,12 +1,12 @@ -using System; -using IronPython.Runtime; +using IronPython.Runtime; using Microsoft.Scripting.Hosting; +using System; namespace MQTTnet.Server.Scripting { public class PythonScriptInstance { - private readonly ScriptScope _scriptScope; + readonly ScriptScope _scriptScope; public PythonScriptInstance(string uid, string path, ScriptScope scriptScope) { diff --git a/Source/MQTTnet.Server/Web/Startup.cs b/Source/MQTTnet.Server/Web/Startup.cs index e6952de..36d872a 100644 --- a/Source/MQTTnet.Server/Web/Startup.cs +++ b/Source/MQTTnet.Server/Web/Startup.cs @@ -152,7 +152,7 @@ namespace MQTTnet.Server.Web .AddCookie(); } - private void ReadMqttSettings(IServiceCollection services) + void ReadMqttSettings(IServiceCollection services) { var mqttSettings = new MqttSettingsModel(); Configuration.Bind("MQTT", mqttSettings); @@ -163,7 +163,7 @@ namespace MQTTnet.Server.Web services.AddSingleton(scriptingSettings); } - private static void ConfigureWebSocketEndpoint( + static void ConfigureWebSocketEndpoint( IApplicationBuilder application, MqttServerService mqttServerService, MqttSettingsModel mqttSettings) diff --git a/Source/MQTTnet.Server/appsettings.json b/Source/MQTTnet.Server/appsettings.json index 71eaf20..fd9019c 100644 --- a/Source/MQTTnet.Server/appsettings.json +++ b/Source/MQTTnet.Server/appsettings.json @@ -52,11 +52,7 @@ }, "Scripting": { "ScriptsPath": "Scripts", - "IncludePaths": [ - "Lib", - "/usr/lib/python2.7", - "C:\\Python27\\Lib" - ] + "IncludePaths": [] }, "Logging": { "LogLevel": {