GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 31baed...a46e53 )
by thatsIch
01:14
created

sink()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 2
rs 10
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)
0 ignored issues
show
Coding Style introduced by
Usage of * or ** arguments should usually be done with care.

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.

Loading history...
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():
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
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
0 ignored issues
show
Coding Style introduced by
TODO and FIXME comments should generally be avoided.
Loading history...
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