X-Git-Url: https://glassweightruler.freedombox.rocks/gitweb/xdg-ninja.git/blobdiff_plain/6efc7cf13187ec9ae2bff84f52a0a77449625ae4..bea3ec92f3d998eecfd3f46fcd33f90da0aec509:/programs/python.json?ds=inline diff --git a/programs/python.json b/programs/python.json index 99b2cf1..c9884bc 100644 --- a/programs/python.json +++ b/programs/python.json @@ -3,7 +3,7 @@ { "path": "$HOME/.python_history", "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" + "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" } ], "name": "python"