#Help with project layout

1 messages · Page 1 of 1 (latest)

vapid steppe
#

I want to implement dagger in my monorepo. My thought is to have 3 layers. Two abstractions in an private dagger module and then the service.

The highest level is all the global functions and language specific things. Then I want to add/overwrite functions in the second layer which is our service "generation"-specific stuff like pip install vs poetry install etc. I then want the service to use this module(s) to handle 99% of the work, my vision is developers could super() or fully override the provided functions. I dunno if my explaination makes sense here (I'm precoffee atm) and my python is rusty.

Also can I only use 1 module named the dagger project name? It seems like none of my other functions are parsed even though I'm importing them.

I'll attach examples

#

src/main/python.py

@object_type
class Python:
    @function
    def build_dev(self) -> dagger.Container:
         return dag.container().from_("alpine:latest")
    
    @function
    def test_echo(self, string_arg: str) -> dagger.Container:
        """Returns a container that echoes whatever string argument is provided"""
        return dag.container().from_("alpine:latest").with_exec(["echo", string_arg])

@object_type
class Gen3(Python):
    service: str = field()

    # def __init__(self, service: str):
    #     super(Python, self).__init__()

    @function
    def test_echo(self, string_arg: str) -> dagger.Container:
        """adds another echo to Python().test_echo"""
        container = super().test_echo().call()
        return container.with_exec(["echo", "additional"])

    @function
    async def grep_dir(self, directory_arg: dagger.Directory, pattern: str) -> str:
        """Returns lines that match a pattern in the files of the provided Directory"""
        return await (
            dag.container()
            .from_("alpine:latest")
            .with_mounted_directory("/mnt", directory_arg)
            .with_workdir("/mnt")
            .with_exec(["grep", "-R", pattern, "."])
            .stdout()
        )
radiant geyser
#

Hey! Do you have any other files in src/main/ besides python.py?

#

If it's the only one, you probably need (depending if you changed pyproject.toml or not) to add a __init__.py with import .python. Dagger tries to import a package named main. If you don't want __init__.py then you need to configure the build backend in pyproject.toml to find the right files for the build.

vapid steppe
#

sorry my laptop had a forced update

src/main/init.py

@object_type
class Daggerpoc(Gen3):
    def __init__(self, service: str):
        super().__init__(service="test")

    @function
    def container_echo(self, string_arg: str) -> dagger.Container:
        """Returns a container that echoes whatever string argument is provided"""
        return dag.container().from_("alpine:latest").with_exec(["echo", string_arg])

    @function
    async def grep_dir(self, directory_arg: dagger.Directory, pattern: str) -> str:
        """Returns lines that match a pattern in the files of the provided Directory"""
        return await (
            dag.container()
            .from_("alpine:latest")
            .with_mounted_directory("/mnt", directory_arg)
            .with_workdir("/mnt")
            .with_exec(["grep", "-R", pattern, "."])
            .stdout()
        )

something like this

#

@radiant geyser even if I include that import in the init.py, the functions from these other modules dont show up

radiant geyser
#

Can you put a repro in a gist, or push to a temporary repo? Would help to have the bigger picture on how you're organizing. The file paths, thee imports on them, pyproject.toml contents...

vapid steppe
#

yeah I'll work on something today