summaryrefslogtreecommitdiff
path: root/solenv/bin/image-sort.py
blob: 5d248684b4a6f2302434f0ae1c09b44a6db04df9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# -*- Mode: Python; tab-width: 4; indent-tabs-mode: nil -*-
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This file incorporates work covered by the following license notice:
#
#   Licensed to the Apache Software Foundation (ASF) under one or more
#   contributor license agreements. See the NOTICE file distributed
#   with this work for additional information regarding copyright
#   ownership. The ASF licenses this file to you under the Apache
#   License, Version 2.0 (the "License"); you may not use this file
#   except in compliance with the License. You may obtain a copy of
#   the License at http://www.apache.org/licenses/LICENSE-2.0 .
#

import sys, os, re
import argparse

global_list = []
global_hash = {}
args = None

def read_icons(fname):
    global args
    images = []
    full_path = os.path.join(args.base_path, fname)
    if not os.path.exists(full_path):
        if not args.quiet:
            print("Skipping non-existent {}".format(full_path), file=sys.stderr)
        return images
    with open(full_path) as fp:
        for line in fp:
            m = re.search(r'xlink:href="\.uno:(\S+)"\s+', line)
            if m:
                images.append(m.group(1).lower())
    return images

# filter out already seen icons & do prefixing
def read_new_icons(fname, prefix):
    images = read_icons(fname)
    new_icons_arr = []
    new_icons_d = {}
    for icon in images:
        iname = "cmd/" + prefix + icon + ".png"
        if iname not in global_hash and \
            iname not in new_icons_d:
            new_icons_arr.append(iname)
            new_icons_d[iname] = 1
    return new_icons_arr

def process_group(prefix, uiconfigs):
    global global_list, global_hash
    group = {}
    cur_max = 1.0

    # a very noddy sorting algorithm
    for uiconfig in uiconfigs:
        images = read_new_icons(uiconfig, prefix)
        prev = ''
        for icon in images:
            if icon not in group:
                if prev not in group:
                    group[icon] = cur_max
                    cur_max += 1.0
                else:
                    group[icon] = group[prev] + (1.0 - 0.5 / cur_max)
    def intvalue(i):
        return group[i]
    for icon in sorted(group.keys(), key=intvalue):
        global_list.append(icon)
        global_hash[icon] = 1

def process_file(fname, prefix):
    global global_list, global_hash
    images = read_new_icons(fname, prefix)

    for icon in images:
        global_list.append(icon)
        global_hash[icon] = 1

def chew_controlfile(ifile):
    global global_list, global_hash
    filelist = []
    for line in ifile:
        line = line.strip()
        if line.startswith('#'):
            continue
        if not line:
            continue

        m = re.match(r'-- (\S+)\s*', line)
        if m:
            # control code
            code = m.group(1)
            small = line.lower().endswith(' small')
            if code.lower() == 'group':
                if not small:
                    process_group("lc_", filelist)
                process_group ("sc_", filelist)
            elif code.lower() == 'ordered':
                if not small:
                    for f in filelist:
                        process_file(f, "lc_")
                for f in filelist:
                    process_file(f, "sc_")
            elif code.lower() == 'literal':
                for f in filelist:
                    if f not in global_hash:
                        global_list.append(f)
                        global_hash[f] = 1
            else:
                sys.exit("Unknown code '{}'".format(code))
            filelist = []
        else:
            filelist.append(line)

parser = argparse.ArgumentParser()
# where the control file lives
parser.add_argument('control_file', metavar='image-sort.lst',
                    help='the sort control file')
# where the uiconfigs live
parser.add_argument('base_path', metavar='directory',
                    help='path to the UIConfigs directory')
parser.add_argument('output', metavar='output file', type=argparse.FileType('w'),
                    nargs='?', default=None, help='optionally write to this output file')
parser.add_argument("-q", "--quiet", action="store_true",
                    help="don't print status messages to stdout")

args = parser.parse_args()

if args.output is not None:
    close_output = True
else:
    args.output = sys.stdout
    close_output = False

with open(args.control_file) as cf:
    chew_controlfile(cf)

for icon in global_list:
    if not icon.startswith('sc_'):
        args.output.write(icon + "\n")

for icon in global_list:
    if icon.startswith('sc_'):
        args.output.write(icon + "\n")

if close_output:
    args.output.close()

# dnl vim:set shiftwidth=4 softtabstop=4 expandtab: