Merge lp:~marco-gallotta/ibid/ascii-501638 into lp:~ibid-core/ibid/old-trunk-1.6

Proposed by marcog
Status: Merged
Approved by: Michael Gorven
Approved revision: 827
Merged at revision: 829
Proposed branch: lp:~marco-gallotta/ibid/ascii-501638
Merge into: lp:~ibid-core/ibid/old-trunk-1.6
Diff against target: 115 lines (+38/-17)
1 file modified
ibid/plugins/ascii.py (+38/-17)
To merge this branch: bzr merge lp:~marco-gallotta/ibid/ascii-501638
Reviewer Review Type Date Requested Status
Michael Gorven Approve
Stefano Rivera Approve
marcog Pending
Review via email: mp+16786@code.launchpad.net

This proposal supersedes a proposal from 2010-01-04.

To post a comment you must log in.
Revision history for this message
marcog (marco-gallotta) wrote : Posted in a previous version of this proposal

Made some minor improvements, such as error handling.

Revision history for this message
Stefano Rivera (stefanor) wrote : Posted in a previous version of this proposal

ibid/plugins/ascii.py:6: 'socket' imported but unused
ibid/plugins/ascii.py:8: 'stderr' imported but unused
ibid/plugins/ascii.py:129: redefinition of unused 'stderr' from line 8
ibid/plugins/ascii.py:129: 'stderr' imported but unused

Also, "Sorry, that string is too long!". Can you make that "Sorry that's too long, nobody will be able to read it"

Users don't know about "strings" and they don't need the exclamation mark

review: Needs Fixing
Revision history for this message
marcog (marco-gallotta) wrote : Posted in a previous version of this proposal

Fixed

> ibid/plugins/ascii.py:6: 'socket' imported but unused
> ibid/plugins/ascii.py:8: 'stderr' imported but unused
> ibid/plugins/ascii.py:129: redefinition of unused 'stderr' from line 8
> ibid/plugins/ascii.py:129: 'stderr' imported but unused
>
> Also, "Sorry, that string is too long!". Can you make that "Sorry that's too
> long, nobody will be able to read it"
>
> Users don't know about "strings" and they don't need the exclamation mark

review: Needs Resubmitting
Revision history for this message
Stefano Rivera (stefanor) wrote :

Looks good to go

