[gnome-icon-theme-extras] Port to python3 and sync from gnome-icon-theme.



commit 4f231d921ebfa6ccf13e7620343b52bbe9ecbc47
Author: Benjamin Berg <benjamin sipsolutions net>
Date:   Mon Feb 25 14:58:52 2013 +0100

    Port to python3 and sync from gnome-icon-theme.
    
    The script runs fine on python3 now. This also adds features
    that were done in gnome-icon-theme but were not copied over.

 render-icon-theme.py |   44 ++++++++++++++++++++++++++++++++------------
 1 files changed, 32 insertions(+), 12 deletions(-)
---
diff --git a/render-icon-theme.py b/render-icon-theme.py
index c746d9b..9473d0a 100755
--- a/render-icon-theme.py
+++ b/render-icon-theme.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/python3
 
 import os
 import sys
@@ -6,20 +6,30 @@ import xml.sax
 import subprocess
 
 INKSCAPE = '/usr/bin/inkscape'
+OPTIPNG = '/usr/bin/optipng'
 SRC = os.path.join('.', 'src')
 
 inkscape_process = None
 
+def optimize_png(png_file):
+    if os.path.exists(OPTIPNG):
+        process = subprocess.Popen([OPTIPNG, '-quiet', '-o7', png_file])
+        process.wait()
+
 def wait_for_prompt(process, command=None):
     if command is not None:
-        process.stdin.write(command+'\n')
+        process.stdin.write((command+'\n').encode('utf-8'))
 
+    # This is kinda ugly ...
+    # Wait for just a '>', or '\n>' if some other char appearead first
     output = process.stdout.read(1)
+    if output == b'>':
+        return
+
     output += process.stdout.read(1)
-    
-    while output != "\n>":
-        output = output[-1:]
+    while output != b'\n>':
         output += process.stdout.read(1)
+        output = output[1:]
 
 def start_inkscape():
     process = subprocess.Popen([INKSCAPE, '--shell'], bufsize=0, stdin=subprocess.PIPE, 
stdout=subprocess.PIPE)
@@ -31,6 +41,7 @@ def inkscape_render_rect(icon_file, rect, output_file):
     if inkscape_process is None:
         inkscape_process = start_inkscape()
     wait_for_prompt(inkscape_process, '%s -i %s -e %s' % (icon_file, rect, output_file))
+    optimize_png(output_file)
 
 class ContentHandler(xml.sax.ContentHandler):
     ROOT = 0
@@ -38,7 +49,7 @@ class ContentHandler(xml.sax.ContentHandler):
     LAYER = 2
     OTHER = 3
     TEXT = 4
-    def __init__(self, path, force=False):
+    def __init__(self, path, force=False, filter=None):
         self.stack = [self.ROOT]
         self.inside = [self.ROOT]
         self.path = path
@@ -46,6 +57,7 @@ class ContentHandler(xml.sax.ContentHandler):
         self.state = self.ROOT
         self.chars = ""
         self.force = force
+        self.filter = filter
 
     def endDocument(self):
         pass
@@ -57,8 +69,8 @@ class ContentHandler(xml.sax.ContentHandler):
                 self.inside.append(self.SVG)
                 return
         elif self.inside[-1] == self.SVG:
-           if (name == "g" and ('inkscape:groupmode' in attrs) and ('inkscape:label' in attrs)
-               and attrs['inkscape:groupmode'] == 'layer' and attrs['inkscape:label'] == 'baseplate'):
+            if (name == "g" and ('inkscape:groupmode' in attrs) and ('inkscape:label' in attrs)
+               and attrs['inkscape:groupmode'] == 'layer' and 
attrs['inkscape:label'].startswith('baseplate')):
                 self.stack.append(self.LAYER)
                 self.inside.append(self.LAYER)
                 self.context = None
@@ -99,7 +111,11 @@ class ContentHandler(xml.sax.ContentHandler):
         elif stacked == self.LAYER:
             assert self.icon_name
             assert self.context
-            print (self.context, ' ', self.icon_name)
+
+            if self.filter is not None and not self.icon_name in self.filter:
+                return
+
+            print (self.context, self.icon_name)
             for rect in self.rects:
                 width = rect['width']
                 height = rect['height']
@@ -131,7 +147,7 @@ class ContentHandler(xml.sax.ContentHandler):
 if len(sys.argv) == 1:
     if not os.path.exists('gnome'):
         os.mkdir('gnome')
-    print ('Rendering from SVGs in ', SRC)
+    print ('Rendering from SVGs in', SRC)
     for file in os.listdir(SRC):
         if file[-4:] == '.svg':
             file = os.path.join(SRC, file)
@@ -139,11 +155,15 @@ if len(sys.argv) == 1:
             xml.sax.parse(open(file), handler)
 else:
     file = os.path.join(SRC, sys.argv[1] + '.svg')
+    if len(sys.argv) > 2:
+        icons = sys.argv[2:]
+    else:
+        icons = None
     if os.path.exists(os.path.join(file)):
-        handler = ContentHandler(file, True)
+        handler = ContentHandler(file, True, filter=icons)
         xml.sax.parse(open(file), handler)
     else:
-        print ("Error: No such file ", file)
+        print ("Error: No such file", file)
         sys.exit(1)
 
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]