@@ -130,6 +130,17 @@ def put_notebook_document(
130130 doc_uri , notebook_type , cells , version , metadata
131131 )
132132
133+ @contextmanager
134+ def temp_document (self , source , path = None ):
135+ if path is None :
136+ path = self .root_path
137+ uri = uris .from_fs_path (os .path .join (path , str (uuid .uuid4 ())))
138+ try :
139+ self .put_document (uri , source )
140+ yield uri
141+ finally :
142+ self .rm_document (uri )
143+
133144 def add_notebook_cells (self , doc_uri , cells , start ):
134145 self ._docs [doc_uri ].add_cells (cells , start )
135146
@@ -139,9 +150,11 @@ def remove_notebook_cells(self, doc_uri, start, delete_count):
139150 def update_notebook_metadata (self , doc_uri , metadata ):
140151 self ._docs [doc_uri ].metadata = metadata
141152
142- def put_cell_document (self , doc_uri , language_id , source , version = None ):
153+ def put_cell_document (
154+ self , doc_uri , notebook_uri , language_id , source , version = None
155+ ):
143156 self ._docs [doc_uri ] = self ._create_cell_document (
144- doc_uri , language_id , source , version
157+ doc_uri , notebook_uri , language_id , source , version
145158 )
146159
147160 def rm_document (self , doc_uri ):
@@ -340,11 +353,14 @@ def _create_notebook_document(
340353 metadata = metadata ,
341354 )
342355
343- def _create_cell_document (self , doc_uri , language_id , source = None , version = None ):
356+ def _create_cell_document (
357+ self , doc_uri , notebook_uri , language_id , source = None , version = None
358+ ):
344359 # TODO: remove what is unnecessary here.
345360 path = uris .to_fs_path (doc_uri )
346361 return Cell (
347362 doc_uri ,
363+ notebook_uri = notebook_uri ,
348364 language_id = language_id ,
349365 workspace = self ,
350366 source = source ,
@@ -585,6 +601,26 @@ def add_cells(self, new_cells: List, start: int) -> None:
585601 def remove_cells (self , start : int , delete_count : int ) -> None :
586602 del self .cells [start : start + delete_count ]
587603
604+ def cell_data (self ):
605+ """Extract current cell data.
606+
607+ Returns a dict (ordered by cell position) where the key is the cell uri and the
608+ value is a dict with line_start, line_end, and source attributes.
609+ """
610+ cell_data = {}
611+ offset = 0
612+ for cell in self .cells :
613+ cell_uri = cell ["document" ]
614+ cell_document = self .workspace .get_cell_document (cell_uri )
615+ num_lines = cell_document .line_count
616+ cell_data [cell_uri ] = {
617+ "line_start" : offset ,
618+ "line_end" : offset + num_lines - 1 ,
619+ "source" : cell_document .source ,
620+ }
621+ offset += num_lines
622+ return cell_data
623+
588624
589625class Cell (Document ):
590626 """
@@ -599,6 +635,7 @@ class Cell(Document):
599635 def __init__ (
600636 self ,
601637 uri ,
638+ notebook_uri ,
602639 language_id ,
603640 workspace ,
604641 source = None ,
@@ -611,6 +648,7 @@ def __init__(
611648 uri , workspace , source , version , local , extra_sys_path , rope_project_builder
612649 )
613650 self .language_id = language_id
651+ self .notebook_uri = notebook_uri
614652
615653 @property
616654 @lock
0 commit comments