Skip to content

Commit

Permalink
Format with Black and add misc output type
Browse files Browse the repository at this point in the history
  • Loading branch information
cumberworth committed Aug 9, 2022
1 parent 8e57bf4 commit 4a0959c
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 144 deletions.
112 changes: 67 additions & 45 deletions bibtools/bib.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
import biblib.bib


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


class Abbreviations:
def __init__(self, abb_filename):
raw = csv.reader(open(abb_filename), delimiter=';')
raw = csv.reader(open(abb_filename), delimiter=";")

self._full_to_abb = {}
self._abb_to_full = {}
Expand All @@ -26,7 +26,7 @@ def abbreviate(self, full):
try:
abb = self._full_to_abb[full]
except KeyError:
print('Abbreviation not in database for journal {}'.format(full))
print("Abbreviation not in database for journal {}".format(full))
raise

return abb
Expand All @@ -35,7 +35,7 @@ def unabbreviate(self, abb):
try:
abb = self._abb_to_full[abb]
except KeyError:
print('Abbreviation not in database for journal {}'.format(abb))
print("Abbreviation not in database for journal {}".format(abb))
raise

return abb
Expand All @@ -49,11 +49,11 @@ def __init__(self, input_list):
terms = []
operators = []
for term in input_list:
if 'field:' in term:
field = term.split(':')[1]
if "field:" in term:
field = term.split(":")[1]
fields.append(field)
terms.append([])
elif term in ['and', 'or', 'not']:
elif term in ["and", "or", "not"]:
operators.append(term)
else:
terms[-1].append(term)
Expand Down Expand Up @@ -82,7 +82,7 @@ def fields_match(self, tested_fields):
if len(self._operators) == 0:
pass
else:
if self._operators[0] == 'not':
if self._operators[0] == "not":
tested_fields[0] = not tested_fields[0]
else:
pass
Expand All @@ -91,29 +91,28 @@ def fields_match(self, tested_fields):
field_index = 1
# Loop through operators, apply negations, and remove from operators
for operator_index, operator in enumerate(self._operators):
if operator in ('and', 'or'):
if operator in ("and", "or"):
try:
if self._operators[operator_index + 1] == 'not':
tested_fields[field_index] = (
not tested_fields[field_index])
if self._operators[operator_index + 1] == "not":
tested_fields[field_index] = not tested_fields[field_index]
else:
pass
except IndexError:
pass
field_index += 1
operators_without_negations.append(operator)
elif operator == 'not':
elif operator == "not":
pass

# Loop through again and apply conjunctions and disjunctions
previous_fields = tested_fields[0]
next_field_index = 1
for operator in operators_without_negations:
next_field = tested_fields[next_field_index]
if operator == 'and':
if operator == "and":
previous_fields = previous_fields and next_field
next_field_index += 1
elif operator == 'or':
elif operator == "or":
previous_fields = previous_fields or next_field
next_field_index += 1

Expand Down Expand Up @@ -153,56 +152,79 @@ def standarize_order_and_fields(self):
key = self._entry.key
typ = self._entry.typ
standard = OrderedDict()
if typ == 'article':
fields = ['author', 'title', 'journal', 'volume', 'pages', 'year',
'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']
elif typ == 'inbook':
fields = ['author', 'editor', 'title', 'booktitle',
'year', 'isbn', 'pages', 'doi', 'publisher', 'address']
if typ == "article":
fields = ["author", "title", "journal", "volume", "pages", "year", "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",
]
elif typ == "inbook":
fields = [
"author",
"editor",
"title",
"booktitle",
"year",
"isbn",
"pages",
"doi",
"publisher",
"address",
]

# This is really for ArXiv.
elif typ == "misc":
fields = ["author", "title", "publisher", "year", "doi"]
else:
print('Standard not defined for entry type {}'.format(typ))
print("Standard not defined for entry type {}".format(typ))
print(key)
raise Exception

for field in fields:
try:
standard[field] = self._entry[field]
except KeyError:
print('Entry {} missing field {}'.format(key, field))
print("Entry {} missing field {}".format(key, field))

self._entry = biblib.bib.Entry(standard, typ=typ, key=key)

def abbreviate_journal(self, abbreviations):
if self._entry.typ == 'article':
journal = self._entry['journal']
if self._entry.typ == "article":
journal = self._entry["journal"]
try:
abb = abbreviations.abbreviate(journal)
except KeyError:
print(f'Found in {self._entry.key}')
print(f"Found in {self._entry.key}")
raise

