Skip to content

Commit

Permalink
Merge pull request sciunto-org#53 from sciunto/type
Browse files Browse the repository at this point in the history
MAINT, change standard for entrytypes and id
  • Loading branch information
sciunto committed Dec 16, 2014
2 parents 261215a + 1c6ddad commit a0b61de
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 70 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
vXXXX
=====

* API: Previous type and id keywords which are automatically added to
the dictionnary are now ENTRYTYPE and ID, respectively (#42).

v0.6.0
======

Expand Down
2 changes: 1 addition & 1 deletion bibtexparser/bibdatabase.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def get_entry_dict(self):
# If the hash has never been made, make it
if not self._entries_dict:
for entry in self.entries:
self._entries_dict[entry['id']] = entry
self._entries_dict[entry['ID']] = entry
return self._entries_dict

entries_dict = property(get_entry_dict)
4 changes: 2 additions & 2 deletions bibtexparser/bparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,8 @@ def _parse_record(self, record, customization=None):
logger.debug('The dict is empty, return it.')
return d

d['type'] = bibtype
d['id'] = id
d['ENTRYTYPE'] = bibtype
d['ID'] = id

if customization is None:
logger.debug('No customization to apply, return dict')
Expand Down
12 changes: 6 additions & 6 deletions bibtexparser/bwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class BibTexWriter(object):
writer = BibTexWriter()
writer.contents = ['comments', 'entries']
writer.indent = ' '
writer.order_entries_by = ('type', 'author', 'year')
writer.order_entries_by = ('ENTRYTYPE', 'author', 'year')
bibtex_str = bibtexparser.dumps(bib_database, writer)
"""
Expand All @@ -45,8 +45,8 @@ def __init__(self):
self.indent = ' '
#: Characters(s) for separating BibTeX entries. Default: new line.
self.entry_separator = '\n'
#: Tuple of fields for ordering entries. Set to `None` to disable sorting. Default: BibTeX key `('id', )`.
self.order_entries_by = ('id', )
#: Tuple of fields for ordering entries. Set to `None` to disable sorting. Default: BibTeX key `('ID', )`.
self.order_entries_by = ('ID', )

def write(self, bib_database):
"""
Expand Down Expand Up @@ -82,15 +82,15 @@ def _entries_to_bibtex(self, bib_database):
def _entry_to_bibtex(self, entry):
bibtex = ''
# Write BibTeX key
bibtex += '@' + entry['type'] + '{' + entry['id']
bibtex += '@' + entry['ENTRYTYPE'] + '{' + entry['ID']

# Write field = value lines
for field in [i for i in sorted(entry) if i not in ['type', 'id']]:
for field in [i for i in sorted(entry) if i not in ['ENTRYTYPE', 'ID']]:
try:
bibtex += ",\n" + self.indent + field + " = {" + entry[field] + "}"
except TypeError:
raise TypeError("The field %s in entry %s must be a string"
% (field, entry['id']))
% (field, entry['ID']))
bibtex += "\n}\n" + self.entry_separator
return bibtex

Expand Down
6 changes: 3 additions & 3 deletions bibtexparser/customization.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def editor(record):
if record["editor"]:
record["editor"] = getnames([i.strip() for i in record["editor"].replace('\n', ' ').split(" and ")])
# convert editor to object
record["editor"] = [{"name": i, "id": i.replace(',', '').replace(' ', '').replace('.', '')} for i in record["editor"]]
record["editor"] = [{"name": i, "ID": i.replace(',', '').replace(' ', '').replace('.', '')} for i in record["editor"]]
else:
del record["editor"]
return record
Expand Down Expand Up @@ -130,7 +130,7 @@ def journal(record):
if "journal" in record:
# switch journal to object
if record["journal"]:
record["journal"] = {"name": record["journal"], "id": record["journal"].replace(',', '').replace(' ', '').replace('.', '')}
record["journal"] = {"name": record["journal"], "ID": record["journal"].replace(',', '').replace(' ', '').replace('.', '')}

return record

Expand Down Expand Up @@ -241,7 +241,7 @@ def homogeneize_latex_encoding(record):
record = convert_to_unicode(record)
# And then, we fall back
for val in record:
if val not in ('id',):
if val not in ('ID',):
logger.debug('Apply string_to_latex to: %s', val)
record[val] = string_to_latex(record[val])
if val == 'title':
Expand Down
4 changes: 2 additions & 2 deletions bibtexparser/tests/test_bibdatabase.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@


