Skip to content

Commit

Permalink
Merge pull request #29 from pcubillos/packaging
Browse files Browse the repository at this point in the history
Packaging
  • Loading branch information
pcubillos authored Jan 12, 2019
2 parents e7335d7 + 4e3f653 commit db82560
Show file tree
Hide file tree
Showing 15 changed files with 198 additions and 95 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -789,5 +789,23 @@ Added MANIFEST.in file.

Bumped bibmanager version to 0.5.6.


***** Sat Jan 12 12:15:33 CET 2019 *****

Refactored files and package structure to a standard package format.
Fixed a few typos here and there.
Updated docstring examples accordingly.

*****

Moved ads_token and ads_display loading into functions
to update them at runtime.
Removed unnecessary constants (url paths) from ads_manager.py

*****

Bumped bibmanager version to 0.6.0.
This next merge closes #12

*****

4 changes: 2 additions & 2 deletions bibmanager/VERSION.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

# bibmanager Version:
BM_VER = 0 # Major version
BM_MIN = 5 # Minor version
BM_REV = 6 # Revision
BM_MIN = 6 # Minor version
BM_REV = 0 # Revision
12 changes: 8 additions & 4 deletions bibmanager/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# Copyright (c) 2018-2019 Patricio Cubillos and contributors.
# bibmanager is open-source software under the MIT license (see LICENSE).

from . import VERSION as ver
from .bib_manager import *
__all__ = ["bib_manager", "config_manager", "latex_manager", "ads_manager",
"utils"]

from .bib_manager import __all__ as bm_all
from . import bib_manager
from . import config_manager
from . import latex_manager
from . import ads_manager
from . import utils

__all__ = bm_all
from . import VERSION as ver

__version__ = f"{ver.BM_VER}.{ver.BM_MIN}.{ver.BM_REV}"

Expand Down
16 changes: 8 additions & 8 deletions bibmanager/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
import os
import argparse
import textwrap

import prompt_toolkit

sys.path.append(os.path.dirname(os.path.realpath(__file__)))
import bibmanager as bm
# FINDME: Temporary hack until setting BM as a package:
import latex_manager as lm
import config_manager as cm
import ads_manager as am
from utils import BOLD, END
from . import bib_manager as bm
from . import latex_manager as lm
from . import config_manager as cm
from . import ads_manager as am
from .utils import BOLD, END
from .__init__ import __version__ as ver


def cli_reset(args):
Expand Down Expand Up @@ -190,7 +190,7 @@ def main():

parser.add_argument('-v', '--version', action='version',
help="Show bibmanager's version.",
version=f'bibmanager version {bm.__version__}')
version=f'bibmanager version {ver}')

