def remove_callback(locations): loc = locations[0] s = '%s: %s' % (loc.__class__.__name__, str(loc)) if len(loc.plants) > 0: msg = _('Please remove the plants from <b>%(location)s</b> ' 'before deleting it.') % { 'location': loc } utils.message_dialog(msg, gtk.MESSAGE_WARNING) return msg = _("Are you sure you want to remove %s?") % \ utils.xml_safe(s) if not utils.yes_no_dialog(msg): return try: session = db.Session() obj = session.query(Location).get(loc.id) session.delete(obj) session.commit() except Exception, e: msg = _('Could not delete.\n\n%s') % utils.xml_safe(e) utils.message_details_dialog(msg, traceback.format_exc(), type=gtk.MESSAGE_ERROR)
def run(self): ssn = db.Session() value, = ssn.execute(self.query).first() idle_add(lambda x: self.widget.set_text(str(x)), value) ssn.close()
def update(self): ''' ''' logger.debug('SplashInfoBox::update') statusbar = bauble.gui.widgets.statusbar sbcontext_id = statusbar.get_context_id('searchview.nresults') statusbar.pop(sbcontext_id) bauble.gui.widgets.main_comboentry.child.set_text('') ssn = db.Session() q = ssn.query(bauble.meta.BaubleMeta) q = q.filter(bauble.meta.BaubleMeta.name.startswith(u'stqr_')) name_tooltip_query = dict( (int(i.name[5:]), (i.value.split(':', 2))) for i in q.all()) ssn.close() for i in range(1, 11): wname = "stqr_%02d_button" % i widget = getattr(self.widgets, wname) name, tooltip, query = name_tooltip_query.get( i, (_('<empty>'), '', '')) widget.set_label(name) widget.set_tooltip_text(tooltip) self.name_tooltip_query = name_tooltip_query # LabelUpdater objects **can** run in a thread. if 'GardenPlugin' in pluginmgr.plugins: LabelUpdater(self.widgets.splash_nplttot, "select count(*) from plant").run() self.start_thread( LabelUpdater(self.widgets.splash_npltuse, "select count(*) from plant where quantity>0")) self.start_thread( LabelUpdater(self.widgets.splash_npltnot, "select count(*) from plant where quantity=0")) LabelUpdater(self.widgets.splash_nacctot, "select count(*) from accession").run() self.start_thread( LabelUpdater( self.widgets.splash_naccuse, "select count(distinct accession.id) " "from accession " "join plant on plant.accession_id=accession.id " "where plant.quantity>0")) self.start_thread( LabelUpdater( self.widgets.splash_naccnot, "select count(id) " "from accession " "where id not in " "(select accession_id from plant " " where plant.quantity>0)")) LabelUpdater(self.widgets.splash_nloctot, "select count(*) from location").run() self.start_thread( LabelUpdater( self.widgets.splash_nlocuse, "select count(distinct location.id) " "from location " "join plant on plant.location_id=location.id " "where plant.quantity>0")) self.start_thread( LabelUpdater( self.widgets.splash_nlocnot, "select count(id) " "from location " "where id not in " "(select location_id from plant " " where plant.quantity>0)")) self.start_thread( LabelUpdater( self.widgets.splash_nspcuse, "select count(distinct species.id) " "from species join accession " "on accession.species_id=species.id")) self.start_thread( LabelUpdater( self.widgets.splash_ngenuse, "select count(distinct species.genus_id) " "from species join accession " "on accession.species_id=species.id")) self.start_thread( LabelUpdater( self.widgets.splash_nfamuse, "select count(distinct genus.family_id) from genus " "join species on species.genus_id=genus.id " "join accession on accession.species_id=species.id ")) LabelUpdater(self.widgets.splash_nspctot, "select count(*) from species").run() LabelUpdater(self.widgets.splash_ngentot, "select count(*) from genus").run() LabelUpdater(self.widgets.splash_nfamtot, "select count(*) from family").run() self.start_thread( LabelUpdater( self.widgets.splash_nspcnot, "select count(id) from species " "where id not in " "(select distinct species.id " " from species join accession " " on accession.species_id=species.id)")) self.start_thread( LabelUpdater( self.widgets.splash_ngennot, "select count(id) from genus " "where id not in " "(select distinct species.genus_id " " from species join accession " " on accession.species_id=species.id)")) self.start_thread( LabelUpdater( self.widgets.splash_nfamnot, "select count(id) from family " "where id not in " "(select distinct genus.family_id from genus " "join species on species.genus_id=genus.id " "join accession on accession.species_id=species.id)"))
def format(objs, **kwargs): # debug('format(%s)' % kwargs) stylesheet = kwargs['stylesheet'] authors = kwargs['authors'] renderer = kwargs['renderer'] source_type = kwargs['source_type'] use_private = kwargs['private'] error_msg = None if not stylesheet: error_msg = _('Please select a stylesheet.') elif not renderer: error_msg = _('Please select a a renderer') if error_msg is not None: utils.message_dialog(error_msg, gtk.MESSAGE_WARNING) return False fo_cmd = renderers_map[renderer] exe = fo_cmd.split(' ')[0] if not on_path(exe): utils.message_dialog( _('Could not find the command "%(exe)s" to ' 'start the %(renderer_name)s ' 'renderer.') % ({ 'exe': exe, 'renderer_name': renderer }), gtk.MESSAGE_ERROR) return False session = db.Session() # convert objects to ABCDAdapters depending on source type for # passing to create_abcd adapted = [] if source_type == plant_source_type: plants = sorted(get_plants_pertinent_to(objs, session=session), key=utils.natsort_key) if len(plants) == 0: utils.message_dialog( _('There are no plants in the search ' 'results. Please try another search.')) return False for p in plants: if use_private: adapted.append(PlantABCDAdapter(p, for_labels=True)) elif not p.accession.private: adapted.append(PlantABCDAdapter(p, for_labels=True)) elif source_type == species_source_type: species = sorted(get_species_pertinent_to(objs, session=session), key=utils.natsort_key) if len(species) == 0: utils.message_dialog( _('There are no species in the search ' 'results. Please try another search.')) return False for s in species: adapted.append(SpeciesABCDAdapter(s, for_labels=True)) elif source_type == accession_source_type: accessions = sorted(get_accessions_pertinent_to(objs, session=session), key=utils.natsort_key) if len(accessions) == 0: utils.message_dialog( _('There are no accessions in the search ' 'results. Please try another search.')) return False for a in accessions: if use_private: adapted.append(AccessionABCDAdapter(a, for_labels=True)) elif not a.private: adapted.append(AccessionABCDAdapter(a, for_labels=True)) else: raise NotImplementedError('unknown source type') if len(adapted) == 0: # nothing adapted....possibly everything was private # TODO: if everything was private and that is really why we got # here then it is probably better to show a dialog with a message # and raise and exception which appears as an error raise Exception('No objects could be adapted to ABCD units.') abcd_data = create_abcd(adapted, authors=authors, validate=False) session.close() logger.debug(etree.dump(abcd_data.getroot())) # create xsl fo file dummy, fo_filename = tempfile.mkstemp() style_etree = etree.parse(stylesheet) transform = etree.XSLT(style_etree) result = transform(abcd_data) fo_outfile = open(fo_filename, 'w') fo_outfile.write(str(result)) fo_outfile.close() dummy, filename = tempfile.mkstemp() filename = '%s.pdf' % filename # TODO: checkout pyexpect for spawning processes # run the report to produce the pdf file, the command has to be # on the path for this to work fo_cmd = fo_cmd % ({ 'fo_filename': fo_filename, 'out_filename': filename }) logger.debug(fo_cmd) # TODO: use popen to get output os.system(fo_cmd) logger.debug(filename) if not os.path.exists(filename): utils.message_dialog( _('Error creating the PDF file. Please ' 'ensure that your PDF formatter is ' 'properly installed.'), gtk.MESSAGE_ERROR) return False else: try: desktop.open(filename) except OSError: utils.message_dialog( _('Could not open the report with the ' 'default program. You can open the ' 'file manually at %s') % filename) return True
def update(self, row): ''' update the expander :param row: the row to get the values from ''' self.current_obj = row # TODO: how do we put the genus in a seperate label so it # can be clickable but still respect the text wrap to wrap # around and indent from the genus name instead of from the # species name session = db.Session() self.widget_set_value('sp_name_data', '<big>%s</big>' % row.markup(True), markup=True) awards = '' if row.awards: awards = utils.utf8(row.awards) self.widget_set_value('sp_awards_data', awards) logger.debug('setting cites data from row %s' % row) cites = '' if row.cites: cites = utils.utf8(row.cites) self.widget_set_value('sp_cites_data', cites) # zone = '' # if row.hardiness_zone: # awards = utils.utf8(row.hardiness_zone) # self.widget_set_value('sp_hardiness_data', zone) habit = '' if row.habit: habit = utils.utf8(row.habit) self.widget_set_value('sp_habit_data', habit) dist = '' if row.distribution: dist = utils.utf8(row.distribution_str()) self.widget_set_value('sp_dist_data', dist) dist = '' if row.label_distribution: dist = row.label_distribution self.widget_set_value('sp_labeldist_data', dist) # stop here if not GardenPluin if 'GardenPlugin' not in pluginmgr.plugins: return from bauble.plugins.garden.accession import Accession from bauble.plugins.garden.plant import Plant nacc = session.query(Accession).join('species').\ filter_by(id=row.id).count() self.widget_set_value('sp_nacc_data', nacc) nplants = session.query(Plant).join('accession', 'species').\ filter_by(id=row.id).count() if nplants == 0: self.widget_set_value('sp_nplants_data', nplants) else: nacc_in_plants = session.query(Plant.accession_id).\ join('accession', 'species').\ filter_by(id=row.id).distinct().count() self.widget_set_value( 'sp_nplants_data', '%s in %s accessions' % (nplants, nacc_in_plants)) session.close()
return plants.Species.retrieve_or_create(ssn, { 'object': 'taxon', 'rank': 'species', 'ht-epithet': gen_epithet, 'epithet': sp_epithet, 'ht-rank': 'genus' }, create=create) def species_to_add(s): return species_to_fix(s, create=True) db.open("sqlite:////home/mario/.bauble/cuchubo-corrected.db") ssn = db.Session() import codecs with codecs.open("/tmp/complete.csv", 'r', 'utf16') as f: keys = f.readline().strip().split('\t') for l in f.readlines(): l = l.strip() values = [i.strip() for i in l.split("\t")] fields = dict(zip(keys, values)) print fields['Name_submitted'], fields['Name_matched'] obj = species_to_fix(fields['Name_submitted']) if obj is None: print fields['Name_submitted'] continue gen_epithet, sp_epithet = fields['Name_matched'].split(' ')
def setUp(self): assert uri is not None, "The database URI is not set" init_bauble(uri) self.session = db.Session()