Maroloccio has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/277057

Change subject: scripts/convert-icons.py clean-up
......................................................................

scripts/convert-icons.py clean-up

Change-Id: I290f233a0bb9cc49d793f63f3401adef87dbb892
---
M scripts/convert-icons.py
1 file changed, 100 insertions(+), 81 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/apps/android/wikipedia 
refs/changes/57/277057/1

diff --git a/scripts/convert-icons.py b/scripts/convert-icons.py
index eb8a6bd..3da020b 100755
--- a/scripts/convert-icons.py
+++ b/scripts/convert-icons.py
@@ -2,95 +2,114 @@
 import os
 import sh
 import sys
+import re
+import glob
+import inspect
 
-from glob import glob
+TRACE = True
 
-DENSITIES = {
-    "mdpi": 1,
-    "hdpi": 1.5,
-    "xhdpi": 2,
-    "xxhdpi": 3,
-    "xxxhdpi": 4
-}
+DENSITIES = {'mdpi': 1,
+             'hdpi': 1.5,
+             'xhdpi': 2,
+             'xxhdpi': 3,
+             'xxxhdpi': 4 }
 
-OUTPUT_PATH_PREFIX = os.path.abspath(os.path.join(os.path.dirname(__file__), 
"../app/src/main/res/"))
+OUTPUT_PATH_PREFIX = os.path.abspath(os.path.join(os.path.dirname(__file__),
+                                                  '../app/src/main/res/'))
 
 
-class ImagesBatch(object):
-    def __init__(self, path, filters):
-        self.dp = int(os.path.basename(path))
-        self.path = path
-        self.svgs = []
-        all_svgs = self.find_svg_files(path)
-        filtered_svgs = self.filter_filenames(all_svgs, filters)
-        self.svgs = self.abspath(filtered_svgs)
+def trace_ident(ident):
+    if not TRACE:
+        return
+    value = inspect.currentframe().f_back.f_locals[ident]
+    value_type = type(value)
+    if value_type in set([int, str]):
+        print('{}: {}'.format(ident, value))
+    elif value_type in set([list, set]) and value:
+        print(ident + ':')
+        print('\n'.join('- ' + x for x in value))
 
-    @staticmethod
-    def find_svg_files(path):
-        return [p for p in glob(os.path.join(path, "*.svg"))]
 
-    @staticmethod
-    def filter_filenames(all_svg_files, filters=None):
-        relative_svg_files = []
-        if filters:
-            for filter in filters:
-                if os.path.join(source_path, filter) in all_svg_files:
-                    relative_svg_files.append(os.path.join(source_path, 
filter))
+def validate_filter_argument(filter_argument):
+    if not filter_argument.endswith('.svg') or os.sep in filter_argument:
+        raise SystemExit('Only svg file names allowed in arguments.')
+    return filter_argument
+
+
+def export(dp, density, filepath, drawable):
+    noflip = '.nonspecific.' in filepath or filepath.endswith('.noflip.svg')
+    filename = os.path.basename(filepath)
+    new_filename = filename[:filename.index('.')] + '.png'
+    folder_path = os.path.join(OUTPUT_PATH_PREFIX, drawable)
+    sh.mkdir('-p', folder_path)
+    output_file_path = os.path.join(folder_path, filename)
+    output_precrush_path = output_file_path + '_'
+    px = int(DENSITIES[density] * dp)
+    sh.rsvg_convert(filepath, '-a', h=px, o=output_precrush_path)
+    sh.pngcrush('-q', '-reduce', output_precrush_path, output_file_path)
+    sh.rm(output_precrush_path)
+    return output_file_path, noflip
+
+
+def flop(density, (filepath, noflip)):
+    if noflip:
+        return
+    folder_name = os.path.join(OUTPUT_PATH_PREFIX, 'drawable-ldrtl-' + density)
+    output_file_path = os.path.join(folder_name, os.path.basename(filepath))
+    sh.mkdir('-p', folder_name)
+    sh.convert(filepath, '-flop', output_file_path)
+
+
+def convert(icon_path, svg_filters):
+    print('\n* icon_path: {}'.format(icon_path))
+
+    dp = int(os.path.basename(icon_path))
+    trace_ident('dp')
+
+    svg_glob = glob.glob(os.path.join(icon_path, '*.svg'))
+    svg_files = ([x for x in svg_glob if os.path.basename(x) in svg_filters]
+                 if svg_filters else svg_glob)
+
+    if not svg_files:
+        return
+    print('converted:')
+    for svg_file in svg_files:
+        if '.nonspecific.' in svg_file:
+            export(dp, 'xxxhdpi', svg_file, 'drawable')
         else:
