How to use the clld.db.models.common.Identifier function in clld

To help you get started, we’ve selected a few clld examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github clld / clld / tests / test_db_migration.py View on Github external
id='iso-csw', name='csw', type=common.IdentifierType.iso.value)
    assert migration.pk(common.Identifier, 'iso-csw') == pk
    assert len(list(migration.select(common.Identifier))) == 1

    identifier = DBSession.query(common.Identifier)\
        .options(undefer('*')).get(pk)
    assert identifier.active
    assert identifier.version == 1
    assert identifier.created
    assert identifier.updated

    migration.update(common.Identifier, [('name', 'cea')], pk=pk)
    DBSession.refresh(identifier)
    assert identifier.name == 'cea'

    migration.delete(common.Identifier, pk=pk)
    with pytest.raises(InvalidRequestError):
        DBSession.refresh(identifier)
github clld / glottolog3 / glottolog3 / scripts / loader / wikipedia.py View on Github external
pk = DBSession.execute(
        "select max(pk) from identifier").fetchone()[0] + 1

    langs = {}
    for gid, name in args.json['wikipedia'].items():
        if gid not in langs:
            langs[gid] = Languoid.get(gid)
        langs[gid].update_jsondata(wikipedia=name.split('/')[-1])

    for gid, codes in args.json['multitree'].items():
        l = langs[gid]
        lcodes = [i.name for i in l.identifiers if i.type == 'multitree']

        for code in set(codes):
            if code not in lcodes:
                identifier = DBSession.query(common.Identifier)\
                    .filter(common.Identifier.type == 'multitree')\
                    .filter(common.Identifier.name == code)\
                    .first()
                if not identifier:
                    identifier = common.Identifier(
                        pk=pk, id=str(iid), name=code, type='multitree')
                    iid += 1
                    pk += 1
                count += 1
                DBSession.add(
                    common.LanguageIdentifier(language=l, identifier=identifier))

    print count, 'new multitree identifiers'
github clld / glottolog3 / glottolog3 / scripts / loader / wikipedia.py View on Github external
if gid not in langs:
            langs[gid] = Languoid.get(gid)
        langs[gid].update_jsondata(wikipedia=name.split('/')[-1])

    for gid, codes in args.json['multitree'].items():
        l = langs[gid]
        lcodes = [i.name for i in l.identifiers if i.type == 'multitree']

        for code in set(codes):
            if code not in lcodes:
                identifier = DBSession.query(common.Identifier)\
                    .filter(common.Identifier.type == 'multitree')\
                    .filter(common.Identifier.name == code)\
                    .first()
                if not identifier:
                    identifier = common.Identifier(
                        pk=pk, id=str(iid), name=code, type='multitree')
                    iid += 1
                    pk += 1
                count += 1
                DBSession.add(
                    common.LanguageIdentifier(language=l, identifier=identifier))

    print count, 'new multitree identifiers'
github clld / glottolog3 / glottolog3 / scripts / update_macrolangs.py View on Github external
def main(args):  # pragma: no cover
    i = 0
    matched = 0
    near = 0
    with transaction.manager:
        max_identifier_pk = DBSession.query(
            Identifier.pk).order_by(desc(Identifier.pk)).first()[0]
        codes = dict(
            (row[0], 1)
            for row in DBSession.query(Languoid.hid).filter(Languoid.hid != None)
            if len(row[0]) == 3)
        macrolangs = dict(
            (k, set(gg[1] for gg in g))
            for k, g in groupby(get_macrolangs(codes), lambda p: p[0]))

        families = []
        for family in DBSession.query(Languoid)\
                .filter(Languoid.level == LanguoidLevel.family)\
                .filter(Language.active == True)\
                .all():
            isoleafs = set()
            i += 1
