2015-07-01 23:14:03 +12:00
|
|
|
#!/usr/bin/env python
|
2016-02-07 00:34:22 +13:00
|
|
|
# --!-- coding: utf8 --!--
|
2016-04-12 19:15:01 +12:00
|
|
|
import os
|
2016-04-02 06:01:27 +13:00
|
|
|
import shutil
|
2016-02-07 00:34:22 +13:00
|
|
|
import subprocess
|
|
|
|
|
2016-04-12 19:15:01 +12:00
|
|
|
from PyQt5.QtCore import QSettings
|
2016-04-02 06:01:27 +13:00
|
|
|
from PyQt5.QtWidgets import QWidget
|
|
|
|
|
2017-11-16 08:33:27 +13:00
|
|
|
from manuskript.models import outlineItem
|
2017-05-08 19:10:33 +12:00
|
|
|
from manuskript.functions import mainWindow
|
2016-04-05 06:00:19 +12:00
|
|
|
|
2019-10-15 01:36:06 +13:00
|
|
|
import logging
|
|
|
|
LOGGER = logging.getLogger(__name__)
|
2016-04-02 06:01:27 +13:00
|
|
|
|
|
|
|
class basicExporter:
|
|
|
|
|
|
|
|
name = ""
|
|
|
|
description = ""
|
|
|
|
exportTo = []
|
|
|
|
cmd = ""
|
2016-04-12 19:15:01 +12:00
|
|
|
customPath = ""
|
2016-04-16 00:04:07 +12:00
|
|
|
icon = ""
|
2017-11-06 22:05:20 +13:00
|
|
|
absentTip = "" # A tip displayed when exporter is absent.
|
|
|
|
absentURL = "" # URL to open if exporter is absent.
|
2015-07-01 23:14:03 +12:00
|
|
|
|
|
|
|
def __init__(self):
|
2016-04-12 19:15:01 +12:00
|
|
|
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)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2016-04-15 21:58:09 +12:00
|
|
|
def getFormatByName(self, name):
|
|
|
|
for f in self.exportTo:
|
2016-04-02 06:01:27 +13:00
|
|
|
if f.name == name:
|
|
|
|
return f
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2016-04-12 19:15:01 +12:00
|
|
|
def isValid(self):
|
|
|
|
if self.path() != None:
|
|
|
|
return 2
|
|
|
|
elif self.customPath and os.path.exists(self.customPath):
|
|
|
|
return 1
|
|
|
|
else:
|
|
|
|
return 0
|
2016-04-02 06:01:27 +13:00
|
|
|
|
2016-04-15 21:58:09 +12:00
|
|
|
def version(self):
|
2016-04-02 06:01:27 +13:00
|
|
|
return ""
|
|
|
|
|
2016-04-15 21:58:09 +12:00
|
|
|
def path(self):
|
|
|
|
return shutil.which(self.cmd)
|
2016-04-02 06:01:27 +13:00
|
|
|
|
2016-04-12 19:15:01 +12:00
|
|
|
def run(self, args):
|
|
|
|
if self.isValid() == 2:
|
|
|
|
run = self.cmd
|
|
|
|
elif self.isValid() == 1:
|
|
|
|
run = self.customPath
|
|
|
|
else:
|
2019-10-15 01:36:06 +13:00
|
|
|
LOGGER.error("No command for %s.", self.name)
|
2019-05-10 06:24:52 +12:00
|
|
|
return None
|
2016-04-12 19:15:01 +12:00
|
|
|
r = subprocess.check_output([run] + args) # timeout=.2
|
2016-04-02 06:01:27 +13:00
|
|
|
return r.decode("utf-8")
|
|
|
|
|
|
|
|
# Example of how to run a command
|
|
|
|
#
|
|
|
|
# cmdl = ['txt2tags', '-t', target, '--enc=utf-8', '--no-headers', '-o', '-', '-']
|
|
|
|
#
|
|
|
|
# cmd = subprocess.Popen(('echo', text), stdout=subprocess.PIPE)
|
|
|
|
# try:
|
|
|
|
# output = subprocess.check_output(cmdl, stdin=cmd.stdout, stderr=subprocess.STDOUT) # , cwd="/tmp"
|
|
|
|
# except subprocess.CalledProcessError as e:
|
2019-10-15 01:36:06 +13:00
|
|
|
# LOGGER.error("Failed to read from process output.")
|
2016-04-02 06:01:27 +13:00
|
|
|
# return text
|
|
|
|
# cmd.wait()
|
|
|
|
#
|
|
|
|
# return output.decode("utf-8")
|
|
|
|
|
|
|
|
|
|
|
|
class basicFormat:
|
|
|
|
|
|
|
|
implemented = False
|
2016-04-08 00:27:51 +12:00
|
|
|
InvalidBecause = ""
|
2016-04-02 06:01:27 +13:00
|
|
|
requires = {
|
|
|
|
"Settings": False,
|
2016-04-06 03:21:07 +12:00
|
|
|
"Preview": False,
|
2016-04-02 06:01:27 +13:00
|
|
|
}
|
2016-04-08 22:48:19 +12:00
|
|
|
icon = ""
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2016-04-08 22:48:19 +12:00
|
|
|
def __init__(self, name, description="", icon=""):
|
2016-04-02 06:01:27 +13:00
|
|
|
self.name = name
|
|
|
|
self.description = description
|
2016-04-08 22:48:19 +12:00
|
|
|
self.icon = icon
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2016-04-02 06:01:27 +13:00
|
|
|
@classmethod
|
2016-04-05 06:00:19 +12:00
|
|
|
def settingsWidget(cls):
|
|
|
|
return QWidget()
|
|
|
|
|
|
|
|
@classmethod
|
2016-04-06 03:21:07 +12:00
|
|
|
def previewWidget(cls):
|
2016-04-02 06:01:27 +13:00
|
|
|
return QWidget()
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2016-04-02 06:01:27 +13:00
|
|
|
@classmethod
|
2016-04-06 03:21:07 +12:00
|
|
|
def preview(cls, settingsWidget, previewWidget):
|
2016-04-07 22:18:20 +12:00
|
|
|
pass
|
|
|
|
|
2016-04-08 22:02:50 +12:00
|
|
|
@classmethod
|
|
|
|
def export(cls, settingsWidget):
|
|
|
|
pass
|
|
|
|
|
2016-04-07 22:18:20 +12:00
|
|
|
@classmethod
|
|
|
|
def shortcodes(cls):
|
|
|
|
return [
|
|
|
|
("\n", "\\n")
|
|
|
|
]
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def escapes(cls, text):
|
|
|
|
for A, B in cls.shortcodes():
|
|
|
|
text = text.replace(A, B)
|
|
|
|
return text
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def descapes(cls, text):
|
|
|
|
"""How do we call that?"""
|
|
|
|
for A, B in cls.shortcodes():
|
|
|
|
text = text.replace(B, A)
|
|
|
|
return text
|
|
|
|
|
2016-04-08 00:27:51 +12:00
|
|
|
@classmethod
|
|
|
|
def isValid(cls):
|
|
|
|
return True
|
2017-11-06 22:05:20 +13:00
|
|
|
|
2017-05-08 19:10:33 +12:00
|
|
|
@classmethod
|
|
|
|
def projectPath(cls):
|
|
|
|
return os.path.dirname(os.path.abspath(mainWindow().currentProject))
|
2016-04-08 00:27:51 +12:00
|
|
|
|