diff options
author | Walter Bender <walter@sugarlabs.org> | 2014-02-11 21:58:11 (GMT) |
---|---|---|
committer | Walter Bender <walter@sugarlabs.org> | 2014-02-11 21:58:11 (GMT) |
commit | 51d502e5f255d5aef941643630cddd2c859a37bf (patch) | |
tree | 75e1bff3c7a312c4b84e013c386dbc49038ae39d | |
parent | ed593baa86ae934b7eca031249648c2bfb339148 (diff) |
fixed the saving behavior -- but still need to clean up the code
-rw-r--r-- | notebook.py | 46 | ||||
-rw-r--r-- | pippy_app.py | 91 |
2 files changed, 89 insertions, 48 deletions
diff --git a/notebook.py b/notebook.py index 06b06ec..f2f636e 100644 --- a/notebook.py +++ b/notebook.py @@ -41,11 +41,12 @@ class TabLabel(Gtk.HBox): ([GObject.TYPE_PYOBJECT])), } - def __init__(self, child, label, tabs): + def __init__(self, child, label, path, tabs): GObject.GObject.__init__(self) self.child = child self.label_text = label + self._path = path self.tabs = tabs self.label_box = Gtk.EventBox() @@ -75,6 +76,12 @@ class TabLabel(Gtk.HBox): def get_text(self): return self._label.get_text() + def get_path(self): + return self._path + + def set_path(self, path): + self._path = path + def update_size(self, size): self.set_size_request(size, -1) @@ -136,7 +143,7 @@ class SourceNotebook(AddNotebook): self.activity = activity self.set_scrollable(True) - def add_tab(self, label=None, buffer_text=None): + def add_tab(self, label=None, buffer_text=None, path=None): # Set text_buffer text_buffer = GtkSource.Buffer() @@ -159,6 +166,7 @@ class SourceNotebook(AddNotebook): if buffer_text: text_buffer.set_text(buffer_text) + text_buffer.set_modified(False) # Set up SourceView text_view = GtkSource.View() @@ -182,11 +190,11 @@ class SourceNotebook(AddNotebook): tabdex = self.get_n_pages() + 1 if label: - tablabel = TabLabel(codesw, label, self) + tablabel = TabLabel(codesw, label, path, self) else: tablabel = TabLabel(codesw, _("New Source File %d" % tabdex), - self) + path, self) tablabel.connect("tab-close", self._tab_closed_cb) codesw.show_all() index = self.append_page(codesw, tablabel) @@ -197,6 +205,11 @@ class SourceNotebook(AddNotebook): widget = self.get_tab_label(child) widget.set_text(self._purify_file(label)) + def set_current_path(self, path): + child = self.get_nth_page(self.get_current_page()) + widget = self.get_tab_label(child) + widget.set_path(path) + def get_text_buffer(self): tab = self.get_nth_page(self.get_current_page()).get_children() text_buffer = tab[0].get_buffer() @@ -220,22 +233,29 @@ class SourceNotebook(AddNotebook): return label - def get_all_data(self, check_modified=False): + def get_all_data(self): # Returns all the names of files and the buffer contents too. names = [] - contents = [] + python_codes = [] + paths = [] + modifieds = [] for i in range(0, self.get_n_pages()): child = self.get_nth_page(i) - text_buffer = child.get_children()[0].get_buffer() + label = self._purify_file(self.get_tab_label(child).get_text()) + names.append(label) + + path = self.get_tab_label(child).get_path() + paths.append(path) + + text_buffer = child.get_children()[0].get_buffer() + text = text_buffer.get_text(*text_buffer.get_bounds(), + include_hidden_chars=True) + python_codes.append(text) - if not check_modified or text_buffer.get_modified(): - text = text_buffer.get_text(*text_buffer.get_bounds(), - include_hidden_chars=True) - contents.append(text) - names.append(label) + modifieds.append(text_buffer.get_modified()) - return (names, contents) + return (names, python_codes, paths, modifieds) def get_current_file_name(self): child = self.get_nth_page(self.get_current_page()) diff --git a/pippy_app.py b/pippy_app.py index 3bb557a..1ebf639 100644 --- a/pippy_app.py +++ b/pippy_app.py @@ -333,10 +333,11 @@ class PippyActivity(ViewSourceActivity, groupthink.sugar_tools.GroupActivity): if self.loaded_from_journal and self.py_file: self.source_tabs.add_tab( self.initial_title, - self.initial_text_buffer) + self.initial_text_buffer, + None) elif self.loaded_session: for name, content in self.loaded_session: - self.source_tabs.add_tab(name, content) + self.source_tabs.add_tab(name, content, None) else: self.source_tabs.add_tab() @@ -423,6 +424,7 @@ class PippyActivity(ViewSourceActivity, groupthink.sugar_tools.GroupActivity): self.stopbutton_cb(None) self._reset_vte() self.source_tabs.set_current_label(value['name']) + self.source_tabs.set_current_path(value['path']) self.source_tabs.get_text_view().grab_focus() def _select_func_cb(self, path): @@ -476,7 +478,7 @@ class PippyActivity(ViewSourceActivity, groupthink.sugar_tools.GroupActivity): def _write_all_buffers(self, tmp_dir): data = self.source_tabs.get_all_data() zipdata = zip(data[0], data[1]) - for name, content in zipdata: + for name, content, path in zipdata: with open(os.path.join(tmp_dir, name), 'w') as f: # Write utf-8 coding prefix if there's not one already if re.match(r'coding[:=]\s*([-\w.]+)', @@ -898,41 +900,49 @@ class PippyActivity(ViewSourceActivity, groupthink.sugar_tools.GroupActivity): def save_to_journal(self, file_path, cloudstring): _file = open(file_path, 'w') if not self.shared_activity: - data = self.source_tabs.get_all_data(check_modified=True) - zipped_data = zip(data[0], data[1]) + data = self.source_tabs.get_all_data() + zipped_data = zip(data[0], data[1], data[2], data[3]) sessionlist = [] app_temp = os.path.join(self.get_activity_root(), 'instance') tmpfile = os.path.join(app_temp, 'pippy-tempfile-storing.py') - for zipdata, dsid in map(None, zipped_data, self.session_data): - name, content = zipdata - - if dsid is not None: - dsitem = datastore.get(dsid) + for zipdata, content in map(None, zipped_data, self.session_data): + name, python_code, path, modified = zipdata + logging.debug(content) + logging.debug(path) + if content is not None and content[0] != '/': + logging.debug('have an existing dsobject') + dsitem = datastore.get(content) __file = open(tmpfile, 'w') - __file.write(content) + __file.write(python_code) __file.close() dsitem.set_file_path(tmpfile) dsitem.metadata['title'] = name datastore.write(dsitem) - elif len(content) > 0: + sessionlist.append([name, dsitem.object_id]) + elif modified: + logging.debug('have newly modified code') dsobject = datastore.create() dsobject.metadata['mime_type'] = 'text/x-python' dsobject.metadata['title'] = name __file = open(tmpfile, 'w') - __file.write(content) + __file.write(python_code) __file.close() dsobject.set_file_path(tmpfile) datastore.write(dsobject) - dsitem = None - else: - continue - - if dsitem is not None: - sessionlist.append([name, dsitem.object_id]) - else: sessionlist.append([name, dsobject.object_id]) + elif content is not None or path is not None: + logging.debug('have a sample file path') + # a path to sample code + if path is None: + sessionlist.append([name, content]) + else: + sessionlist.append([name, path]) + else: + logging.debug('have nothing') self.metadata['mime_type'] = 'application/json' + logging.debug('saving json data') + logging.debug(sessionlist) _file.write(json.dumps(sessionlist)) else: self.metadata['mime_type'] = groupthink_mimetype @@ -962,22 +972,33 @@ class PippyActivity(ViewSourceActivity, groupthink.sugar_tools.GroupActivity): self.initial_title = self.metadata['title'] self.loaded_from_journal = self.py_file = True elif self.metadata['mime_type'] == 'application/json': + logging.debug('loading json data') data = json.loads(open(file_path).read()) - for name, dsid in data: - try: - dsitem = datastore.get(dsid) - except: - logging.debug('could not open dsobject %s; skipping' % - dsid) - continue - try: - content = open(dsitem.get_file_path()).read() - except: - logging.debug('could not open %s; skipping' % - dsitem.get_file_path()) - continue - self.loaded_session.append([name, content]) - self.session_data.append(dsitem.object_id) + logging.debug(data) + for name, content in data: + # content is either a datastore id or the path to some + # sample code + if content is not None and content[0] == '/': # a path + try: + python_code = open(content).read() + except: + logging.debug('could not open %s; skipping' % + content) + else: + try: + dsitem = datastore.get(content) + except: + logging.debug('could not open dsobject %s; skipping' % + content) + continue + try: + python_code = open(dsitem.get_file_path()).read() + except: + logging.debug('could not open %s; skipping' % + dsitem.get_file_path()) + continue + self.loaded_session.append([name, python_code]) + self.session_data.append(content) elif self.metadata['mime_type'] == groupthink_mimetype: return open(file_path).read() |