示例#1
0
    def dataset(self, req):
        ds = Dataset('%s-%s-%s' % (
            req.dataset.id, self.obj.__class__.__name__.lower(), self.obj.id))
        cols = self.columns(req)
        ds.fields = tuple(col['name'] if isinstance(col, dict) else col for col in cols)
        ds.table.schema.aboutUrl = url_template(req, 'value', 'ID')

        for col in cols:
            if isinstance(col, dict):
                name = col.pop('name')
                for attr, value in col.items():
                    setattr(ds.table.schema.columns[name], attr, value)

        ds.metadata['dc:bibliographicCitation '] = text_citation(req, self.obj)
        ds.metadata['dc:publisher'] = '%s, %s' % (
            req.dataset.publisher_name, req.dataset.publisher_place)
        ds.metadata['dc:license'] = req.dataset.license
        ds.metadata['dc:issued'] = req.dataset.published.isoformat()
        ds.metadata['dc:title'] = self.obj.name
        ds.metadata['dc:creator'] = self.obj.formatted_contributors()
        ds.metadata['dc:identifier'] = req.resource_url(self.obj)
        ds.metadata['dc:isPartOf'] = req.resource_url(req.dataset)
        ds.metadata['dcat:accessURL'] = req.route_url('download')

        for value in self.value_query():
            refs, sources = self.refs_and_sources(req, value)
            row = self.row(req, value, refs)
            if row:
                ds.sources.add(*sources)
                ds.add_row(row)
        return ds
示例#2
0
    def test_text_citation(self):
        from clld.web.util.helpers import text_citation

        text_citation(self.env['request'], common.Contribution.first())
示例#3
0
    def create(self, req, filename=None, verbose=True, link_callback=None, lang=None):
        html = []
        lang = lang or Language.get('afr')
        entries = list(
            DBSession.query(ValueSet).join(ValueSet.parameter)
            .filter(ValueSet.language_pk == lang.pk)
            .order_by(
                Taxon.kingdom,
                Taxon.phylum,
                Taxon.class_,
                Taxon.order,
                Taxon.family,
                Parameter.name)
            .options(contains_eager(ValueSet.parameter), joinedload(ValueSet.values)))

        for kingdom, taxa1 in groupby(entries, key=lambda vs: vs.parameter.kingdom):
            html.append('<h2>Kingdom: %s</h2>' % (kingdom or 'other'))
            for phylum, taxa2 in groupby(taxa1, key=lambda vs: vs.parameter.phylum):
                html.append('<h3>Phylum: %s</h3>' % (phylum or 'other'))
                for class_, taxa3 in groupby(taxa2, key=lambda vs: vs.parameter.class_):
                    html.append('<h4>Class: %s</h4>' % (class_ or 'other'))
                    for order, taxa4 in groupby(taxa3, key=lambda vs: vs.parameter.order):
                        html.append('<h5>Order: %s</h5>' % (order or 'other'))
                        for family, taxa5 in groupby(taxa4, key=lambda vs: vs.parameter.family):
                            html.append('<h6>Family: %s</h6>' % (family or 'other'))
                            for entry in taxa5:
                                adapter = get_adapter(
                                    IRepresentation, entry, req, ext='snippet.html')
                                html.append(adapter.render(entry, req))
                                html.append('<p class="separator">&nbsp;<p>')

        with open(str(download_path('%s.pdf' % lang.id)), 'wb') as fp:
            editors = ''
            if lang.contribution.contributor_assocs:
                editors = 'edited by ' + ' and '.join(
                    c.last_first() for c in lang.contribution.primary_contributors)
            pisa.CreatePDF(
                html_tmpl % (
                    css_tmpl.format(charis_font_spec_css()),
                    req.resource_url(req.dataset),
                    """
<h1 style="text-align: center; font-size: 12mm;">%(language)s names for Plants and Animals</h1>
<h2 style="text-align: center; font-size: 8mm;">%(editors)s</h2>
<p style="font-size: 5mm;">
This document was created from <a href="%(url)s">%(dataset)s</a> on %(date)s.
</p>
<p style="font-size: 5mm;">
%(dataset)s is published under a %(license)s and should be cited as
</p>
<blockquote style="font-size: 5mm;"><i>%(citation)s</i></blockquote>
<p style="font-size: 5mm;">
A full list of contributors is available at
<a href="%(url)scontributors">%(url)scontributors</a>
</p>
<p style="font-size: 5mm;">
The list of references cited in this document is available at
<a href="%(url)ssources">%(url)ssources</a>
</p>
""" % dict(
                    language=lang.name,
                    editors=editors,
                    dataset=req.dataset.name,
                    url=req.resource_url(req.dataset),
                    date=date.today(),
                    citation=text_citation(req, req.dataset),
                    license=req.dataset.jsondata['license_name']),
                    ''.join(html)),
                dest=fp,
                link_callback=link_callback,
            )