github clld / clld / clld / web / views / sitemap.py View on Github external
def resourcemap(req):
    """Resource-specific JSON response listing all resource instances."""
    rsc = req.params.get('rsc')
    if rsc == 'language':
        q = DBSession.query(
            common.Language.id,
            common.Language.name,
            common.Language.latitude,
            common.Language.longitude,
            common.Identifier.type.label('itype'),
            common.Identifier.name.label('iname')
        ).outerjoin(join(
            common.LanguageIdentifier,
            common.Identifier, and_(
                common.LanguageIdentifier.identifier_pk == common.Identifier.pk,
                common.Identifier.type != 'name')
        )).filter(common.Language.active == true()).order_by(common.Language.id)

        def resources():
            for (id, name, lat, lon), rows in groupby(q, itemgetter(0, 1, 2, 3)):
                identifiers = [
                    {'type': r.itype, 'identifier': r.iname.lower()
                     if r.itype.startswith('WALS') else r.iname}
                    for r in rows if r.iname is not None]
                yield {'id': id, 'name': name, 'latitude': lat, 'longitude': lon,
                       'identifiers': identifiers}
    elif rsc == 'parameter':
        q = DBSession.query(
            common.Parameter.id,
            common.Parameter.name
        ).order_by(common.Parameter.pk)
github clld / clld / src / clld / web / util / helpers.py View on Github external
def glottolog_url(glottocode):
    return models.Identifier(name=glottocode, type='glottolog').url()
github clld / clld / src / clld / web / views / sitemap.py View on Github external
def resourcemap(req):
    """Resource-specific JSON response listing all resource instances."""
    rsc = req.params.get('rsc')
    if rsc == 'language':
        q = DBSession.query(
            common.Language.id,
            common.Language.name,
            common.Language.latitude,
            common.Language.longitude,
            common.Identifier.type.label('itype'),
            common.Identifier.name.label('iname')
        ).select_from(common.Language).outerjoin(join(
            common.LanguageIdentifier,
            common.Identifier, and_(
                common.LanguageIdentifier.identifier_pk == common.Identifier.pk,
                common.Identifier.type != 'name')
        )).filter(common.Language.active == true()).order_by(common.Language.id)

        def resources():
            for (id, name, lat, lon), rows in groupby(q, itemgetter(0, 1, 2, 3)):
                identifiers = [
                    {'type': r.itype, 'identifier': r.iname.lower()
                     if r.itype.startswith('WALS') else r.iname}
                    for r in rows if r.iname is not None]
                yield {'id': id, 'name': name, 'latitude': lat, 'longitude': lon,
                       'identifiers': identifiers}
github clld / glottolog3 / glottolog3 / scripts / update_macrolangs.py View on Github external
.filter(Languoid.level == LanguoidLevel.language)\
                .filter(Languoid.status == LanguoidStatus.established)\
                .all():
                if len(row[1]) == 3:
                    isoleafs.add(row[1])

            families.append((family, isoleafs))

        families = sorted(families, key=lambda p: len(p[1]))

        for mid, leafs in macrolangs.items():
            found = False
            for family, isoleafs in families:
                if leafs == isoleafs:
                    if mid not in [c.name for c in family.identifiers if c.type == IdentifierType.iso.value]:
                        family.codes.append(Identifier(
                            id=str(max_identifier_pk + 1),
                            name=mid,
                            type=IdentifierType.iso.value))
                        max_identifier_pk += 1
                    matched += 1
                    found = True
                    break
                elif leafs.issubset(isoleafs):
                    print '~~~', family.name, '-->', mid, 'distance:', len(leafs), len(isoleafs)
                    near += 1
                    found = True
                    break
            if not found:
                print '---', mid, leafs

    print 'matched', matched, 'of', len(macrolangs), 'macrolangs'
github cldf / cldf / examples / wals / cldf.py View on Github external
def write_cldf(req, contrib, valuesets, features, outdir):
    ds = Dataset('wals-chapter-%s' % contrib.id)
    ds.fields = (
        'ID',
        'Language_ID',
        'Language_name',
        'Parameter_ID',
        'Value',
        'DomainElement',
        'Source',
        'Comment')
    ds.table.schema.aboutUrl = url_template(req, 'valueset', 'ID')
    ds.table.schema.columns['Language_ID'].valueUrl = Identifier(
        type='glottolog', name='{Language_ID}').url()
    ds.table.schema.columns['Parameter_ID'].valueUrl = url_template(
        req, 'parameter', 'Parameter_ID')

    ds.metadata['dc:bibliographicCitation '] = text_citation(req, contrib)
    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'] = contrib.name
    ds.metadata['dc:creator'] = contrib.formatted_contributors()
    ds.metadata['dc:identifier'] = req.resource_url(contrib)
    ds.metadata['dc:isPartOf'] = req.resource_url(req.dataset)
    ds.metadata['dcat:accessURL'] = req.route_url('download')

    domain = set()