|
1
|
|
|
|
|
|
|
|
|
|
2
|
|
|
import os.path |
|
3
|
|
|
import subprocess |
|
4
|
|
|
import threading |
|
5
|
|
|
|
|
6
|
|
|
import sublime |
|
7
|
|
|
|
|
8
|
|
|
from .. import logger |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
def sink(message): |
|
|
|
|
|
|
12
|
|
|
logger.info(message) |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
def popen_and_call(callback, *popen_args, **popen_kwargs): |
|
16
|
|
|
""" |
|
17
|
|
|
Runs a subprocess.Popen, and then calls the function onExit when the |
|
18
|
|
|
subprocess completes. |
|
19
|
|
|
|
|
20
|
|
|
Use it exactly the way you'd normally use subprocess.Popen, except include a |
|
21
|
|
|
callable to execute as the first argument. onExit is a callable object, and |
|
22
|
|
|
*popenArgs and **popenKWArgs are simply passed up to subprocess.Popen. |
|
23
|
|
|
""" |
|
24
|
|
|
def run_in_thread(callback, popen_args, popen_kwargs): |
|
|
|
|
|
|
25
|
|
|
proc = subprocess.Popen(*popen_args, **popen_kwargs) |
|
|
|
|
|
|
26
|
|
|
output_channel, error_channel = proc.communicate() |
|
27
|
|
|
message = output_channel.decode("utf-8") |
|
28
|
|
|
message = message.replace("\r\n", "") |
|
29
|
|
|
|
|
30
|
|
|
proc.wait() |
|
31
|
|
|
|
|
32
|
|
|
# checking for errors first |
|
33
|
|
|
error = error_channel.decode("utf-8", errors="replace") |
|
34
|
|
|
if error is not None and len(error) != 0: |
|
35
|
|
|
error = error.replace("\r\n", "\n") |
|
36
|
|
|
logger.error("Error in thread for '" + str(callback) + "':" + error) |
|
37
|
|
|
return |
|
38
|
|
|
|
|
39
|
|
|
callback(message) |
|
40
|
|
|
|
|
41
|
|
|
return |
|
42
|
|
|
|
|
43
|
|
|
thread = threading.Thread(target=run_in_thread, |
|
44
|
|
|
args=(callback, popen_args, popen_kwargs)) |
|
45
|
|
|
thread.start() |
|
46
|
|
|
|
|
47
|
|
|
# returns immediately after the thread starts |
|
48
|
|
|
return thread |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
def call_file_and_callback(file, callback): |
|
52
|
|
|
""".""" |
|
53
|
|
|
packages = sublime.packages_path() |
|
54
|
|
|
prompt_dir = os.path.join(packages, "User", "Rainmeter", "path") |
|
55
|
|
|
script_path = os.path.join(prompt_dir, file) |
|
56
|
|
|
|
|
57
|
|
|
popen_args = [ |
|
58
|
|
|
'powershell.exe', |
|
59
|
|
|
'-NoProfile', |
|
60
|
|
|
'-NonInteractive', |
|
61
|
|
|
'-NoLogo', |
|
62
|
|
|
'-ExecutionPolicy', 'RemoteSigned', |
|
63
|
|
|
'-windowstyle', 'hidden', |
|
64
|
|
|
'-File', script_path |
|
65
|
|
|
] |
|
66
|
|
|
|
|
67
|
|
|
st_inf = subprocess.STARTUPINFO() |
|
68
|
|
|
st_inf.dwFlags = st_inf.dwFlags | subprocess.STARTF_USESHOWWINDOW |
|
69
|
|
|
|
|
70
|
|
|
thread = popen_and_call( |
|
71
|
|
|
callback, |
|
72
|
|
|
popen_args, |
|
73
|
|
|
stdout=subprocess.PIPE, |
|
74
|
|
|
stderr=subprocess.PIPE, |
|
75
|
|
|
shell=False, |
|
76
|
|
|
startupinfo=st_inf |
|
77
|
|
|
) |
|
78
|
|
|
|
|
79
|
|
|
return thread |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
def browse_file(callback): |
|
83
|
|
|
""".""" |
|
84
|
|
|
return call_file_and_callback("open_file_dialog.ps1", callback) |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
def browse_folder(callback): |
|
88
|
|
|
""".""" |
|
89
|
|
|
return call_file_and_callback("open_folder_dialog.ps1", callback) |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
def prompt_open_file_dialog(): |
|
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
packages = sublime.packages_path() |
|
95
|
|
|
prompt_dir = os.path.join(packages, "User", "Rainmeter", "path") |
|
96
|
|
|
|
|
97
|
|
|
open_file_dialog_bat = os.path.join(prompt_dir, "open_file_dialog.ps1") |
|
98
|
|
|
|
|
99
|
|
|
st_inf = subprocess.STARTUPINFO() |
|
100
|
|
|
st_inf.dwFlags = st_inf.dwFlags | subprocess.STARTF_USESHOWWINDOW |
|
101
|
|
|
|
|
102
|
|
|
dialog = subprocess.Popen( |
|
103
|
|
|
[ |
|
104
|
|
|
'powershell.exe', |
|
105
|
|
|
'-NoProfile', |
|
106
|
|
|
'-NonInteractive', |
|
107
|
|
|
'-NoLogo', |
|
108
|
|
|
'-ExecutionPolicy', 'RemoteSigned', |
|
109
|
|
|
'-windowstyle', 'hidden', |
|
110
|
|
|
'-File', open_file_dialog_bat |
|
111
|
|
|
], |
|
112
|
|
|
stdout=subprocess.PIPE, |
|
113
|
|
|
stderr=subprocess.PIPE, |
|
114
|
|
|
shell=False, |
|
115
|
|
|
startupinfo=st_inf |
|
116
|
|
|
) |
|
117
|
|
|
output_channel, error_channel = dialog.communicate() |
|
118
|
|
|
raw_output = output_channel.decode("utf-8") |
|
119
|
|
|
|
|
120
|
|
|
# need to wait on the powershell script to execute and user interaction |
|
121
|
|
|
dialog.wait() |
|
122
|
|
|
# TODO need to rework it with http://stackoverflow.com/a/5209746/2787159 |
|
|
|
|
|
|
123
|
|
|
# concurrent callback when finished |
|
124
|
|
|
|
|
125
|
|
|
# checking for errors first |
|
126
|
|
|
error = error_channel.decode("utf-8") |
|
127
|
|
|
if error is not None and len(error) != 0: |
|
128
|
|
|
logger.error("Color Picker Error:\n" + error) |
|
129
|
|
|
return |
|
130
|
|
|
|
|
131
|
|
|
return raw_output |
|
132
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.