示例#4
0
    def test_text_citation(self):
        from clld.web.util.helpers import text_citation

        text_citation(self.env["request"], common.Contribution.first())
示例#5
0
文件: cldf.py 项目: clld/clld
    def create(self, req, filename=None, verbose=True, outfile=None):
        cldf_cfg = req.registry.getUtility(ICldfConfig)

        with TemporaryDirectory() as tmpd:
            cls = getattr(dataset, cldf_cfg.module)
            ds = cls.in_dir(tmpd)
            ds.properties['dc:bibliographicCitation '] = text_citation(req, req.dataset)
            ds.properties['dc:publisher'] = '%s, %s' % (
                req.dataset.publisher_name, req.dataset.publisher_place)
            ds.properties['dc:license'] = req.dataset.license
            ds.properties['dc:issued'] = req.dataset.published.isoformat()
            ds.properties['dc:title'] = req.dataset.name
            ds.properties['dc:creator'] = req.dataset.formatted_editors()
            ds.properties['dc:identifier'] = req.resource_url(req.dataset)
            ds.properties['dcat:accessURL'] = req.route_url('download')
            if DBSession.query(Sentence).count():
                ds.add_component('ExampleTable')
            if DBSession.query(DomainElement).count():
                ds.add_component('CodeTable', {'name': 'Number', 'datatype': 'integer'})
            ds.add_component('ParameterTable')
            ds.add_component('LanguageTable')
            ds.add_table('contributions.csv', 'ID', 'Name', 'Description', 'Contributors')
            ds.add_columns(ds.primary_table, Column.fromvalue(
                {
                    'name': 'Contribution_ID',
                    'datatype': 'string',
                    'valueUrl': url_template(req, 'contribution', 'contribution').uri,
                }))
            ds.add_foreign_key(
                ds.primary_table, 'Contribution_ID', 'contributions.csv', 'ID')
            ds['LanguageTable'].aboutUrl = url_template(req, 'language', 'ID')
            ds['ParameterTable'].aboutUrl = url_template(req, 'parameter', 'ID')
            ds[ds.primary_table].aboutUrl = url_template(req, 'value', 'ID')

            cldf_cfg.custom_schema(req, ds)

            for src in cldf_cfg.query(Source):
                ds.sources.add(cldf_cfg.convert(Source, src, req))
            fname = outfile or self.abspath(req)

            transaction.abort()

            tabledata = defaultdict(list)
            for table, model in [
                ('ParameterTable', Parameter),
                ('CodeTable', DomainElement),
                ('LanguageTable', Language),
                ('ExampleTable', Sentence),
                ('contributions.csv', Contribution),
                (ds.primary_table, Value),
            ]:
                if verbose:
                    print('exporting {0} ...'.format(model))
                transaction.begin()
                for item in cldf_cfg.query(model):
                    tabledata[table].append(cldf_cfg.convert(model, item, req))
                transaction.abort()
                if verbose:
                    print('... done')

            transaction.begin()
            ds.write(**cldf_cfg.custom_tabledata(req, tabledata))
            ds.validate()

            shutil.make_archive(
                fname.parent.joinpath(fname.stem).as_posix(), 'zip', tmpd.as_posix())
示例#6
0
    def create(self, req, filename=None, verbose=True, outfile=None):
        cldf_cfg = req.registry.getUtility(ICldfConfig)

        with TemporaryDirectory() as tmpd:
            cls = getattr(dataset, cldf_cfg.module)
            ds = cls.in_dir(tmpd)
            ds.properties['dc:bibliographicCitation'] = text_citation(
                req, req.dataset)
            ds.properties['dc:publisher'] = '%s, %s' % (
                req.dataset.publisher_name, req.dataset.publisher_place)
            ds.properties['dc:license'] = req.dataset.license
            ds.properties['dc:issued'] = req.dataset.published.isoformat()
            ds.properties['dc:title'] = req.dataset.name
            ds.properties['dc:creator'] = req.dataset.formatted_editors()
            ds.properties['dc:identifier'] = req.resource_url(req.dataset)
            ds.properties['dcat:accessURL'] = req.route_url('download')
            if DBSession.query(Sentence).count():
                ds.add_component('ExampleTable')
            if DBSession.query(DomainElement).count():
                ds.add_component('CodeTable', {
                    'name': 'Number',
                    'datatype': 'integer'
                })
            ds.add_component('ParameterTable')
            ds.add_component('LanguageTable')
            ds.add_table('contributions.csv', 'ID', 'Name', 'Description',
                         'Contributors')
            ds.add_columns(
                ds.primary_table,
                Column.fromvalue({
                    'name':
                    'Contribution_ID',
                    'datatype':
                    'string',
                    'valueUrl':
                    url_template(req, 'contribution', 'contribution').uri,
                }))
            ds.add_foreign_key(ds.primary_table, 'Contribution_ID',
                               'contributions.csv', 'ID')
            ds['LanguageTable'].aboutUrl = url_template(req, 'language', 'ID')
            ds['ParameterTable'].aboutUrl = url_template(
                req, 'parameter', 'ID')
            ds[ds.primary_table].aboutUrl = url_template(req, 'value', 'ID')

            cldf_cfg.custom_schema(req, ds)

            for src in cldf_cfg.query(Source):
                ds.sources.add(cldf_cfg.convert(Source, src, req))
            fname = outfile or self.abspath(req)

            transaction.abort()

            tabledata = defaultdict(list)
            for table, model in [
                ('ParameterTable', Parameter),
                ('CodeTable', DomainElement),
                ('LanguageTable', Language),
                ('ExampleTable', Sentence),
                ('contributions.csv', Contribution),
                (ds.primary_table, Value),
            ]:
                if verbose:
                    print('exporting {0} ...'.format(model))
                transaction.begin()
                for item in cldf_cfg.query(model):
                    tabledata[table].append(cldf_cfg.convert(model, item, req))
                transaction.abort()
                if verbose:
                    print('... done')

            transaction.begin()
            ds.write(**cldf_cfg.custom_tabledata(req, tabledata))
            ds.validate()

            shutil.make_archive(str(fname.parent / fname.stem), 'zip',
                                str(tmpd))