-            relative_svg_files = all_svg_files
-        return relative_svg_files
-
-    @staticmethod
-    def abspath(filenames):
-        output = []
-        for filename in filenames:
-            output.append(os.path.abspath(filename))
-        return output
-
-    def _do_export(self, density, input_path, drawable):
-        nonspecific = ".nonspecific." in input_path
-        noflip = nonspecific or input_path.endswith(".noflip.svg")
-        file_name = 
os.path.basename(os.path.splitext(input_path)[0].split(".")[0] + ".png")
-        folder_path = os.path.join(OUTPUT_PATH_PREFIX, drawable)
-        sh.mkdir("-p", folder_path)
-        output_file_path = os.path.join(folder_path, file_name)
-        output_precrush_path = output_file_path + "_"
-        px = int(DENSITIES[density] * self.dp)
-        sh.rsvg_convert(input_path, "-a", h=px, o=output_precrush_path)
-        sh.pngcrush("-q", "-reduce", output_precrush_path, output_file_path)
-        sh.rm(output_precrush_path)
-        return output_file_path, noflip
-
-    def _do_flop(self, density, (input_path, noflip)):
-        if noflip:
-            return
-        folder_name = os.path.join(OUTPUT_PATH_PREFIX, "drawable-ldrtl-" + 
density)
-        output_file_path = os.path.join(folder_name, 
os.path.basename(input_path))
-        sh.mkdir("-p", folder_name)
-        sh.convert(input_path, "-flop", output_file_path)
-
-    def convert(self):
-        for svg in self.svgs:
-            if ".nonspecific." in svg:
-                self._do_export("xxxhdpi", svg, "drawable")
-            else:
-                for density in DENSITIES.keys():
-                    self._do_flop(density, self._do_export(density, svg, 
"drawable-" + density))
-            print(u"\u2713 %s" % os.path.basename(svg))
+            for density in DENSITIES:
+                flop(density,
+                     export(dp, density, svg_file, 'drawable-' + density))
+        print('+ {}'.format(svg_file))
 
 
-def validate_filters(filter_set):
-    for filter in filter_set:
-        if not filter.endswith(".svg") or "/" in filter:
-            print >> sys.stderr, 'Only svg file names allowed in arguments.'
-            sys.exit(-1)
-    return filter_set
+def main():
+    executed_via = os.path.abspath(__file__)
+    trace_ident('executed_via')
 
-if __name__ == "__main__":
-    svg_filters = None
-    if len(sys.argv) > 1:
-        svg_filters = validate_filters(set(sys.argv[1:]))
-    source_density_paths = glob(os.path.join(os.path.dirname(__file__), 
"../icon-svgs/*"))
-    for source_path in source_density_paths:
-        ib = ImagesBatch(source_path, svg_filters)
-        ib.convert()
+    script_dir = os.path.dirname(executed_via)
+    trace_ident('script_dir')
+
+    icons_dir = os.path.abspath(
+        os.path.join(script_dir, os.pardir, 'icon-svgs'))
+    trace_ident('icons_dir')
+
+    svg_filters = set(validate_filter_argument(x) for x in sys.argv[1:])
+    trace_ident('svg_filters')
+
+    is_a_size = lambda name: re.match(r'[1-9]\d*', name)
+    source_densities = list()
+    for name in os.listdir(icons_dir):
+        if not is_a_size(name):
+            continue
+        icon_path = os.path.join(icons_dir, name)
+        if os.path.isdir(icon_path):
+            source_densities.append(icon_path)
+    trace_ident('source_densities')
+
+    for icon_path in source_densities:
+        convert(icon_path, svg_filters)
+
+
+if __name__ == '__main__':
+    main()

-- 
To view, visit https://gerrit.wikimedia.org/r/277057
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I290f233a0bb9cc49d793f63f3401adef87dbb892
Gerrit-PatchSet: 1
Gerrit-Project: apps/android/wikipedia
Gerrit-Branch: master
Gerrit-Owner: Maroloccio <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to