diff --git a/comictagger.py b/comictagger.py index 75ba339..b7193c8 100755 --- a/comictagger.py +++ b/comictagger.py @@ -28,7 +28,10 @@ import time from pprint import pprint import json import platform +import locale +filename_encoding = sys.getfilesystemencoding() + try: qt_available = True from PyQt4 import QtCore, QtGui @@ -126,7 +129,7 @@ def post_process_matches( match_results, opts, settings ): for mm in match_results.multipleMatches: print mm.filename for (counter,m) in enumerate(mm.matches): - print " {0}. {1} #{2} [{3}] ({4}/{5}) - {6}".format(counter, + print u" {0}. {1} #{2} [{3}] ({4}/{5}) - {6}".format(counter, m['series'], m['issue_number'], m['publisher'], @@ -159,6 +162,7 @@ def cli_mode( opts, settings ): match_results = OnlineMatchResults() for f in opts.file_list: + f = f.decode(filename_encoding, 'replace') process_file_cli( f, opts, settings, match_results ) sys.stdout.flush() @@ -294,23 +298,23 @@ def process_file_cli( filename, opts, settings, match_results ): md = CBLTransformer( md, settings ).apply() if not ca.writeMetadata( md, opts.data_style ): - print "{0}: Tag copy seemed to fail!".format( filename ) + print u"{0}: Tag copy seemed to fail!".format( filename ) else: - print "{0}: Copied {1} tags to {2} .".format( filename, src_style_name, dst_style_name ) + print u"{0}: Copied {1} tags to {2} .".format( filename, src_style_name, dst_style_name ) else: - print "{0}: dry-run. {1} tags not copied".format( filename, src_style_name ) + print u"{0}: dry-run. {1} tags not copied".format( filename, src_style_name ) else: - print "{0}: This archive doesn't have {1} tags to copy.".format( filename, src_style_name ) + print u"{0}: This archive doesn't have {1} tags to copy.".format( filename, src_style_name ) elif opts.save_tags: if opts.no_overwrite and has[ opts.data_style ]: - print "{0}: Already has {1} tags. Not overwriting.".format(filename, MetaDataStyle.name[ opts.data_style ]) + print u"{0}: Already has {1} tags. Not overwriting.".format(filename, MetaDataStyle.name[ opts.data_style ]) return if batch_mode: - print "Processing {0}: ".format(filename) + print u"Processing {0}: ".format(filename) md = create_local_metadata( opts, ca, has[ opts.data_style ] ) @@ -388,7 +392,7 @@ def process_file_cli( filename, opts, settings, match_results ): msg_hdr = "" if batch_mode: - msg_hdr = "{0}: ".format(filename) + msg_hdr = u"{0}: ".format(filename) if opts.data_style is not None: use_tags = has[ opts.data_style ] @@ -429,7 +433,7 @@ def process_file_cli( filename, opts, settings, match_results ): else: suffix = " (dry-run, no change)" - print "renamed '{0}' -> '{1}' {2}".format(os.path.basename(filename), new_name, suffix) + print u"renamed '{0}' -> '{1}' {2}".format(os.path.basename(filename), new_name, suffix) @@ -439,7 +443,7 @@ def process_file_cli( filename, opts, settings, match_results ): def main(): # try to make stdout encodings happy for unicode - sys.stdout = codecs.getwriter('utf8')(sys.stdout) + sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout) opts = Options() opts.parseCmdLineArgs() @@ -469,7 +473,10 @@ def main(): app.processEvents() try: - tagger_window = TaggerWindow( opts.filename, settings ) + fname = None + if opts.filename is not None: + fname = opts.filename.decode(filename_encoding, 'replace') + tagger_window = TaggerWindow( fname, settings ) tagger_window.show() if platform.system() != "Linux": @@ -481,8 +488,7 @@ def main(): if __name__ == "__main__": - main() - + main() diff --git a/comicvinecacher.py b/comicvinecacher.py index 27c60bb..6e00eb3 100644 --- a/comicvinecacher.py +++ b/comicvinecacher.py @@ -93,7 +93,7 @@ class ComicVineCacher: con = lite.connect( self.db_file ) with con: - + con.text_factory = unicode cur = con.cursor() # remove all previous entries with this search term @@ -131,6 +131,7 @@ class ComicVineCacher: results = list() con = lite.connect( self.db_file ) with con: + con.text_factory = unicode cur = con.cursor() @@ -204,6 +205,7 @@ class ComicVineCacher: con = lite.connect( self.db_file ) with con: cur = con.cursor() + con.text_factory = unicode # purge stale volume info a_week_ago = datetime.datetime.today()-datetime.timedelta(days=7) @@ -255,6 +257,7 @@ class ComicVineCacher: with con: cur = con.cursor() + con.text_factory = unicode timestamp = datetime.datetime.now() data = { @@ -273,6 +276,7 @@ class ComicVineCacher: con = lite.connect( self.db_file ) with con: cur = con.cursor() + con.text_factory = unicode cur.execute("SELECT image_url,thumb_image_url,publish_month,publish_year FROM Issues WHERE id=?", [ issue_id ]) row = cur.fetchone() diff --git a/comicvinetalker.py b/comicvinetalker.py index af5f747..5407b3e 100644 --- a/comicvinetalker.py +++ b/comicvinetalker.py @@ -93,7 +93,8 @@ class ComicVineTalker(QObject): original_series_name = series_name - series_name = urllib.quote_plus(str(series_name)) + series_name = urllib.quote_plus(series_name.encode("utf-8")) + #series_name = urllib.quote_plus(unicode(series_name)) search_url = "http://api.comicvine.com/search/?api_key=" + self.api_key + "&format=json&resources=volume&query=" + series_name + "&field_list=name,id,start_year,publisher,image,description,count_of_issues&sort=start_year" content = self.getUrlContent(search_url) diff --git a/filenameparser.py b/filenameparser.py index 06223ab..73715dc 100644 --- a/filenameparser.py +++ b/filenameparser.py @@ -140,7 +140,7 @@ class FileNameParser: tmpstr = tmpstr.replace("#", " ") if issue != "": - # assume that issue substr has at least on space before it + # assume that issue substr has at least one space before it issue_str = " " + str(issue) series = tmpstr.split(issue_str)[0] else: @@ -176,7 +176,7 @@ class FileNameParser: return year def parseFilename( self, filename ): - + # remove the path filename = os.path.basename(filename) diff --git a/filerenamer.py b/filerenamer.py index bdf3f26..0223558 100644 --- a/filerenamer.py +++ b/filerenamer.py @@ -47,7 +47,7 @@ class FileRenamer: return (word[0] == "%" and word[-1:] == "%") if value is not None: - return text.replace( token, str(value) ) + return text.replace( token, unicode(value) ) else: if self.smart_cleanup: # smart cleanup means we want to remove anything appended to token if it's empty @@ -77,7 +77,7 @@ class FileRenamer: new_name = self.replaceToken( new_name, md.volume, '%volume%') if md.issue is not None: - issue_str = "{0}".format( IssueString(md.issue).asString(pad=self.issue_zero_padding) ) + issue_str = u"{0}".format( IssueString(md.issue).asString(pad=self.issue_zero_padding) ) else: issue_str = None new_name = self.replaceToken( new_name, issue_str, '%issue%') @@ -99,7 +99,7 @@ class FileRenamer: new_name = re.sub("(\s-)+", " -", new_name ) # remove duplicate spaces - new_name = " ".join(new_name.split()) + new_name = u" ".join(new_name.split()) if ext is None: ext = os.path.splitext( filename )[1] diff --git a/issueselectionwindow.py b/issueselectionwindow.py index 3ed3878..79d2ea7 100644 --- a/issueselectionwindow.py +++ b/issueselectionwindow.py @@ -95,7 +95,7 @@ class IssueSelectionWindow(QtGui.QDialog): item.setFlags(QtCore.Qt.ItemIsSelectable| QtCore.Qt.ItemIsEnabled) self.twList.setItem(row, 0, item) - item_text = u"{0}".format(record['name']) + item_text = record['name'] item = QtGui.QTableWidgetItem(item_text) item.setFlags(QtCore.Qt.ItemIsSelectable| QtCore.Qt.ItemIsEnabled) self.twList.setItem(row, 1, item) diff --git a/renamewindow.py b/renamewindow.py index c5ef05d..110ac4e 100644 --- a/renamewindow.py +++ b/renamewindow.py @@ -50,7 +50,7 @@ class RenameWindow(QtGui.QDialog): def doPreview( self ): self.new_name = self.renamer.determineName( self.comic_archive.path ) - preview = "\"{0}\" ==> \"{1}\"".format( self.comic_archive.path, self.new_name ) + preview = u"\"{0}\" ==> \"{1}\"".format( self.comic_archive.path, self.new_name ) self.textEdit.setPlainText( preview ) def modifySettings( self ): diff --git a/taggerwindow.py b/taggerwindow.py index d7bead3..8311e51 100644 --- a/taggerwindow.py +++ b/taggerwindow.py @@ -375,7 +375,7 @@ class TaggerWindow( QtGui.QMainWindow): def dropEvent(self, event): if self.dirtyFlagVerification( "Open Archive", "If you open a new archive now, data in the form will be lost. Are you sure?"): - self.openArchive( str(self.droppedFile) ) + self.openArchive( unicode(self.droppedFile)) def openArchive( self, path, explicit_style=None, clear_form=True ): @@ -611,7 +611,7 @@ class TaggerWindow( QtGui.QMainWindow): #helper func def assignText( field, value): if value is not None: - field.setText( u"{0}".format(value) ) + field.setText( unicode(value) ) md = self.metadata @@ -830,7 +830,7 @@ class TaggerWindow( QtGui.QMainWindow): fileList = dialog.selectedFiles() if self.dirtyFlagVerification( "Open Archive", "If you open a new archive now, data in the form will be lost. Are you sure?"): - self.openArchive( str(fileList[0]) ) + self.openArchive( unicode(fileList[0]) ) def autoSelectSearch(self): @@ -849,8 +849,8 @@ class TaggerWindow( QtGui.QMainWindow): QtGui.QMessageBox.information(self,"Automatic Online Search", "Can't auto-select without an issue number (yet!)") return - if str(self.leSeries.text()).strip() != "": - series_name = str(self.leSeries.text()).strip() + if unicode(self.leSeries.text()).strip() != "": + series_name = unicode(self.leSeries.text()).strip() else: QtGui.QMessageBox.information(self, self.tr("Online Search"), self.tr("Need to enter a series name to search.")) return diff --git a/volumeselectionwindow.py b/volumeselectionwindow.py index 32bfa4f..67f7e57 100644 --- a/volumeselectionwindow.py +++ b/volumeselectionwindow.py @@ -219,7 +219,7 @@ class VolumeSelectionWindow(QtGui.QDialog): for record in self.cv_search_results: if record['id'] == self.volume_id: title = record['name'] - title += " (" + str(record['start_year']) + ")" + title += " (" + unicode(record['start_year']) + ")" title += " - " break @@ -288,8 +288,8 @@ class VolumeSelectionWindow(QtGui.QDialog): for record in self.cv_search_results: self.twList.insertRow(row) - item_text = record['name'] - item = QtGui.QTableWidgetItem(item_text) + item_text = record['name'] + item = QtGui.QTableWidgetItem( item_text ) item.setData( QtCore.Qt.UserRole ,record['id']) item.setFlags(QtCore.Qt.ItemIsSelectable| QtCore.Qt.ItemIsEnabled) self.twList.setItem(row, 0, item)