]> glassweightruler.freedombox.rocks Git - xdg-ninja.git/blobdiff - programs/python.json
Add crossnote.json (~/.mume) (#386)
[xdg-ninja.git] / programs / python.json
index c9884bcc8c860b0f856b66412cd73fc0328c22fe..9ad7bbd361054ca91dd80ac49041c66eaf188607 100644 (file)
@@ -1,10 +1,10 @@
 {
     "files": [
         {
 {
     "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 \"\"\"\n    import sys\n    return not hasattr(__builtins__, '__IPYTHON__') and 'bpython' not in sys.argv[0]\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    # https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html#variables\n    if state_home := os.environ.get('XDG_STATE_HOME'):\n            state_home = Path(state_home)\n    else:\n        state_home = Path.home() / '.local' / 'state'\n    if not state_home.is_dir():\n        print(\"Error: XDG_SATE_HOME does not exist at\", state_home)\n\n    history: Path = state_home / 'python_history'\n\n    # https://github.com/python/cpython/issues/105694\n    if not history.is_file():\n        with open(history,\"w\") as f:\n            f.write(\"_HiStOrY_V2_\" + \"\\n\\n\") # 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,
             "movable": true,
-            "help": "Export the following environment variables:\n\n```bash\nexport PYTHONSTARTUP=\"/etc/python/pythonrc\"\n```\n\nNow create the file _/etc/python/pythonrc_, and put the following code into it:\n\n```python\nimport os\nimport atexit\nimport readline\nfrom pathlib import Path\n\nif readline.get_current_history_length() == 0:\n\n    state_home = os.environ.get(\"XDG_STATE_HOME\")\n    if state_home is None:\n        state_home = Path.home() / \".local\" / \"state\"\n    else:\n        state_home = Path(state_home)\n\n    history_path = state_home / \"python_history\"\n    if history_path.is_dir():\n        raise OSError(f\"'{history_path}' cannot be a directory\")\n\n    history = str(history_path)\n\n    try:\n        readline.read_history_file(history)\n    except OSError: # Non existent\n        pass\n\n    def write_history():\n        try:\n            readline.write_history_file(history)\n        except OSError:\n            pass\n\n    atexit.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/questions/630642/change-location-of-python-history"
+            "path": "$HOME/.python_history"
         }
     ],
     "name": "python"
         }
     ],
     "name": "python"
-}
+}
\ No newline at end of file