Laurent@814: #!/usr/bin/env python
Laurent@814: # -*- coding: utf-8 -*-
Laurent@814: 
andrej@1571: # This file is part of Beremiz, a Integrated Development Environment for
andrej@1571: # programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
Laurent@814: #
andrej@1571: # Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
Laurent@814: #
andrej@1571: # See COPYING file for copyrights details.
Laurent@814: #
andrej@1571: # This program is free software; you can redistribute it and/or
andrej@1571: # modify it under the terms of the GNU General Public License
andrej@1571: # as published by the Free Software Foundation; either version 2
andrej@1571: # of the License, or (at your option) any later version.
Laurent@814: #
andrej@1571: # This program is distributed in the hope that it will be useful,
andrej@1571: # but WITHOUT ANY WARRANTY; without even the implied warranty of
andrej@1571: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
andrej@1571: # GNU General Public License for more details.
Laurent@814: #
andrej@1571: # You should have received a copy of the GNU General Public License
andrej@1571: # along with this program; if not, write to the Free Software
andrej@1571: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
Laurent@814: 
andrej@1881: 
andrej@1881: from __future__ import absolute_import
Edouard@3111: import wx
andrej@1732: import subprocess
andrej@1736: 
edouard@3499: 
edouard@3499: def _get_inkscape_path():
Edouard@3111:     """ Return the Inkscape binary path """
Edouard@3111: 
Edouard@2628:     if wx.Platform == '__WXMSW__':
Edouard@2628:         from six.moves import winreg
Edouard@3111:         inkcmd = None
edouard@3494:         tries = [(winreg.HKEY_LOCAL_MACHINE, 'Software\\Classes\\svgfile\\shell\\Inkscape\\command'),
edouard@3494:                  (winreg.HKEY_LOCAL_MACHINE, 'Software\\Classes\\inkscape.svg\\shell\\open\\command'),
edouard@3494:                  (winreg.HKEY_CURRENT_USER, 'Software\\Classes\\inkscape.svg\\shell\\open\\command')]
edouard@3494: 
edouard@3494:         for subreg, key in tries:
Edouard@2705:             try:
edouard@3494:                 inkcmd = winreg.QueryValue(subreg, key)
edouard@3494:                 break;
Edouard@3111:             except OSError:
edouard@3494:                 pass
edouard@3494: 
edouard@3494:         if inkcmd is None:
edouard@3494:             return None
Edouard@2705: 
Edouard@3111:         return inkcmd.replace('"%1"', '').strip().replace('"', '')
Edouard@3111: 
Edouard@2628:     else:
Edouard@3111:         try:
Edouard@3111:             return subprocess.check_output("command -v inkscape", shell=True).strip()
Edouard@3111:         except subprocess.CalledProcessError:
Edouard@3111:             return None
andrej@1736: 
edouard@3499: _inkscape_path = None
edouard@3499: def get_inkscape_path():
edouard@3499:     """ Return the Inkscape binary path """
edouard@3499: 
edouard@3499:     global _inkscape_path
edouard@3499: 
edouard@3499:     if _inkscape_path is not None:
edouard@3499:         return _inkscape_path
edouard@3499: 
edouard@3499:     _inkscape_path = _get_inkscape_path()
edouard@3499:     return _inkscape_path
edouard@3499: 
edouard@3499: 
edouard@3499: def _get_inkscape_version():
edouard@3499:     inkpath = get_inkscape_path()
edouard@3499:     if inkpath is None:
edouard@3499:         return None
edouard@3499:     return map(int, 
edouard@3499:         subprocess.check_output([inkpath,"--version"]).split()[1].split('.'))
edouard@3499: 
edouard@3499: _inkscape_version = None
edouard@3499: def get_inkscape_version():
edouard@3499:     global _inkscape_version
edouard@3499: 
edouard@3499:     if _inkscape_version is not None:
edouard@3499:         return _inkscape_version
edouard@3499: 
edouard@3499:     _inkscape_version = _get_inkscape_version()
edouard@3499:     return _inkscape_version
edouard@3499: 
Laurent@814: def open_svg(svgfile):
Laurent@814:     """ Generic function to open SVG file """
Edouard@3111:     
Edouard@3111:     inkpath = get_inkscape_path()
Edouard@3111:     if inkpath is None:
Edouard@3111:         wx.MessageBox("Inkscape is not found or installed !")
Laurent@814:     else:
Edouard@3111:         subprocess.Popen([inkpath,svgfile])