palm-cli icon indicating copy to clipboard operation
palm-cli copied to clipboard

alias ctx.obj into ctx

Open ethanknox-palmetto opened this issue 4 years ago • 2 comments

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?

  • [x] I have searched the existing issues

ethanknox-palmetto avatar Nov 16 '21 14:11 ethanknox-palmetto

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/

jakeberesford-palmetto avatar Nov 16 '21 15:11 jakeberesford-palmetto

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

ethanknox-palmetto avatar Dec 02 '21 13:12 ethanknox-palmetto