1
|
|
|
"""This module is handling native calls to user file and folder selection.""" |
2
|
|
|
|
3
|
|
|
import os.path |
4
|
|
|
import subprocess |
5
|
|
|
import threading |
6
|
|
|
|
7
|
|
|
import sublime |
8
|
|
|
|
9
|
|
|
from .. import logger |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
def popen_and_call(callback, *popen_args, **popen_kwargs): |
13
|
|
|
""" |
14
|
|
|
Runs a subprocess.Popen, and then calls the function onExit when the |
15
|
|
|
subprocess completes. |
16
|
|
|
|
17
|
|
|
Use it exactly the way you'd normally use subprocess.Popen, except include a |
18
|
|
|
callable to execute as the first argument. onExit is a callable object, and |
19
|
|
|
*popenArgs and **popenKWArgs are simply passed up to subprocess.Popen. |
20
|
|
|
""" |
21
|
|
|
def run_in_thread(callback, popen_args, popen_kwargs): |
22
|
|
|
""" |
23
|
|
|
This is a wrapped method call with a direct callback upon finishing the process. |
24
|
|
|
|
25
|
|
|
You use this because the user input will halt the processing if not started in a new thread. |
26
|
|
|
""" |
27
|
|
|
proc = subprocess.Popen(*popen_args, **popen_kwargs) |
|
|
|
|
28
|
|
|
output_channel, error_channel = proc.communicate() |
29
|
|
|
message = output_channel.decode("utf-8") |
30
|
|
|
message = message.replace("\r\n", "") |
31
|
|
|
|
32
|
|
|
proc.wait() |
33
|
|
|
|
34
|
|
|
# checking for errors first |
35
|
|
|
error = error_channel.decode("utf-8", errors="replace") |
36
|
|
|
if error is not None and len(error) != 0: |
37
|
|
|
error = error.replace("\r\n", "\n") |
38
|
|
|
logger.error("Error in thread for '" + str(callback) + "':" + error) |
39
|
|
|
return |
40
|
|
|
|
41
|
|
|
callback(message) |
42
|
|
|
|
43
|
|
|
return |
44
|
|
|
|
45
|
|
|
thread = threading.Thread(target=run_in_thread, |
46
|
|
|
args=(callback, popen_args, popen_kwargs)) |
47
|
|
|
thread.start() |
48
|
|
|
|
49
|
|
|
# returns immediately after the thread starts |
50
|
|
|
return thread |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
def call_file_and_callback(file_basename, callback): |
54
|
|
|
""".""" |
55
|
|
|
packages = sublime.packages_path() |
56
|
|
|
prompt_dir = os.path.join(packages, "User", "Rainmeter", "path") |
57
|
|
|
script_path = os.path.join(prompt_dir, file_basename) |
58
|
|
|
|
59
|
|
|
popen_args = [ |
60
|
|
|
'powershell.exe', |
61
|
|
|
'-NoProfile', |
62
|
|
|
'-NonInteractive', |
63
|
|
|
'-NoLogo', |
64
|
|
|
'-ExecutionPolicy', 'RemoteSigned', |
65
|
|
|
'-windowstyle', 'hidden', |
66
|
|
|
'-File', script_path |
67
|
|
|
] |
68
|
|
|
|
69
|
|
|
st_inf = subprocess.STARTUPINFO() |
70
|
|
|
st_inf.dwFlags = st_inf.dwFlags | subprocess.STARTF_USESHOWWINDOW |
71
|
|
|
|
72
|
|
|
thread = popen_and_call( |
73
|
|
|
callback, |
74
|
|
|
popen_args, |
75
|
|
|
stdout=subprocess.PIPE, |
76
|
|
|
stderr=subprocess.PIPE, |
77
|
|
|
shell=False, |
78
|
|
|
startupinfo=st_inf |
79
|
|
|
) |
80
|
|
|
|
81
|
|
|
return thread |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
def browse_file(callback): |
85
|
|
|
""".""" |
86
|
|
|
return call_file_and_callback("open_file_dialog.ps1", callback) |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
def browse_folder(callback): |
90
|
|
|
""".""" |
91
|
|
|
return call_file_and_callback("open_folder_dialog.ps1", callback) |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
def prompt_open_file_dialog(): |
|
|
|
|
95
|
|
|
|
96
|
|
|
packages = sublime.packages_path() |
97
|
|
|
prompt_dir = os.path.join(packages, "User", "Rainmeter", "path") |
98
|
|
|
|
99
|
|
|
open_file_dialog_bat = os.path.join(prompt_dir, "open_file_dialog.ps1") |
100
|
|
|
|
101
|
|
|
st_inf = subprocess.STARTUPINFO() |
102
|
|
|
st_inf.dwFlags = st_inf.dwFlags | subprocess.STARTF_USESHOWWINDOW |
103
|
|
|
|
104
|
|
|
dialog = subprocess.Popen( |
105
|
|
|
[ |
106
|
|
|
'powershell.exe', |
107
|
|
|
'-NoProfile', |
108
|
|
|
'-NonInteractive', |
109
|
|
|
'-NoLogo', |
110
|
|
|
'-ExecutionPolicy', 'RemoteSigned', |
111
|
|
|
'-windowstyle', 'hidden', |
112
|
|
|
'-File', open_file_dialog_bat |
113
|
|
|
], |
114
|
|
|
stdout=subprocess.PIPE, |
115
|
|
|
stderr=subprocess.PIPE, |
116
|
|
|
shell=False, |
117
|
|
|
startupinfo=st_inf |
118
|
|
|
) |
119
|
|
|
output_channel, error_channel = dialog.communicate() |
120
|
|
|
raw_output = output_channel.decode("utf-8") |
121
|
|
|
|
122
|
|
|
# need to wait on the powershell script to execute and user interaction |
123
|
|
|
dialog.wait() |
124
|
|
|
# TODO need to rework it with http://stackoverflow.com/a/5209746/2787159 |
|
|
|
|
125
|
|
|
# concurrent callback when finished |
126
|
|
|
|
127
|
|
|
# checking for errors first |
128
|
|
|
error = error_channel.decode("utf-8") |
129
|
|
|
if error is not None and len(error) != 0: |
130
|
|
|
logger.error("Color Picker Error:\n" + error) |
131
|
|
|
return |
132
|
|
|
|
133
|
|
|
return raw_output |
134
|
|
|
|
Generally, there is nothing wrong with usage of
*
or**
arguments. For readability of the code base, we suggest to not over-use these language constructs though.For more information, we can recommend this blog post from Ned Batchelder including its comments which also touches this aspect.