def run(): a = docopt.docopt(__doc__) vi_mode = bool(a['--vi']) config_dir = os.path.expanduser(a['--config-dir'] or '~/.ptpython/') # Create config directory. if not os.path.isdir(config_dir): os.mkdir(config_dir) # Startup path startup_paths = [] if 'PYTHONSTARTUP' in os.environ: startup_paths.append(os.environ['PYTHONSTARTUP']) # --interactive if a['--interactive']: startup_paths.append(a['--interactive']) sys.argv = [a['--interactive']] + a['<arg>'] # Add the current directory to `sys.path`. if sys.path[0] != '': sys.path.insert(0, '') if "VIRTUAL_ENV" in os.environ: virtual_env = Path( os.environ.get("VIRTUAL_ENV"), "lib", "python{0}.{1}".format(*sys.version_info[0:2]), "site-packages", ) if virtual_env.exists(): site.addsitedir(str(virtual_env)) # When a file has been given, run that, otherwise start the shell. if a['<arg>'] and not a['--interactive']: sys.argv = a['<arg>'] path = a['<arg>'][0] with open(path, 'rb') as f: code = compile(f.read(), path, 'exec') six.exec_(code) # Run interactive shell. else: enable_deprecation_warnings() # Apply config file def configure(repl): path = os.path.join(config_dir, 'config.py') if os.path.exists(path): run_config(repl, path) import __main__ embed(vi_mode=vi_mode, history_filename=os.path.join(config_dir, 'history'), configure=configure, locals=__main__.__dict__, globals=__main__.__dict__, startup_paths=startup_paths, title='Python REPL (ptpython)')
def run() -> None: a = create_parser().parse_args() config_file, history_file = get_config_and_history_file(a) # Startup path startup_paths = [] if "PYTHONSTARTUP" in os.environ: startup_paths.append(os.environ["PYTHONSTARTUP"]) # --interactive if a.interactive and a.args: # Note that we shouldn't run PYTHONSTARTUP when -i is given. startup_paths = [a.args[0]] sys.argv = a.args # Add the current directory to `sys.path`. if sys.path[0] != "": sys.path.insert(0, "") # When a file has been given, run that, otherwise start the shell. if a.args and not a.interactive: sys.argv = a.args path = a.args[0] with open(path, "rb") as f: code = compile(f.read(), path, "exec") # NOTE: We have to pass an empty dictionary as namespace. Omitting # this argument causes imports to not be found. See issue #326. exec(code, {}) # Run interactive shell. else: enable_deprecation_warnings() # Apply config file def configure(repl) -> None: if os.path.exists(config_file): run_config(repl, config_file) # Adjust colors if dark/light background flag has been given. if a.light_bg: repl.min_brightness = 0.0 repl.max_brightness = 0.60 elif a.dark_bg: repl.min_brightness = 0.60 repl.max_brightness = 1.0 import __main__ embed( vi_mode=a.vi, history_filename=history_file, configure=configure, locals=__main__.__dict__, globals=__main__.__dict__, startup_paths=startup_paths, title="Python REPL (ptpython)", )
def run(): a = docopt.docopt(__doc__) vi_mode = bool(a['--vi']) config_dir = os.path.expanduser(a['--config-dir'] or '~/.ptpython/') # If IPython is not available, show message and exit here with error status # code. try: import IPython except ImportError: print('IPython not found. Please install IPython (pip install ipython).') sys.exit(1) else: from ptpython.ipython import embed from ptpython.repl import run_config, enable_deprecation_warnings # Add the current directory to `sys.path`. if sys.path[0] != '': sys.path.insert(0, '') # When a file has been given, run that, otherwise start the shell. if a['<file>']: sys.argv = [a['<file>']] + a['<arg>'] six.exec_(compile(open(a['<file>'], "rb").read(), a['<file>'], 'exec')) else: enable_deprecation_warnings() # Create an empty namespace for this interactive shell. (If we don't do # that, all the variables from this function will become available in # the IPython shell.) user_ns = {} # --interactive if a['--interactive']: path = a['--interactive'] if os.path.exists(path): with open(path, 'r') as f: code = compile(f.read(), path, 'exec') six.exec_(code, user_ns, user_ns) else: print('File not found: {}\n\n'.format(path)) sys.exit(1) # Apply config file def configure(repl): path = os.path.join(config_dir, 'config.py') if os.path.exists(path): run_config(repl, path) # Run interactive shell. embed(vi_mode=vi_mode, history_filename=os.path.join(config_dir, 'history'), configure=configure, user_ns=user_ns, title='IPython REPL (ptipython)')
def run(): a = docopt.docopt(__doc__) vi_mode = bool(a['--vi']) config_dir = os.path.expanduser(a['--config-dir'] or '~/.ptpython/') # Create config directory. if not os.path.isdir(config_dir): os.mkdir(config_dir) # Startup path startup_paths = [] if 'PYTHONSTARTUP' in os.environ: startup_paths.append(os.environ['PYTHONSTARTUP']) # --interactive if a['--interactive']: startup_paths.append(a['--interactive']) # Add the current directory to `sys.path`. if sys.path[0] != '': sys.path.insert(0, '') # When a file has been given, run that, otherwise start the shell. if a['<file>']: sys.argv = [a['<file>']] + a['<arg>'] six.exec_(compile(open(a['<file>'], "rb").read(), a['<file>'], 'exec')) # Run interactive shell. else: enable_deprecation_warnings() # Apply config file def configure(repl): path = os.path.join(config_dir, 'conf.cfg') if os.path.exists(path): load_config(repl, path) path = os.path.join(config_dir, 'config.py') if os.path.exists(path): run_config(repl, path) # Save user settings in config file when repl is done def done(repl): path = os.path.join(config_dir, 'conf.cfg') # Create the file if it doesn't exist repl.settings.save_config(path) embed(vi_mode=vi_mode, history_filename=os.path.join(config_dir, 'history'), configure=configure, done=done, startup_paths=startup_paths, title='Python REPL (ptpython)')
def run(): a = docopt.docopt(__doc__) vi_mode = bool(a['--vi']) config_dir = os.path.expanduser(a['--config-dir'] or '~/.ptpython/') # Create config directory. if not os.path.isdir(config_dir): os.mkdir(config_dir) # Startup path startup_paths = [] if 'PYTHONSTARTUP' in os.environ: startup_paths.append(os.environ['PYTHONSTARTUP']) # --interactive if a['--interactive']: startup_paths.append(a['--interactive']) sys.argv = [a['--interactive']] + a['<arg>'] # Add the current directory to `sys.path`. if sys.path[0] != '': sys.path.insert(0, '') # When a file has been given, run that, otherwise start the shell. if a['<arg>'] and not a['--interactive']: sys.argv = a['<arg>'] path = a['<arg>'][0] with open(path, 'rb') as f: code = compile(f.read(), path, 'exec') six.exec_(code) # Run interactive shell. else: enable_deprecation_warnings() # Apply config file def configure(repl): path = os.path.join(config_dir, 'config.py') if os.path.exists(path): run_config(repl, path) import __main__ embed(vi_mode=vi_mode, history_filename=os.path.join(config_dir, 'history'), configure=configure, locals=__main__.__dict__, globals=__main__.__dict__, startup_paths=startup_paths, title='Python REPL (ptpython)')
def run(): a = docopt.docopt(__doc__) vi_mode = bool(a['--vi']) config_dir = os.path.expanduser(a['--config-dir'] or '~/.ptpython/') # Create config directory. if not os.path.isdir(config_dir): os.mkdir(config_dir) # Startup path startup_paths = [] if 'PYTHONSTARTUP' in os.environ: startup_paths.append(os.environ['PYTHONSTARTUP']) # --interactive if a['--interactive']: startup_paths.append(a['--interactive']) sys.argv = [a['--interactive']] + a['<arg>'] # Add the current directory to `sys.path`. if sys.path[0] != '': sys.path.insert(0, '') # When a file has been given, run that, otherwise start the shell. if a['<arg>'] and not a['--interactive']: sys.argv = a['<arg>'] six.exec_( compile(open(a['<arg>'][0], "rb").read(), a['<arg>'][0], 'exec')) # Run interactive shell. else: enable_deprecation_warnings() # Apply config file def configure(repl): path = os.path.join(config_dir, 'config.py') if os.path.exists(path): run_config(repl, path) import __main__ embed(vi_mode=vi_mode, history_filename=os.path.join(config_dir, 'history'), configure=configure, locals=__main__.__dict__, globals=__main__.__dict__, startup_paths=startup_paths, title='Python REPL (ptpython)')
def run_interactive_shell(): def configure(repl): """Apply config file""" path = os.path.join(args['config_dir'], 'config.py') if os.path.exists(path): repl.run_config(repl, path) from ptpython import repl repl.enable_deprecation_warnings() repl.embed( vi_mode=args.get('--vi', True), history_filename=os.path.join(args['config_dir'], 'history'), configure=configure, startup_paths=args.get('startup_paths', []), title=u'Pym REPL (pym)')
def run(): a = docopt.docopt(__doc__) vi_mode = bool(a['--vi']) config_dir = os.path.expanduser(a['--config-dir'] or '~/.ptpython/') # Create config directory. if not os.path.isdir(config_dir): os.mkdir(config_dir) # Startup path startup_paths = [] if 'PYTHONSTARTUP' in os.environ: startup_paths.append(os.environ['PYTHONSTARTUP']) # --interactive if a['--interactive']: startup_paths.append(a['--interactive']) # Add the current directory to `sys.path`. sys.path.append('.') # When a file has been given, run that, otherwise start the shell. if a['<file>']: sys.argv = [a['<file>']] + a['<arg>'] six.exec_(compile(open(a['<file>'], "rb").read(), a['<file>'], 'exec')) # Run interactive shell. else: enable_deprecation_warnings() # Apply config file def configure(repl): path = os.path.join(config_dir, 'config.py') if os.path.exists(path): run_config(repl, path) embed(vi_mode=vi_mode, history_filename=os.path.join(config_dir, 'history'), configure=configure, startup_paths=startup_paths)
def run(user_ns=None): a = create_parser().parse_args() config_file, history_file = get_config_and_history_file(a) # If IPython is not available, show message and exit here with error status # code. try: import IPython except ImportError: print("IPython not found. Please install IPython (pip install ipython).") sys.exit(1) else: from ptpython.ipython import embed from ptpython.repl import enable_deprecation_warnings, run_config # Add the current directory to `sys.path`. if sys.path[0] != "": sys.path.insert(0, "") # When a file has been given, run that, otherwise start the shell. if a.args and not a.interactive: sys.argv = a.args path = a.args[0] with open(path, "rb") as f: code = compile(f.read(), path, "exec") exec(code, {}) else: enable_deprecation_warnings() # Create an empty namespace for this interactive shell. (If we don't do # that, all the variables from this function will become available in # the IPython shell.) if user_ns is None: user_ns = {} # Startup path startup_paths = [] if "PYTHONSTARTUP" in os.environ: startup_paths.append(os.environ["PYTHONSTARTUP"]) # --interactive if a.interactive: startup_paths.append(a.args[0]) sys.argv = a.args # exec scripts from startup paths for path in startup_paths: if os.path.exists(path): with open(path, "rb") as f: code = compile(f.read(), path, "exec") exec(code, user_ns, user_ns) else: print("File not found: {}\n\n".format(path)) sys.exit(1) # Apply config file def configure(repl): if os.path.exists(config_file): run_config(repl, config_file) # Run interactive shell. embed( vi_mode=a.vi, history_filename=history_file, configure=configure, user_ns=user_ns, title="IPython REPL (ptipython)", )
def run(): a = docopt.docopt(__doc__) vi_mode = bool(a['--vi']) config_dir = os.path.expanduser(a['--config-dir'] or '~/.ptpython/') # Create config directory. if not os.path.isdir(config_dir): os.mkdir(config_dir) # If IPython is not available, show message and exit here with error status # code. try: import IPython except ImportError: print( 'IPython not found. Please install IPython (pip install ipython).') sys.exit(1) else: from ptpython.ipython import embed from ptpython.repl import run_config, enable_deprecation_warnings # Add the current directory to `sys.path`. if sys.path[0] != '': sys.path.insert(0, '') # When a file has been given, run that, otherwise start the shell. if a['<arg>'] and not a['--interactive']: sys.argv = a['<arg>'] six.exec_( compile(open(a['<arg>'][0], "rb").read(), a['<arg>'][0], 'exec')) else: enable_deprecation_warnings() # Create an empty namespace for this interactive shell. (If we don't do # that, all the variables from this function will become available in # the IPython shell.) user_ns = {} # Startup path startup_paths = [] if 'PYTHONSTARTUP' in os.environ: startup_paths.append(os.environ['PYTHONSTARTUP']) # --interactive if a['--interactive']: startup_paths.append(a['--interactive']) sys.argv = [a['--interactive']] + a['<arg>'] # exec scripts from startup paths for path in startup_paths: if os.path.exists(path): with open(path, 'r') as f: code = compile(f.read(), path, 'exec') six.exec_(code, user_ns, user_ns) else: print('File not found: {}\n\n'.format(path)) sys.exit(1) # Apply config file def configure(repl): path = os.path.join(config_dir, 'config.py') if os.path.exists(path): run_config(repl, path) # Run interactive shell. embed(vi_mode=vi_mode, history_filename=os.path.join(config_dir, 'history'), configure=configure, user_ns=user_ns, title='IPython REPL (ptipython)')
def run(): a = docopt.docopt(__doc__) vi_mode = bool(a['--vi']) config_dir = appdirs.user_config_dir('ptpython', 'prompt_toolkit') data_dir = appdirs.user_data_dir('ptpython', 'prompt_toolkit') if a['--config-dir']: # Override config_dir. config_dir = os.path.expanduser(a['--config-dir']) else: # Warn about the legacy directory. legacy_dir = os.path.expanduser('~/.ptpython') if os.path.isdir(legacy_dir): print( '{0} is deprecated, migrate your configuration to {1}'.format( legacy_dir, config_dir)) # Create directories. for d in (config_dir, data_dir): if not os.path.isdir(d) and not os.path.islink(d): os.mkdir(d) # Startup path startup_paths = [] if 'PYTHONSTARTUP' in os.environ: startup_paths.append(os.environ['PYTHONSTARTUP']) # --interactive if a['--interactive']: startup_paths.append(a['--interactive']) sys.argv = [a['--interactive']] + a['<arg>'] # Add the current directory to `sys.path`. if sys.path[0] != '': sys.path.insert(0, '') # When a file has been given, run that, otherwise start the shell. if a['<arg>'] and not a['--interactive']: sys.argv = a['<arg>'] path = a['<arg>'][0] with open(path, 'rb') as f: code = compile(f.read(), path, 'exec') # NOTE: We have to pass an empty dictionary as namespace. Omitting # this argument causes imports to not be found. See issue #326. six.exec_(code, {}) # Run interactive shell. else: enable_deprecation_warnings() # Apply config file def configure(repl): path = os.path.join(config_dir, 'config.py') if os.path.exists(path): run_config(repl, path) import __main__ embed(vi_mode=vi_mode, history_filename=os.path.join(data_dir, 'history'), configure=configure, locals=__main__.__dict__, globals=__main__.__dict__, startup_paths=startup_paths, title='Python REPL (ptpython)')