Show More
@@ -1,552 +1,552 b'' | |||
|
1 | 1 | """Various display related classes. |
|
2 | 2 | |
|
3 | 3 | Authors : MinRK, gregcaporaso, dannystaple |
|
4 | 4 | """ |
|
5 | 5 | from os.path import exists, isfile, splitext, abspath, join, isdir |
|
6 | 6 | from os import walk, sep |
|
7 | 7 | |
|
8 | 8 | from IPython.core.display import DisplayObject |
|
9 | 9 | |
|
10 | 10 | __all__ = ['Audio', 'IFrame', 'YouTubeVideo', 'VimeoVideo', 'ScribdDocument', |
|
11 | 11 | 'FileLink', 'FileLinks'] |
|
12 | 12 | |
|
13 | 13 | |
|
14 | 14 | class Audio(DisplayObject): |
|
15 | 15 | """Create an audio object. |
|
16 | 16 | |
|
17 | 17 | When this object is returned by an input cell or passed to the |
|
18 | 18 | display function, it will result in Audio controls being displayed |
|
19 | 19 | in the frontend (only works in the notebook). |
|
20 | 20 | |
|
21 | 21 | Parameters |
|
22 | 22 | ---------- |
|
23 | 23 | data : numpy array, list, unicode, str or bytes |
|
24 | 24 | Can be one of |
|
25 | 25 | |
|
26 | 26 | * Numpy 1d array containing the desired waveform (mono) |
|
27 | 27 | * Numpy 2d array containing waveforms for each channel. |
|
28 | 28 | Shape=(NCHAN, NSAMPLES). For the standard channel order, see |
|
29 | 29 | http://msdn.microsoft.com/en-us/library/windows/hardware/dn653308(v=vs.85).aspx |
|
30 | 30 | * List of float or integer representing the waveform (mono) |
|
31 | 31 | * String containing the filename |
|
32 | 32 | * Bytestring containing raw PCM data or |
|
33 | 33 | * URL pointing to a file on the web. |
|
34 | 34 | |
|
35 | 35 | If the array option is used the waveform will be normalized. |
|
36 | 36 | |
|
37 | 37 | If a filename or url is used the format support will be browser |
|
38 | 38 | dependent. |
|
39 | 39 | url : unicode |
|
40 | 40 | A URL to download the data from. |
|
41 | 41 | filename : unicode |
|
42 | 42 | Path to a local file to load the data from. |
|
43 | 43 | embed : boolean |
|
44 | 44 | Should the image data be embedded using a data URI (True) or should |
|
45 | 45 | the original source be referenced. Set this to True if you want the |
|
46 | 46 | audio to playable later with no internet connection in the notebook. |
|
47 | 47 | |
|
48 | 48 | Default is `True`, unless the keyword argument `url` is set, then |
|
49 | 49 | default value is `False`. |
|
50 | 50 | rate : integer |
|
51 | 51 | The sampling rate of the raw data. |
|
52 | 52 | Only required when data parameter is being used as an array |
|
53 | 53 | autoplay : bool |
|
54 | 54 | Set to True if the audio should immediately start playing. |
|
55 | 55 | Default is `False`. |
|
56 | 56 | |
|
57 | 57 | Examples |
|
58 | 58 | -------- |
|
59 | 59 | :: |
|
60 | 60 | |
|
61 | 61 | # Generate a sound |
|
62 | 62 | import numpy as np |
|
63 | 63 | framerate = 44100 |
|
64 | 64 | t = np.linspace(0,5,framerate*5) |
|
65 | 65 | data = np.sin(2*np.pi*220*t) + np.sin(2*np.pi*224*t)) |
|
66 | 66 | Audio(data,rate=framerate) |
|
67 | 67 | |
|
68 | 68 | # Can also do stereo or more channels |
|
69 | 69 | dataleft = np.sin(2*np.pi*220*t) |
|
70 | 70 | dataright = np.sin(2*np.pi*224*t) |
|
71 | 71 | Audio([dataleft, dataright],rate=framerate) |
|
72 | 72 | |
|
73 | 73 | Audio("http://www.nch.com.au/acm/8k16bitpcm.wav") # From URL |
|
74 | 74 | Audio(url="http://www.w3schools.com/html/horse.ogg") |
|
75 | 75 | |
|
76 | 76 | Audio('/path/to/sound.wav') # From file |
|
77 | 77 | Audio(filename='/path/to/sound.ogg') |
|
78 | 78 | |
|
79 | 79 | Audio(b'RAW_WAV_DATA..) # From bytes |
|
80 | 80 | Audio(data=b'RAW_WAV_DATA..) |
|
81 | 81 | |
|
82 | 82 | """ |
|
83 | 83 | _read_flags = 'rb' |
|
84 | 84 | |
|
85 | 85 | def __init__(self, data=None, filename=None, url=None, embed=None, rate=None, autoplay=False): |
|
86 | 86 | if filename is None and url is None and data is None: |
|
87 | 87 | raise ValueError("No image data found. Expecting filename, url, or data.") |
|
88 | 88 | if embed is False and url is None: |
|
89 | 89 | raise ValueError("No url found. Expecting url when embed=False") |
|
90 | 90 | |
|
91 | 91 | if url is not None and embed is not True: |
|
92 | 92 | self.embed = False |
|
93 | 93 | else: |
|
94 | 94 | self.embed = True |
|
95 | 95 | self.autoplay = autoplay |
|
96 | 96 | super(Audio, self).__init__(data=data, url=url, filename=filename) |
|
97 | 97 | |
|
98 | 98 | if self.data is not None and not isinstance(self.data, bytes): |
|
99 | 99 | self.data = self._make_wav(data,rate) |
|
100 | 100 | |
|
101 | 101 | def reload(self): |
|
102 | 102 | """Reload the raw data from file or URL.""" |
|
103 | 103 | import mimetypes |
|
104 | 104 | if self.embed: |
|
105 | 105 | super(Audio, self).reload() |
|
106 | 106 | |
|
107 | 107 | if self.filename is not None: |
|
108 | 108 | self.mimetype = mimetypes.guess_type(self.filename)[0] |
|
109 | 109 | elif self.url is not None: |
|
110 | 110 | self.mimetype = mimetypes.guess_type(self.url)[0] |
|
111 | 111 | else: |
|
112 | 112 | self.mimetype = "audio/wav" |
|
113 | 113 | |
|
114 | 114 | def _make_wav(self, data, rate): |
|
115 | 115 | """ Transform a numpy array to a PCM bytestring """ |
|
116 | 116 | import struct |
|
117 | 117 | from io import BytesIO |
|
118 | 118 | import wave |
|
119 | 119 | |
|
120 | 120 | try: |
|
121 | 121 | import numpy as np |
|
122 | 122 | |
|
123 | 123 | data = np.array(data, dtype=float) |
|
124 | 124 | if len(data.shape) == 1: |
|
125 | 125 | nchan = 1 |
|
126 | 126 | elif len(data.shape) == 2: |
|
127 | 127 | # In wave files,channels are interleaved. E.g., |
|
128 | 128 | # "L1R1L2R2..." for stereo. See |
|
129 | 129 | # http://msdn.microsoft.com/en-us/library/windows/hardware/dn653308(v=vs.85).aspx |
|
130 | 130 | # for channel ordering |
|
131 | 131 | nchan = data.shape[0] |
|
132 | 132 | data = data.T.ravel() |
|
133 | 133 | else: |
|
134 | 134 | raise ValueError('Array audio input must be a 1D or 2D array') |
|
135 | 135 | scaled = np.int16(data/np.max(np.abs(data))*32767).tolist() |
|
136 | 136 | except ImportError: |
|
137 | 137 | # check that it is a "1D" list |
|
138 | 138 | idata = iter(data) # fails if not an iterable |
|
139 | 139 | try: |
|
140 | 140 | iter(idata.next()) |
|
141 | 141 | raise TypeError('Only lists of mono audio are ' |
|
142 | 142 | 'supported if numpy is not installed') |
|
143 | 143 | except TypeError: |
|
144 | 144 | # this means it's not a nested list, which is what we want |
|
145 | 145 | pass |
|
146 | 146 | maxabsvalue = float(max([abs(x) for x in data])) |
|
147 | 147 | scaled = [int(x/maxabsvalue*32767) for x in data] |
|
148 | 148 | nchan = 1 |
|
149 | 149 | |
|
150 | 150 | fp = BytesIO() |
|
151 | 151 | waveobj = wave.open(fp,mode='wb') |
|
152 | 152 | waveobj.setnchannels(nchan) |
|
153 | 153 | waveobj.setframerate(rate) |
|
154 | 154 | waveobj.setsampwidth(2) |
|
155 | 155 | waveobj.setcomptype('NONE','NONE') |
|
156 | 156 | waveobj.writeframes(b''.join([struct.pack('<h',x) for x in scaled])) |
|
157 | 157 | val = fp.getvalue() |
|
158 | 158 | waveobj.close() |
|
159 | 159 | |
|
160 | 160 | return val |
|
161 | 161 | |
|
162 | 162 | def _data_and_metadata(self): |
|
163 | 163 | """shortcut for returning metadata with url information, if defined""" |
|
164 | 164 | md = {} |
|
165 | 165 | if self.url: |
|
166 | 166 | md['url'] = self.url |
|
167 | 167 | if md: |
|
168 | 168 | return self.data, md |
|
169 | 169 | else: |
|
170 | 170 | return self.data |
|
171 | 171 | |
|
172 | 172 | def _repr_html_(self): |
|
173 | 173 | src = """ |
|
174 | 174 | <audio controls="controls" {autoplay}> |
|
175 | 175 | <source src="{src}" type="{type}" /> |
|
176 | 176 | Your browser does not support the audio element. |
|
177 | 177 | </audio> |
|
178 | 178 | """ |
|
179 | 179 | return src.format(src=self.src_attr(),type=self.mimetype, autoplay=self.autoplay_attr()) |
|
180 | 180 | |
|
181 | 181 | def src_attr(self): |
|
182 | 182 | import base64 |
|
183 | 183 | if self.embed and (self.data is not None): |
|
184 | 184 | data = base64=base64.b64encode(self.data).decode('ascii') |
|
185 | 185 | return """data:{type};base64,{base64}""".format(type=self.mimetype, |
|
186 | 186 | base64=data) |
|
187 | 187 | elif self.url is not None: |
|
188 | 188 | return self.url |
|
189 | 189 | else: |
|
190 | 190 | return "" |
|
191 | 191 | |
|
192 | 192 | def autoplay_attr(self): |
|
193 | 193 | if(self.autoplay): |
|
194 | 194 | return 'autoplay="autoplay"' |
|
195 | 195 | else: |
|
196 | 196 | return '' |
|
197 | 197 | |
|
198 | 198 | class IFrame(object): |
|
199 | 199 | """ |
|
200 | 200 | Generic class to embed an iframe in an IPython notebook |
|
201 | 201 | """ |
|
202 | 202 | |
|
203 | 203 | iframe = """ |
|
204 | 204 | <iframe |
|
205 | 205 | width="{width}" |
|
206 | 206 | height="{height}" |
|
207 | 207 | src="{src}{params}" |
|
208 | 208 | frameborder="0" |
|
209 | 209 | allowfullscreen |
|
210 | 210 | ></iframe> |
|
211 | 211 | """ |
|
212 | 212 | |
|
213 | 213 | def __init__(self, src, width, height, **kwargs): |
|
214 | 214 | self.src = src |
|
215 | 215 | self.width = width |
|
216 | 216 | self.height = height |
|
217 | 217 | self.params = kwargs |
|
218 | 218 | |
|
219 | 219 | def _repr_html_(self): |
|
220 | 220 | """return the embed iframe""" |
|
221 | 221 | if self.params: |
|
222 | 222 | try: |
|
223 | 223 | from urllib.parse import urlencode # Py 3 |
|
224 | 224 | except ImportError: |
|
225 | 225 | from urllib import urlencode |
|
226 | 226 | params = "?" + urlencode(self.params) |
|
227 | 227 | else: |
|
228 | 228 | params = "" |
|
229 | 229 | return self.iframe.format(src=self.src, |
|
230 | 230 | width=self.width, |
|
231 | 231 | height=self.height, |
|
232 | 232 | params=params) |
|
233 | 233 | |
|
234 | 234 | class YouTubeVideo(IFrame): |
|
235 | 235 | """Class for embedding a YouTube Video in an IPython session, based on its video id. |
|
236 | 236 | |
|
237 | 237 | e.g. to embed the video from https://www.youtube.com/watch?v=foo , you would |
|
238 | 238 | do:: |
|
239 | 239 | |
|
240 | 240 | vid = YouTubeVideo("foo") |
|
241 | 241 | display(vid) |
|
242 | 242 | |
|
243 | 243 | To start from 30 seconds:: |
|
244 | 244 | |
|
245 | 245 | vid = YouTubeVideo("abc", start=30) |
|
246 | 246 | display(vid) |
|
247 | 247 | |
|
248 | 248 | To calculate seconds from time as hours, minutes, seconds use |
|
249 | 249 | :class:`datetime.timedelta`:: |
|
250 | 250 | |
|
251 | 251 | start=int(timedelta(hours=1, minutes=46, seconds=40).total_seconds()) |
|
252 | 252 | |
|
253 | 253 | Other parameters can be provided as documented at |
|
254 | 254 | https://developers.google.com/youtube/player_parameters#parameter-subheader |
|
255 | 255 | |
|
256 | 256 | When converting the notebook using nbconvert, a jpeg representation of the video |
|
257 | 257 | will be inserted in the document. |
|
258 | 258 | """ |
|
259 | 259 | |
|
260 | 260 | def __init__(self, id, width=400, height=300, **kwargs): |
|
261 | 261 | self.id=id |
|
262 | 262 | src = "https://www.youtube.com/embed/{0}".format(id) |
|
263 | 263 | super(YouTubeVideo, self).__init__(src, width, height, **kwargs) |
|
264 | 264 | |
|
265 | 265 | def _repr_jpeg_(self): |
|
266 | 266 | from urllib import urlopen |
|
267 | return urlopen("https://img.youtube.com/vi/{id}/default.jpg".format(id=self.id)).read() | |
|
267 | return urlopen("https://img.youtube.com/vi/{id}/hqdefault.jpg".format(id=self.id)).read() | |
|
268 | 268 | |
|
269 | 269 | class VimeoVideo(IFrame): |
|
270 | 270 | """ |
|
271 | 271 | Class for embedding a Vimeo video in an IPython session, based on its video id. |
|
272 | 272 | """ |
|
273 | 273 | |
|
274 | 274 | def __init__(self, id, width=400, height=300, **kwargs): |
|
275 | 275 | src="https://player.vimeo.com/video/{0}".format(id) |
|
276 | 276 | super(VimeoVideo, self).__init__(src, width, height, **kwargs) |
|
277 | 277 | |
|
278 | 278 | class ScribdDocument(IFrame): |
|
279 | 279 | """ |
|
280 | 280 | Class for embedding a Scribd document in an IPython session |
|
281 | 281 | |
|
282 | 282 | Use the start_page params to specify a starting point in the document |
|
283 | 283 | Use the view_mode params to specify display type one off scroll | slideshow | book |
|
284 | 284 | |
|
285 | 285 | e.g to Display Wes' foundational paper about PANDAS in book mode from page 3 |
|
286 | 286 | |
|
287 | 287 | ScribdDocument(71048089, width=800, height=400, start_page=3, view_mode="book") |
|
288 | 288 | """ |
|
289 | 289 | |
|
290 | 290 | def __init__(self, id, width=400, height=300, **kwargs): |
|
291 | 291 | src="https://www.scribd.com/embeds/{0}/content".format(id) |
|
292 | 292 | super(ScribdDocument, self).__init__(src, width, height, **kwargs) |
|
293 | 293 | |
|
294 | 294 | class FileLink(object): |
|
295 | 295 | """Class for embedding a local file link in an IPython session, based on path |
|
296 | 296 | |
|
297 | 297 | e.g. to embed a link that was generated in the IPython notebook as my/data.txt |
|
298 | 298 | |
|
299 | 299 | you would do:: |
|
300 | 300 | |
|
301 | 301 | local_file = FileLink("my/data.txt") |
|
302 | 302 | display(local_file) |
|
303 | 303 | |
|
304 | 304 | or in the HTML notebook, just:: |
|
305 | 305 | |
|
306 | 306 | FileLink("my/data.txt") |
|
307 | 307 | """ |
|
308 | 308 | |
|
309 | 309 | html_link_str = "<a href='%s' target='_blank'>%s</a>" |
|
310 | 310 | |
|
311 | 311 | def __init__(self, |
|
312 | 312 | path, |
|
313 | 313 | url_prefix='', |
|
314 | 314 | result_html_prefix='', |
|
315 | 315 | result_html_suffix='<br>'): |
|
316 | 316 | """ |
|
317 | 317 | Parameters |
|
318 | 318 | ---------- |
|
319 | 319 | path : str |
|
320 | 320 | path to the file or directory that should be formatted |
|
321 | 321 | directory_prefix : str |
|
322 | 322 | prefix to be prepended to all files to form a working link [default: |
|
323 | 323 | 'files'] |
|
324 | 324 | result_html_prefix : str |
|
325 | 325 | text to append to beginning to link [default: none] |
|
326 | 326 | result_html_suffix : str |
|
327 | 327 | text to append at the end of link [default: '<br>'] |
|
328 | 328 | """ |
|
329 | 329 | if isdir(path): |
|
330 | 330 | raise ValueError("Cannot display a directory using FileLink. " |
|
331 | 331 | "Use FileLinks to display '%s'." % path) |
|
332 | 332 | self.path = path |
|
333 | 333 | self.url_prefix = url_prefix |
|
334 | 334 | self.result_html_prefix = result_html_prefix |
|
335 | 335 | self.result_html_suffix = result_html_suffix |
|
336 | 336 | |
|
337 | 337 | def _format_path(self): |
|
338 | 338 | fp = ''.join([self.url_prefix,self.path]) |
|
339 | 339 | return ''.join([self.result_html_prefix, |
|
340 | 340 | self.html_link_str % (fp, self.path), |
|
341 | 341 | self.result_html_suffix]) |
|
342 | 342 | |
|
343 | 343 | def _repr_html_(self): |
|
344 | 344 | """return html link to file |
|
345 | 345 | """ |
|
346 | 346 | if not exists(self.path): |
|
347 | 347 | return ("Path (<tt>%s</tt>) doesn't exist. " |
|
348 | 348 | "It may still be in the process of " |
|
349 | 349 | "being generated, or you may have the " |
|
350 | 350 | "incorrect path." % self.path) |
|
351 | 351 | |
|
352 | 352 | return self._format_path() |
|
353 | 353 | |
|
354 | 354 | def __repr__(self): |
|
355 | 355 | """return absolute path to file |
|
356 | 356 | """ |
|
357 | 357 | return abspath(self.path) |
|
358 | 358 | |
|
359 | 359 | class FileLinks(FileLink): |
|
360 | 360 | """Class for embedding local file links in an IPython session, based on path |
|
361 | 361 | |
|
362 | 362 | e.g. to embed links to files that were generated in the IPython notebook |
|
363 | 363 | under ``my/data``, you would do:: |
|
364 | 364 | |
|
365 | 365 | local_files = FileLinks("my/data") |
|
366 | 366 | display(local_files) |
|
367 | 367 | |
|
368 | 368 | or in the HTML notebook, just:: |
|
369 | 369 | |
|
370 | 370 | FileLinks("my/data") |
|
371 | 371 | """ |
|
372 | 372 | def __init__(self, |
|
373 | 373 | path, |
|
374 | 374 | url_prefix='', |
|
375 | 375 | included_suffixes=None, |
|
376 | 376 | result_html_prefix='', |
|
377 | 377 | result_html_suffix='<br>', |
|
378 | 378 | notebook_display_formatter=None, |
|
379 | 379 | terminal_display_formatter=None, |
|
380 | 380 | recursive=True): |
|
381 | 381 | """ |
|
382 | 382 | See :class:`FileLink` for the ``path``, ``url_prefix``, |
|
383 | 383 | ``result_html_prefix`` and ``result_html_suffix`` parameters. |
|
384 | 384 | |
|
385 | 385 | included_suffixes : list |
|
386 | 386 | Filename suffixes to include when formatting output [default: include |
|
387 | 387 | all files] |
|
388 | 388 | |
|
389 | 389 | notebook_display_formatter : function |
|
390 | 390 | Used to format links for display in the notebook. See discussion of |
|
391 | 391 | formatter functions below. |
|
392 | 392 | |
|
393 | 393 | terminal_display_formatter : function |
|
394 | 394 | Used to format links for display in the terminal. See discussion of |
|
395 | 395 | formatter functions below. |
|
396 | 396 | |
|
397 | 397 | Formatter functions must be of the form:: |
|
398 | 398 | |
|
399 | 399 | f(dirname, fnames, included_suffixes) |
|
400 | 400 | |
|
401 | 401 | dirname : str |
|
402 | 402 | The name of a directory |
|
403 | 403 | fnames : list |
|
404 | 404 | The files in that directory |
|
405 | 405 | included_suffixes : list |
|
406 | 406 | The file suffixes that should be included in the output (passing None |
|
407 | 407 | meansto include all suffixes in the output in the built-in formatters) |
|
408 | 408 | recursive : boolean |
|
409 | 409 | Whether to recurse into subdirectories. Default is True. |
|
410 | 410 | |
|
411 | 411 | The function should return a list of lines that will be printed in the |
|
412 | 412 | notebook (if passing notebook_display_formatter) or the terminal (if |
|
413 | 413 | passing terminal_display_formatter). This function is iterated over for |
|
414 | 414 | each directory in self.path. Default formatters are in place, can be |
|
415 | 415 | passed here to support alternative formatting. |
|
416 | 416 | |
|
417 | 417 | """ |
|
418 | 418 | if isfile(path): |
|
419 | 419 | raise ValueError("Cannot display a file using FileLinks. " |
|
420 | 420 | "Use FileLink to display '%s'." % path) |
|
421 | 421 | self.included_suffixes = included_suffixes |
|
422 | 422 | # remove trailing slashs for more consistent output formatting |
|
423 | 423 | path = path.rstrip('/') |
|
424 | 424 | |
|
425 | 425 | self.path = path |
|
426 | 426 | self.url_prefix = url_prefix |
|
427 | 427 | self.result_html_prefix = result_html_prefix |
|
428 | 428 | self.result_html_suffix = result_html_suffix |
|
429 | 429 | |
|
430 | 430 | self.notebook_display_formatter = \ |
|
431 | 431 | notebook_display_formatter or self._get_notebook_display_formatter() |
|
432 | 432 | self.terminal_display_formatter = \ |
|
433 | 433 | terminal_display_formatter or self._get_terminal_display_formatter() |
|
434 | 434 | |
|
435 | 435 | self.recursive = recursive |
|
436 | 436 | |
|
437 | 437 | def _get_display_formatter(self, |
|
438 | 438 | dirname_output_format, |
|
439 | 439 | fname_output_format, |
|
440 | 440 | fp_format, |
|
441 | 441 | fp_cleaner=None): |
|
442 | 442 | """ generate built-in formatter function |
|
443 | 443 | |
|
444 | 444 | this is used to define both the notebook and terminal built-in |
|
445 | 445 | formatters as they only differ by some wrapper text for each entry |
|
446 | 446 | |
|
447 | 447 | dirname_output_format: string to use for formatting directory |
|
448 | 448 | names, dirname will be substituted for a single "%s" which |
|
449 | 449 | must appear in this string |
|
450 | 450 | fname_output_format: string to use for formatting file names, |
|
451 | 451 | if a single "%s" appears in the string, fname will be substituted |
|
452 | 452 | if two "%s" appear in the string, the path to fname will be |
|
453 | 453 | substituted for the first and fname will be substituted for the |
|
454 | 454 | second |
|
455 | 455 | fp_format: string to use for formatting filepaths, must contain |
|
456 | 456 | exactly two "%s" and the dirname will be subsituted for the first |
|
457 | 457 | and fname will be substituted for the second |
|
458 | 458 | """ |
|
459 | 459 | def f(dirname, fnames, included_suffixes=None): |
|
460 | 460 | result = [] |
|
461 | 461 | # begin by figuring out which filenames, if any, |
|
462 | 462 | # are going to be displayed |
|
463 | 463 | display_fnames = [] |
|
464 | 464 | for fname in fnames: |
|
465 | 465 | if (isfile(join(dirname,fname)) and |
|
466 | 466 | (included_suffixes is None or |
|
467 | 467 | splitext(fname)[1] in included_suffixes)): |
|
468 | 468 | display_fnames.append(fname) |
|
469 | 469 | |
|
470 | 470 | if len(display_fnames) == 0: |
|
471 | 471 | # if there are no filenames to display, don't print anything |
|
472 | 472 | # (not even the directory name) |
|
473 | 473 | pass |
|
474 | 474 | else: |
|
475 | 475 | # otherwise print the formatted directory name followed by |
|
476 | 476 | # the formatted filenames |
|
477 | 477 | dirname_output_line = dirname_output_format % dirname |
|
478 | 478 | result.append(dirname_output_line) |
|
479 | 479 | for fname in display_fnames: |
|
480 | 480 | fp = fp_format % (dirname,fname) |
|
481 | 481 | if fp_cleaner is not None: |
|
482 | 482 | fp = fp_cleaner(fp) |
|
483 | 483 | try: |
|
484 | 484 | # output can include both a filepath and a filename... |
|
485 | 485 | fname_output_line = fname_output_format % (fp, fname) |
|
486 | 486 | except TypeError: |
|
487 | 487 | # ... or just a single filepath |
|
488 | 488 | fname_output_line = fname_output_format % fname |
|
489 | 489 | result.append(fname_output_line) |
|
490 | 490 | return result |
|
491 | 491 | return f |
|
492 | 492 | |
|
493 | 493 | def _get_notebook_display_formatter(self, |
|
494 | 494 | spacer=" "): |
|
495 | 495 | """ generate function to use for notebook formatting |
|
496 | 496 | """ |
|
497 | 497 | dirname_output_format = \ |
|
498 | 498 | self.result_html_prefix + "%s/" + self.result_html_suffix |
|
499 | 499 | fname_output_format = \ |
|
500 | 500 | self.result_html_prefix + spacer + self.html_link_str + self.result_html_suffix |
|
501 | 501 | fp_format = self.url_prefix + '%s/%s' |
|
502 | 502 | if sep == "\\": |
|
503 | 503 | # Working on a platform where the path separator is "\", so |
|
504 | 504 | # must convert these to "/" for generating a URI |
|
505 | 505 | def fp_cleaner(fp): |
|
506 | 506 | # Replace all occurences of backslash ("\") with a forward |
|
507 | 507 | # slash ("/") - this is necessary on windows when a path is |
|
508 | 508 | # provided as input, but we must link to a URI |
|
509 | 509 | return fp.replace('\\','/') |
|
510 | 510 | else: |
|
511 | 511 | fp_cleaner = None |
|
512 | 512 | |
|
513 | 513 | return self._get_display_formatter(dirname_output_format, |
|
514 | 514 | fname_output_format, |
|
515 | 515 | fp_format, |
|
516 | 516 | fp_cleaner) |
|
517 | 517 | |
|
518 | 518 | def _get_terminal_display_formatter(self, |
|
519 | 519 | spacer=" "): |
|
520 | 520 | """ generate function to use for terminal formatting |
|
521 | 521 | """ |
|
522 | 522 | dirname_output_format = "%s/" |
|
523 | 523 | fname_output_format = spacer + "%s" |
|
524 | 524 | fp_format = '%s/%s' |
|
525 | 525 | |
|
526 | 526 | return self._get_display_formatter(dirname_output_format, |
|
527 | 527 | fname_output_format, |
|
528 | 528 | fp_format) |
|
529 | 529 | |
|
530 | 530 | def _format_path(self): |
|
531 | 531 | result_lines = [] |
|
532 | 532 | if self.recursive: |
|
533 | 533 | walked_dir = list(walk(self.path)) |
|
534 | 534 | else: |
|
535 | 535 | walked_dir = [next(walk(self.path))] |
|
536 | 536 | walked_dir.sort() |
|
537 | 537 | for dirname, subdirs, fnames in walked_dir: |
|
538 | 538 | result_lines += self.notebook_display_formatter(dirname, fnames, self.included_suffixes) |
|
539 | 539 | return '\n'.join(result_lines) |
|
540 | 540 | |
|
541 | 541 | def __repr__(self): |
|
542 | 542 | """return newline-separated absolute paths |
|
543 | 543 | """ |
|
544 | 544 | result_lines = [] |
|
545 | 545 | if self.recursive: |
|
546 | 546 | walked_dir = list(walk(self.path)) |
|
547 | 547 | else: |
|
548 | 548 | walked_dir = [next(walk(self.path))] |
|
549 | 549 | walked_dir.sort() |
|
550 | 550 | for dirname, subdirs, fnames in walked_dir: |
|
551 | 551 | result_lines += self.terminal_display_formatter(dirname, fnames, self.included_suffixes) |
|
552 | 552 | return '\n'.join(result_lines) |
General Comments 0
You need to be logged in to leave comments.
Login now