class TestBibDatabase(unittest.TestCase):
entries = [{'type': 'book',
entries = [{'ENTRYTYPE': 'book',
'year': '1987',
'edition': '2',
'publisher': 'Wiley Edition',
'id': 'Bird1987',
'ID': 'Bird1987',
'volume': '1',
'title': 'Dynamics of Polymeric Liquid',
'author': 'Bird, R.B. and Armstrong, R.C. and Hassager, O.'
Expand Down
4 changes: 2 additions & 2 deletions bibtexparser/tests/test_bibtexparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

class TestBibtexParserParserMethods(unittest.TestCase):
input_file_path = 'bibtexparser/tests/data/book.bib'
entries_expected = [{'type': 'book',
entries_expected = [{'ENTRYTYPE': 'book',
'year': '1987',
'edition': '2',
'publisher': 'Wiley Edition',
'id': 'Bird1987',
'ID': 'Bird1987',
'volume': '1',
'title': 'Dynamics of Polymeric Liquid',
'author': 'Bird, R.B. and Armstrong, R.C. and Hassager, O.'
Expand Down
38 changes: 19 additions & 19 deletions bibtexparser/tests/test_bibtexwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ def test_content_comment_only(self):

def test_indent(self):
bib_database = BibDatabase()
bib_database.entries = [{'id': 'abc123',
'type': 'book',
bib_database.entries = [{'ID': 'abc123',
'ENTRYTYPE': 'book',
'author': 'test'}]
writer = BibTexWriter()
writer.indent = ' '
Expand All @@ -72,8 +72,8 @@ def test_indent(self):

def test_entry_separator(self):
bib_database = BibDatabase()
bib_database.entries = [{'id': 'abc123',
'type': 'book',
bib_database.entries = [{'ID': 'abc123',
'ENTRYTYPE': 'book',
'author': 'test'}]
writer = BibTexWriter()
writer.entry_separator = ''
Expand All @@ -88,12 +88,12 @@ def test_entry_separator(self):

class TestEntrySorting(unittest.TestCase):
bib_database = BibDatabase()
bib_database.entries = [{'id': 'b',
'type': 'article'},
{'id': 'c',
'type': 'book'},
{'id': 'a',
'type': 'book'}]
bib_database.entries = [{'ID': 'b',
'ENTRYTYPE': 'article'},
{'ID': 'c',
'ENTRYTYPE': 'book'},
{'ID': 'a',
'ENTRYTYPE': 'book'}]

def test_sort_default(self):
result = bibtexparser.dumps(self.bib_database)
Expand All @@ -109,35 +109,35 @@ def test_sort_none(self):

def test_sort_id(self):
writer = BibTexWriter()
writer.order_entries_by = ('id', )
writer.order_entries_by = ('ID', )
result = bibtexparser.dumps(self.bib_database, writer)
expected = "@book{a\n}\n\n@article{b\n}\n\n@book{c\n}\n\n"
self.assertEqual(result, expected)

def test_sort_type(self):
writer = BibTexWriter()
writer.order_entries_by = ('type', )
writer.order_entries_by = ('ENTRYTYPE', )
result = bibtexparser.dumps(self.bib_database, writer)
expected = "@article{b\n}\n\n@book{c\n}\n\n@book{a\n}\n\n"
self.assertEqual(result, expected)

def test_sort_type_id(self):
writer = BibTexWriter()
writer.order_entries_by = ('type', 'id')
writer.order_entries_by = ('ENTRYTYPE', 'ID')
result = bibtexparser.dumps(self.bib_database, writer)
expected = "@article{b\n}\n\n@book{a\n}\n\n@book{c\n}\n\n"
self.assertEqual(result, expected)

def test_sort_missing_field(self):
bib_database = BibDatabase()
bib_database.entries = [{'id': 'b',
'type': 'article',
bib_database.entries = [{'ID': 'b',
'ENTRYTYPE': 'article',
'year': '2000'},
{'id': 'c',
'type': 'book',
{'ID': 'c',
'ENTRYTYPE': 'book',
'year': '2010'},
{'id': 'a',
'type': 'book'}]
{'ID': 'a',
'ENTRYTYPE': 'book'}]
writer = BibTexWriter()
writer.order_entries_by = ('year', )
result = bibtexparser.dumps(bib_database, writer)
Expand Down
Loading

0 comments on commit a0b61de

Please sign in to comment.