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