add tests for pubkey generation

main
Jonas Kulhanek 5 years ago
parent d2ecdd5669
commit d073e60711

@ -61,6 +61,18 @@ class CitekeyFormatter(Formatter):
def __init__(self): def __init__(self):
super(CitekeyFormatter, self).__init__() super(CitekeyFormatter, self).__init__()
def format_field(self, val, fmt):
if len(fmt) > 0 and fmt[0] == 'u':
s = str(val).upper()
fmt = fmt[1:]
elif len(fmt) > 0 and fmt[0] == 'l':
s = str(val).lower()
fmt = fmt[1:]
else:
s = val
return str2citekey(s.__format__(fmt))
def get_value(self, key, args, kwds): def get_value(self, key, args, kwds):
if len(args) == 0: if len(args) == 0:
raise ValueError('Must pass bibtex entry to the format method') raise ValueError('Must pass bibtex entry to the format method')
@ -73,41 +85,24 @@ class CitekeyFormatter(Formatter):
key = 'author' key = 'author'
if key == 'first_word' and 'title' in entry: if key == 'first_word' and 'title' in entry:
return CitekeyFormatter._U(entry['title'].split(' ')[0]) return entry['title'].split(' ')[0]
if key == 'author_last_name' and 'author' in entry: if key == 'author_last_name' and 'author' in entry:
return CitekeyFormatter._U(author_last(entry['author'][0])) return author_last(entry['author'][0])
else: else:
if key in entry: if key in entry:
return CitekeyFormatter._U(entry[key]) return entry[key]
else: else:
raise ValueError( raise ValueError(
"No {} defined: cannot generate a citekey.".format(okey)) "No {} defined: cannot generate a citekey.".format(okey))
class _U(str): def generate_citekey(bibdata, format_string='{author_last_name}{year}'):
def __init__(self, value):
super(CitekeyFormatter._U, self).__init__(value)
def __format__(self, fmt):
val = self
if len(fmt) > 0 and fmt[0] == 'u':
s = val.upper()
fmt = fmt[1:]
elif len(fmt) > 0 and fmt[0] == 'l':
s = val.lower()
fmt = fmt[1:]
else:
s = str(val)
return s.__format__(fmt)
def generate_citekey(bibdata, format_string='{author_last_name:l}{year}{first_word:l}'):
""" Generate a citekey from bib_data. """ Generate a citekey from bib_data.
:raise ValueError: if no author nor editor is defined. :raise ValueError: if no author nor editor is defined.
""" """
citekey, entry = get_entry(bibdata) citekey, entry = get_entry(bibdata)
citekey = CitekeyFormatter().format(format_string, entry) citekey = CitekeyFormatter().format(format_string, entry)
return str2citekey(citekey) return citekey
def extract_docfile(bibdata, remove=False): def extract_docfile(bibdata, remove=False):

@ -34,15 +34,21 @@ debug = boolean(default=False)
# If true the citekey is normalized using the 'citekey_format' on adding new publications. # If true the citekey is normalized using the 'citekey_format' on adding new publications.
normalize_citekey = boolean(default=False) normalize_citekey = boolean(default=False)
# String specifying how to format the citekey. The following # String specifying how to format the citekey. All strings of
# the form '{{substitution:modifier}}' and '{{substitution}}' will
# be substituted with their appropriate values. The following
# substitutions are used: # substitutions are used:
# %a: last name of the first author in lowercase # author_last_name: last name of the first author
# %A: last name of the first author in PascalCase # year: year of publication
# %Y: four letter year of release (e.g. 2019) # first_word: first word of the title
# %y: two last letters of release year (e.g. 19) # modifiers:
# %w: first word in the title in lowercase # l: converts the text to lowercase
# %W: first work in the title # u: converts the text to uppercase
citekey_format = string(default='%a%Y') # examples:
# {{author_last_name:l}}{{year}} generates 'yang2020'
# {{author_last_name}}{{year}}{{first_word}} generates 'Yang2020Towards'
# {{author_last_name:u}}{{year}} generates 'YANG2020'
citekey_format = string(default='{{author_last_name:l}}{{year}}')
[formating] [formating]

@ -32,6 +32,46 @@ class TestGenerateCitekey(unittest.TestCase):
key = bibstruct.generate_citekey(bibentry) key = bibstruct.generate_citekey(bibentry)
self.assertEqual(key, 'Salinger1961') self.assertEqual(key, 'Salinger1961')
def test_no_modifier(self):
template = '{author_last_name}{year}'
bibentry = copy.deepcopy(fixtures.doe_bibentry)
key = bibstruct.generate_citekey(bibentry, template)
self.assertEqual(key, 'Doe2013')
bibentry = copy.deepcopy(fixtures.franny_bibentry)
key = bibstruct.generate_citekey(bibentry, template)
self.assertEqual(key, 'Salinger1961')
def test_all_keys(self):
template = '{author_last_name}-{year}-{first_word}'
bibentry = copy.deepcopy(fixtures.doe_bibentry)
key = bibstruct.generate_citekey(bibentry, template)
self.assertEqual(key, 'Doe-2013-Nice')
bibentry = copy.deepcopy(fixtures.franny_bibentry)
key = bibstruct.generate_citekey(bibentry, template)
self.assertEqual(key, 'Salinger-1961-Franny')
def test_l_modifier(self):
template = '{author_last_name:l}{year:l}'
bibentry = copy.deepcopy(fixtures.doe_bibentry)
key = bibstruct.generate_citekey(bibentry, template)
self.assertEqual(key, 'doe2013')
bibentry = copy.deepcopy(fixtures.franny_bibentry)
key = bibstruct.generate_citekey(bibentry, template)
self.assertEqual(key, 'salinger1961')
def test_u_modifier(self):
template = '{author_last_name:u}{year:u}'
bibentry = copy.deepcopy(fixtures.doe_bibentry)
key = bibstruct.generate_citekey(bibentry, template)
self.assertEqual(key, 'DOE2013')
bibentry = copy.deepcopy(fixtures.franny_bibentry)
key = bibstruct.generate_citekey(bibentry, template)
self.assertEqual(key, 'SALINGER1961', template)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

Loading…
Cancel
Save