[Sugar-devel] [PATCH] more robust badge handling

Walter Bender walter at sugarlabs.org
Wed Aug 24 10:47:52 EDT 2011


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

This augmentation to the cloning patch does a more robust job of adding
a badge to the cloned icon by encapslating the icon svg before adding the
badge.

---
 src/jarabe/view/customizebundle.py |   79 ++++++++++++++++++++---------------
 1 files changed, 45 insertions(+), 34 deletions(-)

diff --git a/src/jarabe/view/customizebundle.py b/src/jarabe/view/customizebundle.py
index c547ac0..b6fd440 100644
--- a/src/jarabe/view/customizebundle.py
+++ b/src/jarabe/view/customizebundle.py
@@ -32,6 +32,17 @@ _logger = logging.getLogger('ViewSource')
 
 BADGE_SUBPATH = 'emblems/emblem-view-source.svg'
 BADGE_TRANSFORM = '  <g transform="matrix(0.45,0,0,0.45,32,32)">\n'
+ICON_TRANSFORM = ' <g transform="matrix(1.0,0,0,1.0,0,0)">\n'
+XML_HEADER = '<?xml version="1.0" ?> \
+<!DOCTYPE svg  PUBLIC "-//W3C//DTD SVG 1.1//EN" \
+"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" [\n\
+<!ENTITY stroke_color "#010101">\n\
+<!ENTITY fill_color "#FFFFFF">\n]>\n'
+SVG_START = '<svg enable-background="new 0 0 55 55" height="55px" \
+version="1.1" viewBox="0 0 55 55" width="55px" x="0px" xml:space="preserve" \
+xmlns="http://www.w3.org/2000/svg" \
+xmlns:xlink="http://www.w3.org/1999/xlink" y="0px">\n'
+SVG_END = '</svg>\n'
 
 
 def generate_unique_id():
@@ -143,56 +154,56 @@ def _create_custom_icon(user_activities_path, new_basename, icon_name):
         _logger.debug('%s not found', BADGE_SUBPATH)
         return
 
-    # badge_payload is everything between <svg ...> and </svg>
     badge_fd = open(os.path.join(badge_path, 'sugar', 'scalable',
                                 BADGE_SUBPATH), 'r')
-    badge_payload = ''
-    looking_for_svg_token = True
-    looking_for_close_token = True
-    looking_for_end_svg_token = True
-    for line in badge_fd:
-        if looking_for_svg_token:
-            if line.find('<svg') < 0:
-                continue
-            looking_for_svg_token = False
-            line = line.split('<svg', 1)[1]
-        if looking_for_close_token:
-            if line.find('>') < 0:
-                continue
-            looking_for_close_token = False
-            line = line.split('>', 1)[1]
-        if looking_for_end_svg_token:
-            if line.find('</svg>') < 0:
-                badge_payload += line
-                continue
-            badge_payload += line.split('</svg>')[0]
-            break
+    badge_payload = _extract_svg_payload(badge_fd)
     badge_fd.close()
 
     badge_svg = BADGE_TRANSFORM + badge_payload + '\n</g>'
 
-    # Insert badge_svg just before </svg>
     icon_path = os.path.join(user_activities_path, new_basename, 'activity',
                              icon_name + '.svg')
     icon_fd = open(icon_path, 'r')
-
-    icon_svg = ''
-    looking_for_end_svg_token = True
-    for line in icon_fd:
-        if looking_for_end_svg_token:
-            if line.find('</svg>') < 0:
-                icon_svg += line
-                continue
-            icon_svg += line.split('</svg>')[0] + badge_svg + '\n</svg>\n'
-            break
-
+    icon_payload = _extract_svg_payload(icon_fd)
     icon_fd.close()
 
+    icon_svg = ICON_TRANSFORM + icon_payload + '\n</g>'
+
     tmp_path = os.path.join(user_activities_path, new_basename, 'activity',
                             'tmp.svg')
     tmp_icon_fd = open(tmp_path, 'w')
+    tmp_icon_fd.write(XML_HEADER)
+    tmp_icon_fd.write(SVG_START)
     tmp_icon_fd.write(icon_svg)
+    tmp_icon_fd.write(badge_svg)
+    tmp_icon_fd.write(SVG_END)
     tmp_icon_fd.close()
 
     os.remove(icon_path)
     os.rename(tmp_path, icon_path)
+
+
+def _extract_svg_payload(fd):
+    """ Returns everything between <svg ...> and </svg> """
+    payload = ''
+    looking_for_start_svg_token = True
+    looking_for_close_token = True
+    looking_for_end_svg_token = True
+    for line in fd:
+        if looking_for_start_svg_token:
+            if line.find('<svg') < 0:
+                continue
+            looking_for_start_svg_token = False
+            line = line.split('<svg', 1)[1]
+        if looking_for_close_token:
+            if line.find('>') < 0:
+                continue
+            looking_for_close_token = False
+            line = line.split('>', 1)[1]
+        if looking_for_end_svg_token:
+            if line.find('</svg>') < 0:
+                payload += line
+                continue
+            payload += line.split('</svg>')[0]
+            break
+    return payload
-- 
1.7.4.4



More information about the Sugar-devel mailing list