Skip to content

Commit

Permalink
Update name of field
Browse files Browse the repository at this point in the history
  • Loading branch information
cthoyt committed Nov 21, 2023
1 parent 1100d4c commit f263cb8
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/prefixmaps/data/merged.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
context,prefix,namespace,status,source
context,prefix,namespace,status,expansion_source
merged,3DMET,http://identifiers.org/3dmet/,canonical,bioregistry
merged,4DN,http://identifiers.org/4dn/,namespace_alias,bioregistry
merged,4DN.BIOSOURCE,http://identifiers.org/4dn/,canonical,bioregistry
Expand Down
2 changes: 1 addition & 1 deletion src/prefixmaps/data/merged.oak.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
context,prefix,namespace,status,source
context,prefix,namespace,status,expansion_source
merged.oak,3DMET,http://identifiers.org/3dmet/,canonical,bioregistry
merged.oak,4DN,http://identifiers.org/4dn/,namespace_alias,bioregistry
merged.oak,4DN.BIOSOURCE,http://identifiers.org/4dn/,canonical,bioregistry
Expand Down
10 changes: 5 additions & 5 deletions src/prefixmaps/datamodel/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class PrefixExpansion:
status: StatusType
"""Indicates whether the expansion is canonical, a prefix alias, a namespace alias, or both."""

source: Optional[str] = None
expansion_source: Optional[str] = None
"""Indicates the source of the prefix expansion."""

def canonical(self) -> bool:
Expand Down Expand Up @@ -156,15 +156,15 @@ def combine(self, context: "Context"):
:return:
"""
for pe in context.prefix_expansions:
self.add_prefix(pe.prefix, pe.namespace, pe.status, source=context.name)
self.add_prefix(pe.prefix, pe.namespace, pe.status, expansion_source=context.name)

def add_prefix(
self,
prefix: PREFIX,
namespace: NAMESPACE,
status: StatusType = StatusType.canonical,
preferred: bool = False,
source: Optional[str] = None,
expansion_source: Optional[str] = None,
):
"""
Adds a prefix expansion to this context.
Expand All @@ -180,7 +180,7 @@ def add_prefix(
:param namespace: namespace to be added
:param status: the status of the prefix being added
:param preferred:
:param source: An optional annotation to be used when merging contexts together.
:param expansion_source: An optional annotation to be used when merging contexts together.
The source will keep track of the original context that a given prefix
expansion came from. This is used in :meth:`Context.combine`.
:return:
Expand Down Expand Up @@ -210,7 +210,7 @@ def add_prefix(
prefix=prefix,
namespace=namespace,
status=status,
source=source,
expansion_source=expansion_source,
)
)

Expand Down
2 changes: 1 addition & 1 deletion src/prefixmaps/ingest/etl_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def run_etl(output_directory: Union[str, Path]) -> None:
# Write all contexts
for name, context in contexts.items():
with output_directory.joinpath(f"{name}.csv").open("w", encoding="UTF-8") as file:
context_to_file(context, file, include_source=context.name in COMBINED)
context_to_file(context, file, include_expansion_source=context.name in COMBINED)


@click.command
Expand Down
14 changes: 8 additions & 6 deletions src/prefixmaps/io/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,27 @@ def _key(pe: PrefixExpansion):
return pe.prefix.casefold(), STATUS_TYPE_ORDER[pe.status]


def context_to_file(context: Context, file: TextIO, *, include_source: bool = False) -> None:
def context_to_file(
context: Context, file: TextIO, *, include_expansion_source: bool = False
) -> None:
"""
Writes a context to a file
:param context:
:param file:
:param include_source: If true, include a "source" column. This is useful for
:param include_expansion_source: If true, include a "source" column. This is useful for
writing merged contexts since it says the highest priority simple context
from which the row corresponding to a :class:`PrefixExpansion` came.
:return:
"""
field_names = ["context", "prefix", "namespace", "status"]
if include_source:
field_names.append("source")
if include_expansion_source:
field_names.append("expansion_source")
writer = DictWriter(file, fieldnames=field_names)
writer.writeheader()
for pe in sorted(context.prefix_expansions, key=_key):
row = vars(pe)
row["status"] = pe.status.value
if not include_source:
row.pop("source", None)
if not include_expansion_source:
row.pop("expansion_source", None)
writer.writerow(row)

0 comments on commit f263cb8

Please sign in to comment.