From 7675249cff03e9c52a1dc58f46da761ebfee85b5 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Tue, 10 Dec 2013 23:02:00 +0000 Subject: allow load/save heap to file to use media blocks as args --- diff --git a/TurtleArt/talogo.py b/TurtleArt/talogo.py index 5380137..5792043 100644 --- a/TurtleArt/talogo.py +++ b/TurtleArt/talogo.py @@ -835,24 +835,35 @@ class LogoCode: name = float(name) return 'stack3' + str(name) - def load_heap(self, path): + def load_heap(self, obj): """ Load FILO from file """ if self.tw.running_sugar: - # Choose a datastore object and push data to heap (Sugar only) - chooser_dialog(self.tw.parent, path, self.push_file_data_to_heap) + # Is the object a dsobject? + if isinstance(obj, Media) and obj.value: + from sugar.datastore import datastore + try: + dsobject = datastore.get(obj.value) + except: + debug_output("Couldn't find dsobject %s" % + (obj.value), self.tw.running_sugar) + if dsobject is not None: + self.push_file_data_to_heap(dsobject) + # Or is it a path? + elif os.path.exists(obj): + self.push_file_data_to_heap(None, path=obj) + else: + # Finally try choosing a datastore object + chooser_dialog(self.tw.parent, obj, + self.push_file_data_to_heap) else: - if not os.path.exists(path): - path, self.tw.load_save_folder = get_load_name( + # If you cannot find the file, open a chooser. + if not os.path.exists(obj): + obj, self.tw.load_save_folder = get_load_name( '.*', self.tw.load_save_folder) - if path is None: - return + if obj is not None: + self.push_file_data_to_heap(None, path=obj) - data = data_from_file(path) - if data is not None: - for val in data: - self.heap.append(val) - - def save_heap(self, path): + def save_heap(self, obj): """ save FILO to file """ if self.tw.running_sugar: from sugar import profile @@ -861,22 +872,23 @@ class LogoCode: # Save JSON-encoded heap to temporary file heap_file = os.path.join(get_path(activity, 'instance'), - str(path) + '.txt') + 'heap.txt') data_to_file(self.heap, heap_file) - # Create a datastore object - dsobject = datastore.create() - - # Write any metadata (specifically set the title of the file - # and specify that this is a plain text file). - dsobject.metadata['title'] = str(path) - dsobject.metadata['icon-color'] = profile.get_color().to_string() - dsobject.metadata['mime_type'] = 'text/plain' + # Write to an existing or new dsobject + if isinstance(obj, Media) and obj.value: + dsobject = datastore.get(obj.value) + else: + dsobject = datastore.create() + dsobject.metadata['title'] = str(obj) + dsobject.metadata['icon-color'] = \ + profile.get_color().to_string() + dsobject.metadata['mime_type'] = 'text/plain' dsobject.set_file_path(heap_file) datastore.write(dsobject) dsobject.destroy() else: - heap_file = path + heap_file = obj data_to_file(self.heap, heap_file) def get_heap(self): @@ -1172,9 +1184,15 @@ class LogoCode: int(self.tw.canvas.textsize * self.scale / 100.), self.tw.canvas.width - x) - def push_file_data_to_heap(self, dsobject): + def push_file_data_to_heap(self, dsobject, path=None): """ push contents of a data store object (assuming json encoding) """ - data = data_from_file(dsobject.file_path) + if dsobject: + data = data_from_file(dsobject.file_path) + elif path is not None: + data = data_from_file(path) + else: + data = None + debug_output("No file to open", self.tw.running_sugar) if data is not None: for val in data: self.heap.append(val) diff --git a/plugins/turtle_blocks_extras/turtle_blocks_extras.py b/plugins/turtle_blocks_extras/turtle_blocks_extras.py index 62dd40f..2bb07dd 100644 --- a/plugins/turtle_blocks_extras/turtle_blocks_extras.py +++ b/plugins/turtle_blocks_extras/turtle_blocks_extras.py @@ -543,7 +543,7 @@ make "tmp first :taheap\nmake "taheap butfirst :taheap\noutput :tmp\nend\n') last-out heap) to a file')) self.tw.lc.def_prim('saveheap', 1, Primitive(self.tw.lc.save_heap, - arg_descs=[ArgSlot(TYPE_STRING)])) + arg_descs=[ArgSlot(TYPE_OBJECT)])) palette.add_block('loadheap', style='basic-style-1arg', @@ -554,8 +554,7 @@ last-out heap) to a file')) last-out heap) from a file')) self.tw.lc.def_prim('loadheap', 1, Primitive(self.tw.lc.load_heap, - arg_descs=[ArgSlot(TYPE_STRING)], - return_type=TYPE_STRING, + arg_descs=[ArgSlot(TYPE_OBJECT)], call_afterwards=self.after_push)) palette.add_block('isheapempty2', -- cgit v0.9.1