Skip to content

Commit

Permalink
Merge 963be8f into 363ccee
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinywlui committed Aug 15, 2019
2 parents 363ccee + 963be8f commit c2124f2
Show file tree
Hide file tree
Showing 19 changed files with 311 additions and 270 deletions.
41 changes: 29 additions & 12 deletions .pre-commit-config.yaml
@@ -1,14 +1,31 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.2.3
hooks:
- id: trailing-whitespace
- id: check-ast
- id: end-of-file-fixer
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.2.3
hooks:
- id: trailing-whitespace
- id: check-ast
- id: end-of-file-fixer

- id: black
name: black
stages: [commit]
language: system
entry: black
types: [python]
- repo: local
hooks:
- id: black
name: black
stages: [commit]
language: system
entry: black
types: [python]

- id: isort
name: isort
stages: [commit]
language: system
entry: isort
types: [python]

- id: mypy
name: mypy
stages: [commit]
language: system
entry: mypy --ignore-missing-imports caleb
types: [python]
pass_filenames: false
1 change: 1 addition & 0 deletions .travis.yml
@@ -1,6 +1,7 @@
dist: bionic
language: python
python:
- "3.6"
- "3.7"
- "nightly"

Expand Down
1 change: 1 addition & 0 deletions Makefile
Expand Up @@ -2,6 +2,7 @@
.PHONY: tests lint

tests:
poetry run mypy --ignore-missing-imports caleb
poetry run pytest --cov=caleb tests

lint:
Expand Down
110 changes: 0 additions & 110 deletions README.rst

This file was deleted.

2 changes: 1 addition & 1 deletion caleb/__version__.py
@@ -1 +1 @@
__version__ = "0.6.1"
__version__ = "0.6.2"
87 changes: 69 additions & 18 deletions caleb/app.py
@@ -1,12 +1,25 @@
import logging
import os
from typing import Set

from .file_handler import AuxHandler, BibHandler
from .reference import Reference


class Application:
def __init__(self, input_name):
"""Glues together all the functionality of this package.
This class is intended to be called by the `cmdline` module. Afterwards, it
uses the `reference` module to obtain the bibtex entries. Then it uses
`file_handler` to append the entries to .bib file.
Args:
input_name (str): Path to tex file whose bib entries we need to add.
"""

def __init__(
self, input_name: str, take_first: bool = False, method: str = "crossref"
):
# Normalize name by removing .tex and .aux, if necessary.
filename, file_extension = os.path.splitext(input_name)
if file_extension in [".tex", ".aux"]:
Expand All @@ -20,32 +33,70 @@ def __init__(self, input_name):
dirname = os.path.dirname(aux_file)
bib_file = os.path.join(dirname, bib_file_name)

logging.info("aux file: {}".format(aux_file))
logging.info("bib file: {}".format(bib_file))
logging.info(f"aux file: {aux_file}")
logging.info(f"bib file: {bib_file}")
self.aux_file = aux_file
self.bib_file = bib_file

def go(self, take_first=False, method="crossref"):

logging.info(f"Using {method} for citations")
self.method = method

logging.info(f"Taking first citation found: {take_first}")
self.take_first = take_first

def go(self, dry_run: bool = False) -> None:
"""Fill in the bibtex entries.
Args:
take_first (bool): Whether to just take the first bibtex entry.
method (str): Determines whether we use `ams` or `crossref`.
"""
aux_h = AuxHandler(self.aux_file)
bib_h = BibHandler(self.bib_file)

requested_citation_keys = aux_h.citation_keys()
existing_bibs = bib_h.citation_keys()
missing_cits = requested_citation_keys.difference(existing_bibs)
logging.info(f"List of missing citations: {missing_cits}")

entries_to_append = self.get_all(missing_cits)
if dry_run:
print(entries_to_append)

bib_h.append(entries_to_append)

def get_all(self, missing_cits: Set[str]) -> str:
"""Retrieve all citations given the keys.
Args:
missing_cits (list of str): a list of citation keys
Returns:
str: all bibtex entries as a single string
"""
ans = ""
for key in missing_cits:
bibtex = self.get_single(key)
logging.info(f"Appending: \n{bibtex}")
ans = ans + bibtex + "\n"
return ans

def get_single(self, key: str) -> str:
"""Retrieve a citation given the key.
Args:
key (str): the citation key
for cit in missing_cits:
logging.info("Working on: {}".format(cit))
new_bib = Reference(cit, method=method)
if not new_bib.exists():
logging.warning("No results found for: {}".format(cit))
continue
elif not new_bib.is_unique():
logging.warning("Multiple results found for: {}".format(cit))
if not take_first:
continue
bibtex = new_bib.bibtex()
logging.info("Appending: \n{}".format(bibtex))
bib_h.append_a_citation(bibtex)
Returns:
str: a bibtex citation
"""
logging.info(f"Working on: {key}")
new_bib = Reference(key, method=self.method)
if not new_bib.exists():
logging.warning(f"No results found for: {key}")
return ""
elif not new_bib.is_unique():
logging.warning(f"Multiple results found for: {key}")
if not self.take_first:
return ""
return new_bib.bibtex()
25 changes: 21 additions & 4 deletions caleb/cmdline.py
Expand Up @@ -4,9 +4,10 @@

from .__version__ import __version__
from .app import Application
from .reference import Reference


def make_parser():
def make_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser()
parser.add_argument("input_name", nargs="?")
parser.add_argument(
Expand All @@ -26,17 +27,33 @@ def make_parser():
choices=["crossref", "ams"],
default="crossref",
)
parser.add_argument(
"-g",
"--get-this-key",
help="Print the first entry with this key",
action="store",
)
parser.add_argument(
"-dr",
"--dry-run",
help="Write the changes to stdout instead of the bibtex",
action="store_true",
)
return parser


def launch():
def launch() -> None:
parser = make_parser()
args = parser.parse_args(sys.argv[1:])

# User is asking for version
if args.version:
print(__version__)
sys.exit(0)
elif args.get_this_key is not None:
ref = Reference(key=args.get_this_key, method=args.method)
print(ref.bibtex())
sys.exit(0)
elif args.input_name is None:
print("Need input name")
sys.exit(1)
Expand All @@ -48,5 +65,5 @@ def launch():
elif args.verbose >= 2:
logging.basicConfig(level=logging.INFO)

app = Application(args.input_name)
app.go(take_first=args.take_first, method=args.method)
app = Application(args.input_name, take_first=args.take_first, method=args.method)
app.go(dry_run=args.dry_run)

0 comments on commit c2124f2

Please sign in to comment.