{"id":13343232,"url":"https://github.com/Techcable/zsh2xonsh","last_synced_at":"2025-03-12T04:32:58.883Z","repository":{"id":57478575,"uuid":"445746937","full_name":"Techcable/zsh2xonsh","owner":"Techcable","description":"A highly-compatible translator from zsh -\u003e xonsh","archived":false,"fork":false,"pushed_at":"2023-01-25T21:39:46.000Z","size":107,"stargazers_count":3,"open_issues_count":2,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-09-30T10:10:05.486Z","etag":null,"topics":["compat","python","xonsh","zsh"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Techcable.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-01-08T06:52:25.000Z","updated_at":"2024-02-03T06:22:44.000Z","dependencies_parsed_at":"2023-02-14T11:45:54.494Z","dependency_job_id":null,"html_url":"https://github.com/Techcable/zsh2xonsh","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Techcable%2Fzsh2xonsh","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Techcable%2Fzsh2xonsh/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Techcable%2Fzsh2xonsh/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Techcable%2Fzsh2xonsh/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Techcable","download_url":"https://codeload.github.com/Techcable/zsh2xonsh/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221270037,"owners_count":16788788,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["compat","python","xonsh","zsh"],"created_at":"2024-07-29T19:30:42.814Z","updated_at":"2024-10-24T03:30:32.371Z","avatar_url":"https://github.com/Techcable.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"zsh2xonsh ![pypy](https://img.shields.io/pypi/v/zsh2xonsh) ![powered-by-xonsh](https://img.shields.io/badge/powered%20by-xonsh-brightgreen)\n=========\nHave you heard of [xonsh](https://xon.sh/)? It's a Python-powered shell.\n\nYou can do amazing things like this:\n````xonsh\n# Interpolate python -\u003e shell\necho @(i for i in range(42))\n\n# Interpolate shell -\u003e python\nfor filename in `.*`:\n    print(filename)\n    du -sh @(filename)\n\n# Execute regular shell commands too\ncat /etc/passwd | grep root\n````\n\nAs you can immagine, this awesomeness is not POSIX-compliant :(\n\nThis makes it difficult to setup your `$PATH` and do things like `eval $(brew shellenv)`\n\nThis package exists to translate traditional `zsh` scripts into `xonsh` code.\n\n## Compatibility (and how it works)\nThe goal is 100% compatibility for a *subset* of shell. \n\nCompatibility is achived by delegating most of the work to zsh.\n\nThat is, `export FOO=\"$(echo bar)\"` in a shell script becomes (essentially)  `$FOO=$(zsh -c 'echo bar')` in xonsh.\n\nWe have `zsh` handle all the globbing/quoting/bizzare POSIX quirks.\n\nIn the face of ambiguity, or if we encounter an unsupported feature (like a `for` loop), then we fail-fast.\n\nThis is the most important feature. If something can't be supported 100%, then it will throw a descriptive error. Anything else is a bug :)\n\n### Features\nThe included shell features include:\n\n1. Quoted expressions `\"$VAR glob/*\"` (zsh does expansion here)\n2. Unquoted literals `12`, `foo` `~/foo` (mostly translated directly)\n3. Command substitutions \"$(cat file.txt | grep bar)\" \n   - zsh does all the work here\n   - Supports both quoted and unquoted forms\n3. If/then statements\n   - Conditionals are executed by zsh (so `[[ -d \"foo\" ]]` works perfectly)\n   - Translated into python if (so body will not run unless conditional passes)\n4. Exporting variables `export FOO=$BAR`\n   - Translates `$PATH` correctly (xonsh thinks it's a list, zsh thinks it's a string)\n   - This is where the subprocess approach doesn't work blindly....\n      - We support it cleanly by doing the left-hand assignment xonsh, and the right-hand expression in `zsh` :)\n   - Local variables (local var=x) are supported too (with the proper scoping)\n5. Support `alias foo=\"bar\"`\n   - This even supports globbing in the alias, so `alias lsdot=\"echo .*\"` would glob in the same way that zsh does (experimental)\n6. Basic support for function declarations (and having positional arguments)\n   - Local variables are scoped properly inside the function :)\n7. Support for 'echo' builtin as a python 'print'\n\nYou can also add \"external builtins\", which are extra commands that the script can invoke. In my own files, I use this to add an `extend_path` command (even though zsh2xonsh properly translates $PATH variables already, it's still a nice utility)\n\nMy [macbook config](./examples/macbook2021-config.zsh) is a good example of the full power of the translator. It supports function declarations, if/then statements and conditionals (all in an attempt to cleanup homebrew cludges lol).\n\nAll of these behaviors should be 100% compatible with their zsh equivalents.\nIf you try anything else (or encounter an unsupported corner case), then you will get a descripitive error :)\n\n## Installation \u0026 Usage\nThis is a pypi package, install it with `pip install zsh2xonsh`.\n\nThe API is simple, run `translate_to_xonsh(str) -\u003e str` to translate from `zsh` -\u003e `xonsh` code.\nThis does not require xonsh at runtime, and can be done ahead of time. \n\nIf you want to evaluate the code immediately after translating it (for example in a `.xonshrc`), you can use\n. This requires xonsh at runtime (obviously) and uses the `evalx` builtin.\n\nAdditionally you can use the CLI (`python -m zsh2xonsh`), which accepts an import.\n\nIf you want to provide extra utility functions to your code, you can define `extra_builtins`.\n\n### Example\nIn my `.xonshrc`, I dynamically translate and evaluate the output of `brew shellenv`:\n````xonsh\nzsh2xonsh.translate_to_xonsh_and_eval($(/opt/homebrew/bin/brew shellenv))\n````\n\nLikewise, I do the same with my machine-specific `.config.zsh` files (in the example directory).\n\n## Motiviation\nFirst of all, I need support for `eval $(brew shellenv)` .\n\nSecond of all, I still use `zsh` as my backup shell (by the use of my [wrap-shell](https://github.com/Techcable/wrap-shell) utility).\n\nThis means I need to setup `$PATH` and other enviornment variables for both of these shells, indepenently of each other.\n\nThe natural way to set these up is by using shell scripts.\nI have a seperate one for each of my different machines (server, macbook, old lapotop, etc)\n\nFor each of these (tiny) shell scripts, `zsh2xonsh` works very well :)\n\nSo in addition to properly translating `$(brew shellenv)`,\nit also needs to translate these basic shell \"environement files\".\n\nTurns out, dealing with platform specific quirks can get pretty intense and requires things like function definitions and if/then statements.\nIn order to properly support `export`s, control flow has to be emulated in Python (can't just delegate to zsh).","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTechcable%2Fzsh2xonsh","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FTechcable%2Fzsh2xonsh","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTechcable%2Fzsh2xonsh/lists"}