Skip to content

Commit

Permalink
Rename repository and merged local changes.
Browse files Browse the repository at this point in the history
I was updating the scripts in my local bin; the changes have been added
here. The test script needs to be updated.
  • Loading branch information
cumberworth committed Sep 22, 2020
1 parent ea00947 commit 4d6dc21
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 80 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
*__pycache__/
build/
dist/
mybiblib.egg-info/
bibtools.egg-info/
*swp
prereqs.txt
tags
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# SearchRefs
# bibtools

A set of tools for processing a reference database composed of bib files.

The package is built around the [bib file parser](https://github.com/aclements/biblib) of aclement.
Scripts are included for three common tasks: parsing downloaded bib files, searching the database, and creating project specific bibliographies.
File renamed without changes.
17 changes: 11 additions & 6 deletions mybiblib/bib.py → bibtools/bib.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def unabbreviate(self, abb):
try:
abb = self._abb_to_full[abb]
except KeyError:
print('Abbreviation not in database for journal {}'.format(full))
print('Abbreviation not in database for journal {}'.format(abb))
raise

return abb
Expand Down Expand Up @@ -91,7 +91,8 @@ def fields_match(self, tested_fields):
if operator in ('and', 'or'):
try:
if self._operators[operator_index + 1] == 'not':
tested_fields[field_index] = not tested_fields[field_index]
tested_fields[field_index] = (
not tested_fields[field_index])
else:
pass
except IndexError:
Expand Down Expand Up @@ -151,15 +152,18 @@ def standarize_order_and_fields(self):
standard = OrderedDict()
if typ == 'article':
fields = ['author', 'title', 'journal', 'volume', 'pages', 'year',
'doi']
'doi']
elif typ == 'book':
fields = ['author', 'title', 'year', 'publisher', 'isbn']
elif typ == 'phdthesis':
fields = ['author', 'title', 'year', 'school']
elif typ == 'inproceedings':
fields = ['author', 'title', 'booktitle', 'series', 'year', 'isbn', 'location', 'pages', 'doi', 'publisher', 'address']
fields = ['author', 'title', 'booktitle', 'series', 'year',
'isbn', 'location', 'pages', 'doi', 'publisher',
'address']
elif typ == 'inbook':
fields = ['author', 'editor', 'title', 'booktitle', 'year', 'isbn', 'pages', 'doi', 'publisher', 'address']
fields = ['author', 'editor', 'title', 'booktitle',
'year', 'isbn', 'pages', 'doi', 'publisher', 'address']
else:
print('Standard not defined for entry type {}'.format(typ))
print(key)
Expand Down Expand Up @@ -187,7 +191,7 @@ def unabbreviate_journal(self, abbreviations):
self._entry['journal'] = full

def write_to_file(self, filename=None):
if filename == None:
if filename is None:
filename = '{}.tex'.format(self._entry.key)

with open(filename, 'w') as f:
Expand All @@ -199,6 +203,7 @@ def as_string(self):

class Bibliography:
"""Bibliography composed of individual bib files in a directory"""

def __init__(self, bib_directory):
bibfile_names = []
for bibfile_name in os.listdir(bib_directory):
Expand Down
20 changes: 10 additions & 10 deletions scripts/makebib.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import argparse
import re

import mybiblib.bib as mbl
import bibtools.bib as btl


BIB_DIRECTORY = '/home/alexc/refs/bibs/'
Expand All @@ -22,13 +22,13 @@ def main():
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
'latex_file',
type=str,
help='Input tex file')
'latex_file',
type=str,
help='Input tex file')
parser.add_argument(
'bib_file',
type=str,
help='Output bib file')
'bib_file',
type=str,
help='Output bib file')
return parser.parse_args()


Expand All @@ -37,7 +37,7 @@ def parse_latex_file(filename):
latex_file = f.read()

cite_commands = re.finditer(r'(cite)(t|num)?(\{)(?P<keys>[\w*,-]*)(\})',
latex_file)
latex_file)
empty_citations = 0
bib_keys = []
for cite in cite_commands:
Expand Down Expand Up @@ -71,8 +71,8 @@ def empty_citation(keys):

