]> glassweightruler.freedombox.rocks Git - xdg-ninja.git/blobdiff - programs/python.json
docs: update the link of demo gif for xdg-ninja command output (#480)
[xdg-ninja.git] / programs / python.json
index 2841df996646c32a728e5ed462afa43e817efc6d..7322510dd5e2185e6d1e95ad4d2a412d886bea73 100644 (file)
@@ -1,9 +1,9 @@
 {
     "files": [
         {
-            "path": "$HOME/.python_history",
+            "help": "If you're running python v3.13.0a3 or later, you can simply set the `PYTHON_HISTORY` environment variable.\n\nOtherwise, export the following environment variable:\n\n```bash\nexport PYTHONSTARTUP=\"$XDG_CONFIG_HOME\"/python/pythonrc\n```\n\nThen create the file _$XDG_CONFIG_HOME/python/pythonrc_, and put the following code into it:\n\n```python\n#!/usr/bin/env python3\n# This entire thing is unnecessary post v3.13.0a3\n# https://github.com/python/cpython/issues/73965\n\ndef is_vanilla() -> bool:\n    \"\"\" :return: whether running \"vanilla\" Python <3.13 \"\"\"\n    import sys\n    return not hasattr(__builtins__, '__IPYTHON__') and 'bpython' not in sys.argv[0] and\nsys.version_info < (3, 13)\n\n\ndef setup_history():\n    \"\"\" read and write history from state file \"\"\"\n    import os\n    import atexit\n    import readline\n    from pathlib import Path\n\n    # Check PYTHON_HISTORY for future-compatibility with Python 3.13\n    if history := os.environ.get('PYTHON_HISTORY'):\n        history = Path(history)\n    # https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html#variables\n    elif state_home := os.environ.get('XDG_STATE_HOME'):\n        state_home = Path(state_home)\n    else:\n        state_home = Path.home() / '.local' / 'state'\n\n    history: Path = history or state_home / 'python_history'\n\n    # https://github.com/python/cpython/issues/105694\n    if not history.is_file():\n        readline.write_history_file(str(history)) # breaks on macos + python3 without this.\n\n    readline.read_history_file(history)\n    atexit.register(readline.write_history_file, history)\n\n\nif is_vanilla():\n    setup_history()\n```\n",
             "movable": true,
-            "help": "Export the following environment variables:\n\n```bash\nexport PYTHONSTARTUP=\"${XDG_CONFIG_HOME}/python/pythonrc\"\n```\n\nNow create the file _pythonrc_, and put the following code into it:\n\n```python\nimport os\nimport atexit\nimport readline\n\nhistory = os.path.join(os.environ['XDG_CACHE_HOME'], 'python_history')\ntry:\n    readline.read_history_file(history)\nexcept OSError:\n    pass\n\ndef write_history():\n    try:\n        readline.write_history_file(history)\n    except OSError:\n        pass\n\natexit.register(write_history)\n```\n\n_Note: This won't work if python is invoked with -i flag._\n\n_Credit:_ https://unix.stackexchange.com/a/675631/417527\n"
+            "path": "$HOME/.python_history"
         }
     ],
     "name": "python"