Commit a6d43a6e authored by Andrey Golovizin's avatar Andrey Golovizin

Merge branch 'six'

parents 15755fab c8db2919
# Taken from jinja2. Thanks, Armin Ronacher.
# See also http://lucumr.pocoo.org/2010/2/11/porting-to-python-3-a-guide
from lib2to3 import fixer_base
class FixAltUnicode(fixer_base.BaseFix):
PATTERN = "'__unicode__'"
def transform(self, node, results):
new = node.clone()
new.value = '__str__'
return new
......@@ -24,7 +24,7 @@
"""Generate man pages for pybtex and pybtex-convert.
"""
from __future__ import with_statement, print_function
from __future__ import print_function, with_statement
import os
import sys
......
......@@ -25,13 +25,17 @@ from __future__ import absolute_import
import re
from string import ascii_letters, digits
from pygments.style import Style
from pygments.lexer import RegexLexer, ExtendedRegexLexer, include, default, words
from pygments.token import Keyword, Name, Comment, String, Error, \
Number, Operator, Generic, Literal, Punctuation, Text
from pybtex.database.input.bibtex import NAME_CHARS
from pygments.lexer import (
ExtendedRegexLexer, RegexLexer, default, include, words
)
from pygments.style import Style
from pygments.token import (
Comment, Error, Generic, Keyword, Literal, Name, Number, Operator,
Punctuation, String, Text
)
from . import add_entry_point
......@@ -93,7 +97,7 @@ class BibTeXLexer(ExtendedRegexLexer):
filenames = ['*.bib']
flags = re.IGNORECASE
IDENTIFIER = ur'[{0}][{1}]*'.format(re.escape(NAME_CHARS), re.escape(NAME_CHARS + digits))
IDENTIFIER = r'[{0}][{1}]*'.format(re.escape(NAME_CHARS), re.escape(NAME_CHARS + digits))
def open_brace_callback(self, match, ctx):
opening_brace = match.group()
......@@ -200,7 +204,7 @@ class BSTLexer(RegexLexer):
(r'[^#\"\{\}\s]+\$', Name.Builtin),
(r'[^#\"\{\}\s]+', Name.Variable),
(r'"[^\"]*"', String),
(ur'#-?\d+', Number),
(r'#-?\d+', Number),
('{', Text.Punctuation, ('group-end', 'body')),
default('#pop'),
],
......
......@@ -12,8 +12,8 @@
# All configuration values have a default; values that are commented out
# serve to show the default.
import sys
import os
import sys
from datetime import datetime
from pybtex import __version__
......
......@@ -5,6 +5,10 @@ Reading and writing bibliography data
.. testsetup:: *
from __future__ import unicode_literals, print_function
import six
from pybtex.database import BibliographyData, Entry, Person, parse_string
......@@ -35,7 +39,7 @@ Here is a quick example:
>>> from pybtex.database import parse_file
>>> bib_data = parse_file('../examples/tugboat/tugboat.bib')
>>> print bib_data.entries['Knuth:TB8-1-14'].fields['title']
>>> print(bib_data.entries['Knuth:TB8-1-14'].fields['title'])
Mixing right-to-left texts with left-to-right texts
>>> for author in bib_data.entries['Knuth:TB8-1-14'].persons['author']:
... print(unicode(author))
......@@ -65,7 +69,7 @@ symmetrical to the functions described above:
... ('year', '1986'),
... ]),
... })
>>> print bib_data.to_string('bibtex')
>>> print(bib_data.to_string('bibtex'))
@article{article-minimal,
author = "L[eslie] B. Lamport",
title = "The Gnats and Gnus Document Preparation System",
......
......@@ -5,6 +5,10 @@ Designing styles
.. testsetup:: *
from __future__ import unicode_literals, print_function
import six
from pybtex.richtext import Text, String, Tag, HRef, Protected, Symbol, nbsp, textutils
......@@ -38,9 +42,9 @@ The basic workflow is:
>>> from pybtex.richtext import Text, Tag
>>> text = Text('How to be ', Tag('em', 'a cat'), '.')
>>> print text.render_as('html')
>>> print(text.render_as('html'))
How to be <em>a cat</em>.
>>> print text.render_as('latex')
>>> print(text.render_as('latex'))
How to be \emph{a cat}.
......@@ -82,7 +86,7 @@ is represented by this object tree:
... Tag('em', 'comprehensive'),
... '.',
... )
>>> print text.render_as('html')
>>> print(text.render_as('html'))
<a href="http://ctan.org/"><em>Comprehensive</em> TeX Archive Network</a> is <em>comprehensive</em>.
:py:class:`Protected` represents a "protected" piece of text, something like
......
......@@ -12,8 +12,8 @@
# All configuration values have a default; values that are commented out
# serve to show the default.
import sys
import os
import sys
from datetime import datetime
from pybtex import __version__
......
from setuptools import setup
setup(name='Sample Pybtex plugins',
author='Andrey Golovizin',
py_modules=['toyplugins'],
......
......@@ -25,11 +25,10 @@
import imp
from pybtex.utils import OrderedCaseInsensitiveDict
from pybtex.database.output import BaseWriter
from pybtex.database import BibliographyData, Entry, FieldDict, Person
from pybtex.database.input import BaseParser
from pybtex.database import BibliographyData, Entry, Person, FieldDict
from pybtex.database.output import BaseWriter
from pybtex.utils import OrderedCaseInsensitiveDict
class PythonWriter(BaseWriter):
......
......@@ -23,6 +23,7 @@
"""
from __future__ import absolute_import
from __future__ import unicode_literals
from os import path
......
......@@ -21,6 +21,8 @@
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from __future__ import unicode_literals
from os import path
from pybtex.cmdline import CommandLine, make_option, standard_option
......@@ -96,7 +98,7 @@ It is also possible to define bibliography formatting styles in Python.
'abbreviate_names': 'abbreviated names',
}
if style_language != 'python':
for option, what_is_not_supported in not_supported_by_bibtex.iteritems():
for option, what_is_not_supported in not_supported_by_bibtex.items():
if options[option]:
self.opt_parser.error(
'%s are only supported by the Pythonic style engine (-l python)' % what_is_not_supported
......
......@@ -19,9 +19,11 @@
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from __future__ import unicode_literals
import warnings
from pybtex import __version__ as version # noqa
msg = 'pybtex.__version__.version is deprecated since 0.19: use pybtex.__version__ attribute'
warnings.warn(msg, DeprecationWarning)
from pybtex import __version__ as version # noqa
......@@ -22,15 +22,17 @@
"""parse LaTeX aux file
"""
from __future__ import with_statement
from __future__ import unicode_literals, with_statement
import re
import pybtex.io
from pybtex.exceptions import PybtexError
from pybtex.errors import report_error
from pybtex.exceptions import PybtexError
from pybtex import py3compat
@py3compat.python_2_unicode_compatible
class AuxDataError(PybtexError):
def __init__(self, message, context=None):
super(AuxDataError, self).__init__(message, context.filename)
......@@ -41,8 +43,8 @@ class AuxDataError(PybtexError):
marker = '^' * len(self.context.line)
return self.context.line + '\n' + marker
def __unicode__(self):
base_message = super(AuxDataError, self).__unicode__()
def __str__(self):
base_message = py3compat.__str__(super(AuxDataError, self))
lineno = self.context.lineno
location = 'in line {0}: '.format(lineno) if lineno else ''
return location + base_message
......
from __future__ import unicode_literals
# Copyright (c) 2006-2017 Andrey Golovigin
#
# Permission is hereby granted, free of charge, to any person obtaining
......@@ -19,6 +20,8 @@
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import six
import pybtex.io
from pybtex.plugin import Plugin
......@@ -39,7 +42,7 @@ class BaseBackend(Plugin):
tags[u'tt'] : typewrite text, not semantic
"""
RenderType = basestring #: the result of render and render_sequence
RenderType = six.string_types #: the result of render and render_sequence
default_suffix = None #: the default suffix for an output file
def __init__(self, encoding=None):
......
......@@ -27,20 +27,21 @@ HTML output backend.
>>> from pybtex.richtext import Tag, HRef
>>> html = Backend()
>>> print Tag('em', '').render(html)
>>> print(Tag('em', '').render(html))
<BLANKLINE>
>>> print Tag('em', 'Hard &', ' heavy').render(html)
>>> print(Tag('em', 'Hard &', ' heavy').render(html))
<em>Hard &amp; heavy</em>
>>> print HRef('/', '').render(html)
>>> print(HRef('/', '').render(html))
<BLANKLINE>
>>> print HRef('/', 'Hard & heavy').render(html)
>>> print(HRef('/', 'Hard & heavy').render(html))
<a href="/">Hard &amp; heavy</a>
"""
from __future__ import unicode_literals
from xml.sax.saxutils import escape
from pybtex.backends import BaseBackend
import pybtex.io
from pybtex.backends import BaseBackend
PROLOGUE = u"""<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
......@@ -56,7 +57,7 @@ PROLOGUE = u"""<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
class Backend(BaseBackend):
u"""
>>> from pybtex.richtext import Text, Tag, Symbol
>>> print Tag('em', Text(u'Л.:', Symbol('nbsp'), u'<<Химия>>')).render(Backend())
>>> print(Tag('em', Text(u'Л.:', Symbol('nbsp'), u'<<Химия>>')).render(Backend()))
<em>Л.:&nbsp;&lt;&lt;Химия&gt;&gt;</em>
"""
......@@ -72,13 +73,13 @@ class Backend(BaseBackend):
return escape(text)
def format_protected(self, text):
return ur'<span class="bibtex-protected">{}</span>'.format(text)
return r'<span class="bibtex-protected">{}</span>'.format(text)
def format_tag(self, tag, text):
return ur'<{0}>{1}</{0}>'.format(tag, text) if text else u''
return r'<{0}>{1}</{0}>'.format(tag, text) if text else u''
def format_href(self, url, text):
return ur'<a href="{0}">{1}</a>'.format(url, text) if text else u''
return r'<a href="{0}">{1}</a>'.format(url, text) if text else u''
def write_prologue(self):
encoding = self.encoding or pybtex.io.get_default_encoding()
......
......@@ -20,28 +20,27 @@
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
r"""
"""
LaTeX output backend.
>>> from pybtex.richtext import Tag, HRef
>>> latex = Backend()
>>> print Tag('em', '').render(latex)
>>> print(Tag('em', '').render(latex))
<BLANKLINE>
>>> print Tag('em', 'Non-', 'empty').render(latex)
\emph{Non-empty}
>>> print HRef('/', '').render(latex)
>>> print(Tag('em', 'Non-', 'empty').render(latex))
\\emph{Non-empty}
>>> print(HRef('/', '').render(latex))
<BLANKLINE>
>>> print HRef('/', 'Non-', 'empty').render(latex)
\href{/}{Non-empty}
>>> print HRef('http://example.org/', 'http://example.org/').render(latex)
\url{http://example.org/}
>>> print(HRef('/', 'Non-', 'empty').render(latex))
\\href{/}{Non-empty}
>>> print(HRef('http://example.org/', 'http://example.org/').render(latex))
\\url{http://example.org/}
"""
from __future__ import unicode_literals
import codecs
import latexcodec # noqa
from pybtex.backends import BaseBackend
......@@ -72,7 +71,7 @@ class Backend(BaseBackend):
if tag is None:
return u'{%s}' % text if text else u''
else:
return ur'\%s{%s}' % (tag, text) if text else u''
return r'\%s{%s}' % (tag, text) if text else u''
def format_href(self, url, text):
if not text:
......@@ -80,12 +79,12 @@ class Backend(BaseBackend):
elif text == self.format_str(url):
return u'\\url{%s}' % url
else:
return ur'\href{%s}{%s}' % (url, text) if text else u''
return r'\href{%s}{%s}' % (url, text) if text else u''
def format_protected(self, text):
"""
>>> from pybtex.richtext import Protected
>>> print Protected('CTAN').render_as('latex')
>>> print(Protected('CTAN').render_as('latex'))
{CTAN}
"""
......
......@@ -23,27 +23,26 @@
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ur"""
r"""
Markdown output backend.
>>> from pybtex.richtext import Tag, HRef
>>> markdown = Backend()
>>> print Tag('em', '').render(markdown)
>>> print(Tag('em', '').render(markdown))
<BLANKLINE>
>>> print Tag('em', 'Non-', 'empty').render(markdown)
>>> print(Tag('em', 'Non-', 'empty').render(markdown))
*Non\-empty*
>>> print HRef('/', '').render(markdown)
>>> print(HRef('/', '').render(markdown))
<BLANKLINE>
>>> print HRef('/', 'Non-', 'empty').render(markdown)
>>> print(HRef('/', 'Non-', 'empty').render(markdown))
[Non\-empty](/)
"""
from __future__ import unicode_literals
from xml.sax.saxutils import escape
from pybtex.backends import BaseBackend
SPECIAL_CHARS = [
u'\\', # backslash
u'`', # backtick
......@@ -109,10 +108,10 @@ class Backend(BaseBackend):
if tag is None:
return text
else:
return ur'{0}{1}{0}'.format(tag, text) if text else u''
return r'{0}{1}{0}'.format(tag, text) if text else u''
def format_href(self, url, text):
return ur'[%s](%s)' % (text, url) if text else u''
return r'[%s](%s)' % (text, url) if text else u''
def write_entry(self, key, label, text):
# Support http://www.michelf.com/projects/php-markdown/extra/#def-list
......
from __future__ import unicode_literals
from pybtex.backends import BaseBackend
# Copyright (c) 2006-2017 Andrey Golovigin
#
# Permission is hereby granted, free of charge, to any person obtaining
......@@ -19,7 +24,6 @@
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from pybtex.backends import BaseBackend
class Backend(BaseBackend):
......
......@@ -20,6 +20,7 @@
"""BibTeX unnamed stack language interpreter and related stuff
"""
from __future__ import unicode_literals
from os import path
......
......@@ -19,10 +19,18 @@
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from __future__ import unicode_literals
import re
from pybtex.bibtex.interpreter import (Integer, String, QuotedVar,
Identifier, FunctionLiteral)
import pybtex.io
from pybtex.bibtex.interpreter import (
FunctionLiteral, Identifier, Integer, QuotedVar, String
)
from pybtex.scanner import (
Literal, Pattern, PybtexSyntaxError, Scanner, TokenRequired
)
#ParserElement.enablePackrat()
......@@ -44,23 +52,23 @@ def process_function(toks):
return FunctionLiteral(toks[0])
quote_or_comment = re.compile(ur'[%"]')
quote_or_comment = re.compile(r'[%"]')
def strip_comment(line):
"""Strip the commented part of the line."
>>> print strip_comment('a normal line')
>>> print(strip_comment('a normal line'))
a normal line
>>> print strip_comment('%')
>>> print(strip_comment('%'))
<BLANKLINE>
>>> print strip_comment('%comment')
>>> print(strip_comment('%comment'))
<BLANKLINE>
>>> print strip_comment('trailing%')
>>> print(strip_comment('trailing%'))
trailing
>>> print strip_comment('a normal line% and a comment')
>>> print(strip_comment('a normal line% and a comment'))
a normal line
>>> print strip_comment('"100% compatibility" is a myth')
>>> print(strip_comment('"100% compatibility" is a myth'))
"100% compatibility" is a myth
>>> print strip_comment('"100% compatibility" is a myth% or not?')
>>> print(strip_comment('"100% compatibility" is a myth% or not?'))
"100% compatibility" is a myth
"""
......@@ -79,18 +87,14 @@ def strip_comment(line):
return line
from pybtex.scanner import (
Scanner, Pattern, Literal,
TokenRequired, PybtexSyntaxError,
)
class BstParser(Scanner):
LBRACE = Literal(u'{')
RBRACE = Literal(u'}')
STRING = Pattern(ur'"[^\"]*"', 'string')
INTEGER = Pattern(ur'#-?\d+', 'integer')
NAME = Pattern(ur'[^#\"\{\}\s]+', 'name')
LBRACE = Literal('{')
RBRACE = Literal('}')
STRING = Pattern('"[^\"]*"', 'string')
INTEGER = Pattern(r'#-?\d+', 'integer')
NAME = Pattern(r'[^#\"\{\}\s]+', 'name')
COMMANDS = {
'ENTRY': 3,
......
......@@ -23,17 +23,18 @@
CAUTION: functions should PUSH results, not RETURN
"""
from __future__ import print_function
from __future__ import print_function, unicode_literals
from functools import update_wrapper
import six
import pybtex.io
from pybtex.errors import report_error
from pybtex.bibtex.exceptions import BibTeXError
from pybtex.utils import memoize
from pybtex.bibtex import utils
from pybtex.bibtex.exceptions import BibTeXError
from pybtex.bibtex.names import format_name as format_bibtex_name
from pybtex.errors import report_error
from pybtex.utils import memoize
def print_warning(msg):
......@@ -207,7 +208,7 @@ def if_(i):
def int_to_chr(i):
n = i.pop()
try:
char = unichr(n)
char = six.unichr(n)
except ValueError:
raise BibTeXError('%i passed to int.to.chr$', n)
i.push(char)
......
......@@ -19,7 +19,10 @@
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from __future__ import unicode_literals
from pybtex.exceptions import PybtexError
class BibTeXError(PybtexError):
pass
from __future__ import print_function
# Copyright (c) 2006-2017 Andrey Golovigin
#
# Permission is hereby granted, free of charge, to any person obtaining
......@@ -20,10 +19,15 @@ from __future__ import print_function
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from pybtex.utils import CaseInsensitiveDict
from pybtex.bibtex.exceptions import BibTeXError
from __future__ import print_function, unicode_literals
import six
from pybtex.bibtex.builtins import builtins, print_warning
from pybtex.bibtex.exceptions import BibTeXError
from pybtex.bibtex.utils import wrap
from pybtex.utils import CaseInsensitiveDict
#from pybtex.database.input import bibtex
......@@ -81,7 +85,7 @@ class EntryInteger(Integer, EntryVariable):
class String(Variable):
value_type = basestring
value_type = six.string_types
default = ''
......@@ -127,7 +131,7 @@ class Crossref(Field):
class Identifier(Variable):
value_type = basestring
value_type = six.string_types
def execute(self, interpreter):
try:
f = interpreter.vars[self.value()]
......@@ -137,7 +141,7 @@ class Identifier(Variable):
class QuotedVar(Variable):
value_type = basestring
value_type = six.string_types
def execute(self, interpreter):
try:
var = interpreter.vars[self.value()]
......@@ -284,9 +288,9 @@ class Interpreter(object):
self.bib_data = p.parse_files(self.bib_files)
self.citations = self.bib_data.add_extra_citations(self.citations, self.min_crossrefs)
self.citations = list(self.remove_missing_citations(self.citations))
# for k, v in self.bib_data.iteritems():
# for k, v in self.bib_data.items():
# print k
# for field, value in v.fields.iteritems():
# for field, value in v.fields.items():
# print '\t', field, value
# pass
......
......@@ -22,33 +22,33 @@
"""BibTeX-like name formatting.
>>> name = 'Charles Louis Xavier Joseph de la Vallee Poussin'
>>> print format_name(name, '{vv~}{ll}{, jj}{, f.}')
>>> print(format_name(name, '{vv~}{ll}{, jj}{, f.}'))
de~la Vallee~Poussin, C.~L. X.~J.
>>> name = 'abc'
>>> print format_name(name, '{vv~}{ll}{, jj}{, f.}')
>>> print(format_name(name, '{vv~}{ll}{, jj}{, f.}'))
abc
>>> name = 'Jean-Pierre Hansen'
>>> print format_name(name, '{ff~}{vv~}{ll}{, jj}')
>>> print(format_name(name, '{ff~}{vv~}{ll}{, jj}'))
Jean-Pierre Hansen
>>> print format_name(name, '{f.~}{vv~}{ll}{, jj}')
>>> print(format_name(name, '{f.~}{vv~}{ll}{, jj}'))
J.-P. Hansen
>>> name = 'F. Phidias Phony-Baloney'
>>> print format_name(name, '{v{}}{l}')
>>> print(format_name(name, '{v{}}{l}'))
P.-B
>>> print format_name(name, '{v{}}{l.}')
>>> print(format_name(name, '{v{}}{l.}'))
P.-B.
>>> print format_name(name, '{v{}}{l{}}')
>>> print(format_name(name, '{v{}}{l{}}'))
PB
"""
from __future__ import unicode_literals
import re
from pybtex.bibtex.utils import bibtex_abbreviate, bibtex_len
from pybtex.database import Person
from pybtex.bibtex.utils import bibtex_len, bibtex_abbreviate
from pybtex.scanner import (
Scanner, Pattern, Literal,
PybtexSyntaxError, PrematureEOF
Literal, Pattern, PrematureEOF, PybtexSyntaxError, Scanner
)
......@@ -259,9 +259,9 @@ def join(words, tie='~', space=' '):
Otherwise space is inserted.
Should produce the same oubput as BibTeX.
>>> print join(['a', 'long', 'long', 'road'])
>>> print(join(['a', 'long', 'long', 'road']))
a~long long~road
>>> print join(['very', 'long', 'phrase'])
>>> print(join(['very', 'long', 'phrase']))
very long~phrase
"""
......@@ -286,9 +286,9 @@ class UnbalancedBraceError(PybtexSyntaxError):
class NameFormatParser(Scanner):
LBRACE = Literal(u'{')
RBRACE = Literal(u'}')
TEXT = Pattern(ur'[^{}]+', 'text')
NON_LETTERS = Pattern(ur'[^{}\w]|\d+', 'non-letter characters', flags=re.IGNORECASE | re.UNICODE)
FORMAT_CHARS = Pattern(ur'[^\W\d_]+', 'format chars', flags=re.IGNORECASE | re.UNICODE)
TEXT = Pattern(r'[^{}]+', 'text')
NON_LETTERS = Pattern(r'[^{}\w]|\d+', 'non-letter characters', flags=re.IGNORECASE | re.UNICODE)
FORMAT_CHARS = Pattern(r'[^\W\d_]+', 'format chars', flags=re.IGNORECASE | re.UNICODE)
lineno = None
......
This diff is collapsed.
......@@ -7,6 +7,7 @@
# Font style: Roman
# Font version: Version 0.7.0
from __future__ import unicode_literals
charwidths = {u' ': 278,
u'!': 278,
u'"': 500,
......
......@@ -25,7 +25,10 @@
"""
Make a table of character widths to use with bibtex width$ function.
"""
from __future__ import print_function
from __future__ import print_function, unicode_literals