docutil/docsvg.py
author Edouard Tisserant <edouard.tisserant@gmail.com>
Mon, 23 May 2022 18:11:31 +0200
changeset 3477 9ec5ecf9f589
parent 3111 5d9ae04ee50f
child 3482 31f6d7221f60
permissions -rw-r--r--
IDE/Windows: Find Inkscape binary even if Inkscape was installed for current user only.
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     1
#!/usr/bin/env python
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     3
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 814
diff changeset
     4
# This file is part of Beremiz, a Integrated Development Environment for
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 814
diff changeset
     5
# programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     6
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 814
diff changeset
     7
# Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     8
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 814
diff changeset
     9
# See COPYING file for copyrights details.
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    10
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 814
diff changeset
    11
# This program is free software; you can redistribute it and/or
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 814
diff changeset
    12
# modify it under the terms of the GNU General Public License
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 814
diff changeset
    13
# as published by the Free Software Foundation; either version 2
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 814
diff changeset
    14
# of the License, or (at your option) any later version.
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    15
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 814
diff changeset
    16
# This program is distributed in the hope that it will be useful,
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 814
diff changeset
    17
# but WITHOUT ANY WARRANTY; without even the implied warranty of
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 814
diff changeset
    18
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 814
diff changeset
    19
# GNU General Public License for more details.
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    20
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 814
diff changeset
    21
# You should have received a copy of the GNU General Public License
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 814
diff changeset
    22
# along with this program; if not, write to the Free Software
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 814
diff changeset
    23
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    24
1881
091005ec69c4 fix pylint py3k conversion warning: "(no-absolute-import) import missing `from __future__ import absolute_import`"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1876
diff changeset
    25
091005ec69c4 fix pylint py3k conversion warning: "(no-absolute-import) import missing `from __future__ import absolute_import`"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1876
diff changeset
    26
from __future__ import absolute_import
3111
5d9ae04ee50f Docutil : reworked Inkscape launching and path resolution in docsvg.py
Edouard Tisserant
parents: 2705
diff changeset
    27
import wx
1732
94ffe74e6895 clean-up: fix PEP8 E401 multiple imports on one line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
    28
import subprocess
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1735
diff changeset
    29
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    30
def get_inkscape_path():
3111
5d9ae04ee50f Docutil : reworked Inkscape launching and path resolution in docsvg.py
Edouard Tisserant
parents: 2705
diff changeset
    31
    """ Return the Inkscape binary path """
5d9ae04ee50f Docutil : reworked Inkscape launching and path resolution in docsvg.py
Edouard Tisserant
parents: 2705
diff changeset
    32
2628
40c4449001df Allow using docutil.get_inkscape_path() on Linux also.
Edouard Tisserant
parents: 2431
diff changeset
    33
    if wx.Platform == '__WXMSW__':
40c4449001df Allow using docutil.get_inkscape_path() on Linux also.
Edouard Tisserant
parents: 2431
diff changeset
    34
        from six.moves import winreg
3111
5d9ae04ee50f Docutil : reworked Inkscape launching and path resolution in docsvg.py
Edouard Tisserant
parents: 2705
diff changeset
    35
        inkcmd = None
3477
9ec5ecf9f589 IDE/Windows: Find Inkscape binary even if Inkscape was installed for current user only.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3111
diff changeset
    36
        tries = [(winreg.HKEY_LOCAL_MACHINE, 'Software\\Classes\\svgfile\\shell\\Inkscape\\command'),
9ec5ecf9f589 IDE/Windows: Find Inkscape binary even if Inkscape was installed for current user only.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3111
diff changeset
    37
                 (winreg.HKEY_LOCAL_MACHINE, 'Software\\Classes\\inkscape.svg\\shell\\open\\command'),
9ec5ecf9f589 IDE/Windows: Find Inkscape binary even if Inkscape was installed for current user only.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3111
diff changeset
    38
                 (winreg.HKEY_CURRENT_USER, 'Software\\Classes\\inkscape.svg\\shell\\open\\command')]
9ec5ecf9f589 IDE/Windows: Find Inkscape binary even if Inkscape was installed for current user only.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3111
diff changeset
    39
