From cba2d6118b7802ffba0746577bb3e14a0d5e0c84 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Sat, 11 Jan 2014 18:27:54 +0000 Subject: new icons for new exports --- diff --git a/Notebook.py b/Notebook.py index 16eebe5..23b0be3 100644 --- a/Notebook.py +++ b/Notebook.py @@ -185,6 +185,19 @@ class SourceNotebook(AddNotebook): text_view = tab[0] return text_view + def _purify_file(self, label): + import unicodedata + + if not label.endswith(".py"): + label = label + ".py" + + label = label.replace(" ", "_") + if isinstance(label, unicode): + label = \ + unicodedata.normalize('NFKD', label).encode('ascii', 'ignore') + + return label + def get_all_data(self): # Returns all the names of files and the buffer contents too. names = [] @@ -196,13 +209,19 @@ class SourceNotebook(AddNotebook): include_hidden_chars=True) contents.append(text) - label = self.get_tab_label(child).get_text() - if not label.endswith(".py"): - label = label + ".py" + label = self._purify_file(self.get_tab_label(child).get_text()) + names.append(label) return (names, contents) + def get_current_file_name(self): + child = self.get_nth_page(self.get_current_page()) + label = self.get_tab_label(child).get_text() + label = self._purify_file(label) + + return label + def child_exited_cb(self, *args): """Called whenever a child exits. If there's a handler, runadd it.""" h, self.activity._child_exited_handler = \ diff --git a/icons/pippy-create-bundle.svg b/icons/pippy-create-bundle.svg new file mode 100644 index 0000000..43b089a --- /dev/null +++ b/icons/pippy-create-bundle.svg @@ -0,0 +1,54 @@ + + + +image/svg+xml \ No newline at end of file diff --git a/icons/pippy-create-disutils.svg b/icons/pippy-create-disutils.svg new file mode 100644 index 0000000..3f0717c --- /dev/null +++ b/icons/pippy-create-disutils.svg @@ -0,0 +1,50 @@ + + + +image/svg+xml \ No newline at end of file diff --git a/icons/pippy-create_bundle.svg b/icons/pippy-create_bundle.svg deleted file mode 100644 index b5d0a26..0000000 --- a/icons/pippy-create_bundle.svg +++ /dev/null @@ -1,34 +0,0 @@ - - - -]> - \ No newline at end of file diff --git a/icons/pippy-export_doc.svg b/icons/pippy-export-doc.svg index b8e8c86..b8e8c86 100644 --- a/icons/pippy-export_doc.svg +++ b/icons/pippy-export-doc.svg diff --git a/icons/pippy-export_example.svg b/icons/pippy-export-example.svg index 014460b..014460b 100644 --- a/icons/pippy-export_example.svg +++ b/icons/pippy-export-example.svg diff --git a/icons/pippy-export-library.svg b/icons/pippy-export-library.svg new file mode 100644 index 0000000..256cf47 --- /dev/null +++ b/icons/pippy-export-library.svg @@ -0,0 +1,74 @@ + + + +image/svg+xml \ No newline at end of file diff --git a/icons/pippy-import-doc.svg b/icons/pippy-import-doc.svg new file mode 100644 index 0000000..2026290 --- /dev/null +++ b/icons/pippy-import-doc.svg @@ -0,0 +1,83 @@ + + + +image/svg+xml \ No newline at end of file diff --git a/pippy_app.py b/pippy_app.py index 179f314..9148d42 100644 --- a/pippy_app.py +++ b/pippy_app.py @@ -26,6 +26,7 @@ import subprocess from random import uniform import locale import json +import sys import dbus from dbus.mainloop.glib import DBusGMainLoop @@ -52,6 +53,7 @@ from sugar3.activity.activity import get_bundle_name from sugar3.graphics.alert import NotifyAlert from sugar3.graphics import style from sugar3.graphics.toggletoolbutton import ToggleToolButton +from sugar3.graphics.objectchooser import ObjectChooser from jarabe.view.customizebundle import generate_unique_id @@ -104,6 +106,8 @@ class PippyActivity(ViewSourceActivity, groupthink.sugar_tools.GroupActivity): self.loaded_session = [] self.session_data = [] + sys.path.append(os.path.join(self.get_activity_root(), "Library")) + def initialize_display(self): self._logger = logging.getLogger('pippy-activity') @@ -115,26 +119,38 @@ class PippyActivity(ViewSourceActivity, groupthink.sugar_tools.GroupActivity): separator.show() activity_toolbar.insert(separator, -1) - export_doc_button = ToolButton('pippy-export_doc') - export_doc_button.set_tooltip(_("Export as Pippy Document")) + import_py_button = ToolButton("pippy-import-doc") + import_py_button.set_tooltip(_("Import Python file to new tab")) + import_py_button.connect("clicked", self._import_py_cb) + import_py_button.show() + activity_toolbar.insert(import_py_button, -1) + + export_doc_button = ToolButton('pippy-export-doc') + export_doc_button.set_tooltip(_("Export as Pippy document")) export_doc_button.connect('clicked', self._export_document_cb) export_doc_button.show() activity_toolbar.insert(export_doc_button, -1) - export_example_button = ToolButton('pippy-export_example') - export_example_button.set_tooltip(_("Export as Pippy Example")) + save_as_library = ToolButton("pippy-export-library") + save_as_library.set_tooltip(_("Save this file to the Pippy library")) + save_as_library.connect("clicked", self._save_as_library) + save_as_library.show() + activity_toolbar.insert(save_as_library, -1) + + export_example_button = ToolButton('pippy-export-example') + export_example_button.set_tooltip(_("Export as new Pippy example")) export_example_button.connect('clicked', self._export_example_cb) export_example_button.show() activity_toolbar.insert(export_example_button, -1) - create_bundle_button = ToolButton('pippy-create_bundle') - create_bundle_button.set_tooltip(_("Create Activity Bundle")) + create_bundle_button = ToolButton('pippy-create-bundle') + create_bundle_button.set_tooltip(_("Create a Sugar activity bundle")) create_bundle_button.connect('clicked', self._create_bundle_cb) create_bundle_button.show() activity_toolbar.insert(create_bundle_button, -1) - export_disutils = ToolButton("pippy-create_bundle") - export_disutils.set_tooltip(_("Export as disutils package")) + export_disutils = ToolButton("pippy-create-disutils") + export_disutils.set_tooltip(_("Export as a disutils package")) export_disutils.connect("clicked", self.__export_disutils_cb) export_disutils.show() activity_toolbar.insert(export_disutils, -1) @@ -416,18 +432,16 @@ class PippyActivity(ViewSourceActivity, groupthink.sugar_tools.GroupActivity): self._reset_vte() self.source_tabs.get_text_view().grab_focus() - def _write_text_buffer(self, filename): - text_buffer = self.source_tabs.get_text_buffer() - start, end = text_buffer.get_bounds() - text = text_buffer.get_text(start, end, True) - - with open(filename, 'w') as f: - # write utf-8 coding prefix if there's not already one - if re.match(r'coding[:=]\s*([-\w.]+)', - '\n'.join(text.splitlines()[:2])) is None: - f.write(PYTHON_PREFIX) - for line in text: - f.write(line) + 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: + with open(os.path.join(tmp_dir, name), 'w') as f: + # write utf-8 coding prefix if there's not already one + if re.match(r'coding[:=]\s*([-\w.]+)', + '\n'.join(content.splitlines()[:2])) is None: + f.write(PYTHON_PREFIX) + f.write(content) def _reset_vte(self): self._vte.grab_focus() @@ -463,8 +477,13 @@ class PippyActivity(ViewSourceActivity, groupthink.sugar_tools.GroupActivity): self.outbox.show_all() self.toggle_output.set_active(True) - pippy_app_name = '%s/tmp/pippy_app.py' % self.get_activity_root() - self._write_text_buffer(pippy_app_name) + pippy_tmp_dir = '%s/tmp/' % self.get_activity_root() + self._write_all_buffers(pippy_tmp_dir) + + current_file = os.path.join( + pippy_tmp_dir, + self.source_tabs.get_current_file_name()) + # write activity.py here too, to support pippy-based activities. copy2('%s/activity.py' % get_bundle_path(), '%s/tmp/activity.py' % self.get_activity_root()) @@ -472,7 +491,7 @@ class PippyActivity(ViewSourceActivity, groupthink.sugar_tools.GroupActivity): self._pid = self._vte.fork_command_full( Vte.PtyFlags.DEFAULT, get_bundle_path(), - ["/bin/sh", "-c", "python %s; sleep 1" % pippy_app_name, + ["/bin/sh", "-c", "python %s; sleep 1" % current_file, "PYTHONPATH=%s/library:%s" % (get_bundle_path(), os.getenv("PYTHONPATH", ""))], ["PYTHONPATH=%s/library:%s" % (get_bundle_path(), @@ -488,9 +507,67 @@ class PippyActivity(ViewSourceActivity, groupthink.sugar_tools.GroupActivity): except: pass # process must already be dead. + def _save_as_library(self, button): + import unicodedata + library_dir = os.path.join(get_bundle_path(), "library") + file_name = self.source_tabs.get_current_file_name() + text_buffer = self.source_tabs.get_text_buffer() + content = text_buffer.get_text( + *text_buffer.get_bounds(), + include_hidden_chars=True) + + if not os.path.isdir(library_dir): + os.mkdir(library_dir) + + with open(os.path.join(library_dir, file_name), "w") as f: + f.write(content) + success = True + + if success: + alert = NotifyAlert(5) + alert.props.title = _('Python File added to Library') + IMPORT_MESSAGE = _('The file you selected has been added' + ' to the library. Use "import {importname}"' + ' to import the library for using.') + alert.props.msg = IMPORT_MESSAGE.format(importname=file_name[:-3]) + alert.connect('response', self.remove_alert_cb) + self.add_alert(alert) + def _export_document_cb(self, __): self.copy() + def remove_alert_cb(self, alert, response_id): + self.remove_alert(alert) + + def _import_py_cb(self, button): + chooser = ObjectChooser() + result = chooser.run() + if result is Gtk.ResponseType.ACCEPT: + dsitem = chooser.get_selected_object() + if dsitem.metadata['mime_type'] != "text/x-python": + alert = NotifyAlert(5) + alert.props.title = _('Error importing Python file') + alert.props.msg = _('The file you selected is not a ' + 'Python file.') + alert.connect('response', self.remove_alert_cb) + self.add_alert(alert) + elif dsitem.object_id in self.session_data: + alert = NotifyAlert(5) + alert.props.title = _('Error importing Python file') + alert.props.msg = _('The file you selected is already ' + 'open') + alert.connect('response', self.remove_alert_cb) + self.add_alert(alert) + else: + name = dsitem.metadata['title'] + file_path = dsitem.get_file_path() + content = open(file_path, "r").read() + + self.source_tabs.add_tab(name, content) + self.session_data.append(dsitem.object_id) + + chooser.destroy() + def _create_bundle_cb(self, __): from shutil import copytree, copy2, rmtree from tempfile import mkdtemp @@ -542,6 +619,19 @@ class PippyActivity(ViewSourceActivity, groupthink.sugar_tools.GroupActivity): self._vte.feed("\r\n") raise + def _write_text_buffer(self, filename): + text_buffer = self.source_tabs.get_text_buffer() + start, end = text_buffer.get_bounds() + text = text_buffer.get_text(start, end, True) + + with open(filename, 'w') as f: + # write utf-8 coding prefix if there's not already one + if re.match(r'coding[:=]\s*([-\w.]+)', + '\n'.join(text.splitlines()[:2])) is None: + f.write(PYTHON_PREFIX) + for line in text: + f.write(line) + def __export_disutils_cb(self, button): app_temp = os.path.join(self.get_activity_root(), "instance") data = self.source_tabs.get_all_data() @@ -558,8 +648,9 @@ class PippyActivity(ViewSourceActivity, groupthink.sugar_tools.GroupActivity): from sugar3.graphics.icon import Icon alert = Alert() alert.props.title = _('Save as disutils package error') - alert.props.msg = _('Please give your activity a meaningful \ -name before attempting to save it as an disutils package.') + alert.props.msg = _('Please give your activity a meaningful' + 'name before attempting to save it ' + 'as an disutils package.') ok_icon = Icon(icon_name='dialog-ok') alert.add_button(Gtk.ResponseType.OK, _('Ok'), ok_icon) alert.connect('response', self.dismiss_alert_cb) @@ -731,10 +822,8 @@ Do you want to overwrite it?') if dsitem is not None: sessionlist.append([name, dsitem.object_id]) - print dsitem.object_id else: sessionlist.append([name, dsobject.object_id]) - print dsobject.object_id self.metadata['mime_type'] = 'application/json' _file.write(json.dumps(sessionlist)) @@ -744,7 +833,6 @@ Do you want to overwrite it?') def load_from_journal(self, file_path): if self.metadata['mime_type'] == 'text/x-python': - print file_path text = open(file_path).read() # discard the '#!/usr/bin/python' and 'coding: utf-8' lines, # if present @@ -753,13 +841,7 @@ Do you want to overwrite it?') self.initial_text_buffer = text self.initial_title = self.metadata['title'] self.loaded_from_journal = self.py_file = True - # We need to get the ds entry for the file we got. - # So that we can add to session_data and save to it - # for the corresponding tab. - # TASKIDEA: Add import .py file in pippy - # so that .py file can be packed in disutils package. - # Like I have 3 files and need to have a file from - # the journal. Then I will import it and export to disutils. + elif self.metadata['mime_type'] == "application/json": data = json.loads(open(file_path).read()) for name, dsid in data: @@ -928,7 +1010,6 @@ def main(): from shutil import copytree, copy2, rmtree from sugar3 import profile from sugar3.activity import bundlebuilder - import sys parser = OptionParser(usage='%prog [options] [title] [sourcefile]') parser.add_option('-d', '--dir', dest='dir', default='.', metavar='DIR', -- cgit v0.9.1