Skip to content

Commit 3f294ff

Browse files
committed
version 0.0.7
1 parent 8eaa918 commit 3f294ff

File tree

14 files changed

+370
-510
lines changed

14 files changed

+370
-510
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,10 @@
3838
- change python_shell app directory to '.python_shell.dart'
3939
- move 'test.py' file into 'test' directory and fix test.dart file
4040
- add logs.
41+
42+
<br />
43+
44+
## 0.0.7
45+
- change directionality of library.
46+
- add 'ShellManager', 'ShellInstance'.
47+
- update 'README.md'

README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,18 @@ dependencies:
4747
import "package:python_shell/python_shell.dart";
4848

4949
var shell = PythonShell();
50-
await shell.initializeShell();
50+
await shell.initialize();
5151

52-
shell.runString("{pythonCode}");
52+
await shell.runString("{pythonCode}");
5353
```
54+
- use instance
55+
```dart
56+
import "package:python_shell/python_shell.dart";
5457
58+
PythonShell().initialize();
59+
var instance = ShellManager.getInstance("default");
60+
await instance.runString("{pythonCode}");
61+
```
5562
<br />
5663

5764
- onMessage, onError, onComplete
@@ -60,14 +67,14 @@ shell.runString("{pythonCode}");
6067
shell.runString(
6168
"{pythonCode}",
6269
listener: ShellListener(
63-
messageCallback: (message) {
70+
onMessage: (message) {
6471
// if `echo` is `true`, log to console automatically
6572
print("message!");
6673
},
67-
errorCallback: (e, s) {
74+
onError: (e, s) {
6875
print("error!");
6976
},
70-
completeCallback: () {
77+
onComplete: () {
7178
print("complete!");
7279
}
7380
)

example/python_shell_example.dart

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
11
import "package:python_shell/python_shell.dart";
22

33
void main() async {
4-
var shell = PythonShell(shellConfig: PythonShellConfig(
5-
pythonRequires: [ "PySide6" ],
6-
defaultWorkingDirectory: "example"
7-
));
4+
// 현재 스타일
5+
// var shell = PythonShell(shellConfig: PythonShellConfig(
6+
// pythonRequires: [ "PySide6" ],
7+
// defaultWorkingDirectory: "example"
8+
// ));
9+
// await shell.initialize();
10+
11+
// await shell.runString("""
12+
// import os, PySide6
13+
14+
// print("in python: ", os.getcwd())
15+
// print("in python: ", PySide6)
16+
// """, useInstance: true, instanceName: "testInstance1", listener: ShellListener(
17+
// completeCallback: () {
18+
// print(shell.resolved);
19+
// // shell.clear();
20+
// }
21+
// ));
22+
23+
var shell = PythonShell(PythonShellConfig());
824
await shell.initialize();
925

10-
await shell.runString("""
26+
var instance = ShellManager.getInstance("default");
27+
instance.installRequires([ "PySide6" ]);
28+
await instance.runString("""
1129
import os, PySide6
1230
1331
print("in python: ", os.getcwd())
1432
print("in python: ", PySide6)
15-
""", useInstance: true, instanceName: "testInstance1", listener: ShellListener(
16-
completeCallback: () {
17-
print(shell.resolved);
18-
// shell.clear();
19-
}
20-
));
33+
""", echo: true);
34+
35+
print("finished");
2136
}

lib/python_shell.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11

22
library python_shell;
33

4-
export "src/shell.dart";
4+
export "src/python_shell.dart";
5+
export "src/listener.dart";
6+
export "src/shell/manager.dart";
7+
export "src/shell/instance.dart";

lib/src/config.dart

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
import "dart:io";
3+
import "package:path/path.dart" as path;
4+
5+
6+
String appDir = path.join(Platform.environment[Platform.isWindows ? "USERPROFILE" : "HOME"]!, ".python_shell.dart");
7+
String tempDir = path.join(appDir, "temp");
8+
String instanceDir = path.join(appDir, "instances");
9+
10+
String defaultPythonVersion = "3.9.13";
11+
String defaultPythonPath = "python3";
12+
13+
String checkPythonVersion(String rawPythonVersion) {
14+
String realPythonVersion = "3.9.13";
15+
16+
var versions = rawPythonVersion.split(".");
17+
if (versions.length == 3) {
18+
if (rawPythonVersion.endsWith(".")) {
19+
if (versions.last != "") {
20+
versions.removeLast();
21+
realPythonVersion = versions.join(".");
22+
}
23+
}
24+
else {
25+
realPythonVersion = rawPythonVersion;
26+
}
27+
}
28+
else if (versions.length == 2) {
29+
if (rawPythonVersion.endsWith(".")) {
30+
if (versions.last != "") {
31+
realPythonVersion = "${versions.join(".")}.0";
32+
}
33+
}
34+
else {
35+
realPythonVersion = "$rawPythonVersion.0";
36+
}
37+
}
38+
39+
return realPythonVersion;
40+
}

lib/src/listener.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
import "listener.callbacks.dart";
3+
4+
5+
/// PythonShell Listener Class
6+
/// parameters and properties:
7+
/// * [onMessage]: same as messageCallback
8+
/// * [onError]: same as errorCallback
9+
/// * [onComplete]: same as completeCallback
10+
class ShellListener {
11+
Function(String) onMessage;
12+
Function(Object, StackTrace) onError;
13+
Function() onComplete;
14+
15+
ShellListener({
16+
Function(String)? onMessage, Function(Object, StackTrace)? onError, Function()? onComplete
17+
}): onMessage = onMessage ?? emptyMessageCallback, onError = onError ?? emptyErrorCallback, onComplete = onComplete ?? emptyCompleteCallback;
18+
}

lib/src/python_shell.dart

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
2+
import "dart:io";
3+
4+
import "package:path/path.dart" as path;
5+
import "package:dio/dio.dart";
6+
import "package:archive/archive_io.dart";
7+
8+
import "config.dart" as config;
9+
import "listener.dart";
10+
import "shell/manager.dart";
11+
import "shell/instance.dart";
12+
13+
14+
/// PythonShell Class
15+
class PythonShell {
16+
PythonShell(PythonShellConfig shellConfig);
17+
18+
void clear() => ShellManager.clear();
19+
20+
Future<void> initialize() async {
21+
print("Initializing shell...");
22+
config.defaultPythonVersion = config.checkPythonVersion(config.defaultPythonVersion);
23+
if (!Directory(config.appDir).existsSync()) Directory(config.appDir).createSync();
24+
if (!Directory(config.instanceDir).existsSync()) Directory(config.instanceDir).createSync();
25+
if (!Directory(config.tempDir).existsSync()) Directory(config.tempDir).createSync();
26+
27+
String pythonDir = "";
28+
if (Platform.isWindows) {
29+
print("Check default python binary files...");
30+
pythonDir = path.join(config.appDir, "python");
31+
if (!Directory(pythonDir).existsSync()) {
32+
String pythonBinaryFile = path.join(config.tempDir, "python.zip");
33+
await Dio().download("https://www.python.org/ftp/python/${config.defaultPythonVersion}/python-${config.defaultPythonVersion}-embed-amd64.zip", pythonBinaryFile);
34+
await extractFileToDisk(pythonBinaryFile, pythonDir);
35+
File(pythonBinaryFile).deleteSync();
36+
37+
String pythonPthFile = path.join(pythonDir, "python${config.defaultPythonVersion.replaceAll(".${config.defaultPythonVersion.split(".").last}", "").replaceAll(".", "")}._pth");
38+
File(pythonPthFile).writeAsStringSync(File(pythonPthFile).readAsStringSync().replaceAll("#import site", "import site"));
39+
}
40+
print("Python check finished.");
41+
42+
config.defaultPythonPath = path.join(pythonDir, "python.exe");
43+
}
44+
45+
if (File(config.defaultPythonPath).existsSync()) {
46+
print("Default settings for virtualenv...");
47+
var result = Process.runSync(config.defaultPythonPath, [ "-m", "pip", "install", "pip", "--upgrade" ]);
48+
if (result.stderr.toString().trim() != "") {
49+
String pipInstallFile = path.join(config.tempDir, "get-pip.py");
50+
await Dio().download("https://bootstrap.pypa.io/pip/get-pip.py", pipInstallFile);
51+
Process.runSync(config.defaultPythonPath, [ pipInstallFile ]);
52+
File(pipInstallFile).deleteSync();
53+
}
54+
55+
Process.runSync(config.defaultPythonPath, [ "-m", "pip", "install", "virtualenv", "--upgrade" ]);
56+
print("Virtualenv settings finished.");
57+
}
58+
59+
String defaultEnvDir = path.join(config.instanceDir, "default");
60+
if (!Directory(defaultEnvDir).existsSync()) {
61+
print("Creating default env...");
62+
ShellManager.createInstance(instanceName: "default");
63+
print("Default env created.");
64+
}
65+
66+
print("Shell initialized.");
67+
}
68+
69+
Future<void> runFile(String pythonFile, { ShellInstance? instance, String? workingDirectory, ShellListener? listener, bool echo = true }) async {
70+
instance = instance ?? ShellManager.getInstance("default");
71+
await instance.runFile(pythonFile, workingDirectory: workingDirectory, listener: listener, echo: echo);
72+
}
73+
74+
Future<void> runString(String pythonCode, { ShellInstance? instance, String? workingDirectory, ShellListener? listener, bool echo = true }) async {
75+
instance = instance ?? ShellManager.getInstance("default");
76+
await instance.runString(pythonCode, workingDirectory: workingDirectory, listener: listener, echo: echo);
77+
}
78+
}
79+
80+
/// PythonShell Configuration Class
81+
/// * [defaultPythonPath]: Default python path to use
82+
/// * [defaultPythonVersion]: Default python version to use
83+
class PythonShellConfig {
84+
85+
PythonShellConfig({
86+
defaultPythonPath = "python3",
87+
defaultPythonVersion = "3.9.13",
88+
}) {
89+
if ((Platform.isLinux || Platform.isMacOS)) {
90+
if (["python", "python2", "python3"].contains(defaultPythonPath)) {
91+
config.defaultPythonPath = "/usr/bin/$defaultPythonPath";
92+
}
93+
else if (!File(defaultPythonPath).existsSync()) {
94+
config.defaultPythonPath = "/usr/bin/python3";
95+
}
96+
}
97+
}
98+
}

0 commit comments

Comments
 (0)