File: external_editor.py

package info (click to toggle)
blender 4.3.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 309,564 kB
  • sloc: cpp: 2,385,210; python: 330,236; ansic: 280,972; xml: 2,446; sh: 972; javascript: 317; makefile: 170
file content (54 lines) | stat: -rw-r--r-- 1,681 bytes parent folder | download
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
# SPDX-FileCopyrightText: 2023 Blender Authors
#
# SPDX-License-Identifier: GPL-2.0-or-later

__all__ = (
    "open_external_editor",
)


def open_external_editor(filepath, line, column, /):
    # Internal Python implementation for `TEXT_OT_jump_to_file_at_point`.
    # Returning a non-empty string represents an error, an empty string for success.
    import shlex
    import subprocess
    from string import Template
    from bpy import context
    from bpy.app.translations import pgettext_rpt as rpt_

    text_editor = context.preferences.filepaths.text_editor
    text_editor_args = context.preferences.filepaths.text_editor_args

    # The caller should check this.
    assert text_editor

    if not text_editor_args:
        return rpt_(
            "Provide text editor argument format in File Paths/Applications Preferences, "
            "see input field tool-tip for more information",
        )

    if "$filepath" not in text_editor_args:
        return rpt_("Text Editor Args Format must contain $filepath")

    args = [text_editor]
    template_vars = {
        "filepath": filepath,
        "line": line + 1,
        "column": column + 1,
        "line0": line,
        "column0": column,
    }

    try:
        args.extend([Template(arg).substitute(**template_vars) for arg in shlex.split(text_editor_args)])
    except Exception as ex:
        return rpt_("Exception parsing template: {!r}").format(ex)

    try:
        # With `check=True` if `process.returncode != 0` an exception will be raised.
        subprocess.run(args, check=True)
    except Exception as ex:
        return rpt_("Exception running external editor: {!r}").format(ex)

    return ""