Merge remote-tracking branch 'valgaav/tarball' into dev
This commit is contained in:
commit
d440b58f10
@ -17,6 +17,7 @@
|
|||||||
|
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import tarfile
|
||||||
from PyQt4 import QtGui, QtCore
|
from PyQt4 import QtGui, QtCore
|
||||||
from reader_ui import Ui_MainWindowReader
|
from reader_ui import Ui_MainWindowReader
|
||||||
from preferences import DialogPreferences
|
from preferences import DialogPreferences
|
||||||
@ -160,7 +161,8 @@ class MainWindowReader(QtGui.QMainWindow, Ui_MainWindowReader):
|
|||||||
filename = QtGui.QFileDialog.getOpenFileName(
|
filename = QtGui.QFileDialog.getOpenFileName(
|
||||||
parent=self,
|
parent=self,
|
||||||
caption='Select a file to open',
|
caption='Select a file to open',
|
||||||
filter='Text files (*.txt);;All files (*.*)'
|
filter='Archive files (*.bz2 *.gz *.tar *.tgz);;Text files (*.txt);;All files (*.*)',
|
||||||
|
selectedFilter='Text files (*.txt)'
|
||||||
)
|
)
|
||||||
if not filename.isNull():
|
if not filename.isNull():
|
||||||
self.openFile(filename)
|
self.openFile(filename)
|
||||||
@ -318,8 +320,7 @@ class MainWindowReader(QtGui.QMainWindow, Ui_MainWindowReader):
|
|||||||
def openFile(self, filename):
|
def openFile(self, filename):
|
||||||
filename = unicode(filename)
|
filename = unicode(filename)
|
||||||
try:
|
try:
|
||||||
with open(filename, 'rb') as fp:
|
content = self.openFileByExtension(filename)
|
||||||
content = fp.read()
|
|
||||||
except IOError:
|
except IOError:
|
||||||
self.setStatus(u'Failed to load file {0}'.format(filename))
|
self.setStatus(u'Failed to load file {0}'.format(filename))
|
||||||
QtGui.QMessageBox.critical(self, 'Yomichan', 'Cannot open file for read')
|
QtGui.QMessageBox.critical(self, 'Yomichan', 'Cannot open file for read')
|
||||||
@ -349,6 +350,46 @@ class MainWindowReader(QtGui.QMainWindow, Ui_MainWindowReader):
|
|||||||
self.setStatus(u'Loaded file {0}'.format(filename))
|
self.setStatus(u'Loaded file {0}'.format(filename))
|
||||||
self.setWindowTitle(u'Yomichan - {0} ({1})'.format(os.path.split(filename)[1], encoding))
|
self.setWindowTitle(u'Yomichan - {0} ({1})'.format(os.path.split(filename)[1], encoding))
|
||||||
|
|
||||||
|
def openFileByExtension(self, filename):
|
||||||
|
if tarfile.is_tarfile(filename):
|
||||||
|
# opening an empty tar file raises ReadError
|
||||||
|
with tarfile.open(filename, 'r:*') as tp:
|
||||||
|
files = [f for f in tp.getnames() if tp.getmember(f).isfile()]
|
||||||
|
if len(files) == 0:
|
||||||
|
content = unicode()
|
||||||
|
elif len(files) == 1:
|
||||||
|
fp = tp.extractfile(files[0])
|
||||||
|
content = fp.read()
|
||||||
|
fp.close()
|
||||||
|
else:
|
||||||
|
# Using index because of encoding difficulties
|
||||||
|
(index, ok) = self.selectItem([f.decode('utf-8') for f in files])
|
||||||
|
if ok:
|
||||||
|
fp = tp.extractfile(files[index - 1])
|
||||||
|
content = fp.read()
|
||||||
|
fp.close()
|
||||||
|
else:
|
||||||
|
content = unicode()
|
||||||
|
else:
|
||||||
|
with open(filename, 'rb') as fp:
|
||||||
|
content = fp.read()
|
||||||
|
return content
|
||||||
|
|
||||||
|
def selectItem(self, list):
|
||||||
|
items = [self.formatQString(i, x) for i, x in enumerate(list)]
|
||||||
|
(item, ok) = QtGui.QInputDialog.getItem(
|
||||||
|
self,
|
||||||
|
'Yomichan',
|
||||||
|
'Select file to open:',
|
||||||
|
items,
|
||||||
|
current = 0,
|
||||||
|
editable=False)
|
||||||
|
(index, success) = item.split('.').first().toInt()
|
||||||
|
return (index, ok and success)
|
||||||
|
|
||||||
|
def formatQString(self, index, item):
|
||||||
|
return QtCore.QString(str(index + 1) + '. ').append(QtCore.QString(item))
|
||||||
|
|
||||||
def closeFile(self):
|
def closeFile(self):
|
||||||
self.setWindowTitle('Yomichan')
|
self.setWindowTitle('Yomichan')
|
||||||
self.textContent.setPlainText(unicode())
|
self.textContent.setPlainText(unicode())
|
||||||
|
Loading…
Reference in New Issue
Block a user