Skip to content

Commit

Permalink
Making GbibClean available from Python (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdegeus authored Oct 24, 2023
1 parent 5fc9582 commit a14cd8c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
12 changes: 10 additions & 2 deletions GooseBib/bibtex.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io
import os
import re
import sys
import textwrap
import warnings
from collections import defaultdict
Expand Down Expand Up @@ -1181,13 +1182,20 @@ def _split_lines(self, text, width):
return parser


def GbibClean():
def _parse(parser: argparse.ArgumentParser, cli_args: list[str]) -> argparse.ArgumentParser:
if cli_args is None:
return parser.parse_args(sys.argv[1:])

return parser.parse_args([str(arg) for arg in cli_args])


def GbibClean(cli_args: list[str] = None):
"""
Command-line tool to clean a BibTeX database, see ``--help``.
"""

parser = _GbibClean_parser()
args = parser.parse_args()
args = _parse(parser, cli_args)
renamed = {}
merged = {}
is_unique = False
Expand Down
14 changes: 8 additions & 6 deletions tests/test_GbibClean.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import bibtexparser
import yaml

import GooseBib as gbib

dirname = os.path.dirname(__file__)


Expand All @@ -20,7 +22,7 @@ def test_inplace(self):
output = os.path.join(dirname, "output.bib")
shutil.copy2(source, output)
data = os.path.join(dirname, "library.yaml")
subprocess.check_output(["GbibClean", "--in-place", output])
gbib.bibtex.GbibClean(["--in-place", output])

with open(output) as file:
bib = bibtexparser.load(file, parser=bibtexparser.bparser.BibTexParser())
Expand All @@ -43,7 +45,7 @@ def test_mendeley(self):
source = os.path.join(dirname, "library_mendeley.bib")
output = os.path.join(dirname, "output.bib")
data = os.path.join(dirname, "library.yaml")
subprocess.check_output(["GbibClean", "-f", "-o", output, source])
gbib.bibtex.GbibClean(["-f", "-o", output, source])

with open(output) as file:
bib = bibtexparser.load(file, parser=bibtexparser.bparser.BibTexParser())
Expand All @@ -66,7 +68,7 @@ def test_hidden_doi_arxiv(self):
source = os.path.join(dirname, "library_hidden_doi_arxiv.bib")
output = os.path.join(dirname, "output.bib")
data = os.path.join(dirname, "library.yaml")
subprocess.check_output(["GbibClean", "-f", "-o", output, source])
gbib.bibtex.GbibClean(["-f", "-o", output, source])

with open(output) as file:
bib = bibtexparser.load(file, parser=bibtexparser.bparser.BibTexParser())
Expand Down Expand Up @@ -143,7 +145,7 @@ def test_authorsep(self):
source = os.path.join(dirname, "library_mendeley.bib")
output = os.path.join(dirname, "output.bib")
data = os.path.join(dirname, "library.yaml")
subprocess.check_output(["GbibClean", "-f", "--author-sep", " ", "-o", output, source])
gbib.bibtex.GbibClean(["-f", "--author-sep", " ", "-o", output, source])

with open(output) as file:
bib = bibtexparser.load(file, parser=bibtexparser.bparser.BibTexParser())
Expand Down Expand Up @@ -171,7 +173,7 @@ def test_authorsep(self):
def test_no_title(self):
source = os.path.join(dirname, "library_mendeley.bib")
output = os.path.join(dirname, "output.bib")
subprocess.check_output(["GbibClean", "-f", "--no-title", "-o", output, source])
gbib.bibtex.GbibClean(["-f", "--no-title", "-o", output, source])

with open(output) as file:
bib = bibtexparser.load(file, parser=bibtexparser.bparser.BibTexParser())
Expand Down Expand Up @@ -204,7 +206,7 @@ def test_journalrename(self):
source = os.path.join(dirname, "library_mendeley.bib")
output = os.path.join(dirname, "output.bib")
data = os.path.join(dirname, "library.yaml")
subprocess.check_output(["GbibClean", "-f", "-j", key, "-o", output, source])
gbib.bibtex.GbibClean(["-f", "-j", key, "-o", output, source])

with open(output) as file:
bib = bibtexparser.load(file, parser=bibtexparser.bparser.BibTexParser())
Expand Down

0 comments on commit a14cd8c

Please sign in to comment.