manager.py 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  1. """A base class for contents managers."""
  2. # Copyright (c) Jupyter Development Team.
  3. # Distributed under the terms of the Modified BSD License.
  4. from __future__ import annotations
  5. import itertools
  6. import json
  7. import os
  8. import re
  9. import typing as t
  10. import warnings
  11. from fnmatch import fnmatch
  12. from jupyter_core.utils import ensure_async, run_sync
  13. from jupyter_events import EventLogger
  14. from nbformat import ValidationError, sign
  15. from nbformat import validate as validate_nb
  16. from nbformat.v4 import new_notebook
  17. from tornado.web import HTTPError, RequestHandler
  18. from traitlets import (
  19. Any,
  20. Bool,
  21. Dict,
  22. Instance,
  23. List,
  24. TraitError,
  25. Type,
  26. Unicode,
  27. default,
  28. validate,
  29. )
  30. from traitlets.config.configurable import LoggingConfigurable
  31. from jupyter_server import DEFAULT_EVENTS_SCHEMA_PATH, JUPYTER_SERVER_EVENTS_URI
  32. from jupyter_server.transutils import _i18n
  33. from jupyter_server.utils import import_item
  34. from ...files.handlers import FilesHandler
  35. from .checkpoints import AsyncCheckpoints, Checkpoints
  36. copy_pat = re.compile(r"\-Copy\d*\.")
  37. class ContentsManager(LoggingConfigurable):
  38. """Base class for serving files and directories.
  39. This serves any text or binary file,
  40. as well as directories,
  41. with special handling for JSON notebook documents.
  42. Most APIs take a path argument,
  43. which is always an API-style unicode path,
  44. and always refers to a directory.
  45. - unicode, not url-escaped
  46. - '/'-separated
  47. - leading and trailing '/' will be stripped
  48. - if unspecified, path defaults to '',
  49. indicating the root path.
  50. """
  51. event_schema_id = JUPYTER_SERVER_EVENTS_URI + "/contents_service/v1"
  52. event_logger = Instance(EventLogger).tag(config=True)
  53. @default("event_logger")
  54. def _default_event_logger(self):
  55. if self.parent and hasattr(self.parent, "event_logger"):
  56. return self.parent.event_logger
  57. else:
  58. # If parent does not have an event logger, create one.
  59. logger = EventLogger()
  60. schema_path = DEFAULT_EVENTS_SCHEMA_PATH / "contents_service" / "v1.yaml"
  61. logger.register_event_schema(schema_path)
  62. return logger
  63. def emit(self, data):
  64. """Emit event using the core event schema from Jupyter Server's Contents Manager."""
  65. self.event_logger.emit(schema_id=self.event_schema_id, data=data)
  66. root_dir = Unicode("/", config=True)
  67. preferred_dir = Unicode(
  68. "",
  69. config=True,
  70. help=_i18n(
  71. "Preferred starting directory to use for notebooks. This is an API path (`/` separated, relative to root dir)"
  72. ),
  73. )
  74. @validate("preferred_dir")
  75. def _validate_preferred_dir(self, proposal):
  76. value = proposal["value"].strip("/")
  77. try:
  78. import inspect
  79. if inspect.iscoroutinefunction(self.dir_exists):
  80. dir_exists = run_sync(self.dir_exists)(value)
  81. else:
  82. dir_exists = self.dir_exists(value)
  83. except HTTPError as e:
  84. raise TraitError(e.log_message) from e
  85. if not dir_exists:
  86. raise TraitError(_i18n("Preferred directory not found: %r") % value)
  87. if self.parent:
  88. try:
  89. if value != self.parent.preferred_dir:
  90. self.parent.preferred_dir = os.path.join(self.root_dir, *value.split("/"))
  91. except TraitError:
  92. pass
  93. return value
  94. allow_hidden = Bool(False, config=True, help="Allow access to hidden files")
  95. notary = Instance(sign.NotebookNotary)
  96. @default("notary")
  97. def _notary_default(self):
  98. return sign.NotebookNotary(parent=self)
  99. hide_globs = List(
  100. Unicode(),
  101. [
  102. "__pycache__",
  103. "*.pyc",
  104. "*.pyo",
  105. ".DS_Store",
  106. "*~",
  107. ],
  108. config=True,
  109. help="""
  110. Glob patterns to hide in file and directory listings.
  111. """,
  112. )
  113. untitled_notebook = Unicode(
  114. _i18n("Untitled"),
  115. config=True,
  116. help="The base name used when creating untitled notebooks.",
  117. )
  118. untitled_file = Unicode(
  119. "untitled", config=True, help="The base name used when creating untitled files."
  120. )
  121. untitled_directory = Unicode(
  122. "Untitled Folder",
  123. config=True,
  124. help="The base name used when creating untitled directories.",
  125. )
  126. pre_save_hook = Any(
  127. None,
  128. config=True,
  129. allow_none=True,
  130. help="""Python callable or importstring thereof
  131. To be called on a contents model prior to save.
  132. This can be used to process the structure,
  133. such as removing notebook outputs or other side effects that
  134. should not be saved.
  135. It will be called as (all arguments passed by keyword)::
  136. hook(path=path, model=model, contents_manager=self)
  137. - model: the model to be saved. Includes file contents.
  138. Modifying this dict will affect the file that is stored.
  139. - path: the API path of the save destination
  140. - contents_manager: this ContentsManager instance
  141. """,
  142. )
  143. @validate("pre_save_hook")
  144. def _validate_pre_save_hook(self, proposal):
  145. value = proposal["value"]
  146. if isinstance(value, str):
  147. value = import_item(self.pre_save_hook)
  148. if not callable(value):
  149. msg = "pre_save_hook must be callable"
  150. raise TraitError(msg)
  151. if callable(self.pre_save_hook):
  152. warnings.warn(
  153. f"Overriding existing pre_save_hook ({self.pre_save_hook.__name__}) with a new one ({value.__name__}).",
  154. stacklevel=2,
  155. )
  156. return value
  157. post_save_hook = Any(
  158. None,
  159. config=True,
  160. allow_none=True,
  161. help="""Python callable or importstring thereof
  162. to be called on the path of a file just saved.
  163. This can be used to process the file on disk,
  164. such as converting the notebook to a script or HTML via nbconvert.
  165. It will be called as (all arguments passed by keyword)::
  166. hook(os_path=os_path, model=model, contents_manager=instance)
  167. - path: the filesystem path to the file just written
  168. - model: the model representing the file
  169. - contents_manager: this ContentsManager instance
  170. """,
  171. )
  172. @validate("post_save_hook")
  173. def _validate_post_save_hook(self, proposal):
  174. value = proposal["value"]
  175. if isinstance(value, str):
  176. value = import_item(value)
  177. if not callable(value):
  178. msg = "post_save_hook must be callable"
  179. raise TraitError(msg)
  180. if callable(self.post_save_hook):
  181. warnings.warn(
  182. f"Overriding existing post_save_hook ({self.post_save_hook.__name__}) with a new one ({value.__name__}).",
  183. stacklevel=2,
  184. )
  185. return value
  186. def run_pre_save_hook(self, model, path, **kwargs):
  187. """Run the pre-save hook if defined, and log errors"""
  188. warnings.warn(
  189. "run_pre_save_hook is deprecated, use run_pre_save_hooks instead.",
  190. DeprecationWarning,
  191. stacklevel=2,
  192. )
  193. if self.pre_save_hook:
  194. try:
  195. self.log.debug("Running pre-save hook on %s", path)
  196. self.pre_save_hook(model=model, path=path, contents_manager=self, **kwargs)
  197. except HTTPError:
  198. # allow custom HTTPErrors to raise,
  199. # rejecting the save with a message.
  200. raise
  201. except Exception:
  202. # unhandled errors don't prevent saving,
  203. # which could cause frustrating data loss
  204. self.log.error("Pre-save hook failed on %s", path, exc_info=True)
  205. def run_post_save_hook(self, model, os_path):
  206. """Run the post-save hook if defined, and log errors"""
  207. warnings.warn(
  208. "run_post_save_hook is deprecated, use run_post_save_hooks instead.",
  209. DeprecationWarning,
  210. stacklevel=2,
  211. )
  212. if self.post_save_hook:
  213. try:
  214. self.log.debug("Running post-save hook on %s", os_path)
  215. self.post_save_hook(os_path=os_path, model=model, contents_manager=self)
  216. except Exception:
  217. self.log.error("Post-save hook failed o-n %s", os_path, exc_info=True)
  218. msg = "fUnexpected error while running post hook save: {e}"
  219. raise HTTPError(500, msg) from None
  220. _pre_save_hooks: List[t.Any] = List()
  221. _post_save_hooks: List[t.Any] = List()
  222. def register_pre_save_hook(self, hook):
  223. """Register a pre save hook."""
  224. if isinstance(hook, str):
  225. hook = import_item(hook)
  226. if not callable(hook):
  227. msg = "hook must be callable"
  228. raise RuntimeError(msg)
  229. self._pre_save_hooks.append(hook)
  230. def register_post_save_hook(self, hook):
  231. """Register a post save hook."""
  232. if isinstance(hook, str):
  233. hook = import_item(hook)
  234. if not callable(hook):
  235. msg = "hook must be callable"
  236. raise RuntimeError(msg)
  237. self._post_save_hooks.append(hook)
  238. def run_pre_save_hooks(self, model, path, **kwargs):
  239. """Run the pre-save hooks if any, and log errors"""
  240. pre_save_hooks = [self.pre_save_hook] if self.pre_save_hook is not None else []
  241. pre_save_hooks += self._pre_save_hooks
  242. for pre_save_hook in pre_save_hooks:
  243. try:
  244. self.log.debug("Running pre-save hook on %s", path)
  245. pre_save_hook(model=model, path=path, contents_manager=self, **kwargs)
  246. except HTTPError:
  247. # allow custom HTTPErrors to raise,
  248. # rejecting the save with a message.
  249. raise
  250. except Exception:
  251. # unhandled errors don't prevent saving,
  252. # which could cause frustrating data loss
  253. self.log.error(
  254. "Pre-save hook %s failed on %s",
  255. pre_save_hook.__name__,
  256. path,
  257. exc_info=True,
  258. )
  259. def run_post_save_hooks(self, model, os_path):
  260. """Run the post-save hooks if any, and log errors"""
  261. post_save_hooks = [self.post_save_hook] if self.post_save_hook is not None else []
  262. post_save_hooks += self._post_save_hooks
  263. for post_save_hook in post_save_hooks:
  264. try:
  265. self.log.debug("Running post-save hook on %s", os_path)
  266. post_save_hook(os_path=os_path, model=model, contents_manager=self)
  267. except Exception as e:
  268. self.log.error(
  269. "Post-save %s hook failed on %s",
  270. post_save_hook.__name__,
  271. os_path,
  272. exc_info=True,
  273. )
  274. raise HTTPError(500, "Unexpected error while running post hook save: %s" % e) from e
  275. checkpoints_class = Type(Checkpoints, config=True)
  276. checkpoints = Instance(Checkpoints, config=True)
  277. checkpoints_kwargs = Dict(config=True)
  278. @default("checkpoints")
  279. def _default_checkpoints(self):
  280. return self.checkpoints_class(**self.checkpoints_kwargs)
  281. @default("checkpoints_kwargs")
  282. def _default_checkpoints_kwargs(self):
  283. return {
  284. "parent": self,
  285. "log": self.log,
  286. }
  287. files_handler_class = Type(
  288. FilesHandler,
  289. klass=RequestHandler,
  290. allow_none=True,
  291. config=True,
  292. help="""handler class to use when serving raw file requests.
  293. Default is a fallback that talks to the ContentsManager API,
  294. which may be inefficient, especially for large files.
  295. Local files-based ContentsManagers can use a StaticFileHandler subclass,
  296. which will be much more efficient.
  297. Access to these files should be Authenticated.
  298. """,
  299. )
  300. files_handler_params = Dict(
  301. config=True,
  302. help="""Extra parameters to pass to files_handler_class.
  303. For example, StaticFileHandlers generally expect a `path` argument
  304. specifying the root directory from which to serve files.
  305. """,
  306. )
  307. def get_extra_handlers(self):
  308. """Return additional handlers
  309. Default: self.files_handler_class on /files/.*
  310. """
  311. handlers = []
  312. if self.files_handler_class:
  313. handlers.append((r"/files/(.*)", self.files_handler_class, self.files_handler_params))
  314. return handlers
  315. # ContentsManager API part 1: methods that must be
  316. # implemented in subclasses.
  317. def dir_exists(self, path):
  318. """Does a directory exist at the given path?
  319. Like os.path.isdir
  320. Override this method in subclasses.
  321. Parameters
  322. ----------
  323. path : str
  324. The path to check
  325. Returns
  326. -------
  327. exists : bool
  328. Whether the path does indeed exist.
  329. """
  330. raise NotImplementedError
  331. def is_hidden(self, path):
  332. """Is path a hidden directory or file?
  333. Parameters
  334. ----------
  335. path : str
  336. The path to check. This is an API path (`/` separated,
  337. relative to root dir).
  338. Returns
  339. -------
  340. hidden : bool
  341. Whether the path is hidden.
  342. """
  343. raise NotImplementedError
  344. def file_exists(self, path=""):
  345. """Does a file exist at the given path?
  346. Like os.path.isfile
  347. Override this method in subclasses.
  348. Parameters
  349. ----------
  350. path : str
  351. The API path of a file to check for.
  352. Returns
  353. -------
  354. exists : bool
  355. Whether the file exists.
  356. """
  357. raise NotImplementedError
  358. def exists(self, path):
  359. """Does a file or directory exist at the given path?
  360. Like os.path.exists
  361. Parameters
  362. ----------
  363. path : str
  364. The API path of a file or directory to check for.
  365. Returns
  366. -------
  367. exists : bool
  368. Whether the target exists.
  369. """
  370. return self.file_exists(path) or self.dir_exists(path)
  371. def get(self, path, content=True, type=None, format=None, require_hash=False):
  372. """Get a file or directory model.
  373. Parameters
  374. ----------
  375. require_hash : bool
  376. Whether the file hash must be returned or not.
  377. *Changed in version 2.11*: The *require_hash* parameter was added.
  378. """
  379. raise NotImplementedError
  380. def save(self, model, path):
  381. """
  382. Save a file or directory model to path.
  383. Should return the saved model with no content. Save implementations
  384. should call self.run_pre_save_hook(model=model, path=path) prior to
  385. writing any data.
  386. """
  387. raise NotImplementedError
  388. def delete_file(self, path):
  389. """Delete the file or directory at path."""
  390. raise NotImplementedError
  391. def rename_file(self, old_path, new_path):
  392. """Rename a file or directory."""
  393. raise NotImplementedError
  394. # ContentsManager API part 2: methods that have usable default
  395. # implementations, but can be overridden in subclasses.
  396. def delete(self, path):
  397. """Delete a file/directory and any associated checkpoints."""
  398. path = path.strip("/")
  399. if not path:
  400. raise HTTPError(400, "Can't delete root")
  401. self.delete_file(path)
  402. self.checkpoints.delete_all_checkpoints(path)
  403. self.emit(data={"action": "delete", "path": path})
  404. def rename(self, old_path, new_path):
  405. """Rename a file and any checkpoints associated with that file."""
  406. self.rename_file(old_path, new_path)
  407. self.checkpoints.rename_all_checkpoints(old_path, new_path)
  408. self.emit(data={"action": "rename", "path": new_path, "source_path": old_path})
  409. def update(self, model, path):
  410. """Update the file's path
  411. For use in PATCH requests, to enable renaming a file without
  412. re-uploading its contents. Only used for renaming at the moment.
  413. """
  414. path = path.strip("/")
  415. new_path = model.get("path", path).strip("/")
  416. if path != new_path:
  417. self.rename(path, new_path)
  418. model = self.get(new_path, content=False)
  419. return model
  420. def info_string(self):
  421. """The information string for the manager."""
  422. return "Serving contents"
  423. def get_kernel_path(self, path, model=None):
  424. """Return the API path for the kernel
  425. KernelManagers can turn this value into a filesystem path,
  426. or ignore it altogether.
  427. The default value here will start kernels in the directory of the
  428. notebook server. FileContentsManager overrides this to use the
  429. directory containing the notebook.
  430. """
  431. return ""
  432. def increment_filename(self, filename, path="", insert=""):
  433. """Increment a filename until it is unique.
  434. Parameters
  435. ----------
  436. filename : unicode
  437. The name of a file, including extension
  438. path : unicode
  439. The API path of the target's directory
  440. insert : unicode
  441. The characters to insert after the base filename
  442. Returns
  443. -------
  444. name : unicode
  445. A filename that is unique, based on the input filename.
  446. """
  447. # Extract the full suffix from the filename (e.g. .tar.gz)
  448. path = path.strip("/")
  449. basename, dot, ext = filename.rpartition(".")
  450. if ext != "ipynb":
  451. basename, dot, ext = filename.partition(".")
  452. suffix = dot + ext
  453. for i in itertools.count():
  454. insert_i = f"{insert}{i}" if i else ""
  455. name = f"{basename}{insert_i}{suffix}"
  456. if not self.exists(f"{path}/{name}"):
  457. break
  458. return name
  459. def validate_notebook_model(self, model, validation_error=None):
  460. """Add failed-validation message to model"""
  461. try:
  462. # If we're given a validation_error dictionary, extract the exception
  463. # from it and raise the exception, else call nbformat's validate method
  464. # to determine if the notebook is valid. This 'else' condition may
  465. # pertain to server extension not using the server's notebook read/write
  466. # functions.
  467. if validation_error is not None:
  468. e = validation_error.get("ValidationError")
  469. if isinstance(e, ValidationError):
  470. raise e
  471. else:
  472. validate_nb(model["content"])
  473. except ValidationError as e:
  474. model["message"] = "Notebook validation failed: {}:\n{}".format(
  475. str(e),
  476. json.dumps(e.instance, indent=1, default=lambda obj: "<UNKNOWN>"),
  477. )
  478. return model
  479. def new_untitled(self, path="", type="", ext=""):
  480. """Create a new untitled file or directory in path
  481. path must be a directory
  482. File extension can be specified.
  483. Use `new` to create files with a fully specified path (including filename).
  484. """
  485. path = path.strip("/")
  486. if not self.dir_exists(path):
  487. raise HTTPError(404, "No such directory: %s" % path)
  488. model = {}
  489. if type:
  490. model["type"] = type
  491. if ext == ".ipynb":
  492. model.setdefault("type", "notebook")
  493. else:
  494. model.setdefault("type", "file")
  495. insert = ""
  496. if model["type"] == "directory":
  497. untitled = self.untitled_directory
  498. insert = " "
  499. elif model["type"] == "notebook":
  500. untitled = self.untitled_notebook
  501. ext = ".ipynb"
  502. elif model["type"] == "file":
  503. untitled = self.untitled_file
  504. else:
  505. raise HTTPError(400, "Unexpected model type: %r" % model["type"])
  506. name = self.increment_filename(untitled + ext, path, insert=insert)
  507. path = f"{path}/{name}"
  508. return self.new(model, path)
  509. def new(self, model=None, path=""):
  510. """Create a new file or directory and return its model with no content.
  511. To create a new untitled entity in a directory, use `new_untitled`.
  512. """
  513. path = path.strip("/")
  514. if model is None:
  515. model = {}
  516. if path.endswith(".ipynb"):
  517. model.setdefault("type", "notebook")
  518. else:
  519. model.setdefault("type", "file")
  520. # no content, not a directory, so fill out new-file model
  521. if "content" not in model and model["type"] != "directory":
  522. if model["type"] == "notebook":
  523. model["content"] = new_notebook()
  524. model["format"] = "json"
  525. else:
  526. model["content"] = ""
  527. model["type"] = "file"
  528. model["format"] = "text"
  529. model = self.save(model, path)
  530. return model
  531. def copy(self, from_path, to_path=None):
  532. """Copy an existing file and return its new model.
  533. If to_path not specified, it will be the parent directory of from_path.
  534. If to_path is a directory, filename will increment `from_path-Copy#.ext`.
  535. Considering multi-part extensions, the Copy# part will be placed before the first dot for all the extensions except `ipynb`.
  536. For easier manual searching in case of notebooks, the Copy# part will be placed before the last dot.
  537. from_path must be a full path to a file.
  538. """
  539. path = from_path.strip("/")
  540. if to_path is not None:
  541. to_path = to_path.strip("/")
  542. if "/" in path:
  543. from_dir, from_name = path.rsplit("/", 1)
  544. else:
  545. from_dir = ""
  546. from_name = path
  547. model = self.get(path)
  548. model.pop("path", None)
  549. model.pop("name", None)
  550. if model["type"] == "directory":
  551. raise HTTPError(400, "Can't copy directories")
  552. is_destination_specified = to_path is not None
  553. if not is_destination_specified:
  554. to_path = from_dir
  555. if self.dir_exists(to_path):
  556. name = copy_pat.sub(".", from_name)
  557. to_name = self.increment_filename(name, to_path, insert="-Copy")
  558. to_path = f"{to_path}/{to_name}"
  559. elif is_destination_specified:
  560. if "/" in to_path:
  561. to_dir, to_name = to_path.rsplit("/", 1)
  562. if not self.dir_exists(to_dir):
  563. raise HTTPError(404, "No such parent directory: %s to copy file in" % to_dir)
  564. else:
  565. raise HTTPError(404, "No such directory: %s" % to_path)
  566. model = self.save(model, to_path)
  567. self.emit(data={"action": "copy", "path": to_path, "source_path": from_path})
  568. return model
  569. def log_info(self):
  570. """Log the information string for the manager."""
  571. self.log.info(self.info_string())
  572. def trust_notebook(self, path):
  573. """Explicitly trust a notebook
  574. Parameters
  575. ----------
  576. path : str
  577. The path of a notebook
  578. """
  579. model = self.get(path)
  580. nb = model["content"]
  581. self.log.warning("Trusting notebook %s", path)
  582. self.notary.mark_cells(nb, True)
  583. self.check_and_sign(nb, path)
  584. def check_and_sign(self, nb, path=""):
  585. """Check for trusted cells, and sign the notebook.
  586. Called as a part of saving notebooks.
  587. Parameters
  588. ----------
  589. nb : dict
  590. The notebook dict
  591. path : str
  592. The notebook's path (for logging)
  593. """
  594. if self.notary.check_cells(nb):
  595. self.notary.sign(nb)
  596. else:
  597. self.log.warning("Notebook %s is not trusted", path)
  598. def mark_trusted_cells(self, nb, path=""):
  599. """Mark cells as trusted if the notebook signature matches.
  600. Called as a part of loading notebooks.
  601. Parameters
  602. ----------
  603. nb : dict
  604. The notebook object (in current nbformat)
  605. path : str
  606. The notebook's path (for logging)
  607. """
  608. trusted = self.notary.check_signature(nb)
  609. if not trusted:
  610. self.log.warning("Notebook %s is not trusted", path)
  611. self.notary.mark_cells(nb, trusted)
  612. def should_list(self, name):
  613. """Should this file/directory name be displayed in a listing?"""
  614. return not any(fnmatch(name, glob) for glob in self.hide_globs)
  615. # Part 3: Checkpoints API
  616. def create_checkpoint(self, path):
  617. """Create a checkpoint."""
  618. return self.checkpoints.create_checkpoint(self, path)
  619. def restore_checkpoint(self, checkpoint_id, path):
  620. """
  621. Restore a checkpoint.
  622. """
  623. self.checkpoints.restore_checkpoint(self, checkpoint_id, path)
  624. def list_checkpoints(self, path):
  625. return self.checkpoints.list_checkpoints(path)
  626. def delete_checkpoint(self, checkpoint_id, path):
  627. return self.checkpoints.delete_checkpoint(checkpoint_id, path)
  628. class AsyncContentsManager(ContentsManager):
  629. """Base class for serving files and directories asynchronously."""
  630. checkpoints_class = Type(AsyncCheckpoints, config=True)
  631. checkpoints = Instance(AsyncCheckpoints, config=True)
  632. checkpoints_kwargs = Dict(config=True)
  633. @default("checkpoints")
  634. def _default_checkpoints(self):
  635. return self.checkpoints_class(**self.checkpoints_kwargs)
  636. @default("checkpoints_kwargs")
  637. def _default_checkpoints_kwargs(self):
  638. return {
  639. "parent": self,
  640. "log": self.log,
  641. }
  642. # ContentsManager API part 1: methods that must be
  643. # implemented in subclasses.
  644. async def dir_exists(self, path):
  645. """Does a directory exist at the given path?
  646. Like os.path.isdir
  647. Override this method in subclasses.
  648. Parameters
  649. ----------
  650. path : str
  651. The path to check
  652. Returns
  653. -------
  654. exists : bool
  655. Whether the path does indeed exist.
  656. """
  657. raise NotImplementedError
  658. async def is_hidden(self, path):
  659. """Is path a hidden directory or file?
  660. Parameters
  661. ----------
  662. path : str
  663. The path to check. This is an API path (`/` separated,
  664. relative to root dir).
  665. Returns
  666. -------
  667. hidden : bool
  668. Whether the path is hidden.
  669. """
  670. raise NotImplementedError
  671. async def file_exists(self, path=""):
  672. """Does a file exist at the given path?
  673. Like os.path.isfile
  674. Override this method in subclasses.
  675. Parameters
  676. ----------
  677. path : str
  678. The API path of a file to check for.
  679. Returns
  680. -------
  681. exists : bool
  682. Whether the file exists.
  683. """
  684. raise NotImplementedError
  685. async def exists(self, path):
  686. """Does a file or directory exist at the given path?
  687. Like os.path.exists
  688. Parameters
  689. ----------
  690. path : str
  691. The API path of a file or directory to check for.
  692. Returns
  693. -------
  694. exists : bool
  695. Whether the target exists.
  696. """
  697. return await ensure_async(self.file_exists(path)) or await ensure_async(
  698. self.dir_exists(path)
  699. )
  700. async def get(self, path, content=True, type=None, format=None, require_hash=False):
  701. """Get a file or directory model.
  702. Parameters
  703. ----------
  704. require_hash : bool
  705. Whether the file hash must be returned or not.
  706. *Changed in version 2.11*: The *require_hash* parameter was added.
  707. """
  708. raise NotImplementedError
  709. async def save(self, model, path):
  710. """
  711. Save a file or directory model to path.
  712. Should return the saved model with no content. Save implementations
  713. should call self.run_pre_save_hook(model=model, path=path) prior to
  714. writing any data.
  715. """
  716. raise NotImplementedError
  717. async def delete_file(self, path):
  718. """Delete the file or directory at path."""
  719. raise NotImplementedError
  720. async def rename_file(self, old_path, new_path):
  721. """Rename a file or directory."""
  722. raise NotImplementedError
  723. # ContentsManager API part 2: methods that have usable default
  724. # implementations, but can be overridden in subclasses.
  725. async def delete(self, path):
  726. """Delete a file/directory and any associated checkpoints."""
  727. path = path.strip("/")
  728. if not path:
  729. raise HTTPError(400, "Can't delete root")
  730. await self.delete_file(path)
  731. await self.checkpoints.delete_all_checkpoints(path)
  732. self.emit(data={"action": "delete", "path": path})
  733. async def rename(self, old_path, new_path):
  734. """Rename a file and any checkpoints associated with that file."""
  735. await self.rename_file(old_path, new_path)
  736. await self.checkpoints.rename_all_checkpoints(old_path, new_path)
  737. self.emit(data={"action": "rename", "path": new_path, "source_path": old_path})
  738. async def update(self, model, path):
  739. """Update the file's path
  740. For use in PATCH requests, to enable renaming a file without
  741. re-uploading its contents. Only used for renaming at the moment.
  742. """
  743. path = path.strip("/")
  744. new_path = model.get("path", path).strip("/")
  745. if path != new_path:
  746. await self.rename(path, new_path)
  747. model = await self.get(new_path, content=False)
  748. return model
  749. async def increment_filename(self, filename, path="", insert=""):
  750. """Increment a filename until it is unique.
  751. Parameters
  752. ----------
  753. filename : unicode
  754. The name of a file, including extension
  755. path : unicode
  756. The API path of the target's directory
  757. insert : unicode
  758. The characters to insert after the base filename
  759. Returns
  760. -------
  761. name : unicode
  762. A filename that is unique, based on the input filename.
  763. """
  764. # Extract the full suffix from the filename (e.g. .tar.gz)
  765. path = path.strip("/")
  766. basename, dot, ext = filename.rpartition(".")
  767. if ext != "ipynb":
  768. basename, dot, ext = filename.partition(".")
  769. suffix = dot + ext
  770. for i in itertools.count():
  771. insert_i = f"{insert}{i}" if i else ""
  772. name = f"{basename}{insert_i}{suffix}"
  773. file_exists = await ensure_async(self.exists(f"{path}/{name}"))
  774. if not file_exists:
  775. break
  776. return name
  777. async def new_untitled(self, path="", type="", ext=""):
  778. """Create a new untitled file or directory in path
  779. path must be a directory
  780. File extension can be specified.
  781. Use `new` to create files with a fully specified path (including filename).
  782. """
  783. path = path.strip("/")
  784. dir_exists = await ensure_async(self.dir_exists(path))
  785. if not dir_exists:
  786. raise HTTPError(404, "No such directory: %s" % path)
  787. model = {}
  788. if type:
  789. model["type"] = type
  790. if ext == ".ipynb":
  791. model.setdefault("type", "notebook")
  792. else:
  793. model.setdefault("type", "file")
  794. insert = ""
  795. if model["type"] == "directory":
  796. untitled = self.untitled_directory
  797. insert = " "
  798. elif model["type"] == "notebook":
  799. untitled = self.untitled_notebook
  800. ext = ".ipynb"
  801. elif model["type"] == "file":
  802. untitled = self.untitled_file
  803. else:
  804. raise HTTPError(400, "Unexpected model type: %r" % model["type"])
  805. name = await self.increment_filename(untitled + ext, path, insert=insert)
  806. path = f"{path}/{name}"
  807. return await self.new(model, path)
  808. async def new(self, model=None, path=""):
  809. """Create a new file or directory and return its model with no content.
  810. To create a new untitled entity in a directory, use `new_untitled`.
  811. """
  812. path = path.strip("/")
  813. if model is None:
  814. model = {}
  815. if path.endswith(".ipynb"):
  816. model.setdefault("type", "notebook")
  817. else:
  818. model.setdefault("type", "file")
  819. # no content, not a directory, so fill out new-file model
  820. if "content" not in model and model["type"] != "directory":
  821. if model["type"] == "notebook":
  822. model["content"] = new_notebook()
  823. model["format"] = "json"
  824. else:
  825. model["content"] = ""
  826. model["type"] = "file"
  827. model["format"] = "text"
  828. model = await self.save(model, path)
  829. return model
  830. async def copy(self, from_path, to_path=None):
  831. """Copy an existing file and return its new model.
  832. If to_path not specified, it will be the parent directory of from_path.
  833. If to_path is a directory, filename will increment `from_path-Copy#.ext`.
  834. Considering multi-part extensions, the Copy# part will be placed before the first dot for all the extensions except `ipynb`.
  835. For easier manual searching in case of notebooks, the Copy# part will be placed before the last dot.
  836. from_path must be a full path to a file.
  837. """
  838. path = from_path.strip("/")
  839. if to_path is not None:
  840. to_path = to_path.strip("/")
  841. if "/" in path:
  842. from_dir, from_name = path.rsplit("/", 1)
  843. else:
  844. from_dir = ""
  845. from_name = path
  846. model = await self.get(path)
  847. model.pop("path", None)
  848. model.pop("name", None)
  849. if model["type"] == "directory":
  850. raise HTTPError(400, "Can't copy directories")
  851. is_destination_specified = to_path is not None
  852. if not is_destination_specified:
  853. to_path = from_dir
  854. if await ensure_async(self.dir_exists(to_path)):
  855. name = copy_pat.sub(".", from_name)
  856. to_name = await self.increment_filename(name, to_path, insert="-Copy")
  857. to_path = f"{to_path}/{to_name}"
  858. elif is_destination_specified:
  859. if "/" in to_path:
  860. to_dir, to_name = to_path.rsplit("/", 1)
  861. if not await ensure_async(self.dir_exists(to_dir)):
  862. raise HTTPError(404, "No such parent directory: %s to copy file in" % to_dir)
  863. else:
  864. raise HTTPError(404, "No such directory: %s" % to_path)
  865. model = await self.save(model, to_path)
  866. self.emit(data={"action": "copy", "path": to_path, "source_path": from_path})
  867. return model
  868. async def trust_notebook(self, path):
  869. """Explicitly trust a notebook
  870. Parameters
  871. ----------
  872. path : str
  873. The path of a notebook
  874. """
  875. model = await self.get(path)
  876. nb = model["content"]
  877. self.log.warning("Trusting notebook %s", path)
  878. self.notary.mark_cells(nb, True)
  879. self.check_and_sign(nb, path)
  880. # Part 3: Checkpoints API
  881. async def create_checkpoint(self, path):
  882. """Create a checkpoint."""
  883. return await self.checkpoints.create_checkpoint(self, path)
  884. async def restore_checkpoint(self, checkpoint_id, path):
  885. """
  886. Restore a checkpoint.
  887. """
  888. await self.checkpoints.restore_checkpoint(self, checkpoint_id, path)
  889. async def list_checkpoints(self, path):
  890. """List the checkpoints for a path."""
  891. return await self.checkpoints.list_checkpoints(path)
  892. async def delete_checkpoint(self, checkpoint_id, path):
  893. """Delete a checkpoint for a path by id."""
  894. return await self.checkpoints.delete_checkpoint(checkpoint_id, path)