docutil/docsvg.py
branchsvghmi
changeset 3111 5d9ae04ee50f
parent 3052 ffce85221ea5
child 3477 9ec5ecf9f589
equal deleted inserted replaced
3110:476bd870313d 3111:5d9ae04ee50f
    22 # along with this program; if not, write to the Free Software
    22 # along with this program; if not, write to the Free Software
    23 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    23 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    24 
    24 
    25 
    25 
    26 from __future__ import absolute_import
    26 from __future__ import absolute_import
    27 import os
    27 import wx
    28 import subprocess
    28 import subprocess
    29 import wx
       
    30 
       
    31 
    29 
    32 def get_inkscape_path():
    30 def get_inkscape_path():
    33     """ Return the Inkscape path """
    31     """ Return the Inkscape binary path """
       
    32 
    34     if wx.Platform == '__WXMSW__':
    33     if wx.Platform == '__WXMSW__':
    35         from six.moves import winreg
    34         from six.moves import winreg
       
    35         inkcmd = None
    36         try:
    36         try:
    37                 svgexepath = winreg.QueryValue(winreg.HKEY_LOCAL_MACHINE,
    37             inkcmd = winreg.QueryValue(winreg.HKEY_LOCAL_MACHINE,
    38                                                'Software\\Classes\\svgfile\\shell\\Inkscape\\command')
    38                                            'Software\\Classes\\svgfile\\shell\\Inkscape\\command')
    39         except OSError:
    39         except OSError:
    40             try:
    40             try:
    41                 svgexepath = winreg.QueryValue(winreg.HKEY_LOCAL_MACHINE,
    41                 inkcmd = winreg.QueryValue(winreg.HKEY_LOCAL_MACHINE,
    42                                                'Software\\Classes\\inkscape.svg\\shell\\open\\command')
    42                                                'Software\\Classes\\inkscape.svg\\shell\\open\\command')
    43             except Exception:
    43             except OSError:
    44                 return None
    44                 return None
    45 
    45 
    46         svgexepath = svgexepath.replace('"%1"', '').strip()
    46         return inkcmd.replace('"%1"', '').strip().replace('"', '')
    47         return svgexepath.replace('"', '')
    47 
    48     else:
    48     else:
    49         # TODO: search for inkscape in $PATH
    49         try:
    50         svgexepath = os.path.join("/usr/bin", "inkscape")
    50             return subprocess.check_output("command -v inkscape", shell=True).strip()
    51         if os.path.exists(svgexepath):
    51         except subprocess.CalledProcessError:
    52             return svgexepath
    52             return None
    53         return None
       
    54 
       
    55 
       
    56 def open_win_svg(svgexepath, svgfile):
       
    57     """ Open Inkscape on Windows platform """
       
    58     popenargs = [svgexepath]
       
    59     if svgfile is not None:
       
    60         popenargs.append(svgfile)
       
    61     subprocess.Popen(popenargs)
       
    62 
       
    63 
       
    64 def open_lin_svg(svgexepath, svgfile):
       
    65     """ Open Inkscape on Linux platform """
       
    66     if os.path.isfile("/usr/bin/inkscape"):
       
    67         os.system("%s %s &" % (svgexepath, svgfile))
       
    68 
       
    69 
    53 
    70 def open_svg(svgfile):
    54 def open_svg(svgfile):
    71     """ Generic function to open SVG file """
    55     """ Generic function to open SVG file """
    72     if wx.Platform == '__WXMSW__':
    56     
    73         try:
    57     inkpath = get_inkscape_path()
    74             open_win_svg(get_inkscape_path(), svgfile)
    58     if inkpath is None:
    75         except Exception:
    59         wx.MessageBox("Inkscape is not found or installed !")
    76             wx.MessageBox("Inkscape is not found or installed !")
       
    77             return None
       
    78     else:
    60     else:
    79         svgexepath=get_inkscape_path()
    61         subprocess.Popen([inkpath,svgfile])
    80         if os.path.isfile(svgexepath):
       
    81             open_lin_svg(svgexepath, svgfile)
       
    82         else:
       
    83             wx.MessageBox("Inkscape is not found or installed !")
       
    84             return None