Skip to content

Commit

Permalink
closes #180
Browse files Browse the repository at this point in the history
  • Loading branch information
xrotwang committed Feb 19, 2025
1 parent 80a3ff8 commit 53495da
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ The `pycldf` package adheres to [Semantic Versioning](http://semver.org/spec/v2.
## Unreleased

- Make sure all local media files are copied with `Dataset.copy` as well.
- Allow passing a description to `Dataset.add_table` and `Dataset.add_component`.


## [1.41.0] - 2025-02-15
Expand Down
9 changes: 9 additions & 0 deletions src/pycldf/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,12 +567,16 @@ def add_table(self, url: str, *cols: ColSpecType, **kw) -> csvw.Table:
:param cols: Column specifications; anything accepted by :func:`pycldf.dataset.make_column`.
:param kw: Recognized keywords:
- `primaryKey`: specify the column(s) constituting the primary key of the table.
- `description`: a description of the table.
:return: The new table.
"""
t = self.add_component({"url": url, "tableSchema": {"columns": []}}, *cols)
if 'primaryKey' in kw:
t.tableSchema.primaryKey = attr.fields_dict(Schema)['primaryKey'].converter(
kw.pop('primaryKey'))
if kw.get('description'):
t.common_props['dc:description'] = kw.pop('description')
t.common_props.update(kw)
return t

def remove_table(self, table: TableType):
Expand Down Expand Up @@ -600,6 +604,9 @@ def add_component(self,
:param component: A component specified by name or as `dict` representing the JSON \
description of the component.
:param kw: Recognized keywords: \
- `url`: a url property for the table;\
- `description`: a description of the table.
"""
if isinstance(component, str):
component = jsonlib.load(pkg_path('components', '{0}{1}'.format(component, MD_SUFFIX)))
Expand All @@ -609,6 +616,8 @@ def add_component(self,

if kw.get('url'):
component.url = Link(kw['url'])
if kw.get('description'):
component.common_props['dc:description'] = kw['description']

for other_table in self.tables:
if other_table.url == component.url:
Expand Down
14 changes: 11 additions & 3 deletions tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,17 @@ def test_primary_table(ds, ds_tc):


def test_components(ds):
ds.add_component('LanguageTable')
ds.add_table('custom1.csv', 'id', **{'dc:conformsTo': None})
ds.add_table('custom2.csv', 'id', **{'dc:conformsTo': 'http://example.org'})
t = ds.add_component('LanguageTable', description='desc')
assert t.common_props['dc:description'] == 'desc'
t = ds.add_table('custom1.csv', 'id', **{'dc:conformsTo': None})
assert 'dc:conformsTo' in t.common_props
t = ds.add_table(
'custom2.csv',
'id',
description='desc',
**{'dc:conformsTo': 'http://example.org'})
assert 'dc:conformsTo' in t.common_props
assert t.common_props['dc:description'] == 'desc'
assert len(ds.components) == 1


Expand Down

0 comments on commit 53495da

Please sign in to comment.