forked from ahoess-zera/win_lin_path_conv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwin_lin_path_conv.py
executable file
·68 lines (57 loc) · 2.63 KB
/
win_lin_path_conv.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
#!/bin/env python
import time
import subprocess
import tkinter
import argparse
import textwrap
from itertools import chain
class WinLinPathConverter:
@staticmethod
def _convert_win_to_lin_path(win_path: str) -> str:
lin_path = win_path.replace("\\", "/")
if lin_path.startswith("//"):
lin_path = "smb:" + lin_path
elif lin_path.upper().startswith("K:/"):
lin_path = "smb://s-zera-stor01/data" + lin_path[2:]
elif lin_path.upper().startswith("M:/"):
lin_path = "smb://s-zera-stor01/data1" + lin_path[2:]
return lin_path
@staticmethod
def _get_clipboard_string() -> str:
tk = tkinter.Tk()
tk.withdraw()
return tk.selection_get()
@staticmethod
def _parse() -> argparse.Namespace:
class _RawDescriptionHelpFormatterWithNewlines(argparse.RawDescriptionHelpFormatter):
def _split_lines(self, text, width):
return list(chain.from_iterable([textwrap.wrap(t, width) for t in text.splitlines()]))
default_help_message = "Show this help message and exit."
parser = argparse.ArgumentParser(add_help=False, description="Windows to Linux path converter.", formatter_class=_RawDescriptionHelpFormatterWithNewlines)
parser.add_argument("-h", "--help", action="help", help=default_help_message)
parser.add_argument("-c", "--clipboard", type=int, required=False, default=True,
help="Take windows UNC path from clipboard. Otherwise it needs to get entered manually. 0=No, 1=Yes. Default: %(default)s.")
parser.add_argument("-p", "--pause", type=int, required=False, default=True,
help="Pause before leaving the program to see the its output. 0=No, 1=Yes. Default: %(default)s.")
return parser.parse_args()
@staticmethod
def run() -> None:
args = WinLinPathConverter._parse()
print("This tool converts Windows-UNC-paths to Linux-SMB-paths")
print("")
if args.clipboard:
windows_path = WinLinPathConverter._get_clipboard_string()
else:
windows_path = input("Please enter a Windows-UNC-path: ")
linux_path = WinLinPathConverter._convert_win_to_lin_path(windows_path).strip()
print(f"Trying to open entered path \"{windows_path}\"...")
print(f"-> Resulting Linux path: \"{linux_path}\"")
subprocess.run(["xdg-open", linux_path])
if args.pause:
input("Press Enter to leave the program:")
else:
time.sleep(1)
def main() -> None:
WinLinPathConverter.run()
if __name__ == "__main__":
main()