Show More
@@ -1,267 +1,268 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 | self.path = path |
|
88 | self.path = path | |
89 | self.url_prefix = url_prefix |
|
89 | self.url_prefix = url_prefix | |
90 | self.result_html_prefix = result_html_prefix |
|
90 | self.result_html_prefix = result_html_prefix | |
91 | self.result_html_suffix = result_html_suffix |
|
91 | self.result_html_suffix = result_html_suffix | |
92 |
|
92 | |||
93 | def _format_path(self): |
|
93 | def _format_path(self): | |
94 | fp = ''.join([self.url_prefix,self.path]) |
|
94 | fp = ''.join([self.url_prefix,self.path]) | |
95 | return ''.join([self.result_html_prefix, |
|
95 | return ''.join([self.result_html_prefix, | |
96 | self.html_link_str % (fp, self.path), |
|
96 | self.html_link_str % (fp, self.path), | |
97 | self.result_html_suffix]) |
|
97 | self.result_html_suffix]) | |
98 |
|
98 | |||
99 | def _repr_html_(self): |
|
99 | def _repr_html_(self): | |
100 | """return html link to file |
|
100 | """return html link to file | |
101 | """ |
|
101 | """ | |
102 | if not exists(self.path): |
|
102 | if not exists(self.path): | |
103 | return ("Path (<tt>%s</tt>) doesn't exist. " |
|
103 | return ("Path (<tt>%s</tt>) doesn't exist. " | |
104 | "It may still be in the process of " |
|
104 | "It may still be in the process of " | |
105 | "being generated, or you may have the " |
|
105 | "being generated, or you may have the " | |
106 | "incorrect path." % self.path) |
|
106 | "incorrect path." % self.path) | |
107 |
|
107 | |||
108 | return self._format_path() |
|
108 | return self._format_path() | |
109 |
|
109 | |||
110 | def __repr__(self): |
|
110 | def __repr__(self): | |
111 | """return absolute path to file |
|
111 | """return absolute path to file | |
112 | """ |
|
112 | """ | |
113 | return abspath(self.path) |
|
113 | return abspath(self.path) | |
114 |
|
114 | |||
115 | # Create an alias for formatting a single directory name as a link. |
|
115 | # Create an alias for formatting a single directory name as a link. | |
116 | # Right now this is the same as a formatting for a single file, but |
|
116 | # Right now this is the same as a formatting for a single file, but | |
117 | # we'll encourage users to reference these with a different class in |
|
117 | # we'll encourage users to reference these with a different class in | |
118 | # case we want to change this in the future. |
|
118 | # case we want to change this in the future. | |
119 | DirectoryLink = FileLink |
|
119 | DirectoryLink = FileLink | |
120 |
|
120 | |||
121 | class FileLinks(FileLink): |
|
121 | class FileLinks(FileLink): | |
122 | """Class for embedding local file links in an IPython session, based on path |
|
122 | """Class for embedding local file links in an IPython session, based on path | |
123 |
|
123 | |||
124 | e.g. to embed links to files that were generated in the IPython notebook under my/data |
|
124 | e.g. to embed links to files that were generated in the IPython notebook under my/data | |
125 |
|
125 | |||
126 | you would do: |
|
126 | you would do: | |
127 |
|
127 | |||
128 | local_files = FileLinks("my/data") |
|
128 | local_files = FileLinks("my/data") | |
129 | display(local_files) |
|
129 | display(local_files) | |
130 |
|
130 | |||
131 | or in the HTML notebook, just |
|
131 | or in the HTML notebook, just | |
132 |
|
132 | |||
133 | FileLinks("my/data") |
|
133 | FileLinks("my/data") | |
134 |
|
134 | |||
135 | """ |
|
135 | """ | |
136 | def __init__(self, |
|
136 | def __init__(self, | |
137 | path, |
|
137 | path, | |
138 | url_prefix='files/', |
|
138 | url_prefix='files/', | |
139 | included_suffixes=None, |
|
139 | included_suffixes=None, | |
140 | result_html_prefix='', |
|
140 | result_html_prefix='', | |
141 | result_html_suffix='<br>', |
|
141 | result_html_suffix='<br>', | |
142 | notebook_display_formatter=None, |
|
142 | notebook_display_formatter=None, | |
143 | terminal_display_formatter=None): |
|
143 | terminal_display_formatter=None): | |
144 | """ |
|
144 | """ | |
145 | included_suffixes : list of filename suffixes to include when |
|
145 | included_suffixes : list of filename suffixes to include when | |
146 | formatting output [default: include all files] |
|
146 | formatting output [default: include all files] | |
147 |
|
147 | |||
148 | See the FileLink (baseclass of LocalDirectory) docstring for |
|
148 | See the FileLink (baseclass of LocalDirectory) docstring for | |
149 | information on additional parameters. |
|
149 | information on additional parameters. | |
150 |
|
150 | |||
151 | notebook_display_formatter : func passed to os.path.walk when |
|
151 | notebook_display_formatter : func passed to os.path.walk when | |
152 | formatting links for display in the notebook |
|
152 | formatting links for display in the notebook | |
153 |
|
153 | |||
154 | terminal_display_formatter : func passed to os.path.walk when |
|
154 | terminal_display_formatter : func passed to os.path.walk when | |
155 | formatting links for display in the terminal |
|
155 | formatting links for display in the terminal | |
156 |
|
156 | |||
157 | """ |
|
157 | """ | |
158 | self.included_suffixes = included_suffixes |
|
158 | self.included_suffixes = included_suffixes | |
159 | # remove trailing slashs for more consistent output formatting |
|
159 | # remove trailing slashs for more consistent output formatting | |
160 | path = path.rstrip('/') |
|
160 | path = path.rstrip('/') | |
161 | FileLink.__init__(self, |
|
161 | FileLink.__init__(self, | |
162 | path, |
|
162 | path, | |
163 | url_prefix, |
|
163 | url_prefix, | |
164 | result_html_prefix, |
|
164 | result_html_prefix, | |
165 | result_html_suffix) |
|
165 | result_html_suffix) | |
166 |
|
166 | |||
167 | self.notebook_display_formatter = \ |
|
167 | self.notebook_display_formatter = \ | |
168 | notebook_display_formatter or self._get_notebook_display_formatter() |
|
168 | notebook_display_formatter or self._get_notebook_display_formatter() | |
169 | self.terminal_display_formatter = \ |
|
169 | self.terminal_display_formatter = \ | |
170 | terminal_display_formatter or self._get_terminal_display_formatter() |
|
170 | terminal_display_formatter or self._get_terminal_display_formatter() | |
171 |
|
171 | |||
172 | def _get_display_formatter(self, |
|
172 | def _get_display_formatter(self, | |
173 | dirname_output_format, |
|
173 | dirname_output_format, | |
174 | fname_output_format, |
|
174 | fname_output_format, | |
175 | fp_format): |
|
175 | fp_format): | |
176 | """ generate func to pass to os.path.walk |
|
176 | """ generate function to format output- the resulting function will | |
|
177 | take a list to be populated with the output lines to print, | |||
177 |
|
178 | |||
178 | dirname_output_format: string to use for formatting directory |
|
179 | dirname_output_format: string to use for formatting directory | |
179 | names, dirname will be substituted for a single "%s" which |
|
180 | names, dirname will be substituted for a single "%s" which | |
180 | must appear in this string |
|
181 | must appear in this string | |
181 | fname_output_format: string to use for formatting file names, |
|
182 | fname_output_format: string to use for formatting file names, | |
182 | if a single "%s" appears in the string, fname will be substituted |
|
183 | if a single "%s" appears in the string, fname will be substituted | |
183 | if two "%s" appear in the string, the path to fname will be |
|
184 | if two "%s" appear in the string, the path to fname will be | |
184 | substituted for the first and fname will be substituted for the |
|
185 | substituted for the first and fname will be substituted for the | |
185 | second |
|
186 | second | |
186 | fp_format: string to use for formatting filepaths, must contain |
|
187 | fp_format: string to use for formatting filepaths, must contain | |
187 | exactly two "%s" and the dirname will be subsituted for the first |
|
188 | exactly two "%s" and the dirname will be subsituted for the first | |
188 | and fname will be substituted for the second |
|
189 | and fname will be substituted for the second | |
189 | """ |
|
190 | """ | |
190 |
|
191 | |||
191 | included_suffixes = self.included_suffixes |
|
192 | included_suffixes = self.included_suffixes | |
192 |
|
193 | |||
193 |
def f( |
|
194 | def f(dirname, fnames): | |
194 | """ func to be passed to os.path.walk """ |
|
195 | result = [] | |
195 | # begin by figuring out which filenames, if any, |
|
196 | # begin by figuring out which filenames, if any, | |
196 | # are going to be displayed |
|
197 | # are going to be displayed | |
197 | display_fnames = [] |
|
198 | display_fnames = [] | |
198 | for fname in fnames: |
|
199 | for fname in fnames: | |
199 | if (isfile(join(dirname,fname)) and |
|
200 | if (isfile(join(dirname,fname)) and | |
200 | (included_suffixes == None or |
|
201 | (included_suffixes == None or | |
201 | splitext(fname)[1] in included_suffixes)): |
|
202 | splitext(fname)[1] in included_suffixes)): | |
202 | display_fnames.append(fname) |
|
203 | display_fnames.append(fname) | |
203 |
|
204 | |||
204 | if len(display_fnames) == 0: |
|
205 | if len(display_fnames) == 0: | |
205 | # if there are no filenames to display, don't print anything |
|
206 | # if there are no filenames to display, don't print anything | |
206 | # (not even the directory name) |
|
207 | # (not even the directory name) | |
207 | pass |
|
208 | pass | |
208 | else: |
|
209 | else: | |
209 | # otherwise print the formatted directory name followed by |
|
210 | # otherwise print the formatted directory name followed by | |
210 | # the formatted filenames |
|
211 | # the formatted filenames | |
211 | dirname_output_line = dirname_output_format % dirname |
|
212 | dirname_output_line = dirname_output_format % dirname | |
212 |
|
|
213 | result.append(dirname_output_line) | |
213 | for fname in display_fnames: |
|
214 | for fname in display_fnames: | |
214 | fp = fp_format % (dirname,fname) |
|
215 | fp = fp_format % (dirname,fname) | |
215 | try: |
|
216 | try: | |
216 | # output can include both a filepath and a filename... |
|
217 | # output can include both a filepath and a filename... | |
217 | fname_output_line = fname_output_format % (fp, fname) |
|
218 | fname_output_line = fname_output_format % (fp, fname) | |
218 | except TypeError: |
|
219 | except TypeError: | |
219 | # ... or just a single filepath |
|
220 | # ... or just a single filepath | |
220 | fname_output_line = fname_output_format % fname |
|
221 | fname_output_line = fname_output_format % fname | |
221 |
|
|
222 | result.append(fname_output_line) | |
222 | return |
|
223 | return result | |
223 | return f |
|
224 | return f | |
224 |
|
225 | |||
225 | def _get_notebook_display_formatter(self, |
|
226 | def _get_notebook_display_formatter(self, | |
226 | spacer=" "): |
|
227 | spacer=" "): | |
227 | """ generate func to pass to os.path.walk for notebook formatting |
|
228 | """ generate func to pass to os.path.walk for notebook formatting | |
228 | """ |
|
229 | """ | |
229 | dirname_output_format = \ |
|
230 | dirname_output_format = \ | |
230 | self.result_html_prefix + "%s/" + self.result_html_suffix |
|
231 | self.result_html_prefix + "%s/" + self.result_html_suffix | |
231 | fname_output_format = \ |
|
232 | fname_output_format = \ | |
232 | self.result_html_prefix + spacer + self.html_link_str + self.result_html_suffix |
|
233 | self.result_html_prefix + spacer + self.html_link_str + self.result_html_suffix | |
233 | fp_format = self.url_prefix + '%s/%s' |
|
234 | fp_format = self.url_prefix + '%s/%s' | |
234 |
|
235 | |||
235 | return self._get_display_formatter(dirname_output_format, |
|
236 | return self._get_display_formatter(dirname_output_format, | |
236 | fname_output_format, |
|
237 | fname_output_format, | |
237 | fp_format) |
|
238 | fp_format) | |
238 |
|
239 | |||
239 | def _get_terminal_display_formatter(self, |
|
240 | def _get_terminal_display_formatter(self, | |
240 | spacer=" "): |
|
241 | spacer=" "): | |
241 | """ generate func to pass to os.path.walk for terminal formatting |
|
242 | """ generate func to pass to os.path.walk for terminal formatting | |
242 | """ |
|
243 | """ | |
243 | dirname_output_format = "%s/" |
|
244 | dirname_output_format = "%s/" | |
244 | fname_output_format = spacer + "%s" |
|
245 | fname_output_format = spacer + "%s" | |
245 | fp_format = '%s/%s' |
|
246 | fp_format = '%s/%s' | |
246 |
|
247 | |||
247 | return self._get_display_formatter(dirname_output_format, |
|
248 | return self._get_display_formatter(dirname_output_format, | |
248 | fname_output_format, |
|
249 | fname_output_format, | |
249 | fp_format) |
|
250 | fp_format) | |
250 |
|
251 | |||
251 | def _format_path(self): |
|
252 | def _format_path(self): | |
252 | result_lines = [] |
|
253 | result_lines = [] | |
253 | walked_dir = list(walk(self.path)) |
|
254 | walked_dir = list(walk(self.path)) | |
254 | walked_dir.sort() |
|
255 | walked_dir.sort() | |
255 | for dirname, subdirs, fnames in walked_dir: |
|
256 | for dirname, subdirs, fnames in walked_dir: | |
256 |
self.notebook_display_formatter( |
|
257 | result_lines += self.notebook_display_formatter(dirname, fnames) | |
257 | return '\n'.join(result_lines) |
|
258 | return '\n'.join(result_lines) | |
258 |
|
259 | |||
259 | def __repr__(self): |
|
260 | def __repr__(self): | |
260 | """return newline-separated absolute paths |
|
261 | """return newline-separated absolute paths | |
261 | """ |
|
262 | """ | |
262 | result_lines = [] |
|
263 | result_lines = [] | |
263 | walked_dir = list(walk(self.path)) |
|
264 | walked_dir = list(walk(self.path)) | |
264 | walked_dir.sort() |
|
265 | walked_dir.sort() | |
265 | for dirname, subdirs, fnames in walked_dir: |
|
266 | for dirname, subdirs, fnames in walked_dir: | |
266 |
self.terminal_display_formatter( |
|
267 | result_lines += self.terminal_display_formatter(dirname, fnames) | |
267 | return '\n'.join(result_lines) |
|
268 | return '\n'.join(result_lines) |
@@ -1,156 +1,154 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 | """Test classes can be instantiated""" |
|
34 | """Test classes 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 | """Calling _repr_html_ on non-existant files returns a warning""" |
|
38 | """Calling _repr_html_ on non-existant files returns a warning""" | |
39 | fl = display.FileLink('example.txt') |
|
39 | fl = display.FileLink('example.txt') | |
40 | nt.assert_true(fl._repr_html_().startswith('Path (<tt>example.txt</tt>)')) |
|
40 | nt.assert_true(fl._repr_html_().startswith('Path (<tt>example.txt</tt>)')) | |
41 |
|
41 | |||
42 | def test_existing_path_FileLink(): |
|
42 | def test_existing_path_FileLink(): | |
43 | """ Calling _repr_html_ functions as expected on existing filepath """ |
|
43 | """ Calling _repr_html_ functions as expected on existing filepath """ | |
44 | tf = NamedTemporaryFile() |
|
44 | tf = NamedTemporaryFile() | |
45 | fl = display.FileLink(tf.name) |
|
45 | fl = display.FileLink(tf.name) | |
46 | actual = fl._repr_html_() |
|
46 | actual = fl._repr_html_() | |
47 | expected = "<a href='files/%s' target='_blank'>%s</a><br>" % (tf.name,tf.name) |
|
47 | expected = "<a href='files/%s' target='_blank'>%s</a><br>" % (tf.name,tf.name) | |
48 | nt.assert_equal(actual,expected) |
|
48 | nt.assert_equal(actual,expected) | |
49 |
|
49 | |||
50 | def test_existing_path_FileLink_repr(): |
|
50 | def test_existing_path_FileLink_repr(): | |
51 | """ Calling repr() functions as expected on existing filepath """ |
|
51 | """ Calling repr() functions as expected on existing filepath """ | |
52 | tf = NamedTemporaryFile() |
|
52 | tf = NamedTemporaryFile() | |
53 | fl = display.FileLink(tf.name) |
|
53 | fl = display.FileLink(tf.name) | |
54 | actual = repr(fl) |
|
54 | actual = repr(fl) | |
55 | expected = tf.name |
|
55 | expected = tf.name | |
56 | nt.assert_equal(actual,expected) |
|
56 | nt.assert_equal(actual,expected) | |
57 |
|
57 | |||
58 | #-------------------------- |
|
58 | #-------------------------- | |
59 | # FileLinks tests |
|
59 | # FileLinks tests | |
60 | #-------------------------- |
|
60 | #-------------------------- | |
61 |
|
61 | |||
62 | def test_instantiation_FileLinks(): |
|
62 | def test_instantiation_FileLinks(): | |
63 | """Test classes can be instantiated""" |
|
63 | """Test classes can be instantiated""" | |
64 | fls = display.FileLinks('example') |
|
64 | fls = display.FileLinks('example') | |
65 |
|
65 | |||
66 | def test_warning_on_non_existant_path_FileLinks(): |
|
66 | def test_warning_on_non_existant_path_FileLinks(): | |
67 | """Calling _repr_html_ on non-existant files returns a warning""" |
|
67 | """Calling _repr_html_ on non-existant files returns a warning""" | |
68 | fls = display.FileLinks('example') |
|
68 | fls = display.FileLinks('example') | |
69 | nt.assert_true(fls._repr_html_().startswith('Path (<tt>example</tt>)')) |
|
69 | nt.assert_true(fls._repr_html_().startswith('Path (<tt>example</tt>)')) | |
70 |
|
70 | |||
71 | def test_existing_path_FileLinks(): |
|
71 | def test_existing_path_FileLinks(): | |
72 | """ Calling _repr_html_ functions as expected on existing directory """ |
|
72 | """ Calling _repr_html_ functions as expected on existing directory """ | |
73 | td = mkdtemp() |
|
73 | td = mkdtemp() | |
74 | tf1 = NamedTemporaryFile(dir=td) |
|
74 | tf1 = NamedTemporaryFile(dir=td) | |
75 | tf2 = NamedTemporaryFile(dir=td) |
|
75 | tf2 = NamedTemporaryFile(dir=td) | |
76 | fl = display.FileLinks(td) |
|
76 | fl = display.FileLinks(td) | |
77 | actual = fl._repr_html_() |
|
77 | actual = fl._repr_html_() | |
78 | actual = actual.split('\n') |
|
78 | actual = actual.split('\n') | |
79 | actual.sort() |
|
79 | actual.sort() | |
80 | expected = ["%s/<br>" % td, |
|
80 | expected = ["%s/<br>" % td, | |
81 | " <a href='files/%s' target='_blank'>%s</a><br>" % (tf2.name,split(tf2.name)[1]), |
|
81 | " <a href='files/%s' target='_blank'>%s</a><br>" % (tf2.name,split(tf2.name)[1]), | |
82 | " <a href='files/%s' target='_blank'>%s</a><br>" % (tf1.name,split(tf1.name)[1])] |
|
82 | " <a href='files/%s' target='_blank'>%s</a><br>" % (tf1.name,split(tf1.name)[1])] | |
83 | expected.sort() |
|
83 | expected.sort() | |
84 | # We compare the sorted list of links here as that's more reliable |
|
84 | # We compare the sorted list of links here as that's more reliable | |
85 | nt.assert_equal(actual,expected) |
|
85 | nt.assert_equal(actual,expected) | |
86 |
|
86 | |||
87 | def test_existing_path_FileLinks_alt_formatter(): |
|
87 | def test_existing_path_FileLinks_alt_formatter(): | |
88 | """ Calling _repr_html_ functions as expected with an alternative formatter |
|
88 | """ Calling _repr_html_ functions as expected with an alternative formatter | |
89 | """ |
|
89 | """ | |
90 | td = mkdtemp() |
|
90 | td = mkdtemp() | |
91 | tf1 = NamedTemporaryFile(dir=td) |
|
91 | tf1 = NamedTemporaryFile(dir=td) | |
92 | tf2 = NamedTemporaryFile(dir=td) |
|
92 | tf2 = NamedTemporaryFile(dir=td) | |
93 |
def fake_formatter( |
|
93 | def fake_formatter(dirname,fnames): | |
94 |
|
|
94 | return ["hello","world"] | |
95 | return |
|
|||
96 | fl = display.FileLinks(td,notebook_display_formatter=fake_formatter) |
|
95 | fl = display.FileLinks(td,notebook_display_formatter=fake_formatter) | |
97 | actual = fl._repr_html_() |
|
96 | actual = fl._repr_html_() | |
98 | actual = actual.split('\n') |
|
97 | actual = actual.split('\n') | |
99 | actual.sort() |
|
98 | actual.sort() | |
100 | expected = ["hello","world"] |
|
99 | expected = ["hello","world"] | |
101 | expected.sort() |
|
100 | expected.sort() | |
102 | # We compare the sorted list of links here as that's more reliable |
|
101 | # We compare the sorted list of links here as that's more reliable | |
103 | nt.assert_equal(actual,expected) |
|
102 | nt.assert_equal(actual,expected) | |
104 |
|
103 | |||
105 | def test_existing_path_FileLinks_repr(): |
|
104 | def test_existing_path_FileLinks_repr(): | |
106 | """ Calling repr() functions as expected on existing directory """ |
|
105 | """ Calling repr() functions as expected on existing directory """ | |
107 | td = mkdtemp() |
|
106 | td = mkdtemp() | |
108 | tf1 = NamedTemporaryFile(dir=td) |
|
107 | tf1 = NamedTemporaryFile(dir=td) | |
109 | tf2 = NamedTemporaryFile(dir=td) |
|
108 | tf2 = NamedTemporaryFile(dir=td) | |
110 | fl = display.FileLinks(td) |
|
109 | fl = display.FileLinks(td) | |
111 | actual = repr(fl) |
|
110 | actual = repr(fl) | |
112 | actual = actual.split('\n') |
|
111 | actual = actual.split('\n') | |
113 | actual.sort() |
|
112 | actual.sort() | |
114 | expected = ['%s/' % td, ' %s' % split(tf1.name)[1],' %s' % split(tf2.name)[1]] |
|
113 | expected = ['%s/' % td, ' %s' % split(tf1.name)[1],' %s' % split(tf2.name)[1]] | |
115 | expected.sort() |
|
114 | expected.sort() | |
116 | # We compare the sorted list of links here as that's more reliable |
|
115 | # We compare the sorted list of links here as that's more reliable | |
117 | nt.assert_equal(actual,expected) |
|
116 | nt.assert_equal(actual,expected) | |
118 |
|
117 | |||
119 | def test_existing_path_FileLinks_repr_alt_formatter(): |
|
118 | def test_existing_path_FileLinks_repr_alt_formatter(): | |
120 | """ Calling repr() functions as expected with an alternative formatter |
|
119 | """ Calling repr() functions as expected with an alternative formatter | |
121 | """ |
|
120 | """ | |
122 | td = mkdtemp() |
|
121 | td = mkdtemp() | |
123 | tf1 = NamedTemporaryFile(dir=td) |
|
122 | tf1 = NamedTemporaryFile(dir=td) | |
124 | tf2 = NamedTemporaryFile(dir=td) |
|
123 | tf2 = NamedTemporaryFile(dir=td) | |
125 |
def fake_formatter( |
|
124 | def fake_formatter(dirname,fnames): | |
126 |
|
|
125 | return ["hello","world"] | |
127 | return |
|
|||
128 | fl = display.FileLinks(td,terminal_display_formatter=fake_formatter) |
|
126 | fl = display.FileLinks(td,terminal_display_formatter=fake_formatter) | |
129 | actual = repr(fl) |
|
127 | actual = repr(fl) | |
130 | actual = actual.split('\n') |
|
128 | actual = actual.split('\n') | |
131 | actual.sort() |
|
129 | actual.sort() | |
132 | expected = ["hello","world"] |
|
130 | expected = ["hello","world"] | |
133 | expected.sort() |
|
131 | expected.sort() | |
134 | # We compare the sorted list of links here as that's more reliable |
|
132 | # We compare the sorted list of links here as that's more reliable | |
135 | nt.assert_equal(actual,expected) |
|
133 | nt.assert_equal(actual,expected) | |
136 |
|
134 | |||
137 | #-------------------------- |
|
135 | #-------------------------- | |
138 | # DirectoryLink tests |
|
136 | # DirectoryLink tests | |
139 | #-------------------------- |
|
137 | #-------------------------- | |
140 |
|
138 | |||
141 | def test_instantiation_DirectoryLink(): |
|
139 | def test_instantiation_DirectoryLink(): | |
142 | """Test classes can be instantiated""" |
|
140 | """Test classes can be instantiated""" | |
143 | dl = display.DirectoryLink('example') |
|
141 | dl = display.DirectoryLink('example') | |
144 |
|
142 | |||
145 | def test_warning_on_non_existant_path_DirectoryLink(): |
|
143 | def test_warning_on_non_existant_path_DirectoryLink(): | |
146 | """Calling _repr_html_ on non-existant files returns a warning""" |
|
144 | """Calling _repr_html_ on non-existant files returns a warning""" | |
147 | dl = display.DirectoryLink('example') |
|
145 | dl = display.DirectoryLink('example') | |
148 | nt.assert_true(dl._repr_html_().startswith('Path (<tt>example</tt>)')) |
|
146 | nt.assert_true(dl._repr_html_().startswith('Path (<tt>example</tt>)')) | |
149 |
|
147 | |||
150 | def test_existing_path_DirectoryLink(): |
|
148 | def test_existing_path_DirectoryLink(): | |
151 | """ Calling _repr_html_ functions as expected on existing directory """ |
|
149 | """ Calling _repr_html_ functions as expected on existing directory """ | |
152 | td = mkdtemp() |
|
150 | td = mkdtemp() | |
153 | dl = display.DirectoryLink(td) |
|
151 | dl = display.DirectoryLink(td) | |
154 | actual = dl._repr_html_() |
|
152 | actual = dl._repr_html_() | |
155 | expected = "<a href='files/%s' target='_blank'>%s</a><br>" % (td,td) |
|
153 | expected = "<a href='files/%s' target='_blank'>%s</a><br>" % (td,td) | |
156 | nt.assert_equal(actual,expected) |
|
154 | nt.assert_equal(actual,expected) |
General Comments 0
You need to be logged in to leave comments.
Login now