-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dwcahandler package to list darwin core term and pytest
- Loading branch information
Showing
13 changed files
with
642 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
name: Run pytests | ||
|
||
on: | ||
push: | ||
branches: [ "develop" ] | ||
pull_request: | ||
branches: [ "develop" ] | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Set up Python 3.10 | ||
uses: actions/setup-python@v3 | ||
with: | ||
python-version: "3.10" | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install flake8 pytest | ||
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi | ||
- name: Lint with flake8 | ||
run: | | ||
# stop the build if there are Python syntax errors or undefined names | ||
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics | ||
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide | ||
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics | ||
- name: Test with pytest | ||
run: | | ||
pytest | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "dwcahandler" | ||
version = "0.0.1" | ||
version = "0.0.2" | ||
description = "Python package to handle Darwin Core Archive (DwCA) operations. This includes creating a DwCA zip file from one or more csvs, reading a DwCA, merge two DwCAs, validate DwCA and delete records from DwCA based on one or more key columns" | ||
authors = ["Atlas of Living Australia data team <[email protected]>"] | ||
maintainers = ["Atlas of Living Australia data team <[email protected]>"] | ||
|
@@ -9,8 +9,14 @@ license = "MPL-1.1" | |
readme = "README.md" | ||
|
||
[tool.poetry.dependencies] | ||
python = ">=3.9,<3.13" | ||
pandas = "^2.1.1" | ||
python = ">=3.9" | ||
pandas = "^2.1.0" | ||
requests = "^2.31.0" | ||
pytest = "^7.4.3" | ||
pytest-mock = "^3.12.0" | ||
|
||
[tool.poetry.scripts] | ||
update-dwc-terms = "dwcahandler.scripts.update_dwc_terms:update_terms" | ||
|
||
[build-system] | ||
requires = ["poetry-core"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pandas==2.1.0 | ||
requests==2.31.0 | ||
pytest==7.4.3 | ||
pytest-mock==3.12.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import pandas as pd | ||
from dataclasses import dataclass, field, asdict | ||
import os | ||
from pathlib import Path | ||
import re | ||
|
||
this_dir, this_filename = os.path.split(__file__) | ||
|
||
def absolute_file_paths(directory): | ||
"""Convert files in a directory into absolute paths and return | ||
as a generator | ||
:param directory: The directory to scan. | ||
:return: An absolute file path. | ||
""" | ||
for dirpath, _, filenames in os.walk(directory): | ||
for f in filenames: | ||
if re.fullmatch(r'.+\..*', f): | ||
yield os.path.abspath(os.path.join(dirpath, f)) | ||
|
||
|
||
@dataclass | ||
class Terms: | ||
TERMS_DWC_URL = "https://raw.githubusercontent.com/tdwg/rs.tdwg.org/master/terms/terms.csv" | ||
DWC_FILENAME = 'darwin-core-terms.csv' | ||
DUBLIN_CORE_FILENAME = 'dublin-core-terms.csv' | ||
TERMS_DIR = f"{this_dir}/terms" | ||
DWC_FILE_PATH = f"{TERMS_DIR}/{DWC_FILENAME}" | ||
DUBLIN_CORE_PATH = f"{TERMS_DIR}/{DUBLIN_CORE_FILENAME}" | ||
|
||
terms_path: list[Path] = field(default_factory=lambda: [c for c in absolute_file_paths(Terms.TERMS_DIR)], | ||
init=False) | ||
terms_df: pd.DataFrame = field(default_factory=pd.DataFrame, init=False) | ||
dwc_terms_df: pd.DataFrame = field(default_factory=pd.DataFrame, init=False) | ||
|
||
def __post_init__(self): | ||
def _add_to_df(existing_df: pd.DataFrame, df: pd.DataFrame): | ||
if not existing_df.empty: | ||
return existing_df.merge(df, how='outer', left_on=['term', 'uri'], right_on=['term', 'uri']) | ||
return df | ||
|
||
for term_path in self.terms_path: | ||
df = pd.read_csv(term_path, dtype='str') | ||
self.terms_df = _add_to_df(self.terms_df, df) | ||
if term_path == Terms.DWC_FILE_PATH or term_path == Terms.DUBLIN_CORE_PATH: | ||
self.dwc_terms_df = _add_to_df(self.dwc_terms_df, df) | ||
|
||
@staticmethod | ||
def update_dwc_terms(): | ||
""" | ||
Pull the latest terms from gbif dwc csv url and update the darwin core vocab terms in the package | ||
This is still WIP, do we to pull the | ||
:return: | ||
""" | ||
df = pd.read_csv(Terms.TERMS_DWC_URL, delimiter=",", encoding='utf-8', dtype='str') | ||
df = df[df["term_deprecated"].isnull()] | ||
dwc_df = pd.DataFrame() | ||
dwc_df['term'] = df['term_localName'] | ||
dwc_df['uri'] = df['term_isDefinedBy'] + df['term_localName'] | ||
dwc_df.to_csv(Terms.DWC_FILE_PATH, index=False) | ||
return dwc_df |
Oops, something went wrong.