self._entry['journal'] = abb
self._entry["journal"] = abb

def unabbreviate_journal(self, abbreviations):
if self._typ == 'article':
journal = self._entry['journal']
if '.' in journal:
if self._typ == "article":
journal = self._entry["journal"]
if "." in journal:
full = abbreviations.unabbreviate(journal)
self._entry['journal'] = full
self._entry["journal"] = full

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

with open(filename, 'w') as f:
with open(filename, "w") as f:
f.write(self._entry.to_bib())

def as_string(self):
Expand All @@ -217,7 +239,7 @@ def __init__(self, bib_directory):
for bibfile_name in os.listdir(bib_directory):

# Ignore hidden files
if bibfile_name[0] == '.':
if bibfile_name[0] == ".":
continue
bibfile_name_full = bib_directory + bibfile_name
bibfile_names.append(bibfile_name_full)
Expand All @@ -231,7 +253,7 @@ def __init__(self, bib_directory):
try:
bibparser.parse(bibfile)
except:
print('Invalid formatting in {}'.format(bibfile))
print("Invalid formatting in {}".format(bibfile))
raise

self._entries = bibparser.get_entries()
Expand All @@ -240,7 +262,7 @@ def __getitem__(self, key):
return BibFile(self._entries[key])

def match_and_print_fields(self, search_string, fields):
print('')
print("")
for entry in self._entries.values():
bibfile = BibFile(entry)
match = bibfile.search_string_match(search_string)
Expand All @@ -254,4 +276,4 @@ def _print_field_texts(self, field_texts):
for field_text in field_texts:
print(field_text)

print('')
print("")
38 changes: 16 additions & 22 deletions scripts/makebib.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@


ABBREVS_FILE = pkg_resources.resource_filename(
'bibtools', 'data/cassi-abbreviations.csv')
"bibtools", "data/cassi-abbreviations.csv"
)


def main():
Expand All @@ -22,25 +23,18 @@ def main():

def parse_args():
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument(
'latex_file',
type=str,
help='Input tex file')
parser.add_argument(
'bib_file',
type=str,
help='Output bib file')
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument("latex_file", type=str, help="Input tex file")
parser.add_argument("bib_file", type=str, help="Output bib file")
return parser.parse_args()


def parse_latex_file(filename):
with open(filename) as f:
latex_file = f.read()

cite_commands = re.finditer(r'(cite)(t|num)?(\{)(?P<keys>[\w*,-]*)(\})',
latex_file)
cite_commands = re.finditer(r"(cite)(t|num)?(\{)(?P<keys>[\w*,-]*)(\})", latex_file)
empty_citations = 0
bib_keys = []
for cite in cite_commands:
Expand All @@ -55,21 +49,21 @@ def parse_latex_file(filename):
else:
bib_keys.append(key)

print('There are {} empty citations'.format(empty_citations))
print("There are {} empty citations".format(empty_citations))

return bib_keys


def parse_cite_command(cite):
cite = cite.group('keys')
cite = cite.replace('*', '')
bib_keys = cite.split(',')
cite = cite.group("keys")
cite = cite.replace("*", "")
bib_keys = cite.split(",")

return bib_keys


def empty_citation(keys):
return keys == ['']
return keys == [""]


def create_bib_entries(bib_keys):
Expand All @@ -80,7 +74,7 @@ def create_bib_entries(bib_keys):
try:
bib_entry = bib[bib_key]
except KeyError:
print('No reference for {}'.format(bib_key))
print("No reference for {}".format(bib_key))
continue

bib_entry.abbreviate_journal(abbs)
Expand All @@ -92,10 +86,10 @@ def create_bib_entries(bib_keys):


def write_bibfile(bib_entries, filename):
bibstring = '\n\n'.join(bib_entries)
with open(filename, 'w') as out:
bibstring = "\n\n".join(bib_entries)
with open(filename, "w") as out:
out.write(bibstring)


if __name__ == '__main__':
if __name__ == "__main__":
main()
24 changes: 11 additions & 13 deletions scripts/searchbibs.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,22 @@ def main():

def parse_args():
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
)
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',
"-t",
type=str,
nargs='+',
default=['title', 'year', 'author', 'annote'],
dest='terms',
help='Terms to print')
nargs="+",
default=["title", "year", "author", "annote"],
dest="terms",
help="Terms to print",
)

return parser.parse_args()


if __name__ == '__main__':
if __name__ == "__main__":
main()
Loading

0 comments on commit 4a0959c

Please sign in to comment.