1

Implement customized python REPL

This commit is contained in:
Evan Pratten 2023-11-17 19:56:34 -05:00
parent 9867d0c92c
commit fa2248c98a
6 changed files with 114 additions and 3 deletions

View File

@ -55,7 +55,7 @@ if [ "$keybindings" = "[]" ] || [ "$keybindings" = "@as []" ]; then
# Allow Mod+Shift+Enter to open python
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom2/ name "Python REPL"
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom2/ command "gnome-terminal -- python3"
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom2/ command "gnome-terminal -- $EWCONFIG_ROOT/.config/ewconfig/scripts/tinker"
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom2/ binding "<Super><Shift>Return"
fi

View File

@ -0,0 +1,95 @@
"""Python Startup file. Used to customize the Python REPL"""
import sys
import os
# Global stuff
IS_IN_TINKER_MODE = bool(os.environ.get("PYTHON_TINKER_MODE"))
COLOR_ALLOWED = not bool(os.environ.get("NO_COLOR"))
def colored_string(text: str, color: str) -> str:
if COLOR_ALLOWED:
return "\033[" + color + "m" + text + "\033[0m"
else:
return text
# Configure the prompt
class Prompt:
def __init__(self):
self.prompt = colored_string(">>> ", "36")
def __str__(self):
return self.prompt
class MultiLinePrompt:
def __str__(self):
return colored_string("... ", "33")
# return " "
# Hook up the prompts
sys.ps1 = Prompt()
sys.ps2 = MultiLinePrompt()
# "Tinker mode" - automatically import common things
if IS_IN_TINKER_MODE:
print(
colored_string(
"Running in tinker mode. Additional modules are available.", "33"
)
)
# Basics
import time
import json
from pathlib import Path
from dataclasses import dataclass
from typing import (
List,
Dict,
Tuple,
Set,
Optional,
Union,
Any,
Callable,
Iterable,
Generator,
)
from pprint import pprint
from datetime import datetime
from textwrap import dedent
from base64 import b64encode, b64decode
# Math stuff
try:
import numpy as np
np.set_printoptions(suppress=True)
_vec = lambda *fields: np.array([*fields])
pi = np.pi
except ImportError:
pass
try:
from pyquaternion import Quaternion
except ImportError:
pass
try:
import matplotlib.pyplot as plt
except ImportError:
pass
# If we aren't in tinker mode, un-import sys and os
if not IS_IN_TINKER_MODE:
del sys
del os
# Clean up other stuff
del IS_IN_TINKER_MODE
del COLOR_ALLOWED
del colored_string
del Prompt
del MultiLinePrompt

View File

@ -12,5 +12,9 @@ export PATH="$HOME/bin:$PATH"
export PATH="$EWCONFIG_ROOT/scripts:$PATH"
export PATH="$HOME/.local/bin:$PATH"
# I want to be able to load my custom python modules
export PYTHONPATH="$EWCONFIG_ROOT/python_modules:$PYTHONPATH"
export PYTHONSTARTUP="$EWCONFIG_ROOT/configs/python/python_startup.py"
# A basic prompt to display user@host dir sign
export PS1="(${PS1_CTX:-bash}) \[\e[0;32m\]\u@\h \[\e[0;36m\]\w \[\e[0;36m\]\$ \[\e[0m\]"

View File

@ -27,6 +27,7 @@ export LD_LIBRARY_PATH="/usr/local/lib64:$LD_LIBRARY_PATH"
# I want to be able to load my custom python modules
export PYTHONPATH="$EWCONFIG_ROOT/python_modules:$PYTHONPATH"
export PYTHONSTARTUP="$EWCONFIG_ROOT/configs/python/python_startup.py"
# Configure a sane default editor
if type -p nvim > /dev/null; then

View File

@ -132,8 +132,8 @@ if [ -d ~/.config/blender/3.6 ]; then ln -sf $EWCONFIG_ROOT/configs/blender/3.x/
# -- Finalization --
# On systems that need it, configure Gnome
sh ./configs/gnome/gnome-terminal-settings.sh
sh ./configs/gnome/desktop-settings.sh
sh ./configs/gnome/gnome-terminal-settings.sh || true
sh ./configs/gnome/desktop-settings.sh || true
# Attempt to force a termux settings reload on Android devices
termux-reload-settings || true

11
scripts/tinker Executable file
View File

@ -0,0 +1,11 @@
#! /bin/bash
# NOTE: This is a script so that we can launch it from a gnome keybind
set -e
# If PYTHONSTARTUP is not set, we have to set it
if [ -z "$PYTHONSTARTUP" ]; then
export PYTHONSTARTUP="$HOME/.config/ewconfig/configs/python/python_startup.py"
fi
# Launch python
PYTHON_TINKER_MODE=1 python3