{"id":13638433,"url":"https://github.com/anki-code/xontrib-macro","last_synced_at":"2025-03-21T08:31:22.328Z","repository":{"id":62590100,"uuid":"352028930","full_name":"anki-code/xontrib-macro","owner":"anki-code","description":"Library of the useful macros for the xonsh shell.","archived":false,"fork":false,"pushed_at":"2024-03-16T08:07:15.000Z","size":117,"stargazers_count":15,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-05-21T06:56:02.247Z","etag":null,"topics":["docker","fopen","json","macro","macros","xml","xonsh","xontrib","yaml"],"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/anki-code.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"custom":["https://github.com/anki-code","https://www.buymeacoffee.com/xxh","https://github.com/xonsh/xonsh#the-xonsh-shell-community"]}},"created_at":"2021-03-27T09:11:56.000Z","updated_at":"2024-08-02T01:14:41.288Z","dependencies_parsed_at":"2024-01-29T10:09:28.231Z","dependency_job_id":"33027fec-02e0-4acf-bc4a-4a1cadce2c3a","html_url":"https://github.com/anki-code/xontrib-macro","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anki-code%2Fxontrib-macro","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anki-code%2Fxontrib-macro/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anki-code%2Fxontrib-macro/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anki-code%2Fxontrib-macro/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/anki-code","download_url":"https://codeload.github.com/anki-code/xontrib-macro/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244765019,"owners_count":20506747,"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":["docker","fopen","json","macro","macros","xml","xonsh","xontrib","yaml"],"created_at":"2024-08-02T01:00:45.744Z","updated_at":"2025-03-21T08:31:21.745Z","avatar_url":"https://github.com/anki-code.png","language":"Python","funding_links":["https://github.com/anki-code","https://www.buymeacoffee.com/xxh","https://github.com/xonsh/xonsh#the-xonsh-shell-community"],"categories":["Plugins","See also"],"sub_categories":["Prompt tweaks","pytest: xonsh not found"],"readme":"\u003cp align=\"center\"\u003e\nLibrary of the useful \u003ca href=\"https://xon.sh/tutorial_macros.html\"\u003emacros\u003c/a\u003e for the \u003ca href=\"https://xon.sh/\"\u003exonsh shell\u003c/a\u003e.\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e  \nIf you like the idea click ⭐ on the repo and \u003ca href=\"https://twitter.com/intent/tweet?text=Nice%20xontrib%20for%20the%20xonsh%20shell!\u0026url=https://github.com/anki-code/xontrib-macro\" target=\"_blank\"\u003etweet\u003c/a\u003e.\n\u003c/p\u003e\n\n\n## Installation\n\nTo install use pip:\n\n```bash\nxpip install xontrib-macro\n# or: xpip install -U git+https://github.com/anki-code/xontrib-macro\n```\n\n## Usage\n\nBy loading the whole module - recommended for interactive usage (type `macro.\u003cTab\u003e`): \n```xsh\nxontrib load macro\nwith! macro.data.Write('/tmp/hello', replace=True):  # more macros below\n    world\n```\n\nBy importing certain macro - recommended for scripts:\n```xsh\nfrom xontrib.macro.data import Write\nwith! Write('/tmp/hello', replace=True):  # more macros below\n    world\n```\n\n## Macros\n\n### Block (xonsh builtin)\n```python\nfrom xonsh.contexts import Block\n\nwith! Block() as b:\n    any\n    text\n    here\n\nb.macro_block\n# 'any\\ntext\\nhere\\n\\n'\nb.lines\n# ['any', 'text', 'here', '']\n```\n\n### data.Write\n\nWrite a file from block ([rich list of parameters](https://github.com/anki-code/xontrib-macro/blob/main/xontrib/macro/data.py#L12)):\n\n```xsh\nfrom xontrib.macro.data import Write\n\nwith! Write('/tmp/t/hello.xsh', chmod=0o700, replace=True, makedir=True, format={'name': 'world'}, verbose=True):\n    echo {name}\n    \n## Make directories: /tmp/t\n## Write to file: /tmp/t/hello.xsh\n## Set chmod: rw- --- ---\n## Set exec:  rwx --- ---\n\n/tmp/t/hello.xsh\n# world\n```\nThere is also `data.Replace()` macro with `mode='w', replace=True, makedir=True, replace_keep='a'`.\n\nNote! There is an upstream issue described below in \"Known issues\" section - the first lines that begin from `#` will be ignored in the block. As workaround to create [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)) use `Write(..., shebang=\"#!/bin/xonsh\")`.\n\n### data.JsonBlock\n\nMake json block and use it as dict:\n\n```python\nfrom xontrib.macro.data import JsonBlock\n\nwith! JsonBlock() as j:\n    {\"hello\": \"world\"}\n\nj['hello']\n# 'world'\n```\n\n### data.XmlBlock\n\nSimple XML macro context manager from [xonsh macro tutorial](https://xon.sh/tutorial_macros.html#context-manager-macros). This will return the parsed XML tree from a macro block\n\n```xsh\nfrom xontrib.macro.data import XmlBlock\n\nwith! XmlBlock() as tree:\n    \u003cnote\u003e\n      \u003cheading\u003eHello world!\u003c/heading\u003e\n      \u003cbody\u003e\n        Hello!\n      \u003c/body\u003e\n    \u003c/note\u003e\n\ntype(tree)\n# xml.etree.ElementTree.Element\n\ntree.find('body').text\n# '\\n    Hello!\\n  '\n\n```\n\n### run.Once\n\nRun the code once and save mark about it in [XONSH_DATA_DIR](https://xon.sh/envvars.html#xonsh-data-dir). \nIn the next run the code will not be executed if it was not changed. If the code will be changed it will be executed again.\n\nExample:\n```python\nfrom xontrib.macro.run import Once\n\nwith! Once('First install'):\n    if $(which pacman):\n        pacman -S vim htop\n    elif $(which apt):\n        apt update \u0026\u0026 apt install -y vim htop\n```\n\n### podman.RunInPodman\n\n```xsh\nfrom xontrib.macro.container import RunInPodman as podman\n\nwith! podman():  # default: image='ubuntu', executor='bash'\n    echo hello\n\n# hello\n```\n\n### podman.RunInXonshPodman\n\n```python\nfrom xontrib.macro.container import RunInXonshPodman as Pod\n\nwith! Pod():  # default: image='xonsh/xonsh:slim', executor='/usr/local/bin/xonsh'\n   echo Installing...\n   pip install -U -q pip lolcat\n   echo \"We are in podman container now!\" | lolcat\n   \n# We are in podman container now! (colorized)\n```\n\nThis is the same as:\n```python\npodman run -it --rm xonsh/xonsh:slim xonsh -c @(\"\"\"\npip install -U -q pip lolcat\necho \"We are in podman container now!\" | lolcat\n\"\"\")\n```\n\n### signal.DisableInterrupt\n\nThis macro allows disabling SIGINT (Ctrl+C) for group of commands.\n\n```xsh\nfrom xontrib.macro.signal import DisableInterrupt\n\necho start\nwith! DisableInterrupt():\n    echo 'sleep start'\n    sleep 10\n    echo 'sleep end'\necho finish\n\n# start\n# sleep start\n# [Press Ctrl+C]\n# KeyboardInterrupt will be raised at the end of current transaction.\n# sleep end\n# Exception KeyboardInterrupt: KeyboardInterrupt was received during transaction.\n```\n\n## Known issues\n\nContext Manager Macro picks up comments from outside the block and ignore the initial comments in the block ([4207](https://github.com/xonsh/xonsh/issues/4207)). We can fix it in the xontrib by checking the indentation in the beginning line and the end line. PR is welcome!\n\n## Related\n\n* [spec-mod](https://github.com/anki-code/xontrib-spec-mod) - Library of xonsh subprocess specification modifiers e.g. `$(@json echo '{}')`. \n\n## Credits\n\nThis package was created with [xontrib cookiecutter template](https://github.com/xonsh/xontrib-cookiecutter).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanki-code%2Fxontrib-macro","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanki-code%2Fxontrib-macro","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanki-code%2Fxontrib-macro/lists"}