[Sugar-devel] [PATCH] use nick for everything but bundle_id

Walter Bender walter at sugarlabs.org
Fri Aug 26 17:10:08 EDT 2011


From: Walter Bender <walter.bender at gmail.com>

This patch is a reworking of how the cloned activity and related files are
named. The 'unique id' based on an alphanumeric version of the user's nick
and a hash is only used for the bundle_id. The new activity name is simply:
nick + _('copy of') + original activity name. The directory into which it is
created is the same name stripped of all of its spaces. The .xo bundle also
uses that name. The result is that the name is more readily parsed (and
understood) by the end user and the bundle id remains unique in order to avoid
name-space collisions when users with the same nick exchange activities.

Tested with English and Spanish.

---
 src/jarabe/view/customizebundle.py |   43 +++++++++++++++++++++--------------
 src/jarabe/view/viewsource.py      |    7 ++---
 2 files changed, 29 insertions(+), 21 deletions(-)

diff --git a/src/jarabe/view/customizebundle.py b/src/jarabe/view/customizebundle.py
index 42da1b3..7a196de 100644
--- a/src/jarabe/view/customizebundle.py
+++ b/src/jarabe/view/customizebundle.py
@@ -25,6 +25,7 @@ from sugar.activity import bundlebuilder
 from sugar.datastore import datastore
 from sugar.env import get_user_activities_path
 
+from gettext import gettext as _
 import logging
 _logger = logging.getLogger('ViewSource')
 
@@ -44,7 +45,7 @@ xmlns:xlink="http://www.w3.org/1999/xlink" y="0px">\n'
 SVG_END = '</svg>\n'
 
 
-def generate_unique_id():
+def _generate_unique_id():
     """Generate an id based on the user's nick name and their public key
     (Based on schema used by IRC activity).
 
@@ -63,13 +64,21 @@ def generate_unique_id():
     return nick_letters + '_' + hexhash[:4]
 
 
-def generate_bundle(nick, new_basename):
+def generate_new_activity_name(activity_name):
+    """Generate an activity name based on the user name and the current
+    activity name.
+
+    """
+    nick = profile.get_nick_name()
+    return '%s %s %s' % (nick, _('copy of'), activity_name)
+
+
+def generate_bundle(new_basename):
     """Generate a new .xo bundle for the activity and copy it into the
     Journal.
 
     """
-    new_activity_name = _customize_activity_info(
-        nick, new_basename)
+    new_bundle_name = _customize_activity_info(new_basename)
 
     user_activities_path = get_user_activities_path()
     if os.path.exists(os.path.join(user_activities_path, new_basename,
@@ -80,28 +89,28 @@ def generate_bundle(nick, new_basename):
 
     config = bundlebuilder.Config(source_dir=os.path.join(
             user_activities_path, new_basename),
-            dist_name='%s-1.xo' % (new_activity_name))
+            dist_name='%s-1.xo' % (new_bundle_name))
     bundlebuilder.cmd_dist_xo(config, None)
 
     dsobject = datastore.create()
-    dsobject.metadata['title'] = '%s-1.xo' % (new_activity_name)
+    dsobject.metadata['title'] = '%s-1.xo' % (new_bundle_name)
     dsobject.metadata['mime_type'] = 'application/vnd.olpc-sugar'
     dsobject.set_file_path(os.path.join(
             user_activities_path, new_basename, 'dist',
-            '%s-1.xo' % (new_activity_name)))
+            '%s-1.xo' % (new_bundle_name)))
     datastore.write(dsobject)
     dsobject.destroy()
 
 
-def _customize_activity_info(nick, new_basename):
+def _customize_activity_info(new_basename):
     """Modify bundle_id in new activity.info file:
-    (1) change the bundle_id to bundle_id_[NICKNAME];
-    (2) change the activity_icon [NICKNAME]-activity-icon.svg;
-    (3) set activity_version to 1;
-    (4) modify the activity icon by applying a customize overlay.
+    (0) change the activity name to [nick] copy of [activity name];
+    (1) change the bundle_id to bundle_id_[unique name];
+    (2) set activity_version to 1;
+    (3) modify the activity icon by applying a customize overlay.
 
     """
-    new_activity_name = ''
+    new_bundle_name = ''
     user_activities_path = get_user_activities_path()
 
     info_old = open(os.path.join(user_activities_path, new_basename,
@@ -115,15 +124,15 @@ def _customize_activity_info(nick, new_basename):
             continue
         name, value = [token.strip() for token in line.split('=', 1)]
         if name == 'bundle_id':
-            new_value = '%s_%s' % (value, nick)
+            new_value = '%s_%s' % (value, _generate_unique_id())
         elif name == 'activity_version':
             new_value = '1'
         elif name == 'icon':
             new_value = value
             icon_name = value
         elif name == 'name':
-            new_value = '%s_copy_of_%s' % (nick, value)
-            new_activity_name = new_value
+            new_value = generate_new_activity_name(value)
+            new_bundle_name = new_value.replace(' ', '')
         else:
             info_new.write(line)
             continue
@@ -140,7 +149,7 @@ def _customize_activity_info(nick, new_basename):
 
     _create_custom_icon(new_basename, icon_name)
 
-    return new_activity_name
+    return new_bundle_name
 
 
 def _create_custom_icon(new_basename, icon_name):
diff --git a/src/jarabe/view/viewsource.py b/src/jarabe/view/viewsource.py
index 4ec47eb..63ede32 100644
--- a/src/jarabe/view/viewsource.py
+++ b/src/jarabe/view/viewsource.py
@@ -291,15 +291,14 @@ class DocumentButton(RadioToolButton):
     def __copy_to_home_cb(self, menu_item):
         """Make a local copy of the activity bundle in user_activities_path"""
         user_activities_path = get_user_activities_path()
-        nick = customizebundle.generate_unique_id()
-        new_basename = '%s_copy_of_%s' % (
-            nick, os.path.basename(self._document_path))
+        new_basename = customizebundle.generate_new_activity_name(
+            os.path.basename(self._document_path)).replace(' ', '')
         if not os.path.exists(os.path.join(user_activities_path,
                                            new_basename)):
             shutil.copytree(self._document_path,
                             os.path.join(user_activities_path, new_basename),
                             symlinks=True)
-            customizebundle.generate_bundle(nick, new_basename)
+            customizebundle.generate_bundle(new_basename)
         else:
             _logger.debug('%s already exists', new_basename)
 
-- 
1.7.4.4



More information about the Sugar-devel mailing list