MasoniteFramework / masonite4

Temporary Repository for a Masonite Rewrite for Masonite 4
14 stars 3 forks source link

Craft tinker should include python startup #205

Closed tpow closed 2 years ago

tpow commented 2 years ago

The Craft tinker command is handy, but does not process my typical python startup file so the experience is somewhat lacking compared to the usual python interactive shell. It would be nice if it supported the PYTHONSTARTUP variable like the regular shell (possibly with an option to disable it).

Below is a quick stab at adding this support (and also including readline support and tab completion at a minimum similar to the regular shell.)

class TinkerCommand(Command):                                                    
    """                                                                          
    Run a python shell with the container pre-loaded.                            

    tinker                                                                      
    """                                                                          

    def handle(self):                                                            
        from wsgi import container                                              

        version = "{}.{}.{}".format(                                            
            sys.version_info.major, sys.version_info.minor, sys.version_info.micro
        )                                                                        
        banner = BANNER.format(version)                                          
        # BEGIN CHANGE                                                     
        # Set up a dictionary to serve as the environment for the shell, so      
        # that tab completion works on objects that are imported at runtime.    
        import os                                                                
        imported_objects = {}                                                    
        imported_objects["app"] = container  # Always include                    
        try:  # Try activating readline and rlcompleter for nice experience.                  
            import readline                                                      
        except ImportError:                                                      
            pass                                                                
        else:                                                                    
            # No try needed, we know readline was imported successfully.              
            import rlcompleter                                                  
            readline.set_completer(rlcompleter.Completer(imported_objects).complete)
            readline.parse_and_bind("tab:complete")

        startup = os.environ.get("PYTHONSTARTUP")      
        if startup:                                                       
            startup = os.path.expanduser(startup)                                
            if os.path.isfile(startup):                                                        
                try:                                                                
                    with open(startup) as f:                                        
                        eval(compile(f.read(), startup, "exec"), imported_objects)  
                except NameError:                                                    
                    pass                                                            
        code.interact(banner=banner, local=imported_objects)                    
        # END CHANGE  
girardinsamuel commented 2 years ago

With (soon to be released) Masonite 4, the tinker command has been improved ! You should run it with -i option after having installed IPython. Then you will have :

About PYTHONSTARTUP variable, I did not know this one before. I will take a look at it and try to add it in Masonite 4 version 😉

tpow commented 2 years ago

Thanks @girardinsamuel, it is nice know that the tinker command is being enhanced. I am personally happy with the standard python shell, but that may be due in part to my python startup file.

girardinsamuel commented 2 years ago

Yes, because IPython come with a lot of features such as colored output, auto-reloading, and much more.