From a14cd8c8bfc9395854b56aa70d204c7daffddead Mon Sep 17 00:00:00 2001 From: Tom de Geus Date: Tue, 24 Oct 2023 17:09:16 +0200 Subject: [PATCH] Making `GbibClean` available from Python (#78) --- GooseBib/bibtex.py | 12 ++++++++++-- tests/test_GbibClean.py | 14 ++++++++------ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/GooseBib/bibtex.py b/GooseBib/bibtex.py index 56b1484..65b4584 100644 --- a/GooseBib/bibtex.py +++ b/GooseBib/bibtex.py @@ -11,6 +11,7 @@ import io import os import re +import sys import textwrap import warnings from collections import defaultdict @@ -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 diff --git a/tests/test_GbibClean.py b/tests/test_GbibClean.py index bfa10e0..75d4a71 100644 --- a/tests/test_GbibClean.py +++ b/tests/test_GbibClean.py @@ -7,6 +7,8 @@ import bibtexparser import yaml +import GooseBib as gbib + dirname = os.path.dirname(__file__) @@ -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()) @@ -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()) @@ -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()) @@ -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()) @@ -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()) @@ -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())