Skip to content

Commit

Permalink
Applied code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Auto Format committed Aug 13, 2024
1 parent 8c65431 commit 010ea5f
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 93 deletions.
6 changes: 3 additions & 3 deletions bibtex_dblp/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 12 additions & 12 deletions bibtex_dblp/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 = ""
Expand All @@ -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"])
22 changes: 9 additions & 13 deletions bibtex_dblp/dblp_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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:
Expand All @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion bibtex_dblp/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
16 changes: 6 additions & 10 deletions bin/convert_dblp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
39 changes: 19 additions & 20 deletions bin/import_dblp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down
34 changes: 15 additions & 19 deletions bin/update_from_dblp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = ""

Expand Down
30 changes: 15 additions & 15 deletions tests/api/test_dblp_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,32 @@ 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():
search_string = "Output-sensitive autocompletion search"
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
Expand Down

0 comments on commit 010ea5f

Please sign in to comment.