示例#1
0
    def test_get_all_species(self):
        """
        Test getting the species from different types
        """
        get_ids = lambda objs: sorted([o.id for o in objs])

        family = self.session.query(Family).get(1)
        ids = get_ids(get_all_species([family], self.session))
        self.assert_(ids == range(1, 5), ids)

        family = self.session.query(Family).get(1)
        family2 = self.session.query(Family).get(2)
        ids = get_ids(get_all_species([family, family2], self.session))
        self.assert_(ids == range(1, 9), ids)

        genus = self.session.query(Genus).get(1)
        ids = get_ids(get_all_species([genus], self.session))
        self.assert_(ids == [1, 2], ids)

        species = self.session.query(Species).get(1)
        ids = get_ids(get_all_species([species], self.session))
        self.assert_(ids == [1], ids)

        accession = self.session.query(Accession).get(1)
        ids = get_ids(get_all_species([accession], self.session))
        self.assert_(ids == [1], ids)

        plant = self.session.query(Plant).get(1)
        ids = get_ids(get_all_species([plant], self.session))
        self.assert_(ids == [1], ids)

        location = self.session.query(Location).get(1)
        ids = get_ids(get_all_species([location], self.session))
        self.assert_(ids == [1], ids)

        vn = self.session.query(VernacularName).get(1)
        ids = get_ids(get_all_species([vn], self.session))
        self.assert_(ids == [1], ids)

        tag_objects('test', [family, genus])
        tag = self.session.query(Tag).filter_by(tag=u'test').one()
        ids = get_ids(get_all_species([tag], self.session))
        self.assert_(ids == range(1, 5), ids)

        # now test all the objects
        ids = get_ids(
            get_all_species(
                [family, genus, species, accession, plant, location],
                self.session))
        self.assert_(ids == range(1, 5), ids)
示例#2
0
    def test_get_all_species(self):
        """
        Test getting the species from different types
        """
        get_ids = lambda objs: sorted([o.id for o in objs])

        family = self.session.query(Family).get(1)
        ids = get_ids(get_all_species([family], self.session))
        self.assert_(ids == range(1, 5), ids)

        family = self.session.query(Family).get(1)
        family2 = self.session.query(Family).get(2)
        ids = get_ids(get_all_species([family, family2], self.session))
        self.assert_(ids == range(1, 9), ids)

        genus = self.session.query(Genus).get(1)
        ids = get_ids(get_all_species([genus], self.session))
        self.assert_(ids == [1, 2], ids)

        species = self.session.query(Species).get(1)
        ids = get_ids(get_all_species([species], self.session))
        self.assert_(ids == [1], ids)

        accession = self.session.query(Accession).get(1)
        ids = get_ids(get_all_species([accession], self.session))
        self.assert_(ids == [1], ids)

        plant = self.session.query(Plant).get(1)
        ids = get_ids(get_all_species([plant], self.session))
        self.assert_(ids == [1], ids)

        location = self.session.query(Location).get(1)
        ids = get_ids(get_all_species([location], self.session))
        self.assert_(ids == [1], ids)

        vn = self.session.query(VernacularName).get(1)
        ids = get_ids(get_all_species([vn], self.session))
        self.assert_(ids == [1], ids)

        tag_objects('test', [family, genus])
        tag = self.session.query(Tag).filter_by(tag=u'test').one()
        ids = get_ids(get_all_species([tag], self.session))
        self.assert_(ids == range(1, 5), ids)

        # now test all the objects
        ids = get_ids(get_all_species([family, genus, species,
                                       accession, plant, location],
                                      self.session))
        self.assert_(ids == range(1, 5), ids)
示例#3
0
    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_all_plants(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_all_species(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_all_accessions(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()

#        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})
#        print fo_cmd
#        debug(fo_cmd)
        # TODO: use popen to get output
        os.system(fo_cmd)

#        print 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
示例#4
0
    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_all_plants(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_all_species(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_all_accessions(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()

        #        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
        })
        #        print fo_cmd
        #        debug(fo_cmd)
        # TODO: use popen to get output
        os.system(fo_cmd)

        #        print 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