Debian Codegraph @

eyes17/eyes17-manuals

raw
1 #! /usr/bin/python3
2 import locale, re, os, subprocess, sys
3 from PyQt5.QtWidgets import QApplication, QMessageBox, QDialog
4 from PyQt5.QtCore import QTranslator, QLocale, QLibraryInfo
5 from layouts.ui_eyes17_manuals import Ui_Dialog
6
7 class FormatDialog(QDialog, Ui_Dialog):
8 def __init__(self, parent=None):
9 QDialog.__init__(self,parent)
10 #Ui_Dialog.__init__(self)
11 self.setupUi(self)
12
13 class ErrorDialog(QMessageBox):
14 def __init__(self, title, message, parent=None):
15 super(ErrorDialog, self).__init__(parent)
16 self.setWindowTitle(title)
17 self.setText(message)
18 self.setIcon(QMessageBox.Warning)
19
20 def Error(title, message):
21 ed = ErrorDialog(title, message)
22 ed.show()
23
24 def installed_manual(lc, aformat="pdf"):
25 """
26 returns the installed manual if it exists
27 @param lc the locale
28 @param aformat "pdf" or "epub", currently
29 """
30 docdir = "/usr/share/eyes17/doc"
31 for locale in (lc, re.sub("_.*$", "", lc)):
32 # for example if locale == "es_AR", let us try "es_AR" first,
33 # then "es"
34 fname = os.path.join(docdir, locale, "eyes17." + aformat)
35 if os.path.exists(fname):
36 return fname
37 fname = fname + ".gz"
38 if os.path.exists(fname):
39 # if the first match is wrong, let us try a compressed format
40 return fname
41 if not lc.startswith("en"):
42 # in last resort, try to get the English manual
43 fname = installed_manual("en", aformat=aformat)
44 if os.path.exists(fname):
45 return fname
46 return
47
48 def formatDialog():
49 aformat = None
50 app = QApplication(sys.argv)
51 # translation #################
52 lang=QLocale.system().name()
53 tr_eyes=QTranslator()
54 tr_eyes.load("lang/"+lang, os.path.dirname(__file__))
55 app.installTranslator(tr_eyes)
56 tr_qt=QTranslator()
57 tr_qt.load("qt_"+lang,
58 QLibraryInfo.location(QLibraryInfo.TranslationsPath))
59 app.installTranslator(tr_qt)
60 ##############################
61 fd = FormatDialog()
62 fd.show()
63 result = app.exec_()
64 # harvest the radio buttons' status
65 if fd.radioPDF.isChecked():
66 aformat = "pdf"
67 elif fd.radioEPUB.isChecked():
68 aformat = "epub"
69 return aformat
70
71
72
73 if __name__ == "__main__":
74 # which is the format of the User Manual?
75 if len(sys.argv) > 1:
76 # it can be given by the command line
77 aformat = sys.argv[1]
78 else:
79 # or it may be given by a graphic dialog
80 aformat = formatDialog()
81 if aformat not in ("pdf", "epub"):
82 # in case of any error, default to "pdf"
83 aformat = "pdf"
84
85 locale = re.sub(r"\..*", "", os.environ["LANG"])
86 if len(locale) < 2:
87 lc = locale.getlocale()
88 locale = lc[0]
89 fname = installed_manual(locale, aformat)
90 if fname is None:
91 app = QApplication(sys.argv)
92 Error('No User Manual',
93 'Maybe the package eyes17-manual-{locale}\nis not correctly installed'.format(locale=locale[:2])
94 )
95 sys.exit(app.exec_())
96 else:
97 if aformat == "pdf":
98 subprocess.call(["evince", fname])
99 elif aformat == "epub":
100 subprocess.call(["ebook-viewer", fname])