Example #1
0
def add_table_with_columns(
        table: str,
        column_names: t.Set[str],
        data: pycldf.Dataset) -> None:
    """Add a table with the given columns to the dataset.

    If such a table already exists, only add the columns that do not exist
    yet.

    """
    delete = True
    try:
        data[table]
        delete = False
    except KeyError:
        data.add_component(table)
    columns = data[table].tableSchema.columns
    for c in range(len(columns) - 1, -1, -1):
        column = columns[c]
        expected_name = "cldf_{}".format(
            column.propertyUrl.uri.split("#")[-1].lower())
        if expected_name not in column_names and delete:
            del columns[c]
        else:
            column_names.remove(expected_name)
    for column_name in column_names:
        data.add_columns(
            table,
            column_name.replace(
                "cldf_", "http://cldf.clld.org/v1.0/terms.rdf#"))
Example #2
0
    def from_stream(cls, stream, spec=None):
        from csvw.metadata import TableGroup
        cldf = Dataset(TableGroup(fname=pathlib.Path('tmp.json')))
        cldf.add_component('ExampleTable')

        spec = spec or CorpusSpec()
        cols = cls.get_column_names(cldf)
        igts = [
            IGT(
                id=igt[cols.id],
                gloss=igt[cols.gloss].split('\\t'),
                phrase=igt[cols.phrase].split('\\t'),
                language=igt.get(cols.language),
                properties=igt,
                spec=spec,
            )
            for igt in reader(stream.read().splitlines(), dicts=True)]
        return cls(igts, spec=spec)