diff --git a/franklin/config.py b/franklin/config.py new file mode 100644 index 0000000..627d752 --- /dev/null +++ b/franklin/config.py @@ -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 diff --git a/franklin/fetch_doi.py b/franklin/fetch_doi.py index 5039bbe..5d79947 100644 --- a/franklin/fetch_doi.py +++ b/franklin/fetch_doi.py @@ -14,6 +14,7 @@ # along with Franklin. If not, see . import os +from pathlib import Path import argparse import re import logging @@ -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/)?' @@ -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, @@ -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)) diff --git a/franklin/publishers.py b/franklin/publishers.py index 49cbef5..bd6b52b 100644 --- a/franklin/publishers.py +++ b/franklin/publishers.py @@ -21,6 +21,7 @@ import configparser from .exceptions import PDFNotFoundError, UnknownPublisherError, ConfigError +from .config import franklin_config as config from . import __version__ @@ -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): @@ -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: diff --git a/requirements.txt b/requirements.txt index 559bcc7..fb78b89 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/setup.py b/setup.py index 3a3eaea..3106e06 100644 --- a/setup.py +++ b/setup.py @@ -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': [