def __init__(self, path, folder): QtCore.QThread.__init__(self) self.path = path self.folder = folder self.provider = UrlProvider("http://boardgamegeek.com/boardgame/%s") self.plugin = PluginBoardGameGeek() self.stopworking = False
class CSVWorker(QtCore.QThread): """ CSVWorker --------- This worker reads a csv and import the files to the collection. Singals ....... ----------- ------------------------------------------ csvreaded notifies the complete read of the csv error launched when an error happens filecreated emited every time a file is created done emited when the worker has done their job ----------- ------------------------------------------ """ csvreaded = QtCore.pyqtSignal(int) error = QtCore.pyqtSignal(str) filecreated = QtCore.pyqtSignal(int) bgg_csv_schema = [ 'objectname', 'objectid', 'rating', 'numplays', 'weight', 'own', 'fortrade', 'want', 'wanttobuy', 'wanttoplay', 'prevowned', 'preordered', 'wishlist', 'wishlistpriority', 'wishlistcomment', 'comment', 'conditiontext', 'haspartslist', 'wantpartslist', 'collid', 'baverage', 'average', 'avgweight', 'rank', 'numowned', 'objecttype', 'originalname', 'minplayers', 'maxplayers', 'playingtime', 'yearpublished', 'bggrecplayers', 'bggbestplayers', 'bggrecagerange', 'bgglanguagedependence', 'publisherid', 'imageid', 'year', 'language', 'other', 'pricepaid', 'pp_currency', 'currvalue', 'cv_currency', 'acquisitiondate', 'acquiredfrom', 'quantity', 'privatecomment'] def __init__(self, path, folder): QtCore.QThread.__init__(self) self.path = path self.folder = folder self.provider = UrlProvider("http://boardgamegeek.com/boardgame/%s") self.plugin = PluginBoardGameGeek() self.stopworking = False def run(self): self.stopworking = False #Open file reader = csv.reader(open(self.path)) # Check header and read rows (each row is a collection file) if not self.check_first_row(reader): self.error.emit(self.tr("The CSV isn't from Boardgamegeek")) return files = [] count = 0 # We need the count of rows for the dialog progress for row in reader: files.append(row) count += 1 # Emit end of the first step (read file) self.csvreaded.emit(count) # Add the collections files to the deseired collection i = 1 id = self.bgg_csv_schema.index('objectid') man = Collector.get_instance() for item in files: html = self.provider.get(item[id]) try: data = self.plugin.file_filter(html) data['originalname'] = data['title'] data['title'] = unicode(item[0], 'utf-8') # TODO collection id must be a parameter man.add(data, 'boardgames', 'PluginCsvImport') # TODO add user values and csv-only values # from PyQt4.Qt import qDebug; qDebug(unicode(data)) except Exception as e: logging.exception(e) self.provider.flush() self.filecreated.emit(i) if self.stopworking: return i += 1 def check_first_row(self, reader): """Checks that the first row of the CSV matches with a BGG CSV""" row = reader.next() return row == self.bgg_csv_schema