Show More
@@ -1,287 +1,301 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, sep | |
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 using FileLink. " |
|
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): |
|
174 | if isfile(path): | |
175 | raise ValueError,\ |
|
175 | raise ValueError,\ | |
176 | ("Cannot display a file using FileLinks. " |
|
176 | ("Cannot display a file using FileLinks. " | |
177 | "Use FileLink to display '%s'." % path) |
|
177 | "Use FileLink to display '%s'." % path) | |
178 | self.included_suffixes = included_suffixes |
|
178 | self.included_suffixes = included_suffixes | |
179 | # remove trailing slashs for more consistent output formatting |
|
179 | # remove trailing slashs for more consistent output formatting | |
180 | path = path.rstrip('/') |
|
180 | path = path.rstrip('/') | |
181 |
|
181 | |||
182 | self.path = path |
|
182 | self.path = path | |
183 | self.url_prefix = url_prefix |
|
183 | self.url_prefix = url_prefix | |
184 | self.result_html_prefix = result_html_prefix |
|
184 | self.result_html_prefix = result_html_prefix | |
185 | self.result_html_suffix = result_html_suffix |
|
185 | self.result_html_suffix = result_html_suffix | |
186 |
|
186 | |||
187 | self.notebook_display_formatter = \ |
|
187 | self.notebook_display_formatter = \ | |
188 | notebook_display_formatter or self._get_notebook_display_formatter() |
|
188 | notebook_display_formatter or self._get_notebook_display_formatter() | |
189 | self.terminal_display_formatter = \ |
|
189 | self.terminal_display_formatter = \ | |
190 | terminal_display_formatter or self._get_terminal_display_formatter() |
|
190 | terminal_display_formatter or self._get_terminal_display_formatter() | |
191 |
|
191 | |||
192 | def _get_display_formatter(self, |
|
192 | def _get_display_formatter(self, | |
193 | dirname_output_format, |
|
193 | dirname_output_format, | |
194 | fname_output_format, |
|
194 | fname_output_format, | |
195 |
fp_format |
|
195 | fp_format, | |
|
196 | fp_cleaner=None): | |||
196 | """ generate built-in formatter function |
|
197 | """ generate built-in formatter function | |
197 |
|
198 | |||
198 | this is used to define both the notebook and terminal built-in |
|
199 | this is used to define both the notebook and terminal built-in | |
199 | formatters as they only differ by some wrapper text for each entry |
|
200 | formatters as they only differ by some wrapper text for each entry | |
200 |
|
201 | |||
201 | dirname_output_format: string to use for formatting directory |
|
202 | dirname_output_format: string to use for formatting directory | |
202 | names, dirname will be substituted for a single "%s" which |
|
203 | names, dirname will be substituted for a single "%s" which | |
203 | must appear in this string |
|
204 | must appear in this string | |
204 | fname_output_format: string to use for formatting file names, |
|
205 | fname_output_format: string to use for formatting file names, | |
205 | if a single "%s" appears in the string, fname will be substituted |
|
206 | if a single "%s" appears in the string, fname will be substituted | |
206 | if two "%s" appear in the string, the path to fname will be |
|
207 | if two "%s" appear in the string, the path to fname will be | |
207 | substituted for the first and fname will be substituted for the |
|
208 | substituted for the first and fname will be substituted for the | |
208 | second |
|
209 | second | |
209 | fp_format: string to use for formatting filepaths, must contain |
|
210 | fp_format: string to use for formatting filepaths, must contain | |
210 | exactly two "%s" and the dirname will be subsituted for the first |
|
211 | exactly two "%s" and the dirname will be subsituted for the first | |
211 | and fname will be substituted for the second |
|
212 | and fname will be substituted for the second | |
212 | """ |
|
213 | """ | |
213 | def f(dirname, fnames, included_suffixes=None): |
|
214 | def f(dirname, fnames, included_suffixes=None): | |
214 | result = [] |
|
215 | result = [] | |
215 | # begin by figuring out which filenames, if any, |
|
216 | # begin by figuring out which filenames, if any, | |
216 | # are going to be displayed |
|
217 | # are going to be displayed | |
217 | display_fnames = [] |
|
218 | display_fnames = [] | |
218 | for fname in fnames: |
|
219 | for fname in fnames: | |
219 | if (isfile(join(dirname,fname)) and |
|
220 | if (isfile(join(dirname,fname)) and | |
220 | (included_suffixes == None or |
|
221 | (included_suffixes == None or | |
221 | splitext(fname)[1] in included_suffixes)): |
|
222 | splitext(fname)[1] in included_suffixes)): | |
222 | display_fnames.append(fname) |
|
223 | display_fnames.append(fname) | |
223 |
|
224 | |||
224 | if len(display_fnames) == 0: |
|
225 | if len(display_fnames) == 0: | |
225 | # if there are no filenames to display, don't print anything |
|
226 | # if there are no filenames to display, don't print anything | |
226 | # (not even the directory name) |
|
227 | # (not even the directory name) | |
227 | pass |
|
228 | pass | |
228 | else: |
|
229 | else: | |
229 | # otherwise print the formatted directory name followed by |
|
230 | # otherwise print the formatted directory name followed by | |
230 | # the formatted filenames |
|
231 | # the formatted filenames | |
231 | dirname_output_line = dirname_output_format % dirname |
|
232 | dirname_output_line = dirname_output_format % dirname | |
232 | result.append(dirname_output_line) |
|
233 | result.append(dirname_output_line) | |
233 | for fname in display_fnames: |
|
234 | for fname in display_fnames: | |
234 | fp = fp_format % (dirname,fname) |
|
235 | fp = fp_format % (dirname,fname) | |
|
236 | if fp_cleaner != None: | |||
|
237 | fp = fp_cleaner(fp) | |||
235 | try: |
|
238 | try: | |
236 | # output can include both a filepath and a filename... |
|
239 | # output can include both a filepath and a filename... | |
237 | fname_output_line = fname_output_format % (fp, fname) |
|
240 | fname_output_line = fname_output_format % (fp, fname) | |
238 | except TypeError: |
|
241 | except TypeError: | |
239 | # ... or just a single filepath |
|
242 | # ... or just a single filepath | |
240 | fname_output_line = fname_output_format % fname |
|
243 | fname_output_line = fname_output_format % fname | |
241 | result.append(fname_output_line) |
|
244 | result.append(fname_output_line) | |
242 | return result |
|
245 | return result | |
243 | return f |
|
246 | return f | |
244 |
|
247 | |||
245 | def _get_notebook_display_formatter(self, |
|
248 | def _get_notebook_display_formatter(self, | |
246 | spacer=" "): |
|
249 | spacer=" "): | |
247 | """ generate function to use for notebook formatting |
|
250 | """ generate function to use for notebook formatting | |
248 | """ |
|
251 | """ | |
249 | dirname_output_format = \ |
|
252 | dirname_output_format = \ | |
250 | self.result_html_prefix + "%s/" + self.result_html_suffix |
|
253 | self.result_html_prefix + "%s/" + self.result_html_suffix | |
251 | fname_output_format = \ |
|
254 | fname_output_format = \ | |
252 | self.result_html_prefix + spacer + self.html_link_str + self.result_html_suffix |
|
255 | self.result_html_prefix + spacer + self.html_link_str + self.result_html_suffix | |
253 | fp_format = self.url_prefix + '%s/%s' |
|
256 | fp_format = self.url_prefix + '%s/%s' | |
|
257 | if sep == "\\": | |||
|
258 | # Working on a platform where the path separator is "\", so | |||
|
259 | # must convert these to "/" for generating a URI | |||
|
260 | def fp_cleaner(fp): | |||
|
261 | # Replace all occurences of backslash ("\") with a forward | |||
|
262 | # slash ("/") - this is necessary on windows when a path is | |||
|
263 | # provided as input, but we must link to a URI | |||
|
264 | return fp.replace('\\','/') | |||
|
265 | else: | |||
|
266 | fp_cleaner = None | |||
254 |
|
267 | |||
255 | return self._get_display_formatter(dirname_output_format, |
|
268 | return self._get_display_formatter(dirname_output_format, | |
256 | fname_output_format, |
|
269 | fname_output_format, | |
257 |
fp_format |
|
270 | fp_format, | |
|
271 | fp_cleaner) | |||
258 |
|
272 | |||
259 | def _get_terminal_display_formatter(self, |
|
273 | def _get_terminal_display_formatter(self, | |
260 | spacer=" "): |
|
274 | spacer=" "): | |
261 | """ generate function to use for terminal formatting |
|
275 | """ generate function to use for terminal formatting | |
262 | """ |
|
276 | """ | |
263 | dirname_output_format = "%s/" |
|
277 | dirname_output_format = "%s/" | |
264 | fname_output_format = spacer + "%s" |
|
278 | fname_output_format = spacer + "%s" | |
265 | fp_format = '%s/%s' |
|
279 | fp_format = '%s/%s' | |
266 |
|
280 | |||
267 | return self._get_display_formatter(dirname_output_format, |
|
281 | return self._get_display_formatter(dirname_output_format, | |
268 | fname_output_format, |
|
282 | fname_output_format, | |
269 | fp_format) |
|
283 | fp_format) | |
270 |
|
284 | |||
271 | def _format_path(self): |
|
285 | def _format_path(self): | |
272 | result_lines = [] |
|
286 | result_lines = [] | |
273 | walked_dir = list(walk(self.path)) |
|
287 | walked_dir = list(walk(self.path)) | |
274 | walked_dir.sort() |
|
288 | walked_dir.sort() | |
275 | for dirname, subdirs, fnames in walked_dir: |
|
289 | for dirname, subdirs, fnames in walked_dir: | |
276 | result_lines += self.notebook_display_formatter(dirname, fnames, self.included_suffixes) |
|
290 | result_lines += self.notebook_display_formatter(dirname, fnames, self.included_suffixes) | |
277 | return '\n'.join(result_lines) |
|
291 | return '\n'.join(result_lines) | |
278 |
|
292 | |||
279 | def __repr__(self): |
|
293 | def __repr__(self): | |
280 | """return newline-separated absolute paths |
|
294 | """return newline-separated absolute paths | |
281 | """ |
|
295 | """ | |
282 | result_lines = [] |
|
296 | result_lines = [] | |
283 | walked_dir = list(walk(self.path)) |
|
297 | walked_dir = list(walk(self.path)) | |
284 | walked_dir.sort() |
|
298 | walked_dir.sort() | |
285 | for dirname, subdirs, fnames in walked_dir: |
|
299 | for dirname, subdirs, fnames in walked_dir: | |
286 | result_lines += self.terminal_display_formatter(dirname, fnames, self.included_suffixes) |
|
300 | result_lines += self.terminal_display_formatter(dirname, fnames, self.included_suffixes) | |
287 | return '\n'.join(result_lines) |
|
301 | return '\n'.join(result_lines) |
@@ -1,153 +1,158 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 | from os import sep | |||
18 |
|
19 | |||
19 | # Third-party imports |
|
20 | # Third-party imports | |
20 | import nose.tools as nt |
|
21 | import nose.tools as nt | |
21 |
|
22 | |||
22 | # Our own imports |
|
23 | # Our own imports | |
23 | from IPython.lib import display |
|
24 | from IPython.lib import display | |
24 |
|
25 | |||
25 | #----------------------------------------------------------------------------- |
|
26 | #----------------------------------------------------------------------------- | |
26 | # Classes and functions |
|
27 | # Classes and functions | |
27 | #----------------------------------------------------------------------------- |
|
28 | #----------------------------------------------------------------------------- | |
28 |
|
29 | |||
29 | #-------------------------- |
|
30 | #-------------------------- | |
30 | # FileLink tests |
|
31 | # FileLink tests | |
31 | #-------------------------- |
|
32 | #-------------------------- | |
32 |
|
33 | |||
33 | def test_instantiation_FileLink(): |
|
34 | def test_instantiation_FileLink(): | |
34 | """FileLink: Test class can be instantiated""" |
|
35 | """FileLink: Test class can be instantiated""" | |
35 | fl = display.FileLink('example.txt') |
|
36 | fl = display.FileLink('example.txt') | |
36 |
|
37 | |||
37 | def test_warning_on_non_existant_path_FileLink(): |
|
38 | def test_warning_on_non_existant_path_FileLink(): | |
38 | """FileLink: Calling _repr_html_ on non-existant files returns a warning |
|
39 | """FileLink: Calling _repr_html_ on non-existant files returns a warning | |
39 | """ |
|
40 | """ | |
40 | fl = display.FileLink('example.txt') |
|
41 | fl = display.FileLink('example.txt') | |
41 | nt.assert_true(fl._repr_html_().startswith('Path (<tt>example.txt</tt>)')) |
|
42 | nt.assert_true(fl._repr_html_().startswith('Path (<tt>example.txt</tt>)')) | |
42 |
|
43 | |||
43 | def test_existing_path_FileLink(): |
|
44 | def test_existing_path_FileLink(): | |
44 | """FileLink: Calling _repr_html_ functions as expected on existing filepath |
|
45 | """FileLink: Calling _repr_html_ functions as expected on existing filepath | |
45 | """ |
|
46 | """ | |
46 | tf = NamedTemporaryFile() |
|
47 | tf = NamedTemporaryFile() | |
47 | fl = display.FileLink(tf.name) |
|
48 | fl = display.FileLink(tf.name) | |
48 | actual = fl._repr_html_() |
|
49 | actual = fl._repr_html_() | |
49 | expected = "<a href='files/%s' target='_blank'>%s</a><br>" % (tf.name,tf.name) |
|
50 | expected = "<a href='files/%s' target='_blank'>%s</a><br>" % (tf.name,tf.name) | |
50 | nt.assert_equal(actual,expected) |
|
51 | nt.assert_equal(actual,expected) | |
51 |
|
52 | |||
52 | def test_existing_path_FileLink_repr(): |
|
53 | def test_existing_path_FileLink_repr(): | |
53 | """FileLink: Calling repr() functions as expected on existing filepath |
|
54 | """FileLink: Calling repr() functions as expected on existing filepath | |
54 | """ |
|
55 | """ | |
55 | tf = NamedTemporaryFile() |
|
56 | tf = NamedTemporaryFile() | |
56 | fl = display.FileLink(tf.name) |
|
57 | fl = display.FileLink(tf.name) | |
57 | actual = repr(fl) |
|
58 | actual = repr(fl) | |
58 | expected = tf.name |
|
59 | expected = tf.name | |
59 | nt.assert_equal(actual,expected) |
|
60 | nt.assert_equal(actual,expected) | |
60 |
|
61 | |||
61 | def test_error_on_directory_to_FileLink(): |
|
62 | def test_error_on_directory_to_FileLink(): | |
62 | """FileLink: Raises error when passed directory |
|
63 | """FileLink: Raises error when passed directory | |
63 | """ |
|
64 | """ | |
64 | td = mkdtemp() |
|
65 | td = mkdtemp() | |
65 | nt.assert_raises(ValueError,display.FileLink,td) |
|
66 | nt.assert_raises(ValueError,display.FileLink,td) | |
66 |
|
67 | |||
67 | #-------------------------- |
|
68 | #-------------------------- | |
68 | # FileLinks tests |
|
69 | # FileLinks tests | |
69 | #-------------------------- |
|
70 | #-------------------------- | |
70 |
|
71 | |||
71 | def test_instantiation_FileLinks(): |
|
72 | def test_instantiation_FileLinks(): | |
72 | """FileLinks: Test class can be instantiated |
|
73 | """FileLinks: Test class can be instantiated | |
73 | """ |
|
74 | """ | |
74 | fls = display.FileLinks('example') |
|
75 | fls = display.FileLinks('example') | |
75 |
|
76 | |||
76 | def test_warning_on_non_existant_path_FileLinks(): |
|
77 | def test_warning_on_non_existant_path_FileLinks(): | |
77 | """FileLinks: Calling _repr_html_ on non-existant files returns a warning |
|
78 | """FileLinks: Calling _repr_html_ on non-existant files returns a warning | |
78 | """ |
|
79 | """ | |
79 | fls = display.FileLinks('example') |
|
80 | fls = display.FileLinks('example') | |
80 | nt.assert_true(fls._repr_html_().startswith('Path (<tt>example</tt>)')) |
|
81 | nt.assert_true(fls._repr_html_().startswith('Path (<tt>example</tt>)')) | |
81 |
|
82 | |||
82 | def test_existing_path_FileLinks(): |
|
83 | def test_existing_path_FileLinks(): | |
83 | """FileLinks: Calling _repr_html_ functions as expected on existing dir |
|
84 | """FileLinks: Calling _repr_html_ functions as expected on existing dir | |
84 | """ |
|
85 | """ | |
85 | td = mkdtemp() |
|
86 | td = mkdtemp() | |
86 | tf1 = NamedTemporaryFile(dir=td) |
|
87 | tf1 = NamedTemporaryFile(dir=td) | |
87 | tf2 = NamedTemporaryFile(dir=td) |
|
88 | tf2 = NamedTemporaryFile(dir=td) | |
88 | fl = display.FileLinks(td) |
|
89 | fl = display.FileLinks(td) | |
89 | actual = fl._repr_html_() |
|
90 | actual = fl._repr_html_() | |
90 | actual = actual.split('\n') |
|
91 | actual = actual.split('\n') | |
91 | actual.sort() |
|
92 | actual.sort() | |
|
93 | # the links should always have forward slashes, even on windows, so replace | |||
|
94 | # backslashes with forward slashes here | |||
92 | expected = ["%s/<br>" % td, |
|
95 | expected = ["%s/<br>" % td, | |
93 |
" <a href='files/%s' target='_blank'>%s</a><br>" % |
|
96 | " <a href='files/%s' target='_blank'>%s</a><br>" %\ | |
94 | " <a href='files/%s' target='_blank'>%s</a><br>" % (tf1.name,split(tf1.name)[1])] |
|
97 | (tf2.name.replace("\\","/"),split(tf2.name)[1]), | |
|
98 | " <a href='files/%s' target='_blank'>%s</a><br>" %\ | |||
|
99 | (tf1.name.replace("\\","/"),split(tf1.name)[1])] | |||
95 | expected.sort() |
|
100 | expected.sort() | |
96 | # 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 | |
97 | nt.assert_equal(actual,expected) |
|
102 | nt.assert_equal(actual,expected) | |
98 |
|
103 | |||
99 | def test_existing_path_FileLinks_alt_formatter(): |
|
104 | def test_existing_path_FileLinks_alt_formatter(): | |
100 | """FileLinks: Calling _repr_html_ functions as expected w/ an alt formatter |
|
105 | """FileLinks: Calling _repr_html_ functions as expected w/ an alt formatter | |
101 | """ |
|
106 | """ | |
102 | td = mkdtemp() |
|
107 | td = mkdtemp() | |
103 | tf1 = NamedTemporaryFile(dir=td) |
|
108 | tf1 = NamedTemporaryFile(dir=td) | |
104 | tf2 = NamedTemporaryFile(dir=td) |
|
109 | tf2 = NamedTemporaryFile(dir=td) | |
105 | def fake_formatter(dirname,fnames,included_suffixes): |
|
110 | def fake_formatter(dirname,fnames,included_suffixes): | |
106 | return ["hello","world"] |
|
111 | return ["hello","world"] | |
107 | fl = display.FileLinks(td,notebook_display_formatter=fake_formatter) |
|
112 | fl = display.FileLinks(td,notebook_display_formatter=fake_formatter) | |
108 | actual = fl._repr_html_() |
|
113 | actual = fl._repr_html_() | |
109 | actual = actual.split('\n') |
|
114 | actual = actual.split('\n') | |
110 | actual.sort() |
|
115 | actual.sort() | |
111 | expected = ["hello","world"] |
|
116 | expected = ["hello","world"] | |
112 | expected.sort() |
|
117 | expected.sort() | |
113 | # We compare the sorted list of links here as that's more reliable |
|
118 | # We compare the sorted list of links here as that's more reliable | |
114 | nt.assert_equal(actual,expected) |
|
119 | nt.assert_equal(actual,expected) | |
115 |
|
120 | |||
116 | def test_existing_path_FileLinks_repr(): |
|
121 | def test_existing_path_FileLinks_repr(): | |
117 | """FileLinks: Calling repr() functions as expected on existing directory """ |
|
122 | """FileLinks: Calling repr() functions as expected on existing directory """ | |
118 | td = mkdtemp() |
|
123 | td = mkdtemp() | |
119 | tf1 = NamedTemporaryFile(dir=td) |
|
124 | tf1 = NamedTemporaryFile(dir=td) | |
120 | tf2 = NamedTemporaryFile(dir=td) |
|
125 | tf2 = NamedTemporaryFile(dir=td) | |
121 | fl = display.FileLinks(td) |
|
126 | fl = display.FileLinks(td) | |
122 | actual = repr(fl) |
|
127 | actual = repr(fl) | |
123 | actual = actual.split('\n') |
|
128 | actual = actual.split('\n') | |
124 | actual.sort() |
|
129 | actual.sort() | |
125 | expected = ['%s/' % td, ' %s' % split(tf1.name)[1],' %s' % split(tf2.name)[1]] |
|
130 | expected = ['%s/' % td, ' %s' % split(tf1.name)[1],' %s' % split(tf2.name)[1]] | |
126 | expected.sort() |
|
131 | expected.sort() | |
127 | # 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 | |
128 | nt.assert_equal(actual,expected) |
|
133 | nt.assert_equal(actual,expected) | |
129 |
|
134 | |||
130 | def test_existing_path_FileLinks_repr_alt_formatter(): |
|
135 | def test_existing_path_FileLinks_repr_alt_formatter(): | |
131 | """FileLinks: Calling repr() functions as expected w/ alt formatter |
|
136 | """FileLinks: Calling repr() functions as expected w/ alt formatter | |
132 | """ |
|
137 | """ | |
133 | td = mkdtemp() |
|
138 | td = mkdtemp() | |
134 | tf1 = NamedTemporaryFile(dir=td) |
|
139 | tf1 = NamedTemporaryFile(dir=td) | |
135 | tf2 = NamedTemporaryFile(dir=td) |
|
140 | tf2 = NamedTemporaryFile(dir=td) | |
136 | def fake_formatter(dirname,fnames,included_suffixes): |
|
141 | def fake_formatter(dirname,fnames,included_suffixes): | |
137 | return ["hello","world"] |
|
142 | return ["hello","world"] | |
138 | fl = display.FileLinks(td,terminal_display_formatter=fake_formatter) |
|
143 | fl = display.FileLinks(td,terminal_display_formatter=fake_formatter) | |
139 | actual = repr(fl) |
|
144 | actual = repr(fl) | |
140 | actual = actual.split('\n') |
|
145 | actual = actual.split('\n') | |
141 | actual.sort() |
|
146 | actual.sort() | |
142 | expected = ["hello","world"] |
|
147 | expected = ["hello","world"] | |
143 | expected.sort() |
|
148 | expected.sort() | |
144 | # We compare the sorted list of links here as that's more reliable |
|
149 | # We compare the sorted list of links here as that's more reliable | |
145 | nt.assert_equal(actual,expected) |
|
150 | nt.assert_equal(actual,expected) | |
146 |
|
151 | |||
147 | def test_error_on_file_to_FileLinks(): |
|
152 | def test_error_on_file_to_FileLinks(): | |
148 | """FileLinks: Raises error when passed file |
|
153 | """FileLinks: Raises error when passed file | |
149 | """ |
|
154 | """ | |
150 | td = mkdtemp() |
|
155 | td = mkdtemp() | |
151 | tf1 = NamedTemporaryFile(dir=td) |
|
156 | tf1 = NamedTemporaryFile(dir=td) | |
152 | nt.assert_raises(ValueError,display.FileLinks,tf1.name) |
|
157 | nt.assert_raises(ValueError,display.FileLinks,tf1.name) | |
153 |
|
158 |
General Comments 0
You need to be logged in to leave comments.
Login now