palmetto / palm-cli

Palm CLI - the tool-belt for data teams
https://palm-cli.readthedocs.io/en/latest/
Apache License 2.0
47 stars 8 forks source link

alias ctx.obj into ctx #18

Open ethanknox-palmetto opened 3 years ago

ethanknox-palmetto commented 3 years ago

Context While the interface is still young we could consider simplifying it for readability. since this API is a core user interface for palm we probably want it as reads-like-english as possible.

Describe the solution you'd like We could potentially capture the context at top level, then monkeypatch __getattribute__ to make obj the final lookup path. so it would look something like this:

class PalmContext(Context):
    def __getattribute__(self, attr):
        try: 
            return Context.__getattribute__(self, attr)
        except Exception as parent_exception: 
            try:
                 return self.obj.__getattribute__(self.obj, attr)
            except:
                raise parent_exception

then the interface would be

def some_command(context):
    context.run_in_docker("thing")

Describe alternatives you've considered it would be trivial to just alias ctx.obj in the template, and that might actually be better for reasons stated below.

Additional context Naming collisions would suck here. this is the kind of magic that makes OOP great. It's also the kind of magic that makes functional programmers hate OOP if something goes wrong here.

Is there an existing feature request for this?

jakeberesford-palmetto commented 3 years ago

I think we can achieve this with click.make_pass_decorator(Environment) - this documentation has an example: https://click.palletsprojects.com/en/8.0.x/complex/

ethanknox-palmetto commented 2 years ago

This is going to get scooped into whatever we do for #40