我正在寻找一种在python中为游戏Stellaris生成一些配置文件的方法。
我的目标是为每个生成的 Stellaris 配置文件编写一个 python 文件,加载 Python 文件,执行其中的脚本,然后根据 python 文件的名称将输出放入同一目录中。
因此,对于文件“common/ship_sizes/00_ship_sizes.txt”,我只需创建一个文件“common/ship_sizes/00_ship_sizes.txt.py”,然后系统会找到该文件,打开它,在其中运行脚本(可选地带有一些序言,例如“call gen_output
”,或者不,我不在乎),并将python代码的输出放入“common/ship_sizes/00_ship_sizes.txt”。
目标是能够对配置文件的生成进行编码,而不必自己编写它们。
我绝对可以手动执行此操作,方法是遍历目录树、打开 python 文件、运行脚本、根据文件名重定向输出等。我希望有一种更 Pythonic 的方式来做到这一点。
我要生成的示例:
ship_section_template = {
key = "BATTLESHIP2_BOW_L1M1S2"
ship_size = battleship2
fits_on_slot = bow
should_draw_components = yes
entity = "battleship_bow_L1M1S2_entity"
icon = "GFX_ship_part_core_bow"
icon_frame = 1
component_slot = {
name = "LARGE_GUN_01"
template = "large_turret2"
locatorname = "large_gun_01"
}
component_slot = {
name = "MEDIUM_GUN_01"
template = "medium_turret2"
locatorname = "medium_gun_01"
}
component_slot = {
name = "SMALL_GUN_01"
template = "small_turret2"
locatorname = "small_gun_01"
}
component_slot = {
name = "SMALL_GUN_02"
template = "small_turret2"
locatorname = "small_gun_02"
}
large_utility_slots = 3
resources = {
category = ship_sections
cost = {
alloys = @section_cost
}
}
}
如您所见,它确实是无聊的样板;在许多情况下,简单的修改需要对许多文件进行大量重写,并且配置文件不允许您在大多数文件中进行数学运算,这意味着我必须在预先计算所有数学的情况下生成常量,这使得调整非常痛苦.
但老实说,这些中的每一个的实际格式并不重要,我可以轻松地编写字典到此代码。我的问题是构建许多这些文件的干净“系统”。
有没有办法可以利用 python 包系统来做到这一点?即,自动加载一组子目录中的所有包而不再次列出它们,或者类似的东西?我的意思是,我不想重写make。