1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3# Copyright (c) 2021 Huawei Device Co., Ltd. 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16import sys 17import os 18 19theme_list = {} 20 21 22def read_row_value(row, param, offset, platform): 23 if row[offset] == '': 24 return 25 item = {} 26 item['.type'] = row[offset] 27 item['.value'] = row[offset + 1] 28 if item['.type'] == 'ThemeConstantsType::DOUBLE': 29 if item['.value'].find('.') == -1: 30 item['.value'] = item['.value'] + '.0' 31 if row[offset + 2] != '': 32 item['.isPublic'] = (row[offset + 2]).lower() 33 if row[offset + 3] != '': 34 item['.blendAlpha'] = row[offset + 3] 35 param[platform] = item 36 37 38def spitrow(row): 39 values = [] 40 while True: 41 if len(row) == 0: 42 values.append(row) 43 break 44 pos = -1 45 if row[0] == '\"': 46 pos = row.find('\"', 1) 47 if pos == -1: 48 break 49 pos = row.find(',', pos + 1) 50 else: 51 pos = row.find(',') 52 value = '' 53 if pos != -1: 54 value = row[:pos] 55 row = row[pos+1:] 56 else: 57 value = row 58 59 if len(value) > 6 and value[0:3] == '\"\"\"' and \ 60 value[-3:] == '\"\"\"': 61 values.append(value[2:-2]) 62 else: 63 if len(value) > 2 and value[0] == '\"' and value[-1] == '\"': 64 values.append(value[1:-1]) 65 else: 66 values.append(value) 67 if pos == -1: 68 break 69 return values 70 71 72def read_row(row): 73 if row[-1] == '\n': 74 row = row[:-1] 75 values = spitrow(row) 76 param = {} 77 if len(values) != 16: 78 print('error') 79 print(row) 80 return 81 82 if values[1] != '': 83 param['base'] = values[1] 84 if values[2] != '': 85 param['offset'] = int(values[2]) 86 param['value'] = int(values[3]) 87 read_row_value(values, param, 4, 'default') 88 read_row_value(values, param, 8, 'tv') 89 read_row_value(values, param, 12, 'watch') 90 theme_list[values[0]] = param 91 92 93def read_file(file_path): 94 with open(file_path) as file_read: 95 content = file_read.read() 96 return content 97 98 99def write_file(file_path, content): 100 with open(file_path, 'wb+') as file_write: 101 file_write.write(content.encode()) 102 103 104def build_header(file_path): 105 define = 'FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS'\ 106 '_THEME_THEME_CONSTANTS_DEFINES_H' 107 begin_str = '/*\n'\ 108 ' * Copyright (c) 2021 Huawei Device Co., Ltd.\n'\ 109 ' * Licensed under the Apache License, Version 2.0 (the "License");\n'\ 110 ' * you may not use this file except in compliance with the License.\n'\ 111 ' * You may obtain a copy of the License at\n'\ 112 ' *\n'\ 113 ' * http://www.apache.org/licenses/LICENSE-2.0\n'\ 114 ' *\n'\ 115 ' * Unless required by applicable law or agreed to in writing, software\n'\ 116 ' * distributed under the License is distributed on an "AS IS" BASIS,\n'\ 117 ' * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n'\ 118 ' * See the License for the specific language governing permissions and\n'\ 119 ' * limitations under the License.\n'\ 120 ' */\n'\ 121 '\n'\ 122 '#ifndef {}\n'\ 123 '#define {}\n'\ 124 '\n'\ 125 'namespace OHOS::Ace {{\n'\ 126 '\n' 127 128 new_content = begin_str.format(define, define) 129 for key, values in theme_list.items(): 130 row = 'constexpr uint32_t {} = '.format(key) 131 has_base = False 132 if 'base' in values: 133 row = ''.join((row, values['base'])) 134 has_base = True 135 if 'offset' in values: 136 if has_base == True: 137 row = ''.join((row, ' + ')) 138 row = ''.join((row, str(values['offset']))) 139 row = ''.join((row, ';\n')) 140 new_content = ''.join((new_content, row)) 141 end_str = '{}\n}} // namespace OHOS::Ace\n\n'\ 142 '#endif // {}' 143 write_file(file_path, end_str.format(new_content, define)) 144 145 146def get_global_name(name): 147 offset = 0 148 char = name.split('_') 149 ret = '' 150 for i in range(0, len(char)): 151 if i == 0: 152 ret = ''.join((ret, char[i])) 153 else: 154 ret = ''.join((ret, char[i].capitalize())) 155 return ret 156 157 158def make_table(param_map, platform): 159 content = 'const ResValueWrapper* ThemeConstants::styleMap{}[] = '.format( 160 platform.capitalize()) 161 162 table = {} 163 maxindex = 0 164 for key, values in param_map.items(): 165 if maxindex < int(key): 166 maxindex = int(key) 167 168 content = ''.join((content, '{')) 169 for i in range(0, maxindex + 1): 170 if str(i) in param_map: 171 content = '{} &{},'.format(content, param_map[str(i)]) 172 else: 173 content = ''.join((content, ' nullptr,')) 174 content = content[0:len(content) - 1] 175 content = ''.join((content, '};\n')) 176 return content 177 178 179def build_cpp(file_path, platform): 180 begin_str = '/*\n'\ 181 ' * Copyright (c) 2021 Huawei Device Co., Ltd.\n'\ 182 ' * Licensed under the Apache License, Version 2.0 (the "License");\n'\ 183 ' * you may not use this file except in compliance with the License.\n'\ 184 ' * You may obtain a copy of the License at\n'\ 185 ' *\n'\ 186 ' * http://www.apache.org/licenses/LICENSE-2.0\n'\ 187 ' *\n'\ 188 ' * Unless required by applicable law or agreed to in writing, software\n'\ 189 ' * distributed under the License is distributed on an "AS IS" BASIS,\n'\ 190 ' * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n'\ 191 ' * See the License for the specific language governing permissions and\n'\ 192 ' * limitations under the License.\n'\ 193 ' */\n'\ 194 '\n'\ 195 '#include \"core/components/theme/theme_constants.h\"\n'\ 196 '#include \"core/components/theme/theme_constants_defines.h\"\n'\ 197 '\n'\ 198 '#include \"core/components/common/properties/scroll_bar.h\"\n'\ 199 '\n'\ 200 'namespace OHOS::Ace {\n' 201 content = begin_str 202 table = {} 203 for key, values in theme_list.items(): 204 if platform in values: 205 name = 'g_{}{}'.format(get_global_name( 206 key.lower()), platform.capitalize()) 207 value = 'const ResValueWrapper {} {{'.format(name) 208 table[str(values['value'])] = name 209 for key1, values1 in values[platform].items(): 210 value = '{} {} = {},'.format(value, key1, values1) 211 value = value[0: len(value) - 1] 212 value = ''.join((value, ' };\n')) 213 content = ''.join((content, value)) 214 content = '{}\n'\ 215 '{}\n'\ 216 'uint32_t ThemeConstants::{}MapCount = sizeof(styleMap{}) / sizeof(styleMap{}[0]);\n'\ 217 '\n}} // namespace OHOS::Ace'.format(content, make_table(table, platform), 218 platform.capitalize(), platform.capitalize(), platform.capitalize()) 219 file_name = '{}theme_constants_{}.cpp'.format(file_path, platform) 220 write_file(file_name, content) 221 222 223def read_file_lines(file_path): 224 with open(file_path) as file_read: 225 content = file_read.readlines() 226 return content 227 228 229def main(): 230 if len(sys.argv) != 3: 231 print('error, missing required parameters!') 232 print('\tbuild_theme_code theme.xls theme/') 233 return 1 234 235 folder = os.path.exists(sys.argv[2]) 236 if not folder: 237 os.makedirs(sys.argv[2]) 238 239 cvs = read_file_lines(sys.argv[1]) 240 241 for i in range(len(cvs)): 242 if i == 0: 243 continue 244 read_row(cvs[i]) 245 246 dist_path = sys.argv[2] 247 248 build_header(''.join((dist_path, 'theme_constants_defines.h'))) 249 build_cpp(dist_path, 'tv') 250 build_cpp(dist_path, 'default') 251 build_cpp(dist_path, 'watch') 252 253if __name__ == '__main__': 254 main() 255