##// END OF EJS Templates
Initial take on implementing configurable destinations for nbextensions....
Jason Grout -
Show More
@@ -11,6 +11,7 b' import shutil'
11 import sys
11 import sys
12 import tarfile
12 import tarfile
13 import zipfile
13 import zipfile
14 import uuid
14 from os.path import basename, join as pjoin
15 from os.path import basename, join as pjoin
15
16
16 # Deferred imports
17 # Deferred imports
@@ -117,10 +118,11 b' def install_nbextension(files, overwrite=False, symlink=False, user=False, prefi'
117 Parameters
118 Parameters
118 ----------
119 ----------
119
120
120 files : list(paths or URLs)
121 files : list(paths or URLs) or dict(install_name: path or URL)
121 One or more paths or URLs to existing files directories to install.
122 One or more paths or URLs to existing files directories to install.
122 These will be installed with their base name, so '/path/to/foo'
123 If given as a list, these will be installed with their base name, so '/path/to/foo'
123 will install to 'nbextensions/foo'.
124 will install to 'nbextensions/foo'. If given as a dict, such as {'bar': '/path/to/foo'},
125 then '/path/to/foo' will install to 'nbextensions/bar'.
124 Archives (zip or tarballs) will be extracted into the nbextensions directory.
126 Archives (zip or tarballs) will be extracted into the nbextensions directory.
125 overwrite : bool [default: False]
127 overwrite : bool [default: False]
126 If True, always install the files, regardless of what may already be installed.
128 If True, always install the files, regardless of what may already be installed.
@@ -158,8 +160,20 b' def install_nbextension(files, overwrite=False, symlink=False, user=False, prefi'
158 if isinstance(files, string_types):
160 if isinstance(files, string_types):
159 # one file given, turn it into a list
161 # one file given, turn it into a list
160 files = [files]
162 files = [files]
163 if isinstance(files, (list,tuple)):
164 # list given, turn into dict
165 _files = {}
166 for path in map(cast_unicode_py2, files):
167 if path.startswith(('https://', 'http://')):
168 destination = urlparse(path).path.split('/')[-1]
169 elif path.endswith('.zip') or _safe_is_tarfile(path):
170 destination = str(uuid.uuid4()) # ignored for archives
171 else:
172 destination = basename(path)
173 _files[destination] = path
174 files = _files
161
175
162 for path in map(cast_unicode_py2, files):
176 for dest_basename,path in (map(cast_unicode_py2, item) for item in files.items()):
163
177
164 if path.startswith(('https://', 'http://')):
178 if path.startswith(('https://', 'http://')):
165 if symlink:
179 if symlink:
@@ -172,7 +186,7 b' def install_nbextension(files, overwrite=False, symlink=False, user=False, prefi'
172 print("downloading %s to %s" % (path, local_path))
186 print("downloading %s to %s" % (path, local_path))
173 urlretrieve(path, local_path)
187 urlretrieve(path, local_path)
174 # now install from the local copy
188 # now install from the local copy
175 install_nbextension(local_path, overwrite=overwrite, symlink=symlink, nbextensions_dir=nbext, verbose=verbose)
189 install_nbextension({dest_basename: local_path}, overwrite=overwrite, symlink=symlink, nbextensions_dir=nbext, verbose=verbose)
176 continue
190 continue
177
191
178 # handle archives
192 # handle archives
@@ -191,7 +205,7 b' def install_nbextension(files, overwrite=False, symlink=False, user=False, prefi'
191 archive.close()
205 archive.close()
192 continue
206 continue
193
207
194 dest = pjoin(nbext, basename(path))
208 dest = pjoin(nbext, dest_basename)
195 if overwrite and os.path.exists(dest):
209 if overwrite and os.path.exists(dest):
196 if verbose >= 1:
210 if verbose >= 1:
197 print("removing %s" % dest)
211 print("removing %s" % dest)
@@ -209,9 +223,8 b' def install_nbextension(files, overwrite=False, symlink=False, user=False, prefi'
209 continue
223 continue
210
224
211 if os.path.isdir(path):
225 if os.path.isdir(path):
212 strip_prefix_len = len(path) - len(basename(path))
213 for parent, dirs, files in os.walk(path):
226 for parent, dirs, files in os.walk(path):
214 dest_dir = pjoin(nbext, parent[strip_prefix_len:])
227 dest_dir = pjoin(dest, parent[len(path):])
215 if not os.path.exists(dest_dir):
228 if not os.path.exists(dest_dir):
216 if verbose >= 2:
229 if verbose >= 2:
217 print("making directory %s" % dest_dir)
230 print("making directory %s" % dest_dir)
General Comments 0
You need to be logged in to leave comments. Login now