From dcbd8c661ae3a17a98170b8133d85b6ace078f00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pauline=20Gom=C3=A9r?= Date: Tue, 25 Oct 2011 22:12:58 +0200 Subject: [PATCH 1/3] Add: Can open tarballs containing at most one (1) file. --- core/reader.py | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/core/reader.py b/core/reader.py index 4ed707e..10d3fde 100644 --- a/core/reader.py +++ b/core/reader.py @@ -16,6 +16,7 @@ import os +import tarfile from PyQt4 import QtGui, QtCore from reader_ui import Ui_MainWindowReader from preferences import DialogPreferences @@ -164,7 +165,7 @@ class MainWindowReader(QtGui.QMainWindow, Ui_MainWindowReader): filename = QtGui.QFileDialog.getOpenFileName( parent=self, caption='Select a file to open', - filter='Text files (*.txt);;All files (*.*)' + filter='Archive files (*.bz2 *.gz *.tar *.tgz);;Text files (*.txt);;All files (*.*)' ) if not filename.isNull(): self.openFile(filename) @@ -322,8 +323,7 @@ class MainWindowReader(QtGui.QMainWindow, Ui_MainWindowReader): def openFile(self, filename): filename = unicode(filename) try: - with open(filename, 'rb') as fp: - content = fp.read() + content = self.openFileByExtension(filename) except IOError: self.setStatus(u'Failed to load file {0}'.format(filename)) QtGui.QMessageBox.critical(self, 'Yomichan', 'Cannot open file for read') @@ -352,6 +352,25 @@ class MainWindowReader(QtGui.QMainWindow, Ui_MainWindowReader): self.setStatus(u'Loaded file {0}'.format(filename)) 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: + content = unicode() + QtGui.QMessageBox.critical(self, 'Yomichan', 'Archives with more than one (1) file are not supported') + else: + with open(filename, 'rb') as fp: + content = fp.read() + return content def closeFile(self): self.setWindowTitle('Yomichan') From 913836cac8f47508edbbb496f460f2f29ef9abbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pauline=20Gom=C3=A9r?= Date: Sat, 29 Oct 2011 18:43:32 +0200 Subject: [PATCH 2/3] Add: Can open tarballs containing many files. --- core/reader.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/core/reader.py b/core/reader.py index 10d3fde..d0da0c7 100644 --- a/core/reader.py +++ b/core/reader.py @@ -365,12 +365,33 @@ class MainWindowReader(QtGui.QMainWindow, Ui_MainWindowReader): content = fp.read() fp.close() else: - content = unicode() - QtGui.QMessageBox.critical(self, 'Yomichan', 'Archives with more than one (1) file are not supported') + # 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): self.setWindowTitle('Yomichan') From 68c5b7c8305772cdbd7511ec9a4f2196745cdf87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pauline=20Gom=C3=A9r?= Date: Sun, 30 Oct 2011 17:29:12 +0100 Subject: [PATCH 3/3] Fix: Set default filter of dialog to text files. --- core/reader.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/reader.py b/core/reader.py index d0da0c7..a22c08f 100644 --- a/core/reader.py +++ b/core/reader.py @@ -165,7 +165,8 @@ class MainWindowReader(QtGui.QMainWindow, Ui_MainWindowReader): filename = QtGui.QFileDialog.getOpenFileName( parent=self, caption='Select a file to open', - filter='Archive files (*.bz2 *.gz *.tar *.tgz);;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(): self.openFile(filename)