python-fire icon indicating copy to clipboard operation
python-fire copied to clipboard

Support __call__ and __getattr__ on module

Open robertvazan opened this issue 4 years ago • 2 comments

Given code like this:

def hardcoded():
    # ...

def __getattr__(name):
    # ...

def __call__(*args):
    # ...

if __name__ == '__main__':
	fire.Fire(run)

I would like fire to call hardcoded function when hardcoded is specified on command line. Otherwise, I would like it to fall back on __getattr__ or __call__. I don't care which one, but I think both can be supported at the same time with __getattr__ being tried first.

Current workaround is to use helper object with __call__ method on it, but that hides all global functions in the module, so forwarding methods have to be added to the helper object.

robertvazan avatar Mar 30 '21 03:03 robertvazan

Can you share a link to your project? I haven't seen this kind of design before and would be curious what it accomplishes.

nfultz avatar Mar 30 '21 15:03 nfultz

It's a private script repository. I have a couple of scripts that wrap tools in docker containers. For example, I can run cargo.py build that runs cargo build in a docker container. The Python script takes care of container setup and then just passes through all arguments, including subcommand name. It also exposes commands not provided by the underlying cargo tool, for example cargo.py sandbox customizes official Rust docker image and cargo.py release automates release process.

robertvazan avatar Mar 30 '21 19:03 robertvazan