Skip to content

Commit a514d10

Browse files
committed
version 0.0.3
1 parent 0036521 commit a514d10

File tree

11 files changed

+359
-394
lines changed

11 files changed

+359
-394
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,10 @@
66
## 0.0.2
77
- Fixing bugs that occur in windows.
88
- Add logs.
9+
10+
<br />
11+
12+
## 0.0.3
13+
- change directionality of library.
14+
- add 'ShellManager', 'ShellInstance'.
15+
- update 'README.md'

README.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,28 @@ nuget package is not available not
3131
# Usage
3232
- basic usage
3333
```c#
34-
using PythonShell;
3534
using PythonShell.Listener;
35+
using PythonShell.Shell;
3636

37-
var shell = PythonShell();
37+
var shell = new PythonShell.PythonShell(
38+
new PythonShell.PythonShellConfig()
39+
);
3840
await shell.Initialize();
3941

4042
shell.RunString("{pythonCode}");
4143
```
44+
- use instance
45+
```c#
46+
using PythonShell.Listener;
47+
using PythonShell.Shell;
48+
49+
new PythonShell.PythonShell(
50+
new PythonShell.PythonShellConfig()
51+
).Initialize();
52+
53+
var instance = ShellManager.GetInstance("default");
54+
instance.RunString("{pythonCode}");
55+
```
4256

4357
<br />
4458

