1

I'm looking for a way to generate some config files for the game in .

My goal is to write a python file for each generated Stellaris config file, load the Python file, execute the script in it, and put the output into the same directory based on the name of the python file.

So for a for file "common/ship_sizes/00_ship_sizes.txt", I would simply create a file "common/ship_sizes/00_ship_sizes.txt.py", then the system would find that file, open it, run the script in it (optionally with some preamble, like "call gen_output", or not, I don't care), and put the output of the python code into "common/ship_sizes/00_ship_sizes.txt".

The goal is being able to code the generation of the config files, rather than have to write them myself.

I can definitely do this manually, by walking a directory tree, opening python files, running the script, redirecting the output based on the file name, etc. I'm hoping there is a more pythonic way do to this.

An example of what I want to generate:

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
        }
    }
}

as you can see, it is really boring boilerplate; in many cases simple modifications require an extensive rewriting of many files, and the config file does not permit you do math in most of the files, meaning I have to generate constants with all of my math pre-calculated, which makes tweaking a real pain.

But honestly, the actual format of each of these doesn't matter much, I can write dictionary-to-this code easily. My problem is the a clean "system" to build many of these files.

Is there a way I can leverage the python package system to do this? Ie, auto-load all packages in a set of subdirectories without listing them again, or something similar? I mean, I don't want to rewrite make.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524

1 Answers1

-1

Please refer the config parser module from python documentation.

  1. https://docs.python.org/3/library/configparser.html
  2. https://docs.python.org/3/distutils/setupscript.html#

You can refer other file formats at section 'File Formats' in the link. https://docs.python.org/3/library/index.html

Other threads regarding configuration are:

  1. Where to put a configuration file in Python?
  2. https://towardsdatascience.com/from-novice-to-expert-how-to-write-a-configuration-file-in-python-273e171a8eb3