|
| 1 | +"""Contextual menu: 'Open in system file explorer' |
| 2 | +
|
| 3 | +This plugin adds a contextual menu on files and directories in |
| 4 | +the Project View and File View, to reveal the object in the system |
| 5 | +file explorer. |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +import os |
| 10 | +import sys |
| 11 | +import GPS |
| 12 | +from gs_utils import interactive |
| 13 | +import subprocess |
| 14 | + |
| 15 | + |
| 16 | +def can_execute(context): |
| 17 | + """A filter to only allow this action for project or files view""" |
| 18 | + return ( |
| 19 | + context.file() is not None or context.directory() is not None |
| 20 | + ) and context.module_name in ["Files_View", "Project_Explorer_Project"] |
| 21 | + |
| 22 | + |
| 23 | +@interactive( |
| 24 | + name="Open in system file explorer", |
| 25 | + contextual="Open in system file explorer", |
| 26 | + filter=can_execute |
| 27 | +) |
| 28 | +def on_activate(): |
| 29 | + restored = os.environ.copy() |
| 30 | + for k in os.environ: |
| 31 | + if k.startswith("GPS_STARTUP_"): |
| 32 | + old = k[12:] |
| 33 | + if os.environ[k] == "_ABSENT_VARIABLE_": |
| 34 | + if old in restored.keys(): |
| 35 | + restored.pop(old) |
| 36 | + else: |
| 37 | + restored[old] = os.environ[k] |
| 38 | + |
| 39 | + path = GPS.current_context().directory() |
| 40 | + |
| 41 | + if path is None: |
| 42 | + if GPS.current_context().file() is not None: |
| 43 | + path = GPS.current_context().file().path |
| 44 | + else: |
| 45 | + path = '.' |
| 46 | + |
| 47 | + if sys.platform == "win32": |
| 48 | + e = 'explorer.exe' |
| 49 | + path = '"' + path + '"' |
| 50 | + else: |
| 51 | + e = 'xdg-open' |
| 52 | + |
| 53 | + try: |
| 54 | + subprocess.run([e, path], env=restored) |
| 55 | + except Exception as ex: |
| 56 | + GPS.Console().write(str(ex)) |
0 commit comments