# Parser Main Documentation:
main_description = """
Expand Down
14 changes: 14 additions & 0 deletions bibmanager/ads_manager/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright (c) 2018-2019 Patricio Cubillos and contributors.
# bibmanager is open-source software under the MIT license (see LICENSE).

from .ads_manager import *
from .ads_manager import __all__


# Clean up top-level namespace--delete everything that isn't in __all__
# or is a magic attribute, and that isn't a submodule of this package
for varname in dir():
if not ((varname.startswith('__') and varname.endswith('__')) or
varname in __all__ ):
del locals()[varname]
del(varname)
37 changes: 18 additions & 19 deletions bibmanager/ads_manager.py → bibmanager/ads_manager/ads_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,23 @@
import os
import re
import json
import requests
import urllib
import textwrap
import pickle

import bib_manager as bm
import config_manager as cm
from utils import BM_CACHE, BOLD, END, BANNER, ignored, parse_name, get_authors


# FINDME: Is to possible to check a token is valid?
token = cm.get('ads_token')
rows = int(cm.get('ads_display'))
import requests

ADSQUERRY = "https://api.adsabs.harvard.edu/v1/search/query?"
ADSEXPORT = "https://api.adsabs.harvard.edu/v1/export/bibtex"
ADSURL = "https://ui.adsabs.harvard.edu/\#abs/"
from .. import bib_manager as bm
from .. import config_manager as cm
from ..utils import BM_CACHE, BOLD, END, BANNER, ignored, parse_name, \
get_authors


def manager(querry=None):
"""
A manager, it doesn't really do anything, it just delegates.
"""
rows = int(cm.get('ads_display'))
if querry is None and not os.path.exists(BM_CACHE):
print("There are no more entries for this querry.")
return
Expand Down Expand Up @@ -70,7 +64,7 @@ def search(querry, start=0, cache_rows=200, sort='pubdate+desc'):
https://ui.adsabs.harvard.edu/
start: Integer
Starting index of entry to return.
rows: Integer
cache_rows: Integer
Maximum number of entries to return.
sort: String
Sorting field and direction to use.
Expand All @@ -91,7 +85,7 @@ def search(querry, start=0, cache_rows=200, sort='pubdate+desc'):
Examples
--------
>>> import ads_manager as am
>>> import bibmanager.ads_manager as am
>>> # Search entries by author (note the need for double quotes,
>>> # otherwise, the search might produce bogus results):
>>> querry = 'author:"cubillos, p"'
Expand All @@ -113,8 +107,11 @@ def search(querry, start=0, cache_rows=200, sort='pubdate+desc'):
ValueError: Invalid ADS request:
org.apache.solr.search.SyntaxError: org.apache.solr.common.SolrException: undefined field properties
"""
token = cm.get('ads_token')
querry = urllib.parse.quote(querry)
r = requests.get(f'{ADSQUERRY}q={querry}&start={start}&rows={cache_rows}'

r = requests.get('https://api.adsabs.harvard.edu/v1/search/query?'
f'q={querry}&start={start}&rows={cache_rows}'
f'&sort={sort}&fl=title,author,year,bibcode,pub',
headers={'Authorization': f'Bearer {token}'})
resp = r.json()
Expand Down Expand Up @@ -153,7 +150,7 @@ def display(results, start, index, rows, nmatch, short=True):
Examples
--------
>>> import ads_manager as am
>>> import bibmanager.ads_manager as am
>>> start = index = 0
>>> querry = 'author:"^cubillos, p" property:refereed'
>>> results, nmatch = am.search(querry, start=start)
Expand All @@ -165,7 +162,8 @@ def display(results, start, index, rows, nmatch, short=True):
author_list = [parse_name(author) for author in result['author']]
authors = textwrap.fill(f"Authors: {get_authors(author_list, short)}",
**wrap_kw)
adsurl = f"adsurl: {ADSURL}{result['bibcode']}"
adsurl = ("adsurl: https://ui.adsabs.harvard.edu/\#abs/" +
f"{result['bibcode']}")
bibcode = f"\n{BOLD}bibcode{END}: {result['bibcode']}"
print(f"\n{title}\n{authors}\n{adsurl}{bibcode}")
if index + rows < nmatch:
Expand Down Expand Up @@ -194,7 +192,7 @@ def add_bibtex(input_bibcodes, input_keys, update_keys=True):
Examples
--------
>>> import ads_manager as am
>>> import bibmanager.ads_manager as am
>>> # A successful add call:
>>> bibcodes = ['1925PhDT.........1P']
>>> keys = ['Payne1925phdStellarAtmospheres']
Expand All @@ -213,6 +211,7 @@ def add_bibtex(input_bibcodes, input_keys, update_keys=True):
>>> am.add_bibtex(bibcodes, keys)
Warning: bibcode '1925PhDT.....X...1P' not found.
"""
token = cm.get('ads_token')
# Keep the originals untouched (copies will be modified):
bibcodes, keys = input_bibcodes.copy(), input_keys.copy()

Expand Down Expand Up @@ -336,7 +335,7 @@ def key_update(key, bibcode, alternate_bibcode):
Examples
--------
>>> import ads_manager as am
>>> import bibmanager.ads_manager as am
>>> key = 'BeaulieuEtal2010arxivGJ436b'
>>> bibcode = '2011ApJ...731...16B'
>>> alternate_bibcode = '2010arXiv1007.0324B'
Expand Down
14 changes: 14 additions & 0 deletions bibmanager/bib_manager/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright (c) 2018-2019 Patricio Cubillos and contributors.
# bibmanager is open-source software under the MIT license (see LICENSE).

from .bib_manager import *
from .bib_manager import __all__


# Clean up top-level namespace--delete everything that isn't in __all__
# or is a magic attribute, and that isn't a submodule of this package
for varname in dir():
if not ((varname.startswith('__') and varname.endswith('__')) or
varname in __all__ ):
del locals()[varname]
del(varname)
56 changes: 28 additions & 28 deletions bibmanager/bib_manager.py → bibmanager/bib_manager/bib_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import pickle
import urllib
import subprocess

import numpy as np
import prompt_toolkit
from prompt_toolkit.formatted_text import PygmentsTokens
Expand All @@ -21,10 +22,8 @@
from pygments.token import Token
from pygments.lexers.bibtex import BibTeXLexer

ROOT = os.path.realpath(os.path.dirname(__file__)) + '/'
sys.path.append(ROOT)
import config_manager as cm
from utils import HOME, BM_DATABASE, BM_BIBFILE, BM_TMP_BIB, BANNER, \
from .. import config_manager as cm
from ..utils import ROOT, HOME, BM_DATABASE, BM_BIBFILE, BM_TMP_BIB, BANNER, \
Sort_author, ordinal, count, cond_split, parse_name, \
purify, initials, get_authors, get_fields, req_input, ignored

Expand All @@ -50,10 +49,10 @@ def __init__(self, entry):
entry: String
A bibliographic entry text.
Example
-------
>>> import bib_manager as bm
>>> from utils import Author
Examples
--------
>>> import bibmanager.bib_manager as bm
>>> from bibmanager.utils import Author
>>> entry = '''@Misc{JonesEtal2001scipy,
author = {Eric Jones and Travis Oliphant and Pearu Peterson},
title = {{SciPy}: Open source scientific tools for {Python}},
Expand Down Expand Up @@ -145,9 +144,9 @@ def __contains__(self, author):
author: String
An author name in a valid BibTeX format.
Example
-------
>>> import bibm as bm
Examples
--------
>>> import bibmanager.bib_manager as bm
>>> bib = bm.Bib('''@ARTICLE{DoeEtal2020,
author = {{Doe}, J. and {Perez}, J. and {Dupont}, J.},
title = "What Have the Astromomers ever Done for Us?",
Expand Down Expand Up @@ -273,7 +272,7 @@ def display_bibs(labels, bibs):
Examples
--------
>>> import bibm as bm
>>> import bibmanager.bib_manager as bm
>>> e1 = '''@Misc{JonesEtal2001scipy,
author = {Eric Jones and Travis Oliphant and Pearu Peterson},
title = {{SciPy}: Open source scientific tools for {Python}},
Expand Down Expand Up @@ -425,8 +424,9 @@ def loadfile(bibfile=None, text=None):
Examples
--------
>>> import bibm as bm
>>> bibfile = "../examples/sample.bib"
>>> import bibmanager.bib_manager as bm
>>> import os
>>> bibfile = os.path.expanduser("~") + "/.bibmanager/examples/sample.bib"
>>> bibs = bm.loadfile(bibfile)
"""
entries = [] # Store Lists of bibtex entries
Expand Down Expand Up @@ -495,12 +495,11 @@ def merge(bibfile=None, new=None, take="old"):
Examples
--------
>>> import bib_manager as bm
>>> # Load bibmabager database (TBD: update with bm/examples/new.bib):
>>> newbib = (os.path.expanduser("~")
+ "/Dropbox/latex/2018_hd209/current/hd209nuv.bib")
>>> new = bm.loadfile(newbib)
>>> # Merge new into database:
>>> import bibmanager.bib_manager as bm
>>> import os
>>> # TBD: Need to add sample2.bib into package.
>>> newbib = os.path.expanduser("~") + "/.bibmanager/examples/sample2.bib"
>>> # Merge newbib into database:
>>> bm.merge(newbib, take='old')
"""
bibs = load()
Expand Down Expand Up @@ -573,8 +572,8 @@ def save(entries):
Examples
--------
>>> import bibm as bm
>>> # [Load some entries]
>>> import bibmanager.bib_manager as bm
>>> # TBD: Load some entries
>>> bm.save(entries)
"""
# FINDME: Don't pickle-save the Bib() objects directly, but store them
Expand All @@ -594,7 +593,7 @@ def load():
Examples
--------
>>> import bibm as bm
>>> import bibmanager.bib_manager as bm
>>> bibs = bm.load()
"""
try:
Expand Down Expand Up @@ -647,10 +646,11 @@ def init(bibfile=BM_BIBFILE, reset_db=True, reset_config=False):
reset_config: Bool
If True, reset the config file.
Example
-------
>>> import bib_manager as bm
>>> bibfile = '../examples/sample.bib'
Examples
--------
>>> import bibmanager.bib_manager as bm
>>> import os
>>> bibfile = os.path.expanduser("~") + "/.bibmanager/examples/sample.bib"
>>> bm.init(bibfile)
"""
# First install ever:
Expand Down Expand Up @@ -764,7 +764,7 @@ def search(authors=None, year=None, title=None, key=None, bibcode=None):
Examples
--------
>>> import bib_manager as bm
>>> import bibmanager.bib_manager as bm
>>> # Search by last name:
>>> matches = bm.search(authors="Cubillos")
>>> # Search by last name and initial:
Expand Down
14 changes: 14 additions & 0 deletions bibmanager/config_manager/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright (c) 2018-2019 Patricio Cubillos and contributors.
# bibmanager is open-source software under the MIT license (see LICENSE).

from .config_manager import *
from .config_manager import __all__


# Clean up top-level namespace--delete everything that isn't in __all__
# or is a magic attribute, and that isn't a submodule of this package
for varname in dir():
if not ((varname.startswith('__') and varname.endswith('__')) or
varname in __all__ ):
del locals()[varname]
del(varname)
Loading

0 comments on commit db82560

Please sign in to comment.