Skip to content

Commit

Permalink
Read default location for bibtex file and PDF directory from ~/.frank…
Browse files Browse the repository at this point in the history
…linrc
  • Loading branch information
canismarko committed May 19, 2021
1 parent e85fad8 commit d97d344
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 16 deletions.
25 changes: 25 additions & 0 deletions franklin/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from pathlib import Path
import configparser


class FranklinConfig(configparser.ConfigParser):
default_filename = Path("~/.franklinrc").expanduser()

def read(self, filenames=None, *args, **kwargs):
if filenames is None:
filenames = self.default_filename
return super().read(filenames, *args, **kwargs)


franklin_config = FranklinConfig()


def load_config():
"""Load the configuration file from disk."""
config = configparser.ConfigParser()
config['Elsevier'] = {
'api_key': '',
}
# Read the file from disk
config.read(os.path.expanduser('~/.franklinrc'))
return config
25 changes: 20 additions & 5 deletions franklin/fetch_doi.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# along with Franklin. If not, see <https://www.gnu.org/licenses/>.

import os
from pathlib import Path
import argparse
import re
import logging
Expand All @@ -23,11 +24,18 @@

from .article import Article
from .version import __version__
from .config import franklin_config as config
from . import exceptions

log = logging.getLogger(__name__)


config['fetch_doi'] = {
'bibtex_file': "./refs.bib",
'pdf_dir': "./papers/",
}


def parse_doi(doi):
"""Validate and extract a digital object identifier."""
regex = ('(https?://)?((dx\.)?doi.org/)?'
Expand Down Expand Up @@ -190,10 +198,10 @@ def main(argv=None):
)
parser.add_argument('doi', type=str,
help='the digital object identifier to retrieve')
parser.add_argument('-p', '--pdf-dir', dest='pdf_dir', default='./papers/',
parser.add_argument('-p', '--pdf-dir', dest='pdf_dir',
metavar='PATH',
help='where to store the downloaded PDF')
parser.add_argument('-b', '--bibtex-file', dest='bibfile', default='./refs.bib',
parser.add_argument('-b', '--bibtex-file', dest='bibfile',
metavar='FILE',
help='will add the new bibtex entry to this file')
parser.add_argument('-i', '--bibtex-id', dest='bibtex_id', default=None,
Expand All @@ -214,12 +222,19 @@ def main(argv=None):
else:
level = logging.WARNING
logging.basicConfig(level=level)
doi = parse_doi(args.doi)
# Load the franklin rc file if available
config.read()
# Prepare the metadata from the CLI arguments, etc
force = args.force
bibfile = args.bibfile
bibfile = args.bibfile if args.bibfile is not None else config['fetch_doi']['bibtex_file']
bibfile = Path(bibfile).expanduser().resolve()
bibtex_id = args.bibtex_id
pdf_dir = args.pdf_dir
pdf_dir = args.pdf_dir if args.pdf_dir is not None else config['fetch_doi']['pdf_dir']
pdf_dir = Path(pdf_dir).expanduser().resolve()
retrieve_pdf = args.retrieve_pdf
print(bibfile)
import sys; sys.exit(0)
doi = parse_doi(args.doi)
# Check if the file exists
if not os.path.exists(bibfile) and not force:
raise exceptions.BibtexFileNotFoundError("Cannot find bibtex file: {}".format(bibfile))
Expand Down
16 changes: 6 additions & 10 deletions franklin/publishers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import configparser

from .exceptions import PDFNotFoundError, UnknownPublisherError, ConfigError
from .config import franklin_config as config
from . import __version__


Expand All @@ -29,15 +30,10 @@
}


def load_config():
"""Load the configuration file from disk."""
config = configparser.ConfigParser()
config['Elsevier'] = {
'api_key': '',
}
# Read the file from disk
config.read(os.path.expanduser('~/.franklinrc'))
return config
# Prepare default global configuration values
config['Elsevier'] = {
'api_key': '',
}


def get_publisher(publisher):
Expand Down Expand Up @@ -108,7 +104,7 @@ def electrochemical_society(doi, *args, **kwargs):
def elsevier(doi, api_key=None, *args, **kwargs):
# Make sure the API key is set
if api_key is None:
api_key = load_config()['Elsevier']['api_key']
api_key = config.read()['Elsevier']['api_key']
# Prepare the API request
api_url = "https://api.elsevier.com/content/article/doi/{doi}?"
if api_key:
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ imagesize==1.1.0
Jinja2==2.10.1
MarkupSafe==1.1.1
more-itertools==7.0.0
orgparse==0.2.4
packaging==19.0
pandas==0.25.3
pluggy==0.9.0
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def read(fname):
packages=['franklin'],
long_description=read('README.rst'),
install_requires=[
'bibtexparser', 'requests', 'tqdm', 'pandas', 'titlecase',
'bibtexparser', 'requests', 'tqdm', 'pandas', 'titlecase', 'orgparse',
],
entry_points = {
'console_scripts': [
Expand Down

0 comments on commit d97d344

Please sign in to comment.