|
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
|
|
|
"""Runs a script which displays a native open file dialog.""" |
|
86
|
|
|
return call_file_and_callback("open_file_dialog.ps1", callback) |
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
def browse_folder(callback): |
|
90
|
|
|
"""Runs a script which displays a native open folder dialog.""" |
|
91
|
|
|
return call_file_and_callback("open_folder_dialog.ps1", callback) |
|
92
|
|
|
|
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.