diff options
author | Gonzalo Odiard <godiard@gmail.com> | 2014-02-06 19:17:25 (GMT) |
---|---|---|
committer | Gonzalo Odiard <godiard@gmail.com> | 2014-02-06 19:17:25 (GMT) |
commit | 3c72668291b1e857ceb262ef1b6a457c907fbd20 (patch) | |
tree | 22b7d99f1be407c4b64c5e50a230e7a1c3146c34 | |
parent | d5227168d192a35d541b83d916d0828695f4b2be (diff) |
Use skeletons to create activities
-rw-r--r-- | develop-activity/develop_app.py | 29 | ||||
-rw-r--r-- | develop-activity/new_activity.py | 105 |
2 files changed, 56 insertions, 78 deletions
diff --git a/develop-activity/develop_app.py b/develop-activity/develop_app.py index a0f58e5..27089d8 100644 --- a/develop-activity/develop_app.py +++ b/develop-activity/develop_app.py @@ -314,7 +314,7 @@ class DevelopActivity(activity.Activity): create_btn = gtk.Button(_('Start')) create_btn.connect('clicked', self._create_new_activity, - activity_name_entry) + activity_name_entry, project_type_combo) hbox_name.pack_start(create_btn, expand=True, fill=True, padding=10) align = gtk.Alignment(xalign=0.5, yalign=0.5) @@ -356,20 +356,27 @@ class DevelopActivity(activity.Activity): for dir_name in sorted(os.listdir(skeletons_path)): skeletons_combo.append_item(0, dir_name) - def _create_new_activity(self, button, name_entry): + def _create_new_activity(self, button, name_entry, combo_skeletons): """create and open a new activity in working dir """ if name_entry.get_text() == '': self._show_alert(_('You must type the name for the new activity')) - else: - activity_name = name_entry.get_text().strip() - activities_path = os.path.join(os.path.expanduser("~"), - "Activities") - activityDir = new_activity.new_activity(activity_name, - activities_path) - self.first_open_activity(activityDir) - # remove the welcome tab - self.editor.remove_page(0) + return + if combo_skeletons.get_active() == -1: + self._show_alert(_('You must select the project type')) + return + + activity_name = name_entry.get_text().strip() + activities_path = os.path.join(os.path.expanduser("~"), + "Activities") + skel_iter = combo_skeletons.get_active_iter() + skeleton = combo_skeletons.get_model().get_value(skel_iter, 1) + + activityDir = new_activity.create_activity(activity_name, + activities_path, skeleton) + self.first_open_activity(activityDir) + # remove the welcome tab + self.editor.remove_page(0) def _show_alert(self, message, title=None): alert = Alert() diff --git a/develop-activity/new_activity.py b/develop-activity/new_activity.py index 0d164f5..e939005 100644 --- a/develop-activity/new_activity.py +++ b/develop-activity/new_activity.py @@ -15,6 +15,9 @@ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA import os import shutil +import logging + +from sugar.activity import activity def class_template(name): @@ -22,82 +25,50 @@ def class_template(name): return '%s_app' % name.lower(), '%sActivity' % name -def activity_info_template(name): - bundle_id = 'org.laptop.%s' % name.replace(' ', '') +def activity_info_template(name, web_activity=False): + bundle_id = 'org.sugarlabs.%s' % name.replace(' ', '') + if web_activity: + exec_line = 'sugar-activity-web' + else: + exec_line = 'sugar-activity activity.HelloWorldActivity' + return """[Activity] name = %s bundle_id = %s -icon = activity-default -exec = sugar-activity %s.%s -s +icon = activity-helloworld +exec = %s activity_version = 1 show_launcher = yes -""" % ((name, bundle_id) + class_template(name)) - - -def base_file_template(name): - __filen, classn = class_template(name) - return """import gtk -from sugar.activity import activity -from sugar.activity.widgets import ActivityToolbarButton -from sugar.graphics.toolbarbox import ToolbarBox -from sugar.activity.widgets import StopButton - -class %s(activity.Activity): - ''' - The base class for the %s activity. - ''' - - def __init__(self, handle): - activity.Activity.__init__(self, handle) - toolbarbox = ToolbarBox() - - activity_button = ActivityToolbarButton(self) - toolbarbox.toolbar.insert(activity_button, 0) +""" % (name, bundle_id, exec_line) - separator = gtk.SeparatorToolItem() - separator.set_draw(False) - separator.set_expand(True) - toolbarbox.toolbar.insert(separator, -1) - toolbarbox.toolbar.insert(StopButton(self), -1) - toolbarbox.show_all() - self.set_toolbar_box(toolbarbox) - - - def write_file(self, file_path): - ''' - Implement this method to save your activity's state. - ''' - raise NotImplementedError - - def read_file(self, file_path): - ''' - Implement this method to resume state saved in write_file(). - ''' - raise NotImplementedError -""" % (classn, name) - - -def new_activity(name, base_path): +def create_activity(name, base_path, skeleton): path = os.path.expanduser(os.path.join(base_path, '%s.activity' % name.replace(' ', ''))) os.makedirs(path) - activityPath = os.path.join(path, 'activity') - os.mkdir(activityPath) - filen, __classn = class_template(name) - _file = file(os.path.join(path, filen + '.py'), 'w') - _file.write(base_file_template(name)) - _file.close() - - _file = file(os.path.join(activityPath, 'activity.info'), 'w') - _file.write(activity_info_template(name)) - _file.close() - - _file = file(os.path.join(path, 'NEWS'), 'w') - _file.close() - - icon_path = os.path.join(os.path.dirname(__file__), 'activity', - 'activity-default.svg') - shutil.copy(icon_path, activityPath) + activity_path = os.path.join(path, 'activity') + os.mkdir(activity_path) + + # copy all the files in the skeleton directory + skeleton_path = os.path.join(activity.get_bundle_path(), 'skeletons', + skeleton) + for cur, dirs, files in os.walk(skeleton_path): + destination_path = os.path.join(path, cur[len(skeleton_path) + 1:]) + for directory in dirs: + directory_path = os.path.join(destination_path, directory) + try: + os.mkdir(directory_path) + except: + logging.error('Error trying to create %s', directory_path) + + for file_name in files: + shutil.copyfile(os.path.join(cur, file_name), + os.path.join(destination_path, file_name)) + + # create activity.info file + activity_info_path = os.path.join(activity_path, 'activity.info') + with open(activity_info_path, 'w') as activity_info_file: + activity_info_file.write(activity_info_template(name, + (skeleton == 'Web'))) return path |