Mark some Calc slots as inactive in readonly mode
[LibreOffice.git] / solenv / bin / image-sort.py
blob43a26dc5468dd9c8914ce1f689779565607c69a3
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
21 import os
22 import re
23 import argparse
25 global_list = []
26 global_hash = {}
27 args = None
29 def read_icons(fname):
30 global args
31 images = []
32 full_path = os.path.join(args.base_path, fname)
33 if not os.path.exists(full_path):
34 if not args.quiet:
35 print("Skipping non-existent {}".format(full_path), file=sys.stderr)
36 return images
37 with open(full_path) as fp:
38 for line in fp:
39 m = re.search(r'xlink:href="\.uno:(\S+)"\s+', line)
40 if m:
41 images.append(m.group(1).lower())
42 return images
44 # filter out already seen icons & do prefixing
45 def read_new_icons(fname, prefix):
46 images = read_icons(fname)
47 new_icons_arr = []
48 new_icons_d = {}
49 for icon in images:
50 iname = "cmd/" + prefix + icon + ".png"
51 if iname not in global_hash and \
52 iname not in new_icons_d:
53 new_icons_arr.append(iname)
54 new_icons_d[iname] = 1
55 return new_icons_arr
57 def process_group(prefix, uiconfigs):
58 global global_list, global_hash
59 group = {}
60 cur_max = 1.0
62 # a very noddy sorting algorithm
63 for uiconfig in uiconfigs:
64 images = read_new_icons(uiconfig, prefix)
65 prev = ''
66 for icon in images:
67 if icon not in group:
68 if prev not in group:
69 group[icon] = cur_max
70 cur_max += 1.0
71 else:
72 group[icon] = group[prev] + (1.0 - 0.5 / cur_max)
73 def intvalue(i):
74 return group[i]
75 for icon in sorted(group.keys(), key=intvalue):
76 global_list.append(icon)
77 global_hash[icon] = 1
79 def process_file(fname, prefix):
80 global global_list, global_hash
81 images = read_new_icons(fname, prefix)
83 for icon in images:
84 global_list.append(icon)
85 global_hash[icon] = 1
87 def chew_controlfile(ifile):
88 global global_list, global_hash
89 filelist = []
90 for line in ifile:
91 line = line.strip()
92 if line.startswith('#'):
93 continue
94 if not line:
95 continue
97 m = re.match(r'-- (\S+)\s*', line)
98 if m:
99 # control code
100 code = m.group(1)
101 small = line.lower().endswith(' small')
102 if code.lower() == 'group':
103 if not small:
104 process_group("lc_", filelist)
105 process_group ("sc_", filelist)
106 elif code.lower() == 'ordered':
107 if not small:
108 for f in filelist:
109 process_file(f, "lc_")
110 for f in filelist:
111 process_file(f, "sc_")
112 elif code.lower() == 'literal':
113 for f in filelist:
114 if f not in global_hash:
115 global_list.append(f)
116 global_hash[f] = 1
117 else:
118 sys.exit("Unknown code '{}'".format(code))
119 filelist = []
120 else:
121 filelist.append(line)
123 parser = argparse.ArgumentParser()
124 # where the control file lives
125 parser.add_argument('control_file', metavar='image-sort.lst',
126 help='the sort control file')
127 # where the uiconfigs live
128 parser.add_argument('base_path', metavar='directory',
129 help='path to the UIConfigs directory')
130 parser.add_argument('output', metavar='output file', type=argparse.FileType('w'),
131 nargs='?', default=None, help='optionally write to this output file')
132 parser.add_argument("-q", "--quiet", action="store_true",
133 help="don't print status messages to stdout")
135 args = parser.parse_args()
137 if args.output is not None:
138 close_output = True
139 else:
140 args.output = sys.stdout
141 close_output = False
143 with open(args.control_file) as cf:
144 chew_controlfile(cf)
146 for icon in global_list:
147 if not icon.startswith('sc_'):
148 args.output.write(icon + "\n")
150 for icon in global_list:
151 if icon.startswith('sc_'):
152 args.output.write(icon + "\n")
154 if close_output:
155 args.output.close()
157 # dnl vim:set shiftwidth=4 softtabstop=4 expandtab: