plover-python-dictionary-cmd


Nameplover-python-dictionary-cmd JSON
Version 0.0.0 PyPI version JSON
download
home_pagehttps://github.com/user202729/plover-python-dictionary-cmd
SummaryExecute arbitrary command from a Python dictionary
upload_time2024-04-11 08:07:40
maintainerNone
docs_urlNone
authoruser202729
requires_pythonNone
licenseGNU General Public License v3 or later (GPLv3+)
keywords plover plover_plugin
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # plover-python-dictionary-cmd

[![PyPI](https://img.shields.io/pypi/v/plover-python-dictionary-cmd?style=flat)](https://pypi.python.org/pypi/plover-python-dictionary-cmd/)

Execute arbitrary command from a Python dictionary.

**Warning**: While this plugin can do everything what a command plugin can, this should
only be used for personal usage. If the usage is sufficiently general, it's recommended to make
a Plover command plugin instead.

See also: [`plover-run-shell`](https://github.com/user202729/plover_run_shell), [`plover-run-py`](https://github.com/user202729/plover-run-py), [`plover-open-url`](https://github.com/user202729/plover-comment).

## What problem does this plugin solve?

First, this assumes you know what a [Python dictionary](https://github.com/openstenoproject/plover_python_dictionary) is.

Maybe you want to write a dictionary that looks like this:

```python
LONGEST_KEY = 1

def lookup(key):
    if key == ("SKWR-F",):
        return "{PLOVER:OPEN_URL:https://www.openstenoproject.org/}"
```

The `{PLOVER:OPEN_URL:…}` obviously opens the said URL, using [Plover Open URL plugin](https://github.com/nsmarkop/plover_open_url).

Problem: what if the task you want to do is not already covered by some command plugin?

While you can certainly write a new command plugin, that is rather time-consuming.

The following **will not work**:

```python
import webbrowser

LONGEST_KEY = 1

def lookup(key):
    if key == ("SKWR-F",):
        webbrowser.open("https://www.openstenoproject.org/")
```
It's because the dictionary may be looked up **multiple times**.

## The solution

Write the plugin like the following.

```python
import webbrowser
import plover_python_dictionary_cmd

LONGEST_KEY = 1

@plover_python_dictionary_cmd.register
def f(engine):
    webbrowser.open("https://www.openstenoproject.org/")

def lookup(key):
    if key == ("SKWR-F",):
        return str(f)
```

As an extra bonus, you get access to the `engine` object inside the function `f` above.

## Internal implementation detail

It uses a global lookup table to store the reference to the function `f`. Then `str(f)` as above
returns something like `{plover:python_dictionary_cmd:123456}` where `123456` is some unique ID.

Don't rely on this implementation detail.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/user202729/plover-python-dictionary-cmd",
    "name": "plover-python-dictionary-cmd",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "plover plover_plugin",
    "author": "user202729",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/ca/75/cf96bc3f59ed1cc15bde1765138e30d55a5f41fd6491b6bc815ad30b1bf1/plover_python_dictionary_cmd-0.0.0.tar.gz",
    "platform": null,
    "description": "# plover-python-dictionary-cmd\n\n[![PyPI](https://img.shields.io/pypi/v/plover-python-dictionary-cmd?style=flat)](https://pypi.python.org/pypi/plover-python-dictionary-cmd/)\n\nExecute arbitrary command from a Python dictionary.\n\n**Warning**: While this plugin can do everything what a command plugin can, this should\nonly be used for personal usage. If the usage is sufficiently general, it's recommended to make\na Plover command plugin instead.\n\nSee also: [`plover-run-shell`](https://github.com/user202729/plover_run_shell), [`plover-run-py`](https://github.com/user202729/plover-run-py), [`plover-open-url`](https://github.com/user202729/plover-comment).\n\n## What problem does this plugin solve?\n\nFirst, this assumes you know what a [Python dictionary](https://github.com/openstenoproject/plover_python_dictionary) is.\n\nMaybe you want to write a dictionary that looks like this:\n\n```python\nLONGEST_KEY = 1\n\ndef lookup(key):\n    if key == (\"SKWR-F\",):\n        return \"{PLOVER:OPEN_URL:https://www.openstenoproject.org/}\"\n```\n\nThe `{PLOVER:OPEN_URL:\u2026}` obviously opens the said URL, using [Plover Open URL plugin](https://github.com/nsmarkop/plover_open_url).\n\nProblem: what if the task you want to do is not already covered by some command plugin?\n\nWhile you can certainly write a new command plugin, that is rather time-consuming.\n\nThe following **will not work**:\n\n```python\nimport webbrowser\n\nLONGEST_KEY = 1\n\ndef lookup(key):\n    if key == (\"SKWR-F\",):\n        webbrowser.open(\"https://www.openstenoproject.org/\")\n```\nIt's because the dictionary may be looked up **multiple times**.\n\n## The solution\n\nWrite the plugin like the following.\n\n```python\nimport webbrowser\nimport plover_python_dictionary_cmd\n\nLONGEST_KEY = 1\n\n@plover_python_dictionary_cmd.register\ndef f(engine):\n    webbrowser.open(\"https://www.openstenoproject.org/\")\n\ndef lookup(key):\n    if key == (\"SKWR-F\",):\n        return str(f)\n```\n\nAs an extra bonus, you get access to the `engine` object inside the function `f` above.\n\n## Internal implementation detail\n\nIt uses a global lookup table to store the reference to the function `f`. Then `str(f)` as above\nreturns something like `{plover:python_dictionary_cmd:123456}` where `123456` is some unique ID.\n\nDon't rely on this implementation detail.\n",
    "bugtrack_url": null,
    "license": "GNU General Public License v3 or later (GPLv3+)",
    "summary": "Execute arbitrary command from a Python dictionary",
    "version": "0.0.0",
    "project_urls": {
        "Homepage": "https://github.com/user202729/plover-python-dictionary-cmd"
    },
    "split_keywords": [
        "plover",
        "plover_plugin"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f193cbc327aaf78bb2cbc761bf4fc4cda3272c8f174db521d45d2e1c569997e7",
                "md5": "b4e6f88aaeb96ce2f57e10d681a54c47",
                "sha256": "0482106e880531c3dfc88d399a052afec3da92e09ae329424326637383083c3e"
            },
            "downloads": -1,
            "filename": "plover_python_dictionary_cmd-0.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b4e6f88aaeb96ce2f57e10d681a54c47",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 16012,
            "upload_time": "2024-04-11T08:07:38",
            "upload_time_iso_8601": "2024-04-11T08:07:38.617456Z",
            "url": "https://files.pythonhosted.org/packages/f1/93/cbc327aaf78bb2cbc761bf4fc4cda3272c8f174db521d45d2e1c569997e7/plover_python_dictionary_cmd-0.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ca75cf96bc3f59ed1cc15bde1765138e30d55a5f41fd6491b6bc815ad30b1bf1",
                "md5": "4c2e13a987c1ab9ab6a5e2d8202c96e7",
                "sha256": "95c90992c90d5804678637e939f48293e8748b82b518d1d65cfb624b16ed1c26"
            },
            "downloads": -1,
            "filename": "plover_python_dictionary_cmd-0.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "4c2e13a987c1ab9ab6a5e2d8202c96e7",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 15085,
            "upload_time": "2024-04-11T08:07:40",
            "upload_time_iso_8601": "2024-04-11T08:07:40.439947Z",
            "url": "https://files.pythonhosted.org/packages/ca/75/cf96bc3f59ed1cc15bde1765138e30d55a5f41fd6491b6bc815ad30b1bf1/plover_python_dictionary_cmd-0.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-11 08:07:40",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "user202729",
    "github_project": "plover-python-dictionary-cmd",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "plover-python-dictionary-cmd"
}
        
Elapsed time: 0.22686s