@@ -64,4 +78,4 @@ shell.RunString(
6478

6579
<br />
6680

67-
for further informations, refers to [Shell.cs](https://github.com/eseunghwan/PythonShell.NET/blob/master/Source/src/Shell.cs)
81+
for further informations, refers to [PythonShell.cs](https://github.com/eseunghwan/PythonShell.NET/blob/master/Source/src/PythonShell.cs)

Source/PythonShell.NET.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
<PropertyGroup>
1414
<PackageId>PythonShell.NET</PackageId>
15-
<Version>0.0.2</Version>
15+
<Version>0.0.3</Version>
1616
<Authors>eseunghwan</Authors>
1717
<Company>Integrated.Design.Division</Company>
1818
</PropertyGroup>

Source/src/Config.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
using System;
3+
using System.IO;
4+
using System.Linq;
5+
6+
7+
namespace PythonShell {
8+
internal static class Config {
9+
public static String AppDir = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".python_shell.net");
10+
public static String TempDir {
11+
get => Path.Join(Config.AppDir, "temp");
12+
}
13+
public static String InstanceDir {
14+
get => Path.Join(Config.AppDir, "instances");
15+
}
16+
17+
public static String DefaultPythonVersion = "3.9.13";
18+
public static String DefaultPythonPath = "python3";
19+
public static String PythonRuntimeBit {
20+
get => System.Environment.Is64BitProcess ? "amd64" : "win32";
21+
}
22+
23+
public static String CheckPythonVersion(String rawPythonVersion) {
24+
String realPythonVersion = "3.9.13";
25+
26+
var versions = rawPythonVersion.Split(".");
27+
if (versions.Length == 3) {
28+
if (rawPythonVersion.EndsWith(".")) {
29+
if (versions.Last() != "") {
30+
realPythonVersion = String.Join(".", versions.SkipLast(1));
31+
}
32+
}
33+
else {
34+
realPythonVersion = rawPythonVersion;
35+
}
36+
}
37+
else if (versions.Length == 2) {
38+
if (rawPythonVersion.EndsWith(".")) {
39+
if (versions.Last() != "") {
40+
realPythonVersion = String.Join(".", versions) + ".0";
41+
}
42+
}
43+
else {
44+
realPythonVersion = $"{rawPythonVersion}.0";
45+
}
46+
}
47+
48+
return realPythonVersion;
49+
}
50+
}
51+
}

Source/src/PythonShell.cs

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
2+
using System;
3+
using System.IO;
4+
using System.Threading.Tasks;
5+
using System.Linq;
6+
using System.IO.Compression;
7+
using System.Diagnostics;
8+
using System.Collections.Generic;
9+
using System.Runtime.InteropServices;
10+
11+
12+
namespace PythonShell {
13+
public class PythonShellConfig {
14+
public String DefaultPythonPath { get; set; } = "python3";
15+
public String DefaultPythonVersion { get; set; } = "3.9.13";
16+
// public String? DefaultWorkingDirectory { get; set; } = null;
17+
// public String? PythonRequireFile { get; set; } = null;
18+
// public String[]? PythonRequires { get; set; } = null;
19+
20+
// internal String? AppDir { get; set; } = null;
21+
// internal String TempDir {
22+
// get => Path.Join(AppDir, "temp");
23+
// }
24+
// internal String InstanceDir {
25+
// get => Path.Join(AppDir, "instances");
26+
// }
27+
// internal String? DefaultPythonEnvPath { get; set; } = null;
28+
29+
public PythonShellConfig() {
30+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) {
31+
if (this.DefaultPythonPath == "python" || this.DefaultPythonPath == "python2" || this.DefaultPythonPath == "python3") {
32+
this.DefaultPythonPath = $"/usr/bin/{this.DefaultPythonPath}";
33+
}
34+
else if (!File.Exists(this.DefaultPythonPath)) {
35+
this.DefaultPythonPath = "/usr/bin/python3";
36+
}
37+
}
38+
39+
// if (this.DefaultWorkingDirectory != null) {
40+
// this.DefaultWorkingDirectory = Path.GetFullPath(this.DefaultWorkingDirectory);
41+
// }
42+
}
43+
}
44+
45+
public class PythonShell {
46+
public PythonShell(PythonShellConfig shellConfig) {}
47+
48+
public void Clear() {
49+
Shell.ShellManager.Clear();
50+
foreach (var path in Directory.GetDirectories(Config.TempDir)) {
51+
Directory.Delete(path, true);
52+
}
53+
foreach (var path in Directory.GetFiles(Config.TempDir)) {
54+
File.Delete(path);
55+
}
56+
}
57+
58+
public async Task Initialize() {
59+
Console.WriteLine("Initializing shell...");
60+
Config.DefaultPythonVersion = Config.CheckPythonVersion(Config.DefaultPythonVersion);
61+
62+
if (!Directory.Exists(Config.AppDir)) Directory.CreateDirectory(Config.AppDir);
63+
if (!Directory.Exists(Config.InstanceDir)) Directory.CreateDirectory(Config.InstanceDir);
64+
if (!Directory.Exists(Config.TempDir)) Directory.CreateDirectory(Config.TempDir);
65+
66+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
67+
Console.WriteLine("Check default python binary files...");
68+
String pythonDir = Path.Join(Config.AppDir, "python");
69+
if (!Directory.Exists(pythonDir)) {
70+
String pythonZipFile = Path.Join(Config.TempDir, "python.zip");
71+
await Util.DownloadFile($"https://www.python.org/ftp/python/{Config.DefaultPythonVersion}/python-{Config.DefaultPythonVersion}-embed-{Config.PythonRuntimeBit}.zip", pythonZipFile, "binary");
72+
ZipFile.ExtractToDirectory(pythonZipFile, pythonDir);
73+
File.Delete(pythonZipFile);
74+
75+
var pthFile = Path.Join(pythonDir, $"python{Config.DefaultPythonVersion.Replace($".{Config.DefaultPythonVersion.Split(".").Last()}", "").Replace(".", "")}._pth");
76+
File.WriteAllText(
77+
pthFile,
78+
File.ReadAllText(pthFile).Replace("#import site", "import site")
79+
);
80+
}
81+
Console.WriteLine("Python check finished.");
82+
83+
Config.DefaultPythonPath = Path.Join(pythonDir, "python.exe");
84+
}
85+
86+
if (File.Exists(Config.DefaultPythonPath)) {
87+
Console.WriteLine("Default settings for virtualenv...");
88+
var p = Util.RunSettingCmd(Config.DefaultPythonPath, new String[] { "-m", "pip", "install", "pip", "--upgrade" });
89+
if (p.ExitCode != 0) {
90+
String pipInstallFile = Path.Join(Config.TempDir, "get-pip.py");
91+
await Util.DownloadFile("https://bootstrap.pypa.io/pip/get-pip.py", pipInstallFile, "text");
92+
Util.RunSettingCmd(Config.DefaultPythonPath, new String[] { pipInstallFile });
93+
File.Delete(pipInstallFile);
94+
}
95+
96+
Util.RunSettingCmd(Config.DefaultPythonPath, new String[] { "-m", "pip", "install", "virtualenv", "--upgrade" });
97+
Console.WriteLine("Virtualenv settings finished.");
98+
}
99+
100+
String defaultEnvdir = Path.Join(Config.InstanceDir, "default");
101+
if (!Directory.Exists(defaultEnvdir)) {
102+
Console.WriteLine("Creating default env...");
103+
Shell.ShellManager.CreateInstance("default");
104+
Console.WriteLine("Default env created.");
105+
}
106+
107+
Console.WriteLine("Shell initialized.");
108+
}
109+
110+
public async Task RunFile(String pythonFile, Shell.ShellInstance? instance = null, String? workingDirectory = null, Listener.ShellListener? listener = null, Boolean echo = true) {
111+
instance = instance ?? Shell.ShellManager.GetInstance("default");
112+
await instance.RunFile(pythonFile, workingDirectory, listener, echo);
113+
}
114+
115+
public async Task RunString(String pythonCode, Shell.ShellInstance? instance = null, String? workingDirectory = null, Listener.ShellListener? listener = null, Boolean echo = true) {
116+
instance = instance ?? Shell.ShellManager.GetInstance("default");
117+
await instance.RunString(pythonCode, workingDirectory, listener, echo);
118+
}
119+
}
120+
}

Source/src/Shell.cs

Lines changed: 0 additions & 151 deletions
This file was deleted.

0 commit comments

Comments
 (0)