review: Approve
Revision history for this message
Michael Gorven (mgorven) wrote :

 review approve
 status approved

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'ibid/plugins/ascii.py'
--- ibid/plugins/ascii.py 2009-12-31 17:43:05 +0000
+++ ibid/plugins/ascii.py 2010-01-04 16:02:12 +0000
@@ -1,11 +1,12 @@
1from BaseHTTPServer import BaseHTTPRequestHandler
1from cStringIO import StringIO2from cStringIO import StringIO
2import Image3import Image
3from os import remove4from os import remove
4import os.path5import os.path
5import subprocess6import subprocess
6from sys import stderr
7from tempfile import mkstemp7from tempfile import mkstemp
8from urllib2 import urlopen8from urllib2 import HTTPError, URLError, urlopen
9from urlparse import urlparse
9from zipfile import ZipFile10from zipfile import ZipFile
1011
11from aalib import AsciiScreen12from aalib import AsciiScreen
@@ -43,24 +44,41 @@
4344
44 @match(r'^draw\s+(\S+\.\S+)(\s+in\s+colou?r)?(?:\s+w(?:idth)?\s+(\d+))?(?:\s+h(?:eight)\s+(\d+))?$')45 @match(r'^draw\s+(\S+\.\S+)(\s+in\s+colou?r)?(?:\s+w(?:idth)?\s+(\d+))?(?:\s+h(?:eight)\s+(\d+))?$')
45 def draw(self, event, url, colour, width, height):46 def draw(self, event, url, colour, width, height):
46 f = urlopen(url)47 if not urlparse(url).netloc:
4748 url = 'http://' + url
48 filesize = int(f.info().getheaders('Content-Length')[0])49 if urlparse(url).scheme == 'file':
49 if filesize > self.max_filesize * 1024:50 event.addresponse(u'Are you trying to haxor me?')
50 event.addresponse(u'File too large (limit is %i KiB)', self.max_filesize)51 return
51 return52 if not urlparse(url).path:
5253 url += '/'
54
55 try:
56 f = urlopen(url)
57 except HTTPError, e:
58 event.addresponse(u'Sorry, error fetching URL: %s', BaseHTTPRequestHandler.responses[e.code][0])
59 except URLError:
60 event.addresponse(u'Sorry, error fetching URL')
61
62 content_length = f.info().getheaders('Content-Length')
63 if content_length and int(content_length[0]) > self.max_filesize * 1024:
64 event.addresponse(u'File too large (limit is %i KiB)', self.max_filesize)
65 return
66
67 buffer = f.read(self.max_filesize * 1024)
68 if f.read(1) != '':
69 event.addresponse(u'File too large (limit is %i KiB)', self.max_filesize)
70 return
53 try:71 try:
54 ext = os.path.splitext(url)[1]72 ext = os.path.splitext(url)[1]
55 image = mkstemp(suffix=ext)[1]73 image = mkstemp(suffix=ext)[1]
56 file = open(image, 'w')74 file = open(image, 'w')
57 file.write(f.read())75 file.write(buffer)
58 file.close()76 file.close()
5977
60 try:78 try:
61 img = Image.open(StringIO(open(image, 'r').read())).convert('L')79 img = Image.open(StringIO(open(image, 'r').read())).convert('L')
62 except:80 except IOError:
63 event.addresponse(u'Cannot understand image format')81 event.addresponse(u"Sorry, that doesn't look like an image")
64 return82 return
65 input_width, input_height = img.size[0], img.size[1]83 input_width, input_height = img.size[0], img.size[1]
6684
@@ -100,8 +118,8 @@
100 def draw_aa(self, event, image, width, height):118 def draw_aa(self, event, image, width, height):
101 try:119 try:
102 image = Image.open(StringIO(open(image, 'r').read())).convert('L')120 image = Image.open(StringIO(open(image, 'r').read())).convert('L')
103 except:121 except IOError:
104 event.addresponse(u'Cannot understand image format')122 event.addresponse(u"Sorry, that doesn't look like an image")
105 return123 return
106 screen = AsciiScreen(width=width, height=height)124 screen = AsciiScreen(width=width, height=height)
107 image = image.resize(screen.virtual_size)125 image = image.resize(screen.virtual_size)
@@ -109,7 +127,6 @@
109 event.addresponse(unicode(screen.render()), address=False, conflate=False)127 event.addresponse(unicode(screen.render()), address=False, conflate=False)
110128
111 def draw_caca(self, event, image, width, height):129 def draw_caca(self, event, image, width, height):
112 from sys import stderr
113 process = subprocess.Popen(130 process = subprocess.Popen(
114 [self.img2txt_bin, '-f', 'irc', '-W', str(width), '-H', str(height), image],131 [self.img2txt_bin, '-f', 'irc', '-W', str(width), '-H', str(height), image],
115 shell=False, stdout=subprocess.PIPE)132 shell=False, stdout=subprocess.PIPE)
@@ -118,14 +135,15 @@
118 if code == 0:135 if code == 0:
119 event.addresponse(unicode(response.replace('\r', '')), address=False, conflate=False)136 event.addresponse(unicode(response.replace('\r', '')), address=False, conflate=False)
120 else:137 else:
121 event.addresponse(u'Sorry, cannot understand image format')138 event.addresponse(u"Sorry, that doesn't look like an image")
122139
123class WriteFiglet(Processor):140class WriteFiglet(Processor):
124 u"""figlet <text> [in <font>]141 u"""figlet <text> [in <font>]
125 list figlet fonts [from <index>]"""142 list figlet fonts [from <index>]"""
126 feature = 'figlet'143 feature = 'figlet'
127144
128 fonts_zip = Option('fonts_zip', 'Zip file containing figlet fonts', 'data/figlet-fonts.zip')145 max_width = IntOption('max_width', 'Maximum width for ascii output', 60)
146 fonts_zip = Option('fonts_zip', 'Zip file containing figlet fonts', 'ibid/data/figlet-fonts.zip')
129147
130 def __init__(self, name):148 def __init__(self, name):
131 Processor.__init__(self, name)149 Processor.__init__(self, name)
@@ -158,4 +176,7 @@
158 del rendered[0]176 del rendered[0]
159 while rendered and rendered[-1].strip() == '':177 while rendered and rendered[-1].strip() == '':
160 del rendered[-1]178 del rendered[-1]
179 if rendered and len(rendered[0]) > self.max_width:
180 event.addresponse(u"Sorry that's too long, nobody will be able to read it")
181 return
161 event.addresponse(unicode('\n'.join(rendered)), address=False, conflate=False)182 event.addresponse(unicode('\n'.join(rendered)), address=False, conflate=False)

Subscribers

People subscribed via source and target branches