Colibre: tdf#146410 update Clone Formatting icons
[LibreOffice.git] / solenv / bin / image-sort.py
blob75b5da6ce0e7541f9ff48d58a63dce5197afb51f
1 # -*- Mode: Python; tab-width: 4; indent-tabs-mode: nil -*-
3 # This file is part of the LibreOffice project.
5 # This Source Code Form is subject to the terms of the Mozilla Public
6 # License, v. 2.0. If a copy of the MPL was not distributed with this
7 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 # This file incorporates work covered by the following license notice:
11 # Licensed to the Apache Software Foundation (ASF) under one or more
12 # contributor license agreements. See the NOTICE file distributed
13 # with this work for additional information regarding copyright
14 # ownership. The ASF licenses this file to you under the Apache
15 # License, Version 2.0 (the "License"); you may not use this file
16 # except in compliance with the License. You may obtain a copy of
17 # the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 import sys, os, re
21 import argparse
23 global_list = []
24 global_hash = {}
25 args = None
27 def read_icons(fname):
28 global args
29 images = []
30 full_path = os.path.join(args.base_path, fname)
31 if not os.path.exists(full_path):
32 if not args.quiet:
33 print("Skipping non-existent {}\n".format(full_path), file=sys.stderr)
34 return images
35 with open(full_path) as fp:
36 for line in fp:
37 m = re.search(r'xlink:href="\.uno:(\S+)"\s+', line)
38 if m:
39 images.append(m.group(1).lower())
40 return images
42 # filter out already seen icons & do prefixing
43 def read_new_icons(fname, prefix):
44 images = read_icons(fname)
45 new_icons_arr = []
46 new_icons_d = {}
47 for icon in images:
48 iname = "cmd/" + prefix + icon + ".png"
49 if iname not in global_hash and \
50 iname not in new_icons_d:
51 new_icons_arr.append(iname)
52 new_icons_d[iname] = 1
53 return new_icons_arr
55 def process_group(prefix, uiconfigs):
56 global global_list, global_hash
57 group = {}
58 cur_max = 1.0
60 # a very noddy sorting algorithm
61 for uiconfig in uiconfigs:
62 images = read_new_icons(uiconfig, prefix)
63 prev = ''
64 for icon in images:
65 if icon not in group:
66 if prev not in group:
67 group[icon] = cur_max
68 cur_max += 1.0
69 else:
70 group[icon] = group[prev] + (1.0 - 0.5 / cur_max)
71 def intvalue(i):
72 return group[i]
73 for icon in sorted(group.keys(), key=intvalue):
74 global_list.append(icon)
75 global_hash[icon] = 1
77 def process_file(fname, prefix):
78 global global_list, global_hash
79 images = read_new_icons(fname, prefix)
81 for icon in images:
82 global_list.append(icon)
83 global_hash[icon] = 1
85 def chew_controlfile(ifile):
86 global global_list, global_hash
87 filelist = []
88 for line in ifile:
89 line = line.strip()
90 if line.startswith('#'):
91 continue
92 if not line:
93 continue
95 m = re.match(r'-- (\S+)\s*', line)
96 if m:
97 # control code
98 code = m.group(1)
99 small = line.lower().endswith(' small')
100 if code.lower() == 'group':
101 if not small:
102 process_group("lc_", filelist)
103 process_group ("sc_", filelist)
104 elif code.lower() == 'ordered':
105 if not small:
106 for f in filelist:
107 process_file(f, "lc_")
108 for f in filelist:
109 process_file(f, "sc_")
110 elif code.lower() == 'literal':
111 for f in filelist:
112 if f not in global_hash:
113 global_list.append(f)
114 global_hash[f] = 1
115 else:
116 sys.exit("Unknown code '{}'".format(code))
117 filelist = []
118 else:
119 filelist.append(line)
121 parser = argparse.ArgumentParser()
122 # where the control file lives
123 parser.add_argument('control_file', metavar='image-sort.lst',
124 help='the sort control file')
125 # where the uiconfigs live
126 parser.add_argument('base_path', metavar='directory',
127 help='path to the UIConfigs directory')
128 parser.add_argument('output', metavar='output file', type=argparse.FileType('w'),
129 nargs='?', default=None, help='optionally write to this output file')
130 parser.add_argument("-q", "--quiet", action="store_true",
131 help="don't print status messages to stdout")
133 args = parser.parse_args()
135 if args.output is not None:
136 close_output = True
137 else:
138 args.output = sys.stdout
139 close_output = False
141 with open(args.control_file) as cf:
142 chew_controlfile(cf)
144 for icon in global_list:
145 if not icon.startswith('sc_'):
146 args.output.write(icon + "\n")
148 for icon in global_list:
149 if icon.startswith('sc_'):
150 args.output.write(icon + "\n")
152 if close_output:
153 args.output.close()
155 # dnl vim:set shiftwidth=4 softtabstop=4 expandtab: