def test_remap(self): """Test for the remap function""" data = {'aspect': 'yellow', 'underline': 'false', 'unknow': 'disapear'} mapping = {'aspect': 'color', 'underline': 'bold'} control = {'color': 'yellow', 'bold': 'false'} newdata = Collector.remap(data, mapping) self.assertEquals(newdata, control)
def docomplete(self, data): """Completes the file with the new data""" self.progress.setLabelText(self.tr("Autocomplete running (Step 3/3)")) # self.progress.setCancelButton(0) if isinstance(data, list): # TODO allow multiple values if len(data) == 0: self.progress.hide() return data = data[0] if not isinstance(data, dict): self.progress.hide() raise ValueError("Expected dict found *%s*", type(data)) Collector.get_instance().complete(self.collection.get_id(), self.obj["id"], data) self.progress.hide() self.parent().display_view("fitxa", params={"item": self.obj["id"], "collection": self.collection.get_id()})
def saveandclose(self): """Saves unsaved data and closes""" config = Collector.get_instance().get_manager('config') config.set('lang', self.codes[self.lang_combo.currentIndex()]) config.set('copy', self.copy_dict[self.copy_combo.currentText()]) config.save() self.close()
def __init__(self, parent, flags=None): """ Creates a new dialog to render the application preferences""" if flags is None: flags = QtCore.Qt.WindowFlags(0) super(Ui_Preferences, self).__init__(parent, flags) self.manager = Collector.get_instance().get_manager("plugin") self.current_lang = None self.codes = [":system:", 'ca_ES', 'en_UK', 'es_ES'] self.setupUi()
def setupUi(self): """Creates the ui elements for the preferences. This function overrides the Ui_Form function creating thinks that aren't easy to do with the QT Designer""" super(Ui_Preferences, self).setupUi(self) self.refresh() self.connect( self.buttonBox, QtCore.SIGNAL(_fromUtf8("accepted()")), self.saveandclose) self.connect( self.b_disable, QtCore.SIGNAL(_fromUtf8("clicked()")), self.disable) self.connect( self.b_enable, QtCore.SIGNAL(_fromUtf8("clicked()")), self.enable) # Language selector self.current_lang = Collector.get_instance().conf('lang') if not self.current_lang is None: index = 1 try: index = self.codes.index(str(self.current_lang)) except ValueError: index = 1 self.lang_combo.setCurrentIndex(index) # Copy file self.copy = Collector.get_instance().conf('copy') self.copy_dict = { self.tr("Always"): "always", self.tr("Never"): "never", self.tr("Remote only"): "http" } index = 0 for i in self.copy_dict.items(): self.copy_combo.addItem(i[0]) if i[1] == self.copy: self.copy_combo.setCurrentIndex(index) index += 1 self.home.setText(_fromUtf8(Collector.get_instance().conf('home')))
def __init__(self, query, results, parent, collection=None, flags=None): """ Creates a new search view""" if flags is None: flags = QtCore.Qt.WindowFlags(0) super(Ui_Search, self).__init__(parent, flags) self.results = [] self.collection = None collections = Collector.get_instance().get_manager('collection') if collection is not None: self.collection = collections.get_collection(collection) self.pretty = self.collection.schema.default self.query = query if results is None: results = [] self.setupUi(query, results)
def run(self): """The run function""" collector = Collector.get_instance() results = [] while not self.queue.empty(): i = self.queue.get() try: file_ = collector.get_plugin_file( i['id'], i['plugin'], ) results.append(file_) except Exception as e: logging.exception(e) self.queue.task_done() # q.join() self.complete.emit(results)
def run(self): collector = Collector.get_instance() try: results = collector.get_plugin_file( self.uri, self.plugin_id ) self.load_complete.emit(WorkerResult(STATUS_OK, results)) except Exception as e: logging.exception(e) self.load_complete.emit( WorkerResult( STATUS_ERROR, msg="Plugin %s file load failed with uri %s" % (self.plugin_id, self.uri) ) )
def __init__(self, parent=None): super(CSVFileSelector, self).__init__() self.file = None self.collection = None # Creating ui elements self.resize(400, 135) self.setWindowTitle(self.tr("Select a CSV")) self.setObjectName(QStringU8("CSVFileSelector")) self.mainlayout = QtGui.QHBoxLayout(self) self.icon = QtGui.QLabel() self.icon.setPixmap(QtGui.QPixmap(':/csvbgg.png')) self.mainlayout.addWidget(self.icon) self.layout = QtGui.QVBoxLayout() self.label = QtGui.QLabel(self.tr( QStringU8("Select the CSV file to import:"))) self.layout.addWidget(self.label) self.file_selector = FileSelector(self, 'CSV (*.csv)') self.layout.addWidget(self.file_selector) self.label2 = QtGui.QLabel(self.tr( QStringU8("To the folder:"))) self.layout.addWidget(self.label2) self.collections = QtGui.QComboBox() self.layout.addWidget(self.collections) self.mainlayout.addLayout(self.layout) self.options = QtGui.QDialogButtonBox(self) self.options.setOrientation(QtCore.Qt.Horizontal) self.options.setStandardButtons(QtGui.QDialogButtonBox.Cancel | QtGui.QDialogButtonBox.Ok) self.options.setObjectName(QStringU8("options")) self.layout.addWidget(self.options) # Add collections to the combo box man = Collector.get_instance().get_manager('collection') for i in man.collections.values(): self.collections.addItem(i.get_name(), i.get_id()) # Connect things self.options.rejected.connect(self.reject) self.options.accepted.connect(self.accept)
def __init__(self, argv, hidden=False): super(CollectorApplication, self).__init__(argv) # Create and display the splash screen if not hidden: self.splash = SplashScreen() self.splash.show() self.processEvents() self.view = None self.uri = None self.home = None if argv is not None: self.parse_args(argv) # Launch collector self.collector = Collector(self.home) self.load_translations(":/lang") # Create the main window # FIXME: Some view variables are language dependent, # is needed to import them after load the language settings from mainwindow import MainWindow self.main = MainWindow() self.main.views = CollectorApplication.get_views(self.main) self.main.display_view('dashboard') if not hidden: # Show main window self.main.show() # Hide splash self.splash.finish(self.main) # Bring window to front if development if 'COLLECTION_PATH' in os.environ: self.main.raise_() if self.view is not None: self.main.display_view(self.view) elif self.uri is not None: self.main.collector_uri_call(self.uri)
def add_to_my_collection(self): """Add the current plugin file to the user collection""" if self.data is not None: self.progress = QtGui.QProgressDialog( self.tr("Saving"), QtCore.QString(), 0, 0) self.progress.show() collector = Collector.get_instance() result = collector.add( self.data, self.collection, use_mapping=self.plugin.get_id()) self.progress.hide() if result is not None: # Ok case # TODO ask the user if want's to see the # added file or not id_ = result.id self.parent().display_view( 'fitxa', {'item': id_, 'collection': self.collection} ) else: # Error case QtGui.QMessageBox.warning( self, self.tr("Collector"), self.tr("Ooops, an error ocurred" + " and no data couldn't be added.")) else: # Trying to add not ready content. QtGui.QMessageBox.warning( self, self.tr("Collector"), self.tr("The content isn't yet available," + " and no data couldn't be added."))
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 run(self): collector = Collector.get_instance() plugins = collector.get_manager('plugin').filter(PluginCollector) logging.debug("Discover using: " + str(plugins)) # Call discover for all the plugins all_results = [] for plugin in plugins: try: results = collector.discover(self.params['query'], plugin) self.partialResult.emit(results) all_results.extend(results) except Exception as e: logging.debug(e) self.searchComplete.emit( WorkerResult( STATUS_ERROR, msg="Plugin %s has failed" % plugin ) ) # TODO continue when a plugin has failed return # Launch the complete self.searchComplete.emit(WorkerResult(STATUS_OK, all_results))
def test_to_single_single(self): """Test the multivalue to single conversion, case single value""" result = Collector.to_single('a') assert result == 'a'
class CollectorApplication(QtGui.QApplication): """The GUI Application for Collector""" translators = {} current = None collector = None def __init__(self, argv, hidden=False): super(CollectorApplication, self).__init__(argv) # Create and display the splash screen if not hidden: self.splash = SplashScreen() self.splash.show() self.processEvents() self.view = None self.uri = None self.home = None if argv is not None: self.parse_args(argv) # Launch collector self.collector = Collector(self.home) self.load_translations(":/lang") # Create the main window # FIXME: Some view variables are language dependent, # is needed to import them after load the language settings from mainwindow import MainWindow self.main = MainWindow() self.main.views = CollectorApplication.get_views(self.main) self.main.display_view('dashboard') if not hidden: # Show main window self.main.show() # Hide splash self.splash.finish(self.main) # Bring window to front if development if 'COLLECTION_PATH' in os.environ: self.main.raise_() if self.view is not None: self.main.display_view(self.view) elif self.uri is not None: self.main.collector_uri_call(self.uri) def parse_args(self, argv): """Parse argv, the input arguments""" if '--view' in argv: self.view = argv[argv.index('--view') + 1] elif '--uri' in argv: self.uri = argv[argv.index('--uri') + 1] if '--home' in argv: self.home = self._checkhome(argv[argv.index('--home') + 1]) elif '-h' in argv: self.home = self._checkhome(argv[argv.index('-h') + 1]) def _checkhome(self, value): """Checks the home value""" truehome = os.path.realpath(value) if truehome is None: raise Exception("Wrong home folder") self.quit() return truehome # The language code selector is from: # "switch translations dynamically in a PyQt4 application" # # PyQt version by Hans-Peter Jansen <*****@*****.**> def load_translations(self, folder): """Loads the transaltions from the parameter folder, the translations must match the pattern *_*.qm""" if not isinstance(folder, QtCore.QDir): folder = QtCore.QDir(folder) pattern = "*_*.qm" # <language>_<country>.qm filters = QtCore.QDir.Files | QtCore.QDir.Readable sort = QtCore.QDir.SortFlags(QtCore.QDir.Name) for lang_file in folder.entryInfoList([pattern], filters, sort): # pick country and language out of the file name language, country = lang_file.baseName().split("_", 1) language = language.toLower() country = country.toUpper() locale = language + "_" + country # only load translation, if it does not exist already if not locale in CollectorApplication.translators: # create and load translator translator = QtCore.QTranslator(self.instance()) if translator.load(lang_file.absoluteFilePath()): CollectorApplication.translators[locale] = translator system = QtCore.QLocale.system() # Get user language from collector.conf user_language = self.collector.conf('lang') # if user_language is ':system:' or is empty, use the system language if user_language in [':system:', '']: user_language = system.name() # Look if the user_language exists, then set as current language for lang in CollectorApplication.available_languages(): if str(lang) == user_language: # language match the current system CollectorApplication.set_language(lang) @staticmethod def available_languages(): """ Returns the avaible languages (code_country) as a list""" return sorted(CollectorApplication.translators.keys()) @staticmethod def set_language(locale): """ Sets the language of the application using the deseired locale""" app = CollectorApplication if app.current: app.removeTranslator(app.current) app.current = app.translators.get(locale, None) if app.current is not None: app.installTranslator(app.current) qt_qm = app.translators.get('qt' + locale[2:], None) if qt_qm is not None: app.installTranslator(qt_qm) @staticmethod def get_views(parent): """ Initialize the avaible views, each view is loaded by a provider""" return { 'dashboard': DashboardView(parent), 'fitxa': FitxaView(parent), 'edit': FitxaEditView(parent), 'collection': CollectionView(parent), 'add': FitxaNewView(parent), 'search': SearchView(parent), 'discover': DiscoverView(parent), 'preferences': PreferencesView(parent), 'quicksearch': SearchDialog(parent), 'properties': PropertiesView(parent), 'pluginfile': PluginFileView(parent), 'filters': AdvancedSearch(parent), 'import': ImportView(parent), 'export': ExportView(parent), }
def run(self): self.error = None collector = Collector.get_instance() self.results = collector.filter(self.params['collection'], [self.params['filter']]) self.searchComplete.emit(WorkerResult(STATUS_OK, self.results))
def test_to_single_from_multivalue(self): """Test the multivalue to single conversion, case multivalue value""" result = Collector.to_single(['a', 'b', 'c']) assert result == 'a'