9ec5ecf9f589 IDE/Windows: Find Inkscape binary even if Inkscape was installed for current user only.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3111
diff changeset
    40
        for subreg, key in tries:
2705
889ec6db6f7d docsvg.py : Better error message when inkscape is not installed.
Edouard Tisserant
parents: 2628
diff changeset
    41
            try:
3477
9ec5ecf9f589 IDE/Windows: Find Inkscape binary even if Inkscape was installed for current user only.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3111
diff changeset
    42
                inkcmd = winreg.QueryValue(subreg, key)
9ec5ecf9f589 IDE/Windows: Find Inkscape binary even if Inkscape was installed for current user only.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3111
diff changeset
    43
                break;
3111
5d9ae04ee50f Docutil : reworked Inkscape launching and path resolution in docsvg.py
Edouard Tisserant
parents: 2705
diff changeset
    44
            except OSError:
3477
9ec5ecf9f589 IDE/Windows: Find Inkscape binary even if Inkscape was installed for current user only.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3111
diff changeset
    45
                pass
9ec5ecf9f589 IDE/Windows: Find Inkscape binary even if Inkscape was installed for current user only.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3111
diff changeset
    46
9ec5ecf9f589 IDE/Windows: Find Inkscape binary even if Inkscape was installed for current user only.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3111
diff changeset
    47
        if inkcmd is None:
9ec5ecf9f589 IDE/Windows: Find Inkscape binary even if Inkscape was installed for current user only.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3111
diff changeset
    48
            return None
2705
889ec6db6f7d docsvg.py : Better error message when inkscape is not installed.
Edouard Tisserant
parents: 2628
diff changeset
    49
3111
5d9ae04ee50f Docutil : reworked Inkscape launching and path resolution in docsvg.py
Edouard Tisserant
parents: 2705
diff changeset
    50
        return inkcmd.replace('"%1"', '').strip().replace('"', '')
5d9ae04ee50f Docutil : reworked Inkscape launching and path resolution in docsvg.py
Edouard Tisserant
parents: 2705
diff changeset
    51
2628
40c4449001df Allow using docutil.get_inkscape_path() on Linux also.
Edouard Tisserant
parents: 2431
diff changeset
    52
    else:
3111
5d9ae04ee50f Docutil : reworked Inkscape launching and path resolution in docsvg.py
Edouard Tisserant
parents: 2705
diff changeset
    53
        try:
5d9ae04ee50f Docutil : reworked Inkscape launching and path resolution in docsvg.py
Edouard Tisserant
parents: 2705
diff changeset
    54
            return subprocess.check_output("command -v inkscape", shell=True).strip()
5d9ae04ee50f Docutil : reworked Inkscape launching and path resolution in docsvg.py
Edouard Tisserant
parents: 2705
diff changeset
    55
        except subprocess.CalledProcessError:
5d9ae04ee50f Docutil : reworked Inkscape launching and path resolution in docsvg.py
Edouard Tisserant
parents: 2705
diff changeset
    56
            return None
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1735
diff changeset
    57
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    58
def open_svg(svgfile):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    59
    """ Generic function to open SVG file """
3111
5d9ae04ee50f Docutil : reworked Inkscape launching and path resolution in docsvg.py
Edouard Tisserant
parents: 2705
diff changeset
    60
    
5d9ae04ee50f Docutil : reworked Inkscape launching and path resolution in docsvg.py
Edouard Tisserant
parents: 2705
diff changeset
    61
    inkpath = get_inkscape_path()
5d9ae04ee50f Docutil : reworked Inkscape launching and path resolution in docsvg.py
Edouard Tisserant
parents: 2705
diff changeset
    62
    if inkpath is None:
5d9ae04ee50f Docutil : reworked Inkscape launching and path resolution in docsvg.py
Edouard Tisserant
parents: 2705
diff changeset
    63
        wx.MessageBox("Inkscape is not found or installed !")
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    64
    else:
3111
5d9ae04ee50f Docutil : reworked Inkscape launching and path resolution in docsvg.py
Edouard Tisserant
parents: 2705
diff changeset
    65
        subprocess.Popen([inkpath,svgfile])