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

Proposed by marcog on 2010-01-04
Status: Merged
Approved by: Michael Gorven on 2010-01-05
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 2010-01-04 Approve on 2010-01-05
Stefano Rivera 2010-01-04 Approve on 2010-01-04
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.
marcog (marco-gallotta) wrote : Posted in a previous version of this proposal

Made some minor improvements, such as error handling.

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
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: Resubmit
Stefano Rivera (stefanor) wrote :

Looks good to go

review: Approve
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
1=== modified file 'ibid/plugins/ascii.py'
2--- ibid/plugins/ascii.py 2009-12-31 17:43:05 +0000
3+++ ibid/plugins/ascii.py 2010-01-04 16:02:12 +0000
4@@ -1,11 +1,12 @@
5+from BaseHTTPServer import BaseHTTPRequestHandler
6 from cStringIO import StringIO
7 import Image
8 from os import remove
9 import os.path
10 import subprocess
11-from sys import stderr
12 from tempfile import mkstemp
13-from urllib2 import urlopen
14+from urllib2 import HTTPError, URLError, urlopen
15+from urlparse import urlparse
16 from zipfile import ZipFile
17
18 from aalib import AsciiScreen
19@@ -43,24 +44,41 @@
20
21 @match(r'^draw\s+(\S+\.\S+)(\s+in\s+colou?r)?(?:\s+w(?:idth)?\s+(\d+))?(?:\s+h(?:eight)\s+(\d+))?$')
22 def draw(self, event, url, colour, width, height):
23- f = urlopen(url)
24-
25- filesize = int(f.info().getheaders('Content-Length')[0])
26- if filesize > self.max_filesize * 1024:
27- event.addresponse(u'File too large (limit is %i KiB)', self.max_filesize)
28- return
29-
30+ if not urlparse(url).netloc:
31+ url = 'http://' + url
32+ if urlparse(url).scheme == 'file':
33+ event.addresponse(u'Are you trying to haxor me?')
34+ return
35+ if not urlparse(url).path:
36+ url += '/'
37+
38+ try:
39+ f = urlopen(url)
40+ except HTTPError, e:
41+ event.addresponse(u'Sorry, error fetching URL: %s', BaseHTTPRequestHandler.responses[e.code][0])
42+ except URLError:
43+ event.addresponse(u'Sorry, error fetching URL')
44+
45+ content_length = f.info().getheaders('Content-Length')
46+ if content_length and int(content_length[0]) > self.max_filesize * 1024:
47+ event.addresponse(u'File too large (limit is %i KiB)', self.max_filesize)
48+ return
49+
50+ buffer = f.read(self.max_filesize * 1024)
51+ if f.read(1) != '':
52+ event.addresponse(u'File too large (limit is %i KiB)', self.max_filesize)
53+ return
54 try:
55 ext = os.path.splitext(url)[1]
56 image = mkstemp(suffix=ext)[1]
57 file = open(image, 'w')
58- file.write(f.read())
59+ file.write(buffer)
60 file.close()
61
62 try:
63 img = Image.open(StringIO(open(image, 'r').read())).convert('L')
64- except:
65- event.addresponse(u'Cannot understand image format')
66+ except IOError:
67+ event.addresponse(u"Sorry, that doesn't look like an image")
68 return
69 input_width, input_height = img.size[0], img.size[1]
70
71@@ -100,8 +118,8 @@
72 def draw_aa(self, event, image, width, height):
73 try:
74 image = Image.open(StringIO(open(image, 'r').read())).convert('L')
75- except:
76- event.addresponse(u'Cannot understand image format')
77+ except IOError:
78+ event.addresponse(u"Sorry, that doesn't look like an image")
79 return
80 screen = AsciiScreen(width=width, height=height)
81 image = image.resize(screen.virtual_size)
82@@ -109,7 +127,6 @@
83 event.addresponse(unicode(screen.render()), address=False, conflate=False)
84
85 def draw_caca(self, event, image, width, height):
86- from sys import stderr
87 process = subprocess.Popen(
88 [self.img2txt_bin, '-f', 'irc', '-W', str(width), '-H', str(height), image],
89 shell=False, stdout=subprocess.PIPE)
90@@ -118,14 +135,15 @@
91 if code == 0:
92 event.addresponse(unicode(response.replace('\r', '')), address=False, conflate=False)
93 else:
94- event.addresponse(u'Sorry, cannot understand image format')
95+ event.addresponse(u"Sorry, that doesn't look like an image")
96
97 class WriteFiglet(Processor):
98 u"""figlet <text> [in <font>]
99 list figlet fonts [from <index>]"""
100 feature = 'figlet'
101
102- fonts_zip = Option('fonts_zip', 'Zip file containing figlet fonts', 'data/figlet-fonts.zip')
103+ max_width = IntOption('max_width', 'Maximum width for ascii output', 60)
104+ fonts_zip = Option('fonts_zip', 'Zip file containing figlet fonts', 'ibid/data/figlet-fonts.zip')
105
106 def __init__(self, name):
107 Processor.__init__(self, name)
108@@ -158,4 +176,7 @@
109 del rendered[0]
110 while rendered and rendered[-1].strip() == '':
111 del rendered[-1]
112+ if rendered and len(rendered[0]) > self.max_width:
113+ event.addresponse(u"Sorry that's too long, nobody will be able to read it")
114+ return
115 event.addresponse(unicode('\n'.join(rendered)), address=False, conflate=False)

Subscribers

People subscribed via source and target branches