示例#7
0
    def create(self,
               req,
               filename=None,
               verbose=True,
               link_callback=None,
               lang=None):
        html = []
        lang = lang or Language.get('afr')
        entries = list(
            DBSession.query(ValueSet).join(ValueSet.parameter).filter(
                ValueSet.language_pk == lang.pk).order_by(
                    Taxon.kingdom, Taxon.phylum, Taxon.class_, Taxon.order,
                    Taxon.family,
                    Parameter.name).options(contains_eager(ValueSet.parameter),
                                            joinedload(ValueSet.values)))

        for kingdom, taxa1 in groupby(entries,
                                      key=lambda vs: vs.parameter.kingdom):
            html.append('<h2>Kingdom: %s</h2>' % (kingdom or 'other'))
            for phylum, taxa2 in groupby(taxa1,
                                         key=lambda vs: vs.parameter.phylum):
                html.append('<h3>Phylum: %s</h3>' % (phylum or 'other'))
                for class_, taxa3 in groupby(
                        taxa2, key=lambda vs: vs.parameter.class_):
                    html.append('<h4>Class: %s</h4>' % (class_ or 'other'))
                    for order, taxa4 in groupby(
                            taxa3, key=lambda vs: vs.parameter.order):
                        html.append('<h5>Order: %s</h5>' % (order or 'other'))
                        for family, taxa5 in groupby(
                                taxa4, key=lambda vs: vs.parameter.family):
                            html.append('<h6>Family: %s</h6>' %
                                        (family or 'other'))
                            for entry in taxa5:
                                adapter = get_adapter(IRepresentation,
                                                      entry,
                                                      req,
                                                      ext='snippet.html')
                                html.append(adapter.render(entry, req))
                                html.append('<p class="separator">&nbsp;<p>')

        with open(str(download_path('%s.pdf' % lang.id)), 'wb') as fp:
            editors = ''
            if lang.contribution.contributor_assocs:
                editors = 'edited by ' + ' and '.join(
                    c.last_first()
                    for c in lang.contribution.primary_contributors)
            pisa.CreatePDF(
                html_tmpl % (css_tmpl.format(
                    charis_font_spec_css()), req.resource_url(req.dataset), """
<h1 style="text-align: center; font-size: 12mm;">%(language)s names for Plants and Animals</h1>
<h2 style="text-align: center; font-size: 8mm;">%(editors)s</h2>
<p style="font-size: 5mm;">
This document was created from <a href="%(url)s">%(dataset)s</a> on %(date)s.
</p>
<p style="font-size: 5mm;">
%(dataset)s is published under a %(license)s and should be cited as
</p>
<blockquote style="font-size: 5mm;"><i>%(citation)s</i></blockquote>
<p style="font-size: 5mm;">
A full list of contributors is available at
<a href="%(url)scontributors">%(url)scontributors</a>
</p>
<p style="font-size: 5mm;">
The list of references cited in this document is available at
<a href="%(url)ssources">%(url)ssources</a>
</p>
""" % dict(language=lang.name,
            editors=editors,
            dataset=req.dataset.name,
            url=req.resource_url(req.dataset),
            date=date.today(),
            citation=text_citation(req, req.dataset),
            license=req.dataset.jsondata['license_name']), ''.join(html)),
                dest=fp,
                link_callback=link_callback,
            )