4 # |
4 # |
5 |
5 |
6 """ |
6 """ |
7 Module implementing the base64 data URI encoder wizard dialog. |
7 Module implementing the base64 data URI encoder wizard dialog. |
8 """ |
8 """ |
9 |
|
10 from __future__ import unicode_literals |
|
11 try: |
|
12 str = unicode # __IGNORE_EXCEPTION__ |
|
13 except NameError: |
|
14 pass |
|
15 |
9 |
16 import os |
10 import os |
17 import mimetypes |
11 import mimetypes |
18 import base64 |
12 import base64 |
19 import getpass |
13 import getpass |
20 import datetime |
14 import datetime |
21 |
15 |
22 from PyQt5.QtCore import pyqtSlot |
16 from PyQt5.QtCore import pyqtSlot |
23 from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QApplication, \ |
17 from PyQt5.QtWidgets import ( |
24 QInputDialog |
18 QDialog, QDialogButtonBox, QApplication, QInputDialog |
|
19 ) |
25 |
20 |
26 from E5Gui.E5Completers import E5FileCompleter |
21 from E5Gui.E5Completers import E5FileCompleter |
27 from E5Gui import E5FileDialog, E5MessageBox |
22 from E5Gui import E5FileDialog, E5MessageBox |
28 |
23 |
29 from .Ui_DataUriEncoderWizardDialog import Ui_DataUriEncoderWizardDialog |
24 from .Ui_DataUriEncoderWizardDialog import Ui_DataUriEncoderWizardDialog |
30 |
25 |
31 import Preferences |
26 import Preferences |
32 import Utilities |
27 import Utilities |
33 import UI.PixmapCache |
28 import UI.PixmapCache |
34 |
|
35 |
|
36 Python2Template = """#!/usr/bin/env python |
|
37 # -*- coding: utf-8 -*- |
|
38 |
|
39 from base64 import b64decode |
|
40 from cStringIO import StringIO |
|
41 |
|
42 #metadata |
|
43 __author__ = '{0}' |
|
44 __date__ = '{1}' |
|
45 |
|
46 |
|
47 embedded_file = StringIO(b64decode({2})) |
|
48 print(embedded_file.read()) |
|
49 """ |
|
50 |
29 |
51 |
30 |
52 Python3Template = """#!/usr/bin/env python3 |
31 Python3Template = """#!/usr/bin/env python3 |
53 # -*- coding: utf-8 -*- |
32 # -*- coding: utf-8 -*- |
54 |
33 |
83 self.__fileCompleter = E5FileCompleter(self.fileEdit) |
62 self.__fileCompleter = E5FileCompleter(self.fileEdit) |
84 |
63 |
85 self.fileButton.setIcon(UI.PixmapCache.getIcon("open.png")) |
64 self.fileButton.setIcon(UI.PixmapCache.getIcon("open.png")) |
86 |
65 |
87 self.embeddingComboBox.addItems([ |
66 self.embeddingComboBox.addItems([ |
88 self.tr('Do not generate code'), |
67 self.tr('Do not generate code'), # 0 |
89 self.tr('Generate CSS embed code'), |
68 self.tr('Generate Python3 embed code'), # 1 |
90 self.tr('Generate Python3 embed code'), |
69 self.tr('Generate CSS embed code'), # 2 |
91 self.tr('Generate Python2 embed code'), |
70 self.tr('Generate HTML embed code'), # 3 |
92 self.tr('Generate HTML embed code'), |
71 self.tr('Generate JS embed code'), # 4 |
93 self.tr('Generate JS embed code'), |
72 self.tr('Generate QML embed code'), # 5 |
94 self.tr('Generate QML embed code'), |
|
95 ]) |
73 ]) |
96 |
74 |
97 def __getStartDir(self): |
75 def __getStartDir(self): |
98 """ |
76 """ |
99 Private method to get the start directory for selection dialogs. |
77 Private method to get the start directory for selection dialogs. |
106 @pyqtSlot() |
84 @pyqtSlot() |
107 def on_fileButton_clicked(self): |
85 def on_fileButton_clicked(self): |
108 """ |
86 """ |
109 Private slot to select the file to be encoded via a selection dialog. |
87 Private slot to select the file to be encoded via a selection dialog. |
110 """ |
88 """ |
111 start = Utilities.fromNativeSeparators(self.fileEdit.text()) or \ |
89 start = ( |
|
90 Utilities.fromNativeSeparators(self.fileEdit.text()) or |
112 self.__getStartDir() |
91 self.__getStartDir() |
|
92 ) |
113 inputFile = E5FileDialog.getOpenFileName( |
93 inputFile = E5FileDialog.getOpenFileName( |
114 self, |
94 self, |
115 self.tr("Data URI Encoder"), |
95 self.tr("Data URI Encoder"), |
116 start, |
96 start, |
117 self.tr( |
97 self.tr( |
130 """ |
110 """ |
131 Private slot to handle the selection of an embedding method. |
111 Private slot to handle the selection of an embedding method. |
132 |
112 |
133 @param index index of the selected entry (integer) |
113 @param index index of the selected entry (integer) |
134 """ |
114 """ |
135 if index in [1, 4]: |
115 if index in [2, 3]: |
136 self.encryptCheckBox.setChecked(False) |
116 self.encryptCheckBox.setChecked(False) |
137 self.dataCheckBox.setChecked(True) |
117 self.dataCheckBox.setChecked(True) |
138 elif index in [0, 2, 3, 5, 6]: |
118 else: |
139 self.encryptCheckBox.setChecked(False) |
119 self.encryptCheckBox.setChecked(False) |
140 self.dataCheckBox.setChecked(False) |
120 self.dataCheckBox.setChecked(False) |
141 |
121 |
142 @pyqtSlot() |
122 @pyqtSlot() |
143 def on_encodeButton_clicked(self): |
123 def on_encodeButton_clicked(self): |
177 self.tr("Data URI Encoder"), |
157 self.tr("Data URI Encoder"), |
178 self.tr( |
158 self.tr( |
179 """<p>The file <b>{0}</b> could not be read.</p>""" |
159 """<p>The file <b>{0}</b> could not be read.</p>""" |
180 """<p>Reason: {1}</p>""").format(filepath, str(err))) |
160 """<p>Reason: {1}</p>""").format(filepath, str(err))) |
181 return |
161 return |
182 if self.embeddingComboBox.currentIndex() == 1: |
162 if self.embeddingComboBox.currentIndex() == 1: # Python 3 |
|
163 output = Python3Template.format( |
|
164 getpass.getuser(), |
|
165 datetime.datetime.now().isoformat().split('.')[0], output) |
|
166 elif self.embeddingComboBox.currentIndex() == 2: # CSS |
183 output = ('html, body {{ margin:0; padding:0; background: url({0})' |
167 output = ('html, body {{ margin:0; padding:0; background: url({0})' |
184 ' no-repeat center center fixed; background-size:cover' |
168 ' no-repeat center center fixed; background-size:cover' |
185 ' }}'.format(output)) |
169 ' }}'.format(output)) |
186 elif self.embeddingComboBox.currentIndex() == 2: |
170 elif self.embeddingComboBox.currentIndex() == 3: # HTML |
187 output = Python3Template.format( |
|
188 getpass.getuser(), |
|
189 datetime.datetime.now().isoformat().split('.')[0], output) |
|
190 elif self.embeddingComboBox.currentIndex() == 3: |
|
191 output = Python2Template.format( |
|
192 getpass.getuser(), |
|
193 datetime.datetime.now().isoformat().split('.')[0], output) |
|
194 elif self.embeddingComboBox.currentIndex() == 4: |
|
195 output = '<img src={0} alt="{1}" title="{1}"/>'.format( |
171 output = '<img src={0} alt="{1}" title="{1}"/>'.format( |
196 output, os.path.basename(filepath)) |
172 output, os.path.basename(filepath)) |
197 elif self.embeddingComboBox.currentIndex() == 5: |
173 elif self.embeddingComboBox.currentIndex() == 4: # JS |
198 output = 'var embedded_file = window.atob({0}); '.format(output) |
174 output = 'var embedded_file = window.atob({0}); '.format(output) |
199 elif self.embeddingComboBox.currentIndex() == 6: |
175 elif self.embeddingComboBox.currentIndex() == 5: # QML |
200 output = 'Image {{ source: {0} }} '.format(output) |
176 output = 'Image {{ source: {0} }} '.format(output) |
201 |
177 |
202 if self.encryptCheckBox.isChecked(): |
178 if self.encryptCheckBox.isChecked(): |
203 output = output.encode('rot_13') |
179 output = output.encode('rot_13') |
204 |
180 |