~ubuntu-branches/ubuntu/maverick/pyfiglet/maverick

« back to all changes in this revision

Viewing changes to pyfiglet.py

  • Committer: Bazaar Package Importer
  • Author(s): Stefano Rivera
  • Date: 2010-01-28 13:00:41 UTC
  • Revision ID: james.westby@ubuntu.com-20100128130041-1bfzkpkap84x1vwq
Tags: 0.4+dfsg-1
Initial release (Closes: #564609)

Show diffs side-by-side

added added

removed removed

Lines of Context:
53
53
meta-data about how it should be displayed by default
54
54
"""
55
55
class FigletFont(object):
56
 
        def __init__(self, dir='.', font='standard'):
 
56
        def __init__(self, dir='.', font='future'):
57
57
                self.dir = dir
58
58
                self.font = font
59
59
 
62
62
                self.width = {}
63
63
                self.data = None
64
64
 
65
 
                self.reMagicNumber = re.compile(r'^flf2.')
 
65
                self.reMagicNumber = re.compile(r'^[ft]lf2.')
66
66
                self.reEndMarker = re.compile(r'(.)\s*$')
67
67
 
68
68
                self.readFontFile()
73
73
        a superclass to create different font sources.
74
74
        """
75
75
        def readFontFile(self):
76
 
                fontPath = '%s/%s.flf' % (self.dir, self.font)
77
 
                if os.path.exists(fontPath) is False:
 
76
                for ext in ('flf', 'tlf'):
 
77
                        fontPath = '%s/%s.%s' % (self.dir, self.font, ext)
 
78
                        if os.path.exists(fontPath):
 
79
                                break
 
80
                else:
78
81
                        raise FontNotFound, "%s doesn't exist" % fontPath
79
82
 
80
83
                try:
86
89
                finally: fo.close()
87
90
 
88
91
        def getFonts(self):
89
 
                return [font[:-4] for font in os.walk(self.dir).next()[2] if font.endswith('.flf')]
 
92
                return [font[:-4] for font in os.walk(self.dir).next()[2]
 
93
                        if font.endswith('.flf') or font.endswith('.tlf')]
90
94
                
91
95
 
92
96
 
104
108
 
105
109
                        header = data.pop(0)
106
110
                        if self.reMagicNumber.search(header) is None:
107
 
                                raise FontError, '%s is not a valid figlet font' % fontPath
 
111
                                raise FontError, '%s is not a valid figlet font' % self.font
108
112
 
109
113
                        header = self.reMagicNumber.sub('', header)
110
114
                        header = header.split()
111
115
                        
112
116
                        if len(header) < 6:
113
 
                                raise FontError, 'malformed header for %s' % fontPath
 
117
                                raise FontError, 'malformed header for %s' % self.font
114
118
 
115
119
                        hardBlank = header[0]
116
120
                        height, baseLine, maxLength, oldLayout, commentLines = map(int, header[1:6])
220
224
Use this Font class if it exists inside of a zipfile.
221
225
"""
222
226
class ZippedFigletFont(FigletFont):
223
 
        def __init__(self, dir='.', font='standard', zipfile='fonts.zip'):
 
227
        def __init__(self, dir='.', font='future', zipfile='fonts.zip'):
224
228
                self.zipfile = zipfile
225
229
                FigletFont.__init__(self, dir=dir, font=font)
226
230
 
228
232
                if os.path.exists(self.zipfile) is False:
229
233
                        raise FontNotFound, "%s doesn't exist" % self.zipfile
230
234
 
231
 
                fontPath = 'fonts/%s.flf' % self.font
232
 
 
233
 
                try:
234
 
                        z = ZipFile(self.zipfile, 'r')
235
 
                        files = z.namelist()
236
 
                        if fontPath not in files:
237
 
                                raise FontNotFound, '%s not found in %s' % (self.font, self.zipfile)
238
 
 
239
 
                        self.data = z.read(fontPath)
240
 
 
241
 
                except Exception, e:
242
 
                        raise FontError, "couldn't open %s: %s" % (fontPath, e)
 
235
                for ext in ('.flf', '.tlf'):
 
236
                        fontPath = 'fonts/%s.%s' % (self.font, ext)
 
237
 
 
238
                        try:
 
239
                                z = ZipFile(self.zipfile, 'r')
 
240
                                files = z.namelist()
 
241
                                if fontPath not in files:
 
242
                                        raise FontNotFound, '%s not found in %s' % (self.font, self.zipfile)
 
243
 
 
244
                                self.data = z.read(fontPath)
 
245
 
 
246
                        except Exception, e:
 
247
                                raise FontError, "couldn't open %s: %s" % (fontPath, e)
243
248
 
244
249
        def getFonts(self):
245
250
                if os.path.exists(self.zipfile) is False:
246
251
                        raise FontNotFound, "%s doesn't exist" % self.zipfile
247
252
 
248
253
                z = ZipFile(self.zipfile, 'r')
249
 
                return [font[6:-4] for font in z.namelist() if font.endswith('.flf')]
 
254
                return [font[6:-4] for font in z.namelist()
 
255
                        if font.endswith('.flf') and font.endswith('.tlf')]
250
256
 
251
257
 
252
258
 
456
462
Main figlet class.
457
463
"""
458
464
class Figlet(object):
459
 
        def __init__(self, dir=None, zipfile=None, font='standard', direction='auto', justify='auto', width=80):
 
465
        def __init__(self, dir=None, zipfile=None, font='future', direction='auto', justify='auto', width=80):
460
466
                self.dir = dir
461
467
                self.font = font
462
468
                self._direction = direction
534
540
 
535
541
        parser = OptionParser(version=__version__, usage='%prog [options] text..')
536
542
 
537
 
        parser.add_option(      '-f', '--font', default='standard',
 
543
        parser.add_option(      '-f', '--font', default='future',
538
544
                                help='font to render with (default: %default)', metavar='FONT' )
539
545
 
540
 
        parser.add_option(      '-d', '--fontdir', default=None,
 
546
        parser.add_option(      '-d', '--fontdir', default='/usr/share/figlet',
541
547
                                help='location of font files', metavar='DIR' )
542
548
 
543
549
        parser.add_option(      '-z', '--zipfile', default=dir+'/fonts.zip',