#Read Variables from a Config File

1 messages · Page 1 of 1 (latest)

frail shoal
#

I'm trying to work out how I can read a set of variable values from a config file with Python SDK. I have the config file at the same location as my Python module, and it contains values like Docker image, path, license key values etc. I need to read this config before I run my main code which builds software in the target Docker container.
From testing it seems to be using a folder called /scratch to try and read the config file from, but I can't understand how that relates to what is stored locally on my PC. Can anyone help?

formal yacht
#

There is a built in function called current_module() that should work for this if the files are inside of the module already.

Given a file called example.txt here are two ways to read it

@object_type
class Python:
    @function 
    def debug(self) -> dagger.Container:
        current_module_source = dag.current_module().source()
        return (
            dag.
            container().
            from_("alpine").
            with_mounted_directory("/src", current_module_source)
        )

    @function 
    def read_with_python(self) -> str:
        sample_file_contents = dag.current_module().source().file("example.txt").contents()

        return sample_file_contents

The function debug above shows how to mount the file into a container you are building. This will add all the contents of the current module to the /src directory which you can then read from as you continue to build the image

The function read_with_python shows how to read the contents of a file and store them as a variable in python