Custom command for exporters

This commit is contained in:
Olivier Keshavjee 2016-04-12 09:15:01 +02:00
parent 3bddbf4c64
commit 1a2a80a41a
5 changed files with 74 additions and 22 deletions

View file

@ -6,9 +6,9 @@ from manuskript.exporter.mmd import mmdExporter
from manuskript.exporter.pandoc import pandocExporter
exporters = [
manuskriptExporter,
pandocExporter,
mmdExporter
manuskriptExporter(),
pandocExporter(),
mmdExporter()
]
def getExporterByName(name):

View file

@ -1,9 +1,10 @@
#!/usr/bin/env python
# --!-- coding: utf8 --!--
import os
import shutil
import subprocess
from PyQt5.QtCore import QSettings
from PyQt5.QtWidgets import QWidget
from manuskript.models.outlineModel import outlineItem
@ -15,9 +16,16 @@ class basicExporter:
description = ""
exportTo = []
cmd = ""
customPath = ""
def __init__(self):
pass
settings = QSettings()
self.customPath = settings.value("Exporters/{}_customPath".format(self.name), "")
def setCustomPath(self, path):
self.customPath = path
settings = QSettings()
settings.setValue("Exporters/{}_customPath".format(self.name), self.customPath)
@classmethod
def getFormatByName(cls, name):
@ -27,9 +35,13 @@ class basicExporter:
return None
@classmethod
def isValid(cls):
return cls.path() != None
def isValid(self):
if self.path() != None:
return 2
elif self.customPath and os.path.exists(self.customPath):
return 1
else:
return 0
@classmethod
def version(cls):
@ -39,9 +51,15 @@ class basicExporter:
def path(cls):
return shutil.which(cls.cmd)
@classmethod
def run(cls, args):
r = subprocess.check_output([cls.cmd] + args) # timeout=.2
def run(self, args):
if self.isValid() == 2:
run = self.cmd
elif self.isValid() == 1:
run = self.customPath
else:
print("Error: no command for", self.name)
return
r = subprocess.check_output([run] + args) # timeout=.2
return r.decode("utf-8")
# Example of how to run a command

View file

@ -19,10 +19,9 @@ class mmdExporter(basicExporter):
]
cmd = "mmd"
@classmethod
def version(cls):
if cls.isValid():
r = cls.run(["-v"])
def version(self):
if self.isValid():
r = self.run(["-v"])
return r.split("\n")[1]
else:
return ""

View file

@ -33,10 +33,12 @@ class pandocExporter(basicExporter):
]
cmd = "pandoc"
@classmethod
def version(cls):
if cls.isValid():
r = cls.run(["-v"])
def __init__(self):
basicExporter.__init__(self)
def version(self):
if self.isValid():
r = self.run(["-v"])
return r.split("\n")[0]
else:
return ""

View file

@ -6,7 +6,7 @@ from collections import OrderedDict
from PyQt5.QtCore import QSize
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QWidget, QListWidgetItem
from PyQt5.QtWidgets import QWidget, QListWidgetItem, QFileDialog
from manuskript import exporter
from manuskript.ui.exporters.exportersManager_ui import Ui_ExportersManager
@ -37,6 +37,9 @@ class exportersManager(QWidget, Ui_ExportersManager):
self.lstExporters.setCurrentRow(0)
self.btnSetPath.clicked.connect(self.setAppPath)
self.txtPath.editingFinished.connect(self.updateAppPath)
def updateUi(self, name):
E = exporter.getExporterByName(name)
self.currentExporter = E
@ -67,12 +70,18 @@ class exportersManager(QWidget, Ui_ExportersManager):
self.grpPath.setVisible(E.name != "Manuskript") # We hide if exporter is manuskript
# Installed
if E.isValid():
if E.isValid() == 2:
self.lblStatus.setText(self.tr("Installed"))
self.lblStatus.setStyleSheet("color: darkGreen;")
self.lblHelpText.setVisible(False)
self.lblVersion.setVisible(True)
self.lblVersionName.setVisible(True)
elif E.isValid() == 1:
self.lblStatus.setText(self.tr("Custom"))
self.lblStatus.setStyleSheet("color: darkOrange;")
self.lblHelpText.setVisible(False)
self.lblVersion.setVisible(True)
self.lblVersionName.setVisible(True)
else:
self.lblStatus.setText(self.tr("Not found"))
self.lblStatus.setStyleSheet("color: red;")
@ -85,7 +94,10 @@ class exportersManager(QWidget, Ui_ExportersManager):
self.lblVersion.setText(E.version())
# Path
self.txtPath.setText(E.path())
if E.path():
self.txtPath.setText(E.path())
else:
self.txtPath.setText(E.customPath)
def updateFormatDescription(self, name):
if self.currentExporter:
@ -107,3 +119,24 @@ class exportersManager(QWidget, Ui_ExportersManager):
self.tr("<b>Requires:</b> ") + f.InvalidBecause
self.lblExportToDescription.setText(desc)
def setAppPath(self):
if self.currentExporter:
E = self.currentExporter
fn = QFileDialog.getOpenFileName(self,
caption=self.tr("Set {} executable path.").format(E.cmd),
directory=E.customPath)
if fn[0]:
self.updateAppPath(fn[0])
def updateAppPath(self, path=""):
if not path:
path = self.txtPath.text()
if self.currentExporter:
E = self.currentExporter
E.setCustomPath(path)
self.txtPath.setText(E.customPath)
self.updateUi(E.name)