/usr/lib/python2.7/dist-packages/pybtex/database/output/bibtex.py is in pybtex 0.20.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | # Copyright (c) 2006-2016 Andrey Golovizin
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from pybtex.bibtex.exceptions import BibTeXError
from pybtex.bibtex.utils import scan_bibtex_string
from pybtex.database.output import BaseWriter
class Writer(BaseWriter):
"""Outputs BibTeX markup"""
unicode_io = True
def quote(self, s):
"""
>>> w = Writer()
>>> print w.quote('The World')
"The World"
>>> print w.quote(r'The \emph{World}')
"The \emph{World}"
>>> print w.quote(r'The "World"')
{The "World"}
>>> try:
... print w.quote(r'The {World')
... except BibTeXError, error:
... print error
String has unmatched braces: The {World
"""
self.check_braces(s)
if '"' not in s:
return '"%s"' % s
else:
return '{%s}' % s
def check_braces(self, s):
"""
Raise an exception if the given string has unmatched braces.
>>> w = Writer()
>>> w.check_braces('Cat eats carrots.')
>>> w.check_braces('Cat eats {carrots}.')
>>> w.check_braces('Cat eats {carrots{}}.')
>>> w.check_braces('')
>>> w.check_braces('end}')
>>> try:
... w.check_braces('{')
... except BibTeXError, error:
... print error
String has unmatched braces: {
>>> w.check_braces('{test}}')
>>> try:
... w.check_braces('{{test}')
... except BibTeXError, error:
... print error
String has unmatched braces: {{test}
"""
tokens = list(scan_bibtex_string(s))
if tokens:
end_brace_level = tokens[-1][1]
if end_brace_level != 0:
raise BibTeXError('String has unmatched braces: %s' % s)
def write_stream(self, bib_data, stream):
def write_field(type, value):
stream.write(u',\n %s = %s' % (type, self.quote(value)))
def format_name(person):
def join(l):
return ' '.join([name for name in l if name])
first = person.get_part_as_text('first')
middle = person.get_part_as_text('middle')
prelast = person.get_part_as_text('prelast')
last = person.get_part_as_text('last')
lineage = person.get_part_as_text('lineage')
s = ''
if last:
s += join([prelast, last])
if lineage:
s += ', %s' % lineage
if first or middle:
s += ', '
s += join([first, middle])
return s
def write_persons(persons, role):
# persons = getattr(entry, role + 's')
if persons:
write_field(role, u' and '.join([format_name(person) for person in persons]))
def write_preamble(preamble):
if preamble:
stream.write(u'@preamble{%s}\n\n' % self.quote(preamble))
write_preamble(bib_data.preamble)
first = True
for key, entry in bib_data.entries.iteritems():
if not first:
stream.write(u'\n')
first = False
stream.write(u'@%s' % entry.original_type)
stream.write(u'{%s' % key)
# for role in ('author', 'editor'):
for role, persons in entry.persons.iteritems():
write_persons(persons, role)
for type, value in entry.fields.iteritems():
write_field(type, value)
stream.write(u'\n}\n')
|