Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Py3k #21

Merged
merged 7 commits into from
Mar 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ language: python
python:
- "2.6"
- "2.7"
- "3.3"
- "pypy"
before_install:
- sudo apt-get update -qq
Expand Down
31 changes: 17 additions & 14 deletions pyfiglet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Python FIGlet adaption
"""

from __future__ import print_function
import pkg_resources
import re
import sys
Expand Down Expand Up @@ -35,11 +36,11 @@


def figlet_format(text, font=DEFAULT_FONT, **kwargs):
fig = Figlet(font)
return fig.renderText(text, **kwargs)
fig = Figlet(font, **kwargs)
return fig.renderText(text)

def print_figlet(text, font=DEFAULT_FONT, **kwargs):
print figlet_format(text, font, **kwargs)
print(figlet_format(text, font, **kwargs))


class FigletError(Exception):
Expand Down Expand Up @@ -88,7 +89,9 @@ def preloadFont(cls, font):
for extension in ('tlf', 'flf'):
fn = '%s.%s' % (font, extension)
if pkg_resources.resource_exists('pyfiglet.fonts', fn):
return pkg_resources.resource_string('pyfiglet.fonts', fn)
data = pkg_resources.resource_string('pyfiglet.fonts', fn)
data = data.decode('ascii', 'replace')
return data
else:
raise FontNotFound(font)

Expand All @@ -98,7 +101,7 @@ def getFonts(cls):
in pkg_resources.resource_listdir('pyfiglet', 'fonts')
if font.endswith(('.flf', '.tlf'))
and cls.reMagicNumber.search(pkg_resources.resource_stream(
'pyfiglet.fonts', font).readline())]
'pyfiglet.fonts', font).readline().decode('ascii', 'replace'))]

@classmethod
def infoFont(cls, font, short=False):
Expand Down Expand Up @@ -131,7 +134,7 @@ def loadFont(self):

header = self.reMagicNumber.sub('', header)
header = header.split()

if len(header) < 6:
raise FontError('malformed header for %s' % self.font)

Expand Down Expand Up @@ -322,11 +325,11 @@ def smushAmount(self, left=None, right=None, buffer=[], curChar=[]):
lineRight = curChar[row]
if self.base.direction == 'right-to-left':
lineLeft, lineRight = lineRight, lineLeft

linebd = len(lineLeft.rstrip()) - 1
if linebd < 0:
linebd = 0

if linebd < len(lineLeft):
ch1 = lineLeft[linebd]
else:
Expand Down Expand Up @@ -360,7 +363,7 @@ def render(self, text):
buffer = ['' for i in range(self.base.Font.height)]

for c in map(ord, list(text)):
if self.base.Font.chars.has_key(c) is False: continue
if not c in self.base.Font.chars: continue
curChar = self.base.Font.chars[c]
self.curCharWidth = self.base.Font.width[c]
maxSmush = self.smushAmount(buffer=buffer, curChar=curChar)
Expand Down Expand Up @@ -428,9 +431,9 @@ def __init__(self, font=DEFAULT_FONT, direction='auto', justify='auto',
self.engine = FigletRenderingEngine(base=self)

def setFont(self, **kwargs):
if kwargs.has_key('font'):
if 'font' in kwargs:
self.font = kwargs['font']

self.Font = FigletFont(font=self.font)

def getDirection(self):
Expand Down Expand Up @@ -485,11 +488,11 @@ def main():
opts, args = parser.parse_args()

if opts.list_fonts:
print FigletFont.getFonts()
print(FigletFont.getFonts())
exit(0)

if opts.info_font:
print FigletFont.infoFont(opts.font)
print(FigletFont.infoFont(opts.font))
exit(0)

if len(args) == 0:
Expand All @@ -506,7 +509,7 @@ def main():
r = f.renderText(text)
if opts.reverse is True: r = r.reverse()
if opts.flip is True: r = r.flip()
print r
print(r)

return 0

Expand Down
21 changes: 11 additions & 10 deletions pyfiglet/test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python

from __future__ import print_function
import os.path
import sys
from optparse import OptionParser
Expand All @@ -10,7 +11,7 @@

def dump(text):
for line in text.split('\n'):
print repr(line)
print(repr(line))

def main():
parser = OptionParser(version=__version__)
Expand Down Expand Up @@ -41,30 +42,30 @@ def main():
elif os.path.isfile(fontpath + '.tlf'):
cmd = ('toilet', '-d', 'pyfiglet/fonts', '-f', font, 'foo')
else:
raise Exception('Missing font file')
raise Exception('Missing font file: '+fontpath)

p = Popen(cmd, bufsize=1,stdout=PIPE)
outputFiglet = p.communicate()[0]
outputFiglet = p.communicate()[0].decode('ascii', 'replace')

if outputPyfiglet == outputFiglet:
print '[OK] %s' % font
print('[OK] %s' % font)
ok += 1
continue

print '[FAIL] %s' % font
print('[FAIL] %s' % font)
fail += 1
failed.append(font)

if opts.show is True:
print '[PYTHON] *** %s\n\n' % font
print('[PYTHON] *** %s\n\n' % font)
dump(outputPyfiglet)
print '[FIGLET] *** %s\n\n' % font
print('[FIGLET] *** %s\n\n' % font)
dump(outputFiglet)
raw_input()

print 'OK = %d, FAIL = %d' % (ok, fail)
print('OK = %d, FAIL = %d' % (ok, fail))
if len(failed) > 0:
print 'FAILED = %s' % repr(failed)
print('FAILED = %s' % repr(failed))

return 0

Expand Down