From 010ea5fe5297368099605660dc3d1466027f2faf Mon Sep 17 00:00:00 2001 From: Auto Format <> Date: Tue, 13 Aug 2024 14:56:20 +0000 Subject: [PATCH 1/2] Applied code formatting --- bibtex_dblp/config.py | 6 +++--- bibtex_dblp/database.py | 24 +++++++++++------------ bibtex_dblp/dblp_api.py | 22 +++++++++------------ bibtex_dblp/search.py | 2 +- bin/convert_dblp.py | 16 ++++++---------- bin/import_dblp.py | 39 +++++++++++++++++++------------------- bin/update_from_dblp.py | 34 +++++++++++++++------------------ tests/api/test_dblp_api.py | 30 ++++++++++++++--------------- 8 files changed, 80 insertions(+), 93 deletions(-) diff --git a/bibtex_dblp/config.py b/bibtex_dblp/config.py index 6012901..67d8f76 100644 --- a/bibtex_dblp/config.py +++ b/bibtex_dblp/config.py @@ -3,9 +3,9 @@ """ # DBLP URLs -DBLP_BASE_URL = 'https://dblp.org/' -DBLP_PUBLICATION_SEARCH_URL = DBLP_BASE_URL + 'search/publ/api' -DBLP_PUBLICATION_BIBTEX = DBLP_BASE_URL + 'rec/{key}.bib?param={bib_format}' +DBLP_BASE_URL = "https://dblp.org/" +DBLP_PUBLICATION_SEARCH_URL = DBLP_BASE_URL + "search/publ/api" +DBLP_PUBLICATION_BIBTEX = DBLP_BASE_URL + "rec/{key}.bib?param={bib_format}" # DBLP API MAX_SEARCH_RESULTS = 30 diff --git a/bibtex_dblp/database.py b/bibtex_dblp/database.py index ee51d09..1cb8b36 100644 --- a/bibtex_dblp/database.py +++ b/bibtex_dblp/database.py @@ -59,7 +59,7 @@ def convert_dblp_entries(bib, bib_format=dblp_api.BibFormat.condensed): # DBLP key is not used as bibtex key -> remember DBLP key key = next(iter(data.entries)) new_entry = data.entries[key] - new_entry.fields['biburl'] = entry.fields['biburl'] + new_entry.fields["biburl"] = entry.fields["biburl"] bib.entries[entry_str] = new_entry else: @@ -87,10 +87,10 @@ def search(bib, search_string): """ results = [] for _, entry in bib.entries.items(): - if 'author' in entry.persons: - authors = entry.persons['author'] + if "author" in entry.persons: + authors = entry.persons["author"] author_names = " and ".join([str(author) for author in authors]) - elif 'organization' in entry.fields: + elif "organization" in entry.fields: author_names = str(entry.fields["organization"]) else: author_names = "" @@ -108,15 +108,15 @@ def print_entry(bib_entry): :param bib_entry: Pybtex entry. :return: String. """ - if 'author' in bib_entry.persons: - authors = ", ".join([str(author) for author in bib_entry.persons['author']]) - elif 'organization' in bib_entry.fields: + if "author" in bib_entry.persons: + authors = ", ".join([str(author) for author in bib_entry.persons["author"]]) + elif "organization" in bib_entry.fields: authors = str(bib_entry.fields["organization"]) else: authors = "" book = "" - if 'booktitle' in bib_entry.fields: - book = bib_entry.fields['booktitle'] - if 'volume' in bib_entry.fields: - book += " ({})".format(bib_entry.fields['volume']) - return "{}:\n\t{} {} {}".format(authors, bib_entry.fields['title'], book, bib_entry.fields['year']) + if "booktitle" in bib_entry.fields: + book = bib_entry.fields["booktitle"] + if "volume" in bib_entry.fields: + book += " ({})".format(bib_entry.fields["volume"]) + return "{}:\n\t{} {} {}".format(authors, bib_entry.fields["title"], book, bib_entry.fields["year"]) diff --git a/bibtex_dblp/dblp_api.py b/bibtex_dblp/dblp_api.py index 504a3a8..7ba0119 100644 --- a/bibtex_dblp/dblp_api.py +++ b/bibtex_dblp/dblp_api.py @@ -15,10 +15,11 @@ class BibFormat(Enum): """ Format of DBLP bibtex. """ - condensed = 'condensed' - standard = 'standard' - crossref = 'crossref' - condensed_doi = 'condensed_doi' + + condensed = "condensed" + standard = "standard" + crossref = "crossref" + condensed_doi = "condensed_doi" def bib_url(self): """ @@ -88,13 +89,12 @@ def get_bibtex(dblp_id, bib_format=BibFormat.condensed): else: raise err - bibtex = resp.content.decode('utf-8') + bibtex = resp.content.decode("utf-8") if bib_format == BibFormat.condensed_doi: # Also get DOI and insert it into bibtex - resp = perform_request( - config.DBLP_PUBLICATION_BIBTEX.format(key=dblp_id, bib_format=BibFormat.standard.bib_url())) - lines = resp.content.decode('utf-8').split('\n') + resp = perform_request(config.DBLP_PUBLICATION_BIBTEX.format(key=dblp_id, bib_format=BibFormat.standard.bib_url())) + lines = resp.content.decode("utf-8").split("\n") keep_lines = [line for line in lines if line.startswith(" doi")] assert len(keep_lines) <= 1 if keep_lines: @@ -118,11 +118,7 @@ def search_publication(pub_query, max_search_results=config.MAX_SEARCH_RESULTS): :param max_search_results: Maximal number of search results to return. :return: Search results. """ - parameters = dict( - q=pub_query, - format="json", - h=max_search_results - ) + parameters = dict(q=pub_query, format="json", h=max_search_results) resp = perform_request(config.DBLP_PUBLICATION_SEARCH_URL, params=parameters) results = bibtex_dblp.dblp_data.DblpSearchResults(resp.json()) diff --git a/bibtex_dblp/search.py b/bibtex_dblp/search.py index 35367e4..a33aa6d 100644 --- a/bibtex_dblp/search.py +++ b/bibtex_dblp/search.py @@ -11,6 +11,6 @@ def search_score(input_string, search_query): score = 0 search_words = search_query.split() for word in search_words: - if re.search(r'\b{}\b'.format(word), input_string, re.IGNORECASE) is not None: + if re.search(r"\b{}\b".format(word), input_string, re.IGNORECASE) is not None: score += 1 return score / len(search_words) diff --git a/bin/convert_dblp.py b/bin/convert_dblp.py index 81b0ec7..acc46c0 100644 --- a/bin/convert_dblp.py +++ b/bin/convert_dblp.py @@ -11,20 +11,16 @@ def main(): - parser = argparse.ArgumentParser( - description='Convert DBLP entries to specific format (condensed, standard, crossref).') + parser = argparse.ArgumentParser(description="Convert DBLP entries to specific format (condensed, standard, crossref).") - parser.add_argument('infile', help='Input bibtex file', type=str) - parser.add_argument('--out', '-o', - help='Output bibtex file. If no output file is given, the input file will be overwritten.', - type=str, default=None) - parser.add_argument('--format', '-f', help='DBLP format type to convert into', type=BibFormat, - choices=list(BibFormat), default=BibFormat.condensed) + parser.add_argument("infile", help="Input bibtex file", type=str) + parser.add_argument("--out", "-o", help="Output bibtex file. If no output file is given, the input file will be overwritten.", type=str, default=None) + parser.add_argument("--format", "-f", help="DBLP format type to convert into", type=BibFormat, choices=list(BibFormat), default=BibFormat.condensed) - parser.add_argument('--verbose', '-v', help='print more output', action="store_true") + parser.add_argument("--verbose", "-v", help="print more output", action="store_true") args = parser.parse_args() - logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.DEBUG if args.verbose else logging.INFO) + logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.DEBUG if args.verbose else logging.INFO) outfile = args.infile if args.out is None else args.out diff --git a/bin/import_dblp.py b/bin/import_dblp.py index cb7c210..b483c9d 100644 --- a/bin/import_dblp.py +++ b/bin/import_dblp.py @@ -16,24 +16,25 @@ def main(): - parser = argparse.ArgumentParser(description='Import entry from DBLP according to given search input from cli.') - - parser.add_argument('--query', '-q', - help='The query to search for the publication. If none is given the query is obtained from CLI input.', - type=str, default=None) - parser.add_argument('--bib', '-b', - help='Bibtex file where the imported entry will be appended. If no bibtex file is given, the bibtex is printed to the CLI.', - type=str, - default=None) - parser.add_argument('--format', '-f', help='DBLP format type to convert into.', type=BibFormat, - choices=list(BibFormat), default=BibFormat.condensed) - parser.add_argument('--max-results', help="Maximal number of search results to display.", type=int, - default=bibtex_dblp.config.MAX_SEARCH_RESULTS) - - parser.add_argument('--verbose', '-v', help='print more output', action="store_true") + parser = argparse.ArgumentParser(description="Import entry from DBLP according to given search input from cli.") + + parser.add_argument( + "--query", "-q", help="The query to search for the publication. If none is given the query is obtained from CLI input.", type=str, default=None + ) + parser.add_argument( + "--bib", + "-b", + help="Bibtex file where the imported entry will be appended. If no bibtex file is given, the bibtex is printed to the CLI.", + type=str, + default=None, + ) + parser.add_argument("--format", "-f", help="DBLP format type to convert into.", type=BibFormat, choices=list(BibFormat), default=BibFormat.condensed) + parser.add_argument("--max-results", help="Maximal number of search results to display.", type=int, default=bibtex_dblp.config.MAX_SEARCH_RESULTS) + + parser.add_argument("--verbose", "-v", help="print more output", action="store_true") args = parser.parse_args() - logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.DEBUG if args.verbose else logging.INFO) + logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.DEBUG if args.verbose else logging.INFO) max_search_results = args.max_results bib = None @@ -58,8 +59,7 @@ def main(): print("The bibliography already contains the following matches:") for i in range(len(bib_result)): print("({})\t{}".format(i + 1, bibtex_dblp.database.print_entry(bib_result[i][0]))) - select = bibtex_dblp.io.get_user_number("Select the intended publication (0 to search online): ", 0, - len(bib_result)) + select = bibtex_dblp.io.get_user_number("Select the intended publication (0 to search online): ", 0, len(bib_result)) if select > 0: selected_entry = bib_result[select - 1][0] pyperclip.copy(selected_entry.key) @@ -79,8 +79,7 @@ def main(): print("({})\t{}".format(i + 1, result.publication)) # Let user select correct publication - select = bibtex_dblp.io.get_user_number("Select the intended publication (0 to abort): ", 0, - search_results.total_matches) + select = bibtex_dblp.io.get_user_number("Select the intended publication (0 to abort): ", 0, search_results.total_matches) if select == 0: print("Cancelled.") exit(1) diff --git a/bin/update_from_dblp.py b/bin/update_from_dblp.py index 7e486e8..39e47d7 100644 --- a/bin/update_from_dblp.py +++ b/bin/update_from_dblp.py @@ -41,24 +41,20 @@ def search_entry(search_string, include_arxiv=False, max_search_results=bibtex_d def main(): - parser = argparse.ArgumentParser(description='Update entries in bibliography via DBLP.') - - parser.add_argument('infile', help='Input bibtex file', type=str) - parser.add_argument('--out', '-o', - help='Output bibtex file. If no output file is given, the input file will be overwritten.', - type=str, default=None) - parser.add_argument('--format', '-f', help='DBLP format type to convert into', type=BibFormat, - choices=list(BibFormat), default=BibFormat.condensed) - parser.add_argument('--max-results', help="Maximal number of search results to display.", type=int, - default=bibtex_dblp.config.MAX_SEARCH_RESULTS) - parser.add_argument('--disable-auto', help='Disable automatic selection of publications.', action="store_true") - parser.add_argument('--include-arxiv', help='Include entries from arXiv in search results.', action="store_true") - parser.add_argument('--sleep-time', '-t', help='Sleep time (in seconds) between requests. Can prevent errors with too many requests)', type=int, default=3) - parser.add_argument('--verbose', '-v', help='Print more output', action="store_true") + parser = argparse.ArgumentParser(description="Update entries in bibliography via DBLP.") + + parser.add_argument("infile", help="Input bibtex file", type=str) + parser.add_argument("--out", "-o", help="Output bibtex file. If no output file is given, the input file will be overwritten.", type=str, default=None) + parser.add_argument("--format", "-f", help="DBLP format type to convert into", type=BibFormat, choices=list(BibFormat), default=BibFormat.condensed) + parser.add_argument("--max-results", help="Maximal number of search results to display.", type=int, default=bibtex_dblp.config.MAX_SEARCH_RESULTS) + parser.add_argument("--disable-auto", help="Disable automatic selection of publications.", action="store_true") + parser.add_argument("--include-arxiv", help="Include entries from arXiv in search results.", action="store_true") + parser.add_argument("--sleep-time", "-t", help="Sleep time (in seconds) between requests. Can prevent errors with too many requests)", type=int, default=3) + parser.add_argument("--verbose", "-v", help="Print more output", action="store_true") args = parser.parse_args() - logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.DEBUG if args.verbose else logging.INFO) + logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.DEBUG if args.verbose else logging.INFO) outfile = args.infile if args.out is None else args.out bib_format = args.format max_search_results = args.max_results @@ -77,12 +73,12 @@ def main(): if dblp_id is not None: continue - if 'author' in entry.persons: - authors = ", ".join([str(author) for author in entry.persons['author']]) + if "author" in entry.persons: + authors = ", ".join([str(author) for author in entry.persons["author"]]) else: authors = "" - if 'title' in entry.fields: - title = entry.fields['title'] + if "title" in entry.fields: + title = entry.fields["title"] else: title = "" diff --git a/tests/api/test_dblp_api.py b/tests/api/test_dblp_api.py index 882f427..12e522a 100644 --- a/tests/api/test_dblp_api.py +++ b/tests/api/test_dblp_api.py @@ -8,24 +8,24 @@ def test_search_publication(): assert search_results.total_matches == 2 for i in range(len(search_results.results)): result = search_results.results[i].publication - if result.doi == '10.1007/S10791-008-9048-X': - assert result.title == 'Output-sensitive autocompletion search.' + if result.doi == "10.1007/S10791-008-9048-X": + assert result.title == "Output-sensitive autocompletion search." assert result.booktitle == None - assert result.volume == '11' - assert result.venue == 'Inf. Retr.' - assert result.pages == '269-286' + assert result.volume == "11" + assert result.venue == "Inf. Retr." + assert result.pages == "269-286" assert result.year == 2008 - assert result.type == 'Journal Articles' - assert result.key == 'journals/ir/BastMW08' - assert result.doi == '10.1007/S10791-008-9048-X' - assert result.ee == 'https://doi.org/10.1007/s10791-008-9048-x' - assert result.url == 'https://dblp.org/rec/journals/ir/BastMW08' + assert result.type == "Journal Articles" + assert result.key == "journals/ir/BastMW08" + assert result.doi == "10.1007/S10791-008-9048-X" + assert result.ee == "https://doi.org/10.1007/s10791-008-9048-x" + assert result.url == "https://dblp.org/rec/journals/ir/BastMW08" authors = [author.name for author in result.authors] - assert 'Hannah Bast' in authors - assert 'Christian Worm Mortensen' in authors - assert 'Ingmar Weber' in authors + assert "Hannah Bast" in authors + assert "Christian Worm Mortensen" in authors + assert "Ingmar Weber" in authors else: - assert result.doi == '10.1007/11880561_13' + assert result.doi == "10.1007/11880561_13" def test_dblp_bibtex(): @@ -33,7 +33,7 @@ def test_dblp_bibtex(): search_results = bibtex_dblp.dblp_api.search_publication(search_string, max_search_results=30) assert search_results.total_matches == 2 result = search_results.results[1].publication - assert result.doi == '10.1007/11880561_13' + assert result.doi == "10.1007/11880561_13" bibtex_standard = bibtex_dblp.dblp_api.get_bibtex(result.key, bib_format=BibFormat.standard) assert "booktitle = {String Processing and Information Retrieval, 13th International Conference," in bibtex_standard From 54516c09eb74d7f7d8c6f758ccfe0e062d1f3d08 Mon Sep 17 00:00:00 2001 From: Auto Format <> Date: Tue, 13 Aug 2024 14:56:20 +0000 Subject: [PATCH 2/2] Add code formatting commit to .git-blame-ignore-revs --- .git-blame-ignore-revs | 1 + 1 file changed, 1 insertion(+) diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs index e69de29..2b9ddc7 100644 --- a/.git-blame-ignore-revs +++ b/.git-blame-ignore-revs @@ -0,0 +1 @@ +010ea5fe5297368099605660dc3d1466027f2faf