Skip to content

Commit e125d59

Browse files
committed
reorder logic for setting python path
1 parent c9b232a commit e125d59

File tree

1 file changed

+54
-48
lines changed

1 file changed

+54
-48
lines changed

Flow.Launcher.Core/Plugin/PluginsLoader.cs

Lines changed: 54 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -115,63 +115,65 @@ public static IEnumerable<PluginPair> PythonPlugins(List<PluginMetadata> source,
115115
if (!source.Any(o => o.Language.ToUpper() == AllowedLanguage.Python))
116116
return new List<PluginPair>();
117117

118-
if (string.IsNullOrEmpty(settings.PythonDirectory))
118+
if (!string.IsNullOrEmpty(settings.PythonDirectory))
119+
return SetPythonPathForPluginPairs(source, Path.Combine(settings.PythonDirectory, PythonExecutable));
120+
121+
var pythonPath = string.Empty;
122+
123+
if (MessageBox.Show("Flow detected you have installed Python plugins, " +
124+
"would you like to install Python to run them? " +
125+
Environment.NewLine + Environment.NewLine +
126+
"Click no if it's already installed, " +
127+
"and you will be prompted to select the folder that contains the Python executable",
128+
string.Empty, MessageBoxButtons.YesNo) == DialogResult.No
129+
&& string.IsNullOrEmpty(settings.PythonDirectory))
119130
{
120-
if (MessageBox.Show("Flow detected you have installed Python plugins, " +
121-
"would you like to install Python to run them? " +
122-
Environment.NewLine + Environment.NewLine +
123-
"Click no if it's already installed, " +
124-
"and you will be prompted to select the folder that contains the Python executable",
125-
string.Empty, MessageBoxButtons.YesNo) == DialogResult.No
126-
&& string.IsNullOrEmpty(settings.PythonDirectory))
131+
var dlg = new FolderBrowserDialog
127132
{
128-
var dlg = new FolderBrowserDialog
129-
{
130-
SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
131-
};
133+
SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
134+
};
132135

133-
var result = dlg.ShowDialog();
134-
if (result == DialogResult.OK)
136+
var result = dlg.ShowDialog();
137+
if (result == DialogResult.OK)
138+
{
139+
string pythonDirectory = dlg.SelectedPath;
140+
if (!string.IsNullOrEmpty(pythonDirectory))
135141
{
136-
string pythonDirectory = dlg.SelectedPath;
137-
if (!string.IsNullOrEmpty(pythonDirectory))
142+
pythonPath = Path.Combine(pythonDirectory, PythonExecutable);
143+
if (File.Exists(pythonPath))
138144
{
139-
var pythonPath = Path.Combine(pythonDirectory, PythonExecutable);
140-
if (File.Exists(pythonPath))
141-
{
142-
settings.PythonDirectory = pythonDirectory;
143-
Constant.PythonPath = pythonPath;
144-
}
145-
else
146-
{
147-
MessageBox.Show("Can't find python in given directory");
148-
}
145+
settings.PythonDirectory = pythonDirectory;
146+
Constant.PythonPath = pythonPath;
147+
}
148+
else
149+
{
150+
MessageBox.Show("Can't find python in given directory");
149151
}
150152
}
151153
}
152-
else
153-
{
154-
var installedPythonDirectory = Path.Combine(DataLocation.DataDirectory(), "Python Embeddable");
154+
}
155+
else
156+
{
157+
var installedPythonDirectory = Path.Combine(DataLocation.DataDirectory(), "Python Embeddable");
155158

156-
// Python 3.8.9 is used for Windows 7 compatibility
157-
DroplexPackage.Drop(App.python_3_8_9_embeddable, installedPythonDirectory).Wait();
159+
// Python 3.8.9 is used for Windows 7 compatibility
160+
DroplexPackage.Drop(App.python_3_8_9_embeddable, installedPythonDirectory).Wait();
158161

159-
var pythonPath = Path.Combine(installedPythonDirectory, PythonExecutable);
160-
if (FilesFolders.FileExists(pythonPath))
161-
{
162-
settings.PythonDirectory = installedPythonDirectory;
163-
Constant.PythonPath = pythonPath;
164-
}
165-
else
166-
{
167-
Log.Error("PluginsLoader",
168-
$"Failed to set Python path after Droplex install, {pythonPath} does not exist",
169-
"PythonPlugins");
170-
}
162+
pythonPath = Path.Combine(installedPythonDirectory, PythonExecutable);
163+
if (FilesFolders.FileExists(pythonPath))
164+
{
165+
settings.PythonDirectory = installedPythonDirectory;
166+
Constant.PythonPath = pythonPath;
167+
}
168+
else
169+
{
170+
Log.Error("PluginsLoader",
171+
$"Failed to set Python path after Droplex install, {pythonPath} does not exist",
172+
"PythonPlugins");
171173
}
172174
}
173175

174-
if (string.IsNullOrEmpty(settings.PythonDirectory))
176+
if (string.IsNullOrEmpty(settings.PythonDirectory) || string.IsNullOrEmpty(pythonPath))
175177
{
176178
MessageBox.Show(
177179
"Unable to set Python executable path, please try from Flow's settings (scroll down to the bottom).");
@@ -182,16 +184,20 @@ public static IEnumerable<PluginPair> PythonPlugins(List<PluginMetadata> source,
182184
return new List<PluginPair>();
183185
}
184186

185-
return source
187+
return SetPythonPathForPluginPairs(source, pythonPath);
188+
}
189+
190+
private static IEnumerable<PluginPair> SetPythonPathForPluginPairs(List<PluginMetadata> source, string pythonPath)
191+
=> source
186192
.Where(o => o.Language.ToUpper() == AllowedLanguage.Python)
187193
.Select(metadata => new PluginPair
188194
{
189-
Plugin = new PythonPlugin(Constant.PythonPath), Metadata = metadata
195+
Plugin = new PythonPlugin(pythonPath),
196+
Metadata = metadata
190197
})
191198
.ToList();
192-
}
193199

194-
public static IEnumerable<PluginPair> ExecutablePlugins(IEnumerable<PluginMetadata> source)
200+
public static IEnumerable<PluginPair> ExecutablePlugins(IEnumerable<PluginMetadata> source)
195201
{
196202
return source
197203
.Where(o => o.Language.ToUpper() == AllowedLanguage.Executable)

0 commit comments

Comments
 (0)