##// END OF EJS Templates
added tests to confirm that error is raise when FileLink is passed a directory. Also specifically disallowed passing a file to FileLinks for consistency.
Greg Caporaso -
Show More
@@ -1,283 +1,287 b''
1 """Various display related classes.
1 """Various display related classes.
2
2
3 Authors : MinRK, gregcaporaso, dannystaple
3 Authors : MinRK, gregcaporaso, dannystaple
4 """
4 """
5 import urllib
5 import urllib
6
6
7 from os.path import exists, isfile, splitext, abspath, join, isdir
7 from os.path import exists, isfile, splitext, abspath, join, isdir
8 from os import walk
8 from os import walk
9
9
10
10
11 class YouTubeVideo(object):
11 class YouTubeVideo(object):
12 """Class for embedding a YouTube Video in an IPython session, based on its video id.
12 """Class for embedding a YouTube Video in an IPython session, based on its video id.
13
13
14 e.g. to embed the video on this page:
14 e.g. to embed the video on this page:
15
15
16 http://www.youtube.com/watch?v=foo
16 http://www.youtube.com/watch?v=foo
17
17
18 you would do:
18 you would do:
19
19
20 vid = YouTubeVideo("foo")
20 vid = YouTubeVideo("foo")
21 display(vid)
21 display(vid)
22
22
23 To start from 30 seconds:
23 To start from 30 seconds:
24
24
25 vid = YouTubeVideo("abc", start=30)
25 vid = YouTubeVideo("abc", start=30)
26 display(vid)
26 display(vid)
27
27
28 To calculate seconds from time as hours, minutes, seconds use:
28 To calculate seconds from time as hours, minutes, seconds use:
29 start=int(timedelta(hours=1, minutes=46, seconds=40).total_seconds())
29 start=int(timedelta(hours=1, minutes=46, seconds=40).total_seconds())
30
30
31 Other parameters can be provided as documented at
31 Other parameters can be provided as documented at
32 https://developers.google.com/youtube/player_parameters#parameter-subheader
32 https://developers.google.com/youtube/player_parameters#parameter-subheader
33 """
33 """
34
34
35 def __init__(self, id, width=400, height=300, **kwargs):
35 def __init__(self, id, width=400, height=300, **kwargs):
36 self.id = id
36 self.id = id
37 self.width = width
37 self.width = width
38 self.height = height
38 self.height = height
39 self.params = kwargs
39 self.params = kwargs
40
40
41 def _repr_html_(self):
41 def _repr_html_(self):
42 """return YouTube embed iframe for this video id"""
42 """return YouTube embed iframe for this video id"""
43 if self.params:
43 if self.params:
44 params = "?" + urllib.urlencode(self.params)
44 params = "?" + urllib.urlencode(self.params)
45 else:
45 else:
46 params = ""
46 params = ""
47 return """
47 return """
48 <iframe
48 <iframe
49 width="%i"
49 width="%i"
50 height="%i"
50 height="%i"
51 src="http://www.youtube.com/embed/%s%s"
51 src="http://www.youtube.com/embed/%s%s"
52 frameborder="0"
52 frameborder="0"
53 allowfullscreen
53 allowfullscreen
54 ></iframe>
54 ></iframe>
55 """ % (self.width, self.height, self.id, params)
55 """ % (self.width, self.height, self.id, params)
56
56
57 class FileLink(object):
57 class FileLink(object):
58 """Class for embedding a local file link in an IPython session, based on path
58 """Class for embedding a local file link in an IPython session, based on path
59
59
60 e.g. to embed a link that was generated in the IPython notebook as my/data.txt
60 e.g. to embed a link that was generated in the IPython notebook as my/data.txt
61
61
62 you would do:
62 you would do:
63
63
64 local_file = FileLink("my/data.txt")
64 local_file = FileLink("my/data.txt")
65 display(local_file)
65 display(local_file)
66
66
67 or in the HTML notebook, just
67 or in the HTML notebook, just
68
68
69 FileLink("my/data.txt")
69 FileLink("my/data.txt")
70 """
70 """
71
71
72 html_link_str = "<a href='%s' target='_blank'>%s</a>"
72 html_link_str = "<a href='%s' target='_blank'>%s</a>"
73
73
74 def __init__(self,
74 def __init__(self,
75 path,
75 path,
76 url_prefix='files/',
76 url_prefix='files/',
77 result_html_prefix='',
77 result_html_prefix='',
78 result_html_suffix='<br>'):
78 result_html_suffix='<br>'):
79 """
79 """
80 path : path to the file or directory that should be formatted
80 path : path to the file or directory that should be formatted
81 directory_prefix : prefix to be prepended to all files to form a
81 directory_prefix : prefix to be prepended to all files to form a
82 working link [default: 'files']
82 working link [default: 'files']
83 result_html_prefix : text to append to beginning to link
83 result_html_prefix : text to append to beginning to link
84 [default: none]
84 [default: none]
85 result_html_suffix : text to append at the end of link
85 result_html_suffix : text to append at the end of link
86 [default: '<br>']
86 [default: '<br>']
87 """
87 """
88 if isdir(path):
88 if isdir(path):
89 raise ValueError,\
89 raise ValueError,\
90 ("Cannot display a directory as a FileLink object. "
90 ("Cannot display a directory using FileLink. "
91 "Use FileLinks to display '%s'." % path)
91 "Use FileLinks to display '%s'." % path)
92 self.path = path
92 self.path = path
93 self.url_prefix = url_prefix
93 self.url_prefix = url_prefix
94 self.result_html_prefix = result_html_prefix
94 self.result_html_prefix = result_html_prefix
95 self.result_html_suffix = result_html_suffix
95 self.result_html_suffix = result_html_suffix
96
96
97 def _format_path(self):
97 def _format_path(self):
98 fp = ''.join([self.url_prefix,self.path])
98 fp = ''.join([self.url_prefix,self.path])
99 return ''.join([self.result_html_prefix,
99 return ''.join([self.result_html_prefix,
100 self.html_link_str % (fp, self.path),
100 self.html_link_str % (fp, self.path),
101 self.result_html_suffix])
101 self.result_html_suffix])
102
102
103 def _repr_html_(self):
103 def _repr_html_(self):
104 """return html link to file
104 """return html link to file
105 """
105 """
106 if not exists(self.path):
106 if not exists(self.path):
107 return ("Path (<tt>%s</tt>) doesn't exist. "
107 return ("Path (<tt>%s</tt>) doesn't exist. "
108 "It may still be in the process of "
108 "It may still be in the process of "
109 "being generated, or you may have the "
109 "being generated, or you may have the "
110 "incorrect path." % self.path)
110 "incorrect path." % self.path)
111
111
112 return self._format_path()
112 return self._format_path()
113
113
114 def __repr__(self):
114 def __repr__(self):
115 """return absolute path to file
115 """return absolute path to file
116 """
116 """
117 return abspath(self.path)
117 return abspath(self.path)
118
118
119 class FileLinks(FileLink):
119 class FileLinks(FileLink):
120 """Class for embedding local file links in an IPython session, based on path
120 """Class for embedding local file links in an IPython session, based on path
121
121
122 e.g. to embed links to files that were generated in the IPython notebook under my/data
122 e.g. to embed links to files that were generated in the IPython notebook under my/data
123
123
124 you would do:
124 you would do:
125
125
126 local_files = FileLinks("my/data")
126 local_files = FileLinks("my/data")
127 display(local_files)
127 display(local_files)
128
128
129 or in the HTML notebook, just
129 or in the HTML notebook, just
130
130
131 FileLinks("my/data")
131 FileLinks("my/data")
132
132
133 """
133 """
134 def __init__(self,
134 def __init__(self,
135 path,
135 path,
136 url_prefix='files/',
136 url_prefix='files/',
137 included_suffixes=None,
137 included_suffixes=None,
138 result_html_prefix='',
138 result_html_prefix='',
139 result_html_suffix='<br>',
139 result_html_suffix='<br>',
140 notebook_display_formatter=None,
140 notebook_display_formatter=None,
141 terminal_display_formatter=None):
141 terminal_display_formatter=None):
142 """
142 """
143 included_suffixes : list of filename suffixes to include when
143 included_suffixes : list of filename suffixes to include when
144 formatting output [default: include all files]
144 formatting output [default: include all files]
145
145
146 See the FileLink (baseclass of LocalDirectory) docstring for
146 See the FileLink (baseclass of LocalDirectory) docstring for
147 information on additional parameters.
147 information on additional parameters.
148
148
149 notebook_display_formatter : func used to format links for display
149 notebook_display_formatter : func used to format links for display
150 in the notebook. See discussion of formatter function below.
150 in the notebook. See discussion of formatter function below.
151
151
152 terminal_display_formatter : func used to format links for display
152 terminal_display_formatter : func used to format links for display
153 in the terminal. See discussion of formatter function below.
153 in the terminal. See discussion of formatter function below.
154
154
155
155
156 Passing custom formatter functions
156 Passing custom formatter functions
157 ----------------------------------
157 ----------------------------------
158 Formatter functions must be of the form:
158 Formatter functions must be of the form:
159 f(dirname, fnames, included_suffixes)
159 f(dirname, fnames, included_suffixes)
160 dirname : the name of a directory (a string),
160 dirname : the name of a directory (a string),
161 fnames : a list of the files in that directory
161 fnames : a list of the files in that directory
162 included_suffixes : a list of the file suffixes that should be
162 included_suffixes : a list of the file suffixes that should be
163 included in the output (passing None means
163 included in the output (passing None means
164 to include all suffixes in the output in
164 to include all suffixes in the output in
165 the built-in formatters)
165 the built-in formatters)
166
166
167 returns a list of lines that should will be print in the
167 returns a list of lines that should will be print in the
168 notebook (if passing notebook_display_formatter) or the terminal
168 notebook (if passing notebook_display_formatter) or the terminal
169 (if passing terminal_display_formatter). This function is iterated
169 (if passing terminal_display_formatter). This function is iterated
170 over for each directory in self.path. Default formatters are in
170 over for each directory in self.path. Default formatters are in
171 place, can be passed here to support alternative formatting.
171 place, can be passed here to support alternative formatting.
172
172
173 """
173 """
174 if isfile(path):
175 raise ValueError,\
176 ("Cannot display a file using FileLinks. "
177 "Use FileLink to display '%s'." % path)
174 self.included_suffixes = included_suffixes
178 self.included_suffixes = included_suffixes
175 # remove trailing slashs for more consistent output formatting
179 # remove trailing slashs for more consistent output formatting
176 path = path.rstrip('/')
180 path = path.rstrip('/')
177
181
178 self.path = path
182 self.path = path
179 self.url_prefix = url_prefix
183 self.url_prefix = url_prefix
180 self.result_html_prefix = result_html_prefix
184 self.result_html_prefix = result_html_prefix
181 self.result_html_suffix = result_html_suffix
185 self.result_html_suffix = result_html_suffix
182
186
183 self.notebook_display_formatter = \
187 self.notebook_display_formatter = \
184 notebook_display_formatter or self._get_notebook_display_formatter()
188 notebook_display_formatter or self._get_notebook_display_formatter()
185 self.terminal_display_formatter = \
189 self.terminal_display_formatter = \
186 terminal_display_formatter or self._get_terminal_display_formatter()
190 terminal_display_formatter or self._get_terminal_display_formatter()
187
191
188 def _get_display_formatter(self,
192 def _get_display_formatter(self,
189 dirname_output_format,
193 dirname_output_format,
190 fname_output_format,
194 fname_output_format,
191 fp_format):
195 fp_format):
192 """ generate built-in formatter function
196 """ generate built-in formatter function
193
197
194 this is used to define both the notebook and terminal built-in
198 this is used to define both the notebook and terminal built-in
195 formatters as they only differ by some wrapper text for each entry
199 formatters as they only differ by some wrapper text for each entry
196
200
197 dirname_output_format: string to use for formatting directory
201 dirname_output_format: string to use for formatting directory
198 names, dirname will be substituted for a single "%s" which
202 names, dirname will be substituted for a single "%s" which
199 must appear in this string
203 must appear in this string
200 fname_output_format: string to use for formatting file names,
204 fname_output_format: string to use for formatting file names,
201 if a single "%s" appears in the string, fname will be substituted
205 if a single "%s" appears in the string, fname will be substituted
202 if two "%s" appear in the string, the path to fname will be
206 if two "%s" appear in the string, the path to fname will be
203 substituted for the first and fname will be substituted for the
207 substituted for the first and fname will be substituted for the
204 second
208 second
205 fp_format: string to use for formatting filepaths, must contain
209 fp_format: string to use for formatting filepaths, must contain
206 exactly two "%s" and the dirname will be subsituted for the first
210 exactly two "%s" and the dirname will be subsituted for the first
207 and fname will be substituted for the second
211 and fname will be substituted for the second
208 """
212 """
209 def f(dirname, fnames, included_suffixes=None):
213 def f(dirname, fnames, included_suffixes=None):
210 result = []
214 result = []
211 # begin by figuring out which filenames, if any,
215 # begin by figuring out which filenames, if any,
212 # are going to be displayed
216 # are going to be displayed
213 display_fnames = []
217 display_fnames = []
214 for fname in fnames:
218 for fname in fnames:
215 if (isfile(join(dirname,fname)) and
219 if (isfile(join(dirname,fname)) and
216 (included_suffixes == None or
220 (included_suffixes == None or
217 splitext(fname)[1] in included_suffixes)):
221 splitext(fname)[1] in included_suffixes)):
218 display_fnames.append(fname)
222 display_fnames.append(fname)
219
223
220 if len(display_fnames) == 0:
224 if len(display_fnames) == 0:
221 # if there are no filenames to display, don't print anything
225 # if there are no filenames to display, don't print anything
222 # (not even the directory name)
226 # (not even the directory name)
223 pass
227 pass
224 else:
228 else:
225 # otherwise print the formatted directory name followed by
229 # otherwise print the formatted directory name followed by
226 # the formatted filenames
230 # the formatted filenames
227 dirname_output_line = dirname_output_format % dirname
231 dirname_output_line = dirname_output_format % dirname
228 result.append(dirname_output_line)
232 result.append(dirname_output_line)
229 for fname in display_fnames:
233 for fname in display_fnames:
230 fp = fp_format % (dirname,fname)
234 fp = fp_format % (dirname,fname)
231 try:
235 try:
232 # output can include both a filepath and a filename...
236 # output can include both a filepath and a filename...
233 fname_output_line = fname_output_format % (fp, fname)
237 fname_output_line = fname_output_format % (fp, fname)
234 except TypeError:
238 except TypeError:
235 # ... or just a single filepath
239 # ... or just a single filepath
236 fname_output_line = fname_output_format % fname
240 fname_output_line = fname_output_format % fname
237 result.append(fname_output_line)
241 result.append(fname_output_line)
238 return result
242 return result
239 return f
243 return f
240
244
241 def _get_notebook_display_formatter(self,
245 def _get_notebook_display_formatter(self,
242 spacer="&nbsp;&nbsp;"):
246 spacer="&nbsp;&nbsp;"):
243 """ generate function to use for notebook formatting
247 """ generate function to use for notebook formatting
244 """
248 """
245 dirname_output_format = \
249 dirname_output_format = \
246 self.result_html_prefix + "%s/" + self.result_html_suffix
250 self.result_html_prefix + "%s/" + self.result_html_suffix
247 fname_output_format = \
251 fname_output_format = \
248 self.result_html_prefix + spacer + self.html_link_str + self.result_html_suffix
252 self.result_html_prefix + spacer + self.html_link_str + self.result_html_suffix
249 fp_format = self.url_prefix + '%s/%s'
253 fp_format = self.url_prefix + '%s/%s'
250
254
251 return self._get_display_formatter(dirname_output_format,
255 return self._get_display_formatter(dirname_output_format,
252 fname_output_format,
256 fname_output_format,
253 fp_format)
257 fp_format)
254
258
255 def _get_terminal_display_formatter(self,
259 def _get_terminal_display_formatter(self,
256 spacer=" "):
260 spacer=" "):
257 """ generate function to use for terminal formatting
261 """ generate function to use for terminal formatting
258 """
262 """
259 dirname_output_format = "%s/"
263 dirname_output_format = "%s/"
260 fname_output_format = spacer + "%s"
264 fname_output_format = spacer + "%s"
261 fp_format = '%s/%s'
265 fp_format = '%s/%s'
262
266
263 return self._get_display_formatter(dirname_output_format,
267 return self._get_display_formatter(dirname_output_format,
264 fname_output_format,
268 fname_output_format,
265 fp_format)
269 fp_format)
266
270
267 def _format_path(self):
271 def _format_path(self):
268 result_lines = []
272 result_lines = []
269 walked_dir = list(walk(self.path))
273 walked_dir = list(walk(self.path))
270 walked_dir.sort()
274 walked_dir.sort()
271 for dirname, subdirs, fnames in walked_dir:
275 for dirname, subdirs, fnames in walked_dir:
272 result_lines += self.notebook_display_formatter(dirname, fnames, self.included_suffixes)
276 result_lines += self.notebook_display_formatter(dirname, fnames, self.included_suffixes)
273 return '\n'.join(result_lines)
277 return '\n'.join(result_lines)
274
278
275 def __repr__(self):
279 def __repr__(self):
276 """return newline-separated absolute paths
280 """return newline-separated absolute paths
277 """
281 """
278 result_lines = []
282 result_lines = []
279 walked_dir = list(walk(self.path))
283 walked_dir = list(walk(self.path))
280 walked_dir.sort()
284 walked_dir.sort()
281 for dirname, subdirs, fnames in walked_dir:
285 for dirname, subdirs, fnames in walked_dir:
282 result_lines += self.terminal_display_formatter(dirname, fnames, self.included_suffixes)
286 result_lines += self.terminal_display_formatter(dirname, fnames, self.included_suffixes)
283 return '\n'.join(result_lines)
287 return '\n'.join(result_lines)
@@ -1,140 +1,153 b''
1 """Tests for IPython.lib.display.
1 """Tests for IPython.lib.display.
2
2
3 """
3 """
4 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
5 # Copyright (c) 2012, the IPython Development Team.
5 # Copyright (c) 2012, the IPython Development Team.
6 #
6 #
7 # Distributed under the terms of the Modified BSD License.
7 # Distributed under the terms of the Modified BSD License.
8 #
8 #
9 # The full license is in the file COPYING.txt, distributed with this software.
9 # The full license is in the file COPYING.txt, distributed with this software.
10 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
11
11
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13 # Imports
13 # Imports
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15 from __future__ import print_function
15 from __future__ import print_function
16 from tempfile import NamedTemporaryFile, mkdtemp
16 from tempfile import NamedTemporaryFile, mkdtemp
17 from os.path import split
17 from os.path import split
18
18
19 # Third-party imports
19 # Third-party imports
20 import nose.tools as nt
20 import nose.tools as nt
21
21
22 # Our own imports
22 # Our own imports
23 from IPython.lib import display
23 from IPython.lib import display
24
24
25 #-----------------------------------------------------------------------------
25 #-----------------------------------------------------------------------------
26 # Classes and functions
26 # Classes and functions
27 #-----------------------------------------------------------------------------
27 #-----------------------------------------------------------------------------
28
28
29 #--------------------------
29 #--------------------------
30 # FileLink tests
30 # FileLink tests
31 #--------------------------
31 #--------------------------
32
32
33 def test_instantiation_FileLink():
33 def test_instantiation_FileLink():
34 """FileLink: Test class can be instantiated"""
34 """FileLink: Test class can be instantiated"""
35 fl = display.FileLink('example.txt')
35 fl = display.FileLink('example.txt')
36
36
37 def test_warning_on_non_existant_path_FileLink():
37 def test_warning_on_non_existant_path_FileLink():
38 """FileLink: Calling _repr_html_ on non-existant files returns a warning
38 """FileLink: Calling _repr_html_ on non-existant files returns a warning
39 """
39 """
40 fl = display.FileLink('example.txt')
40 fl = display.FileLink('example.txt')
41 nt.assert_true(fl._repr_html_().startswith('Path (<tt>example.txt</tt>)'))
41 nt.assert_true(fl._repr_html_().startswith('Path (<tt>example.txt</tt>)'))
42
42
43 def test_existing_path_FileLink():
43 def test_existing_path_FileLink():
44 """FileLink: Calling _repr_html_ functions as expected on existing filepath
44 """FileLink: Calling _repr_html_ functions as expected on existing filepath
45 """
45 """
46 tf = NamedTemporaryFile()
46 tf = NamedTemporaryFile()
47 fl = display.FileLink(tf.name)
47 fl = display.FileLink(tf.name)
48 actual = fl._repr_html_()
48 actual = fl._repr_html_()
49 expected = "<a href='files/%s' target='_blank'>%s</a><br>" % (tf.name,tf.name)
49 expected = "<a href='files/%s' target='_blank'>%s</a><br>" % (tf.name,tf.name)
50 nt.assert_equal(actual,expected)
50 nt.assert_equal(actual,expected)
51
51
52 def test_existing_path_FileLink_repr():
52 def test_existing_path_FileLink_repr():
53 """FileLink: Calling repr() functions as expected on existing filepath
53 """FileLink: Calling repr() functions as expected on existing filepath
54 """
54 """
55 tf = NamedTemporaryFile()
55 tf = NamedTemporaryFile()
56 fl = display.FileLink(tf.name)
56 fl = display.FileLink(tf.name)
57 actual = repr(fl)
57 actual = repr(fl)
58 expected = tf.name
58 expected = tf.name
59 nt.assert_equal(actual,expected)
59 nt.assert_equal(actual,expected)
60
60
61 def test_error_on_directory_to_FileLink():
62 """FileLink: Raises error when passed directory
63 """
64 td = mkdtemp()
65 nt.assert_raises(ValueError,display.FileLink,td)
66
61 #--------------------------
67 #--------------------------
62 # FileLinks tests
68 # FileLinks tests
63 #--------------------------
69 #--------------------------
64
70
65 def test_instantiation_FileLinks():
71 def test_instantiation_FileLinks():
66 """FileLinks: Test class can be instantiated
72 """FileLinks: Test class can be instantiated
67 """
73 """
68 fls = display.FileLinks('example')
74 fls = display.FileLinks('example')
69
75
70 def test_warning_on_non_existant_path_FileLinks():
76 def test_warning_on_non_existant_path_FileLinks():
71 """FileLinks: Calling _repr_html_ on non-existant files returns a warning
77 """FileLinks: Calling _repr_html_ on non-existant files returns a warning
72 """
78 """
73 fls = display.FileLinks('example')
79 fls = display.FileLinks('example')
74 nt.assert_true(fls._repr_html_().startswith('Path (<tt>example</tt>)'))
80 nt.assert_true(fls._repr_html_().startswith('Path (<tt>example</tt>)'))
75
81
76 def test_existing_path_FileLinks():
82 def test_existing_path_FileLinks():
77 """FileLinks: Calling _repr_html_ functions as expected on existing dir
83 """FileLinks: Calling _repr_html_ functions as expected on existing dir
78 """
84 """
79 td = mkdtemp()
85 td = mkdtemp()
80 tf1 = NamedTemporaryFile(dir=td)
86 tf1 = NamedTemporaryFile(dir=td)
81 tf2 = NamedTemporaryFile(dir=td)
87 tf2 = NamedTemporaryFile(dir=td)
82 fl = display.FileLinks(td)
88 fl = display.FileLinks(td)
83 actual = fl._repr_html_()
89 actual = fl._repr_html_()
84 actual = actual.split('\n')
90 actual = actual.split('\n')
85 actual.sort()
91 actual.sort()
86 expected = ["%s/<br>" % td,
92 expected = ["%s/<br>" % td,
87 "&nbsp;&nbsp;<a href='files/%s' target='_blank'>%s</a><br>" % (tf2.name,split(tf2.name)[1]),
93 "&nbsp;&nbsp;<a href='files/%s' target='_blank'>%s</a><br>" % (tf2.name,split(tf2.name)[1]),
88 "&nbsp;&nbsp;<a href='files/%s' target='_blank'>%s</a><br>" % (tf1.name,split(tf1.name)[1])]
94 "&nbsp;&nbsp;<a href='files/%s' target='_blank'>%s</a><br>" % (tf1.name,split(tf1.name)[1])]
89 expected.sort()
95 expected.sort()
90 # We compare the sorted list of links here as that's more reliable
96 # We compare the sorted list of links here as that's more reliable
91 nt.assert_equal(actual,expected)
97 nt.assert_equal(actual,expected)
92
98
93 def test_existing_path_FileLinks_alt_formatter():
99 def test_existing_path_FileLinks_alt_formatter():
94 """FileLinks: Calling _repr_html_ functions as expected w/ an alt formatter
100 """FileLinks: Calling _repr_html_ functions as expected w/ an alt formatter
95 """
101 """
96 td = mkdtemp()
102 td = mkdtemp()
97 tf1 = NamedTemporaryFile(dir=td)
103 tf1 = NamedTemporaryFile(dir=td)
98 tf2 = NamedTemporaryFile(dir=td)
104 tf2 = NamedTemporaryFile(dir=td)
99 def fake_formatter(dirname,fnames,included_suffixes):
105 def fake_formatter(dirname,fnames,included_suffixes):
100 return ["hello","world"]
106 return ["hello","world"]
101 fl = display.FileLinks(td,notebook_display_formatter=fake_formatter)
107 fl = display.FileLinks(td,notebook_display_formatter=fake_formatter)
102 actual = fl._repr_html_()
108 actual = fl._repr_html_()
103 actual = actual.split('\n')
109 actual = actual.split('\n')
104 actual.sort()
110 actual.sort()
105 expected = ["hello","world"]
111 expected = ["hello","world"]
106 expected.sort()
112 expected.sort()
107 # We compare the sorted list of links here as that's more reliable
113 # We compare the sorted list of links here as that's more reliable
108 nt.assert_equal(actual,expected)
114 nt.assert_equal(actual,expected)
109
115
110 def test_existing_path_FileLinks_repr():
116 def test_existing_path_FileLinks_repr():
111 """FileLinks: Calling repr() functions as expected on existing directory """
117 """FileLinks: Calling repr() functions as expected on existing directory """
112 td = mkdtemp()
118 td = mkdtemp()
113 tf1 = NamedTemporaryFile(dir=td)
119 tf1 = NamedTemporaryFile(dir=td)
114 tf2 = NamedTemporaryFile(dir=td)
120 tf2 = NamedTemporaryFile(dir=td)
115 fl = display.FileLinks(td)
121 fl = display.FileLinks(td)
116 actual = repr(fl)
122 actual = repr(fl)
117 actual = actual.split('\n')
123 actual = actual.split('\n')
118 actual.sort()
124 actual.sort()
119 expected = ['%s/' % td, ' %s' % split(tf1.name)[1],' %s' % split(tf2.name)[1]]
125 expected = ['%s/' % td, ' %s' % split(tf1.name)[1],' %s' % split(tf2.name)[1]]
120 expected.sort()
126 expected.sort()
121 # We compare the sorted list of links here as that's more reliable
127 # We compare the sorted list of links here as that's more reliable
122 nt.assert_equal(actual,expected)
128 nt.assert_equal(actual,expected)
123
129
124 def test_existing_path_FileLinks_repr_alt_formatter():
130 def test_existing_path_FileLinks_repr_alt_formatter():
125 """FileLinks: Calling repr() functions as expected w/ alt formatter
131 """FileLinks: Calling repr() functions as expected w/ alt formatter
126 """
132 """
127 td = mkdtemp()
133 td = mkdtemp()
128 tf1 = NamedTemporaryFile(dir=td)
134 tf1 = NamedTemporaryFile(dir=td)
129 tf2 = NamedTemporaryFile(dir=td)
135 tf2 = NamedTemporaryFile(dir=td)
130 def fake_formatter(dirname,fnames,included_suffixes):
136 def fake_formatter(dirname,fnames,included_suffixes):
131 return ["hello","world"]
137 return ["hello","world"]
132 fl = display.FileLinks(td,terminal_display_formatter=fake_formatter)
138 fl = display.FileLinks(td,terminal_display_formatter=fake_formatter)
133 actual = repr(fl)
139 actual = repr(fl)
134 actual = actual.split('\n')
140 actual = actual.split('\n')
135 actual.sort()
141 actual.sort()
136 expected = ["hello","world"]
142 expected = ["hello","world"]
137 expected.sort()
143 expected.sort()
138 # We compare the sorted list of links here as that's more reliable
144 # We compare the sorted list of links here as that's more reliable
139 nt.assert_equal(actual,expected)
145 nt.assert_equal(actual,expected)
146
147 def test_error_on_file_to_FileLinks():
148 """FileLinks: Raises error when passed file
149 """
150 td = mkdtemp()
151 tf1 = NamedTemporaryFile(dir=td)
152 nt.assert_raises(ValueError,display.FileLinks,tf1.name)
140
153
General Comments 0
You need to be logged in to leave comments. Login now