-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_path.py
69 lines (47 loc) · 1.59 KB
/
convert_path.py
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
#!/usr/bin/python3
import os
import argparse
import subprocess
import pyperclip
CYGPATH = r"C:\cygwin64\bin\cygpath.exe"
def normalize(path):
path = os.path.normpath(path)
if path.startswith("\\") and not path.startswith("\\\\"):
path = "\\" + path
return path
def escape_backslashes(path):
path = normalize(path)
return path.replace("\\", "\\\\")
def backward_to_forward_slashes(path):
path = path = normalize(path)
return path.replace("\\", "/")
def run_cygpath(path, arg):
if not os.path.isfile(CYGPATH):
raise Exception(f"Cygpath executable '{CYGPATH}' was not found!")
proc = subprocess.run([CYGPATH, path, arg], capture_output=True, text=True)
proc.check_returncode()
return proc.stdout.strip()
def convert_to_cygpath(path):
return run_cygpath(path, "-u")
def convert_from_cygpath(path):
return run_cygpath(path, "-w")
available_features = {
"normalize": normalize,
"double": escape_backslashes,
"forward": backward_to_forward_slashes,
"to_cygpath": convert_to_cygpath,
"from_cygpath": convert_from_cygpath
}
def execute_action(action):
path = pyperclip.paste()
print("Input:", path)
runner = available_features[action]
print("Action:", action)
processed_path = runner(path)
print("Output:", processed_path)
pyperclip.copy(processed_path)
if __name__ == '__main__':
arg_parser = argparse.ArgumentParser(description="")
arg_parser.add_argument("action", type=str, choices=available_features.keys(), help="")
args = arg_parser.parse_args()
execute_action(args.action)