-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathserious_python.dart
More file actions
103 lines (94 loc) · 3.79 KB
/
serious_python.dart
File metadata and controls
103 lines (94 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import 'dart:io';
import 'package:path/path.dart' as path;
import 'package:serious_python_platform_interface/serious_python_platform_interface.dart';
export 'package:serious_python_platform_interface/src/utils.dart';
/// Provides cross-platform functionality for running Python programs.
class SeriousPython {
SeriousPython._();
/// Returns the current name and version of the operating system.
static Future<String?> getPlatformVersion() {
return SeriousPythonPlatform.instance.getPlatformVersion();
}
/// Runs Python program from an asset.
///
/// [assetPath] is the path to an asset which is a zip archive
/// with a Python program. When the app starts the archive is unpacked
/// to a temporary directory and Serious Python plugin will try to run
/// `main.py` in the root of the archive. Current directory is changed to
/// a temporary directory.
///
/// If a Python app has a different entry point
/// it could be specified with [appFileName] parameter.
///
/// Environment variables that must be available to a Python program could
/// be passed in [environmentVariables].
///
/// By default, Serious Python expects Python dependencies installed into
/// `__pypackages__` directory in the root of app directory. Additional paths
/// to look for 3rd-party packages can be specified with [modulePaths] parameter.
///
/// Set [sync] to `true` to sychronously run Python program; otherwise the
/// program starts in a new thread.
static Future<String?> run(String assetPath,
{String? appFileName,
List<String>? modulePaths,
Map<String, String>? environmentVariables,
bool? sync}) async {
// unpack app from asset
String appPath = "";
if (path.extension(assetPath) == ".zip") {
appPath = await extractAssetZip(assetPath);
if (appFileName != null) {
appPath = path.join(appPath, appFileName);
} else if (await File(path.join(appPath, "main.pyc")).exists()) {
appPath = path.join(appPath, "main.pyc");
} else if (await File(path.join(appPath, "main.py")).exists()) {
appPath = path.join(appPath, "main.py");
} else {
throw Exception(
"App archive must contain either `main.py` or `main.pyc`; otherwise `appFileName` must be specified.");
}
} else {
appPath = await extractAsset(assetPath);
}
// set current directory to app path
Directory.current = path.dirname(appPath);
// run python program
return runProgram(appPath,
modulePaths: modulePaths,
environmentVariables: environmentVariables,
script: Platform.isWindows ? "" : null,
sync: sync);
}
/// Runs Python program from a path.
///
/// This is low-level method.
/// Make sure `Directory.current` is set before calling this method.
///
/// [appPath] is the full path to a .py or .pyc file to run.
///
/// Environment variables that must be available to a Python program could
/// be passed in [environmentVariables].
///
/// By default, Serious Python expects Python dependencies installed into
/// `__pypackages__` directory in the root of app directory. Additional paths
/// to look for 3rd-party packages can be specified with [modulePaths] parameter.
///
/// Set [sync] to `true` to sychronously run Python program; otherwise the
/// program starts in a new thread.
static Future<String?> runProgram(String appPath,
{String? script,
List<String>? modulePaths,
Map<String, String>? environmentVariables,
bool? sync}) async {
// run python program
return SeriousPythonPlatform.instance.run(appPath,
script: script,
modulePaths: modulePaths,
environmentVariables: environmentVariables,
sync: sync);
}
static void terminate() {
SeriousPythonPlatform.instance.terminate();
}
}