|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# update-css-files.py creates color analyse files in css/colors and updates the |
| 4 | +# `<colors></colors>` section in all css files. |
| 5 | +# |
| 6 | +# Copyright (c) 2020 The Dash Core developers |
| 7 | +# Distributed under the MIT/X11 software license, see the accompanying |
| 8 | +# file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 9 | + |
| 10 | +from pathlib import Path |
| 11 | +import re |
| 12 | +import subprocess |
| 13 | +import sys |
| 14 | + |
| 15 | +MATCH_REPLACE = '<colors>.+?</colors>' |
| 16 | +MATCH_COLORS = '#(?:[0-9a-fA-F]{2}){2,4}|#(?:[0-9a-f]{1}){3}' |
| 17 | + |
| 18 | +def error(msg): |
| 19 | + exit('\nERROR: {}\n'.format(msg)) |
| 20 | + |
| 21 | +def parse_css(file_css): |
| 22 | + # Temporarily |
| 23 | + state = 0 |
| 24 | + selectors = [] |
| 25 | + |
| 26 | + # Results |
| 27 | + by_attribute = {} |
| 28 | + by_color = {} |
| 29 | + |
| 30 | + for line in file_css.read_text().splitlines(): |
| 31 | + |
| 32 | + if line == '': |
| 33 | + continue |
| 34 | + |
| 35 | + # start of a comment |
| 36 | + if state == 0 and line.startswith('/*'): |
| 37 | + if '*/' in line: |
| 38 | + state = 0 |
| 39 | + else: |
| 40 | + state = 1 |
| 41 | + # we are in a comment section |
| 42 | + elif state == 1: |
| 43 | + # end of the comment |
| 44 | + if '*/' in line: |
| 45 | + state = 0 |
| 46 | + else: |
| 47 | + continue |
| 48 | + # first line of multiple selector |
| 49 | + elif (state == 0 or state == 2) and ',' in line: |
| 50 | + state = 2 |
| 51 | + # first line of single selector or end of multiple |
| 52 | + elif (state == 0 or state == 2) and '{' in line: |
| 53 | + state = 3 |
| 54 | + # end of element |
| 55 | + elif state == 4 and line == '}': |
| 56 | + state = 0 |
| 57 | + |
| 58 | + if state == 0 and len(selectors): |
| 59 | + selectors = [] |
| 60 | + |
| 61 | + if state == 2: |
| 62 | + selector = line.split(",")[0].strip(' ') |
| 63 | + selectors.append(selector) |
| 64 | + |
| 65 | + if state == 3: |
| 66 | + selector = line.split("{")[0].strip(' ') |
| 67 | + selectors.append(selector) |
| 68 | + state = 4 |
| 69 | + continue |
| 70 | + |
| 71 | + if state == 4: |
| 72 | + matched_colors = re.findall(MATCH_COLORS, line) |
| 73 | + |
| 74 | + if len(matched_colors) > 1: |
| 75 | + error("Multiple colors in a line.\n\n {}\n\nSeems to be an invalid file!".format(line)) |
| 76 | + elif len(matched_colors) == 1: |
| 77 | + matched_color = matched_colors[0] |
| 78 | + element = line.split(":")[0].strip(' ') |
| 79 | + |
| 80 | + if not matched_color in by_color: |
| 81 | + by_color[matched_color] = [] |
| 82 | + |
| 83 | + by_color[matched_color].append(element) |
| 84 | + |
| 85 | + entry = element + " " + matched_color |
| 86 | + |
| 87 | + if not entry in by_attribute: |
| 88 | + by_attribute[entry] = [] |
| 89 | + |
| 90 | + by_attribute[entry].extend(selectors) |
| 91 | + |
| 92 | + def sort_color(color): |
| 93 | + tmp = color[0].replace('#', '0x') |
| 94 | + return int(tmp, 0) |
| 95 | + |
| 96 | + def remove_duplicates(l): |
| 97 | + no_duplicates = [] |
| 98 | + [no_duplicates.append(i) for i in l if not no_duplicates.count(i)] |
| 99 | + return no_duplicates |
| 100 | + |
| 101 | + colors = [] |
| 102 | + |
| 103 | + # sort colors just by hex value |
| 104 | + if len(by_color): |
| 105 | + colors = sorted(by_color.items(), key=lambda x: sort_color(x)) |
| 106 | + |
| 107 | + for k, l in by_attribute.items(): |
| 108 | + by_attribute[k] = remove_duplicates(l) |
| 109 | + |
| 110 | + for k, l in by_color.items(): |
| 111 | + by_color[k] = remove_duplicates(l) |
| 112 | + |
| 113 | + return {'fileName': file_css.stem, 'byAttribute': by_attribute, 'byColor': by_color, 'colors': colors} |
| 114 | + |
| 115 | + |
| 116 | +def create_color_file(content, commit): |
| 117 | + |
| 118 | + str_result = "Color analyse of " +\ |
| 119 | + content['fileName'] + ".css " + \ |
| 120 | + "by " + \ |
| 121 | + Path(__file__).name + \ |
| 122 | + " for commit " + \ |
| 123 | + commit + \ |
| 124 | + "\n\n" |
| 125 | + |
| 126 | + if not len(content['colors']): |
| 127 | + return None |
| 128 | + |
| 129 | + str_result += "# Used colors\n\n" |
| 130 | + for c in content['colors']: |
| 131 | + str_result += c[0] + '\n' |
| 132 | + |
| 133 | + str_result += "\n# Grouped by attribute\n" |
| 134 | + |
| 135 | + for k, v in content['byAttribute'].items(): |
| 136 | + str_result += '\n' + k + '\n' |
| 137 | + for val in v: |
| 138 | + str_result += ' ' + val + '\n' |
| 139 | + |
| 140 | + str_result += "\n# Grouped by color\n" |
| 141 | + |
| 142 | + for k, v in content['byColor'].items(): |
| 143 | + str_result += '\n' + k + '\n' |
| 144 | + for val in v: |
| 145 | + str_result += ' ' + val + '\n' |
| 146 | + |
| 147 | + return str_result |
| 148 | + |
| 149 | +if __name__ == '__main__': |
| 150 | + |
| 151 | + if len(sys.argv) > 1: |
| 152 | + error('No argument required!') |
| 153 | + |
| 154 | + try: |
| 155 | + css_folder_path = Path(__file__).parent.absolute() / Path('../../src/qt/res/css/') |
| 156 | + css_folder_path = css_folder_path.resolve(strict=True) |
| 157 | + except Exception: |
| 158 | + error("Path doesn't exist: {}".format(css_folder_path)) |
| 159 | + |
| 160 | + if not len(list(css_folder_path.glob('*.css'))): |
| 161 | + error("No .css files found in {}".format(css_folder_path)) |
| 162 | + |
| 163 | + results = [parse_css(x) for x in css_folder_path.glob('*.css') if x.is_file()] |
| 164 | + |
| 165 | + colors_folder_path = css_folder_path / Path('colors/') |
| 166 | + if not colors_folder_path.is_dir(): |
| 167 | + try: |
| 168 | + colors_folder_path.mkdir() |
| 169 | + except Exception: |
| 170 | + error("Can't create new folder: {}".format(colors_folder_path)) |
| 171 | + |
| 172 | + commit = subprocess.check_output(['git', '-C', css_folder_path, 'rev-parse', '--short', 'HEAD']).decode("utf-8") |
| 173 | + |
| 174 | + for r in results: |
| 175 | + |
| 176 | + # Update the css file |
| 177 | + css_file = css_folder_path / Path(r['fileName'] + '.css') |
| 178 | + css_content = css_file.read_text() |
| 179 | + to_replace = re.findall(MATCH_REPLACE, css_content, re.DOTALL) |
| 180 | + |
| 181 | + str_result = "\n# Used colors in {}.css for commit {}\n".format(r['fileName'], commit) |
| 182 | + for c in r['colors']: |
| 183 | + str_result += c[0] + '\n' |
| 184 | + |
| 185 | + str_replace = "<colors>\n{}\n</colors>".format(str_result) |
| 186 | + css_content = css_content.replace(to_replace[0], str_replace) |
| 187 | + css_file.write_text(css_content) |
| 188 | + |
| 189 | + # Write the <css>_color.txt files |
| 190 | + str_result = create_color_file(r, commit) |
| 191 | + |
| 192 | + if str_result is not None: |
| 193 | + color_file = colors_folder_path / Path(r['fileName'] + '_css_colors.txt') |
| 194 | + color_file.write_text(str_result) |
| 195 | + |
| 196 | + print('\n{}.css -> {} created!'.format(r['fileName'], color_file)) |
| 197 | + else: |
| 198 | + print('\n{}.css -> No colors found..'.format(r['fileName'] + ".css")) |
0 commit comments