def create_bib_entries(bib_keys):
bib_entries = []
bib = mbl.Bibliography(BIB_DIRECTORY)
abbs = mbl.Abbreviations(ABBREVS_FILE)
bib = btl.Bibliography(BIB_DIRECTORY)
abbs = btl.Abbreviations(ABBREVS_FILE)
for bib_key in bib_keys:
try:
bib_entry = bib[bib_key]
Expand Down
65 changes: 40 additions & 25 deletions scripts/parsebib.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,51 @@
#!/usr/bin/env python3

"""Parse bib files and output in standard format; warn if missing information"""
"""Parse bib files and output in standard format"""

# For now assumes bib file is article

import argparse


FIELDS = ['title', 'author', 'journal', 'volume', 'number', 'pages', 'year', 'issn', 'doi', 'url', 'abstract']
FIELDS = ['title', 'author', 'journal', 'volume', 'number',
'pages', 'year', 'issn', 'doi', 'url', 'abstract']


def main():
parser = argparse.ArgumentParser()
parser.add_argument('filebase', help='Filebase of bib file')
args = parser.parse_args()
filebase = args.filebase

filename = filebase + '.bib'

with open(filename) as inp:
lines = inp.readlines()

field_dic = {}
merged_line = ''
for line in lines[1:]:
if '@' in line:
continue
line = line.lstrip()
line = line.rstrip('\n')
if line[0] == '}':
continue
if line[-2] != '}':
merged_line = merged_line + line
continue
else:
if merged_line != '':
line = merged_line + line
merged_line = ''
if line == '\n':
continue
else:
field_dic = find_entry(line, field_dic)


# modify field contents (capitilization, brackets, etc.)
output_standard_bib(filebase, field_dic)


def find_entry(line, field_dic):
Expand Down Expand Up @@ -36,28 +74,5 @@ def output_standard_bib(filebase, field_dic):
print('}')


def main():
parser = argparse.ArgumentParser()
parser.add_argument('filebase', help='Filebase of bib file')
args = parser.parse_args()
filebase = args.filebase

filename = filebase + '.bib'


with open(filename) as inp:
lines = inp.readlines()

field_dic = {}
for line in lines[1:]:
if line == '\n':
continue
else:
field_dic = find_entry(line, field_dic)

#modify field contents (capitilization, brackets, etc.)
output_standard_bib(filebase, field_dic)


if __name__ == '__main__':
main()
34 changes: 22 additions & 12 deletions scripts/searchbibs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,37 @@
"""Search bibliography database with and output given terms."""


import argparse

import bibtools.bib as btl


BIB_DIRECTORY = '/home/alexc/refs/bibs/'


def main():
args = parse_args()
bibliography = Bibliography(BIB_DIRECTORY)
search_string = SearchString(args.search_string)
bibliography = btl.Bibliography(BIB_DIRECTORY)
search_string = btl.SearchString(args.search_string)
bibliography.match_and_print_fields(search_string, args.terms)


def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
'-s',
type=str,
nargs='+',
dest='search_string',
help='Search string')
'-s',
type=str,
nargs='+',
dest='search_string',
help='Search string')
parser.add_argument(
'-t',
type=str,
nargs='+',
dest='terms',
help='Terms to print')
'-t',
type=str,
nargs='+',
default=['title', 'year', 'author', 'annote'],
dest='terms',
help='Terms to print')

return parser.parse_args()


Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from setuptools import setup

setup(
name='mybiblib',
packages=['mybiblib'],
name='bibtools',
packages=['bibtools'],
scripts=glob.glob('scripts/*.py'),
install_requires=['biblib']
)
55 changes: 32 additions & 23 deletions tests/test_bib.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
#!/usr/env python

from searchRefs import *
import pytest

import bibtools as btl


@pytest.fixture
def search_string_simple():
input_list= ['field:keywords', 'stuff', 'thing']
search_string = SearchString(input_list)
input_list = ['field:keywords', 'stuff', 'thing']
search_string = btl.SearchString(input_list)
return search_string


@pytest.fixture
def search_string_negation():
input_list = ['not', 'field:keywords', 'stuff', 'thing']
search_string = SearchString(input_list)
search_string = btl.SearchString(input_list)
return search_string


@pytest.fixture
def search_string_conjunction():
input_list= ['field:keywords', 'stuff', 'thing', 'and', 'field:author', 'pande']
search_string = SearchString(input_list)
input_list = ['field:keywords', 'stuff',
'thing', 'and', 'field:author', 'pande']
search_string = btl.SearchString(input_list)
return search_string


@pytest.fixture
def search_string_disjunction():
input_list = ['field:keywords', 'stuff', 'thing', 'or', 'field:author', 'pande']
search_string = SearchString(input_list)
input_list = ['field:keywords', 'stuff',
'thing', 'or', 'field:author', 'pande']
search_string = btl.SearchString(input_list)
return search_string


Expand All @@ -38,9 +41,11 @@ class TestSearchString:
(search_string_simple(), ['keywords'], [['stuff', 'thing']]),
(search_string_negation(), ['keywords'], [['stuff', 'thing']]),
(search_string_conjunction(), ['keywords', 'author'], [['stuff',
'thing'], ['pande']]),
'thing'],
['pande']]),
(search_string_disjunction(), ['keywords', 'author'], [['stuff',
'thing'], ['pande']])])
'thing'],
['pande']])])
def test__iter__(self, search_string, field_list, terms_list):
test_field_list = []
test_terms_list = []
Expand Down Expand Up @@ -71,41 +76,45 @@ def test_fields_match(self, search_string, tested_fields, match):

@pytest.fixture()
def bibfile_pande():
bibfile_pande = BibFile('pande2010.bib')
bibfile_pande = btl.BibFile('pande2010.bib')
return bibfile_pande


@pytest.fixture
def search_string_pande_1():
input_list= ['field:keywords', 'review', 'msm']
search_string = SearchString(input_list)
input_list = ['field:keywords', 'review', 'msm']
search_string = btl.SearchString(input_list)
return search_string


@pytest.fixture
def search_string_pande_2():
input_list= ['field:keywords', 'review', 'msm', 'thing']
search_string = SearchString(input_list)
input_list = ['field:keywords', 'review', 'msm', 'thing']
search_string = btl.SearchString(input_list)
return search_string


@pytest.fixture
def search_string_pande_3():
input_list= ['field:keywords', 'review', 'msm', 'and', 'field:author', 'pande']
search_string = SearchString(input_list)
input_list = ['field:keywords', 'review',
'msm', 'and', 'field:author', 'pande']
search_string = btl.SearchString(input_list)
return search_string


@pytest.fixture
def search_string_pande_4():
input_list= ['field:keywords', 'review', 'msm', 'and', 'not', 'field:author', 'pande']
search_string = SearchString(input_list)
input_list = ['field:keywords', 'review', 'msm',
'and', 'not', 'field:author', 'pande']
search_string = btl.SearchString(input_list)
return search_string


@pytest.fixture
def search_string_pande_5():
input_list= ['field:keywords', 'review', 'msm', 'or', 'not', 'field:author', 'pande']
search_string = SearchString(input_list)
input_list = ['field:keywords', 'review',
'msm', 'or', 'not', 'field:author', 'pande']
search_string = btl.SearchString(input_list)
return search_string


Expand All @@ -126,12 +135,12 @@ def test_search_string_match(self, bibfile_pande, search_string, match):
(['annote', 'title'], ['Everything', 'Review'])])
def test_get_field(self, bibfile_pande, fields, first_words):
field_texts = bibfile_pande.get_field_texts(fields)
test_first_words = [field_text.split()[0] for field_text in field_texts]
test_first_words = [field_text.split()[0]
for field_text in field_texts]
assert test_first_words == first_words


class TestBibliography:

def test_match_and_print_fields(self):
pass

0 comments on commit 4d6dc21

Please sign in to comment.