awtk/scripts/update_res_app.py

267 lines
9.6 KiB
Python
Raw Permalink Normal View History

2020-06-14 08:59:23 +08:00
#!/usr/bin/python
2019-12-19 16:11:17 +08:00
import os
2020-06-14 08:59:23 +08:00
import sys
2020-08-14 16:43:12 +08:00
import json
import collections
2020-06-14 08:59:23 +08:00
# AWTK_ROOT/scripts/update_res_common.py
2019-12-19 16:11:17 +08:00
import update_res_common as common
2023-07-04 18:11:46 +08:00
import res_config
RES_CONFIG = None
RES_OUTPUT_DIR='./res'
2019-12-19 16:11:17 +08:00
2021-09-16 16:00:56 +08:00
def get_theme(i):
return THEMES[i]
2023-07-04 18:11:46 +08:00
def use_theme_config_from_res_config(res_config_path, config = None):
2020-08-14 16:43:12 +08:00
global DPI
global THEMES
global OUTPUT_ROOT
global IS_GENERATE_INC_RES
global IS_GENERATE_INC_BITMAP
global APP_THEME
global APP_ROOT
2022-05-09 09:50:08 +08:00
global LCD_ORIENTATION
global LCD_FAST_ROTATION_MODE
2023-07-04 18:11:46 +08:00
content = None
if config == None :
config_json = common.join_path(APP_ROOT, res_config_path)
if not os.path.exists(config_json) or res_config_path == '':
config_json = common.join_path(APP_ROOT, 'project.json')
if not os.path.exists(config_json) :
print(config_json + ' is not exists.')
return
content = res_config.res_config()
content.load_file(config_json)
else :
content = config;
OUTPUT_ROOT = common.join_path(APP_ROOT, common.join_path(content.get_res_res_root(), 'assets'))
APP_THEME = content.get_res_actived_theme()
LCD_FAST_ROTATION_MODE = content.get_res_lcd_fast_rotation_mode()
LCD_ORIENTATION = content.get_res_lcd_orientation()
DPI = content.get_res_dpi()
IS_GENERATE_INC_RES = content.get_inc_res()
IS_GENERATE_INC_BITMAP = content.get_inc_bitmap()
if not content.has_themes():
2020-08-14 16:43:12 +08:00
return
2023-07-04 18:11:46 +08:00
for theme_name in content.get_res_themes_key() :
color_format = content.get_res_color_format(theme_name)
color_depth = content.get_res_color_depth(theme_name)
2020-08-14 16:43:12 +08:00
if color_format == 'MONO':
imagegen_options = 'mono'
elif color_format == 'BGR(A)':
if color_depth == '16bit':
imagegen_options = 'bgr565'
else:
imagegen_options = 'bgra'
else:
if color_depth == '16bit':
imagegen_options = 'rgb565'
else:
imagegen_options = 'rgba'
if IS_GENERATE_INC_BITMAP:
2023-07-04 18:35:35 +08:00
config_dir = common.join_path(ASSETS_ROOT, common.join_path(theme_name, 'fonts/config'))
2020-08-14 16:43:12 +08:00
common.remove_dir(config_dir)
common.make_dirs(config_dir)
2023-07-04 18:11:46 +08:00
for font_name in content.get_res_fonts_key(theme_name):
for font_size in content.get_res_font_size_key(theme_name, font_name):
font_name = common.to_file_system_coding(font_name)
font_size = common.to_file_system_coding(font_size)
filename = common.join_path(config_dir, font_name+'_'+font_size+'.txt')
2024-02-21 17:46:12 +08:00
common.write_file(filename, content.get_res_font_value(theme_name, font_name, font_size, ''))
2023-07-04 18:11:46 +08:00
2024-02-21 17:46:12 +08:00
theme = {'name': theme_name, 'imagegen_options': imagegen_options, 'packaged': content.get_res_packaged(theme_name), 'storage_dir': content.get_storage_dir()}
2020-08-14 16:43:12 +08:00
if theme_name == 'default':
THEMES.insert(0, theme)
else:
THEMES.append(theme)
2023-07-04 18:11:46 +08:00
def use_default_theme_config(res_config_path, res_config = None):
2020-08-14 16:43:12 +08:00
global THEMES
2023-07-04 18:11:46 +08:00
use_theme_config_from_res_config(res_config_path, res_config)
2020-08-14 16:43:12 +08:00
if len(THEMES) == 0:
if os.path.isdir(ASSETS_ROOT):
for file in os.listdir(ASSETS_ROOT):
if os.path.isdir(common.join_path(ASSETS_ROOT, file)):
if file == 'default':
THEMES.insert(0, file)
else:
THEMES.append(file)
def is_dependencies_ok():
dependencies = ['bsvggen', 'fontgen', 'imagegen', 'resgen', 'strgen', 'themegen', 'xml_to_ui']
for d in dependencies:
if not os.path.exists(common.join_path(TOOLS_ROOT, common.to_exe(d))):
print('Can\'t find ' + common.to_exe(d) + ', please build AWTK.')
return False
if IS_GENERATE_INC_BITMAP:
for t in THEMES:
if isinstance(t, dict):
print(t['imagegen_options'])
return True
def on_generate_res_event():
if os.path.exists(common.join_path(APP_ROOT, 'scripts/update_res_generate_res_handler.py')):
import update_res_generate_res_handler as generate_res
if hasattr(generate_res, 'on_generate_res_before'):
common.on_generate_res_before(generate_res.on_generate_res_before)
if hasattr(generate_res, 'on_generate_res_after'):
common.on_generate_res_after(generate_res.on_generate_res_after)
2021-02-20 15:03:40 +08:00
def getopt(args):
return common.get_args(args);
2020-08-14 16:43:12 +08:00
2023-07-04 18:11:46 +08:00
def set_res_config(config) :
global RES_CONFIG
RES_CONFIG = config
def set_res_config_by_script(script_path, res_config_script_argv):
return set_res_config(res_config.set_res_config_by_script(script_path, res_config_script_argv))
def get_dict_value(dict, key, default_value) :
if key in dict :
return dict[key]
return default_value
def run(awtk_root, is_excluded_file_handler = None, is_new_usage = False) :
2020-10-30 07:11:48 +08:00
global DPI
2020-08-14 16:43:12 +08:00
global AWTK_ROOT
global TOOLS_ROOT
global THEMES
2020-10-30 07:11:48 +08:00
global APP_THEME
2020-08-14 16:43:12 +08:00
global APP_ROOT
global ASSETS_ROOT
2020-10-30 07:11:48 +08:00
global OUTPUT_ROOT
global IS_GENERATE_INC_RES
2020-08-14 16:43:12 +08:00
global IS_GENERATE_INC_BITMAP
2022-05-09 09:50:08 +08:00
global LCD_ORIENTATION
global LCD_FAST_ROTATION_MODE
2023-07-04 18:11:46 +08:00
global RES_CONFIG
common.show_usage(is_new_usage);
2020-08-14 16:43:12 +08:00
2021-02-20 15:03:40 +08:00
GDPI=''
2023-07-04 18:11:46 +08:00
TMP_APP_ROOT = ''
2021-02-20 15:03:40 +08:00
IMAGEGEN_OPTIONS=''
2023-07-04 18:11:46 +08:00
TMP_LCD_ORIENTATION=''
RES_CONFIG_JSON_PATH=''
2023-07-05 17:57:40 +08:00
RES_OUTPUT_DIR='./res'
2023-07-04 18:11:46 +08:00
TMP_LCD_FAST_ROTATION_MODE=None
args = sys.argv[1:];
2023-12-06 17:38:26 +08:00
is_longsots_res_output_dir = False
2023-07-04 18:11:46 +08:00
if common.is_all_sopts_args(args) :
longsots_dict = common.get_longsopts_args(args)
common.set_action(longsots_dict['action'])
awtk_root = get_dict_value(longsots_dict, 'awtk_root', awtk_root)
GDPI = get_dict_value(longsots_dict, 'dpi', GDPI)
IMAGEGEN_OPTIONS = get_dict_value(longsots_dict, 'image_options', IMAGEGEN_OPTIONS)
TMP_LCD_ORIENTATION = get_dict_value(longsots_dict, 'lcd_orientation', TMP_LCD_ORIENTATION)
TMP_LCD_FAST_ROTATION_MODE = get_dict_value(longsots_dict, 'lcd_enable_fast_rotation', TMP_LCD_FAST_ROTATION_MODE)
RES_CONFIG_JSON_PATH = get_dict_value(longsots_dict, 'res_config_file', RES_CONFIG_JSON_PATH)
TMP_APP_ROOT = get_dict_value(longsots_dict, 'app_root', TMP_APP_ROOT)
2023-12-06 17:38:26 +08:00
if 'output_dir' in longsots_dict:
is_longsots_res_output_dir = True
RES_OUTPUT_DIR = longsots_dict['output_dir']
2023-07-04 18:11:46 +08:00
if 'res_config_script' in longsots_dict :
res_config_script_argv = ''
res_config_script = longsots_dict['res_config_script']
if 'res_config_script_argv' in longsots_dict :
res_config_script_argv = longsots_dict['res_config_script_argv']
set_res_config_by_script(res_config_script, res_config_script_argv)
else :
sys_args = common.get_args(args)
if len(sys_args) > 0 :
common.set_action(sys_args[0])
if len(sys_args) > 1:
GDPI = sys_args[1]
if len(sys_args) > 2:
IMAGEGEN_OPTIONS = sys_args[2]
if len(sys_args) > 3:
TMP_LCD_ORIENTATION = sys_args[3]
TMP_LCD_FAST_ROTATION_MODE = False
2021-02-20 15:03:40 +08:00
2020-08-14 16:43:12 +08:00
AWTK_ROOT = awtk_root
2020-08-17 08:43:23 +08:00
action = common.get_action()
2023-07-04 18:11:46 +08:00
if TMP_APP_ROOT == '' :
APP_ROOT = common.getcwd()
if APP_ROOT.endswith('scripts'):
APP_ROOT = os.path.dirname(APP_ROOT)
else :
APP_ROOT = TMP_APP_ROOT
2020-06-14 08:59:23 +08:00
2020-08-14 16:43:12 +08:00
os.chdir(APP_ROOT)
2020-06-14 08:59:23 +08:00
2020-08-14 16:43:12 +08:00
DPI = 'x1'
THEMES = []
APP_THEME = 'default'
2023-07-04 18:11:46 +08:00
LCD_ORIENTATION = ''
2023-12-06 17:38:26 +08:00
OUTPUT_ROOT = None
2020-08-14 16:43:12 +08:00
IS_GENERATE_INC_RES = True
IS_GENERATE_INC_BITMAP = True
2023-07-04 18:11:46 +08:00
LCD_FAST_ROTATION_MODE = False
2020-08-14 16:43:12 +08:00
TOOLS_ROOT = common.join_path(AWTK_ROOT, 'bin')
AWTK_ROOT = common.join_path(APP_ROOT, AWTK_ROOT)
ASSETS_ROOT = common.join_path(APP_ROOT, 'design')
2020-06-14 08:59:23 +08:00
2023-07-04 18:11:46 +08:00
use_default_theme_config(RES_CONFIG_JSON_PATH, RES_CONFIG)
2023-12-06 17:38:26 +08:00
if is_longsots_res_output_dir or OUTPUT_ROOT == None :
OUTPUT_ROOT = os.path.abspath(common.join_path(RES_OUTPUT_DIR, 'assets'))
2023-07-04 18:11:46 +08:00
if TMP_LCD_ORIENTATION != '' :
LCD_ORIENTATION = TMP_LCD_ORIENTATION
if TMP_LCD_FAST_ROTATION_MODE != None :
LCD_FAST_ROTATION_MODE = TMP_LCD_FAST_ROTATION_MODE
2020-10-30 07:11:48 +08:00
ASSET_C = common.join_path(OUTPUT_ROOT, '../assets.inc')
if action == 'json':
ASSET_C = common.join_path(APP_ROOT, 'assets_web.js')
2020-08-14 16:43:12 +08:00
if not is_dependencies_ok():
print('For details, please read scripts/README.md.')
elif len(THEMES) == 0:
print('Not found theme.')
print('For details, please read scripts/README.md.')
else:
2021-02-20 15:03:40 +08:00
if GDPI != '':
DPI = GDPI
if IMAGEGEN_OPTIONS != '':
for theme in THEMES :
theme["imagegen_options"] = IMAGEGEN_OPTIONS
2022-03-08 10:07:25 +08:00
if LCD_ORIENTATION != '':
for theme in THEMES :
theme["lcd_orientation"] = LCD_ORIENTATION
2021-02-20 15:03:40 +08:00
2020-08-14 16:43:12 +08:00
common.init(AWTK_ROOT, ASSETS_ROOT, THEMES, ASSET_C, OUTPUT_ROOT)
common.set_tools_dir(TOOLS_ROOT)
2022-05-09 09:50:08 +08:00
common.set_lcd_rortrail_info(LCD_ORIENTATION, LCD_FAST_ROTATION_MODE)
2020-08-14 16:43:12 +08:00
common.set_dpi(DPI)
common.set_app_theme(APP_THEME)
common.set_enable_generate_inc_res(IS_GENERATE_INC_RES)
common.set_enable_generate_inc_bitmap(IS_GENERATE_INC_BITMAP)
2020-09-18 11:31:59 +08:00
common.set_is_excluded_file_handler(is_excluded_file_handler)
2020-08-14 16:43:12 +08:00
on_generate_res_event()
common.update_res()
2020-06-14 08:59:23 +08:00
2020-08-14 16:43:12 +08:00
if isinstance(THEMES[0], dict):
2021-09-29 17:42:33 +08:00
if action != 'clean' and action != 'web' and action != 'json' and action != 'pinyin' and action != 'res':
2020-08-14 16:43:12 +08:00
common.gen_res_c(False)