Skip to content

Commit

Permalink
Introduce option to not validate dois
Browse files Browse the repository at this point in the history
  • Loading branch information
vaneseltine committed Jul 11, 2024
1 parent 26fb2ef commit 87a5da9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/python-check-pip.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
python-version: ["3.10", "3.11", "3.12"]

steps:
# - uses: actions/checkout@v4
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
Expand All @@ -28,6 +28,7 @@ jobs:
run: |
python -m pip install ash-williams
python -m pip freeze
- name: Literally just invoke it # Consider a more elaborate test
ls
- name: Barebones demo script
run: |
python -c "from ash import Paper, RetractionDatabase;print(ash.Paper)"
python ./test/demo.py
22 changes: 19 additions & 3 deletions ash/ash.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,16 +209,32 @@ def from_path(cls, path: Path | str, mime_type: str | None = None) -> "Paper":
with path.open("rb") as stream:
return cls(stream, mime_type)

def report(self, db: RetractionDatabase | Path | str) -> dict[str, Any]:
def report(
self,
db: RetractionDatabase | Path | str,
validate_dois: bool = True,
) -> dict[str, Any]:
if isinstance(db, (Path, str)):
db = RetractionDatabase(db)
all_dois = {
dois_report = self._generate_dois_report(db, validate=validate_dois)
zombie_report = self._generate_zombie_report(db)
return {"dois": dois_report, "zombies": zombie_report}

def _generate_dois_report(
self, db: RetractionDatabase, validate: bool
) -> dict[str, Any]:
if not validate:
return {doi: {"Retracted": (doi in db.dois)} for doi in self.dois}

return {
doi: {
"DOI is valid": DOI(doi).exists(),
"Retracted": (doi in db.dois),
}
for doi in self.dois
}

def _generate_zombie_report(self, db: RetractionDatabase) -> list[dict[str, Any]]:
zombies = sorted([doi for doi in self.dois if doi in db.dois])
zombie_report = [
{
Expand All @@ -230,7 +246,7 @@ def report(self, db: RetractionDatabase | Path | str) -> dict[str, Any]:
for doi in zombies
for record in db.data[doi]
]
return {"dois": all_dois, "zombies": zombie_report}
return zombie_report

@classmethod
def register_handler(
Expand Down
10 changes: 10 additions & 0 deletions test/demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import ash

TEXT = """
You can find this in the sporting goods department. That's right, this sweet baby was
made in Grand Rapids, Michigan. Retails for about doi: 10.10995/walnutstock.
That's right. "Short Smart: Shop S-Mart!"
"""
db = ash.RetractionDatabase("./test/mock/rw_database.csv")
paper = ash.Paper(TEXT, mime_type="text/plain")
print(paper.report(db, validate_dois=False))
9 changes: 9 additions & 0 deletions test/test_ash.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,15 @@ def test_unclear_response_from_server(self, fake_db, mock_http, tmpdir):
"Retracted": True,
}

def test_no_socket_hits_with_no_validation(self, fake_db):
text = """
You can find this in the sporting goods department. That's right, this sweet
baby was made in Grand Rapids, Michigan. Retails for about
doi:10.10995/walnutstock. That's right. "Short Smart: Shop S-Mart!"
"""
paper = Paper(text, mime_type="text/plain")
print(paper.report(fake_db, validate_dois=False))


class TestMIMEBehavior:
@pytest.mark.parametrize(
Expand Down

0 comments on commit 87a5da9

Please sign in to comment.