robotframework-pythonlibcore


Namerobotframework-pythonlibcore JSON
Version 4.4.1 PyPI version JSON
download
home_pagehttps://github.com/robotframework/PythonLibCore
SummaryTools to ease creating larger test libraries for Robot Framework using Python.
upload_time2024-04-05 22:27:12
maintainerNone
docs_urlNone
authorTatu Aalto
requires_python<4,>=3.8
licenseApache License 2.0
keywords robotframework testing testautomation library development
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Python Library Core

Tools to ease creating larger test libraries for [Robot
Framework](http://robotframework.org) using Python. The Robot Framework
[hybrid](https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#hybrid-library-api)
and [dynamic library
API](https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#dynamic-library-api)
gives more flexibility for library than the static library API, but they
also sets requirements for libraries which needs to be implemented in
the library side. PythonLibCore eases the problem by providing simpler
interface and handling all the requirements towards the Robot Framework
library APIs.

Code is stable and is already used by
[SeleniumLibrary](https://github.com/robotframework/SeleniumLibrary/)
and
[Browser library](https://github.com/MarketSquare/robotframework-browser/).
Project supports two latest version of Robot Framework.

[![Version](https://img.shields.io/pypi/v/robotframework-pythonlibcore.svg)](https://pypi.python.org/pypi/robotframework-pythonlibcore/)
[![Actions Status](https://github.com/robotframework/PythonLibCore/workflows/CI/badge.svg)](https://github.com/robotframework/PythonLibCore/actions)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)

## Usage

There are two ways to use PythonLibCore, either by
`HybridCore` or by using `DynamicCore`. `HybridCore` provides support for
the hybrid library API and `DynamicCore` provides support for dynamic library API.
Consult the Robot Framework [User
Guide](https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#creating-test-libraries),
for choosing the correct API for library.

Regardless which library API is chosen, both have similar requirements.

1)  Library must inherit either the `HybridCore` or `DynamicCore`.
2)  Library keywords must be decorated with Robot Framework
    [\@keyword](https://github.com/robotframework/robotframework/blob/master/src/robot/api/deco.py)
    decorator.
3)  Provide a list of class instances implementing keywords to
    `library_components` argument in the `HybridCore` or `DynamicCore` `__init__`.

It is also possible implement keywords in the library main class, by marking method with
`@keyword` as keywords. It is not required pass main library instance in the
`library_components` argument.

All keyword, also keywords implemented in the classes outside of the
main library are available in the library instance as methods. This
automatically publish library keywords in as methods in the Python
public API.

The example in below demonstrates how the PythonLibCore can be used with
a library.

# Example

``` python
"""Main library."""

from robotlibcore import DynamicCore

from mystuff import Library1, Library2


class MyLibrary(DynamicCore):
    """General library documentation."""

    def __init__(self):
        libraries = [Library1(), Library2()]
        DynamicCore.__init__(self, libraries)

    @keyword
    def keyword_in_main(self):
        pass
```

``` python
"""Library components."""

from robotlibcore import keyword


class Library1(object):

    @keyword
    def example(self):
        """Keyword documentation."""
        pass

    @keyword
    def another_example(self, arg1, arg2='default'):
        pass

    def not_keyword(self):
        pass


class Library2(object):

    @keyword('Custom name')
    def this_name_is_not_used(self):
        pass

    @keyword(tags=['tag', 'another'])
    def tags(self):
        pass
```

# Plugin API

It is possible to create plugin API to a library by using PythonLibCore.
This allows extending library with external Python classes. Plugins can
be imported during library import time, example by defining argumet in
library [\_\_init\_\_]{.title-ref} which allows defining the plugins. It
is possible to define multiple plugins, by seperating plugins with with
comma. Also it is possible to provide arguments to plugin by seperating
arguments with semicolon.

``` python
from robot.api.deco import keyword  # noqa F401

from robotlibcore import DynamicCore, PluginParser

from mystuff import Library1, Library2


class PluginLib(DynamicCore):

    def __init__(self, plugins):
        plugin_parser = PluginParser()
        libraries = [Library1(), Library2()]
        parsed_plugins = plugin_parser.parse_plugins(plugins)
        libraries.extend(parsed_plugins)
        DynamicCore.__init__(self, libraries)
```

When plugin class can look like this:

``` python
class MyPlugi:

    @keyword
    def plugin_keyword(self):
        return 123
```

Then Library can be imported in Robot Framework side like this:

``` robotframework
Library    ${CURDIR}/PluginLib.py    plugins=${CURDIR}/MyPlugin.py
```

# Translation

PLC supports translation of keywords names and documentation, but arguments names, tags and types
can not be currently translated. Translation is provided as a file containing
[Json](https://www.json.org/json-en.html) and as a
[Path](https://docs.python.org/3/library/pathlib.html) object. Translation is provided in
`translation` argument in the `HybridCore` or `DynamicCore` `__init__`. Providing translation
file is optional, also it is not mandatory to provide translation to all keyword.

The keys of json are the methods names, not the keyword names, which implements keyword. Value
of key is json object which contains two keys: `name` and `doc`. `name` key contains the keyword
translated name and `doc` contains keyword translated documentation. Providing
`doc` and `name` is optional, example translation json file can only provide translations only
to keyword names or only to documentatin. But it is always recomended to provide translation to
both `name` and `doc`.

Library class documentation and instance documetation has special keys, `__init__` key will
replace instance documentation and `__intro__` will replace libary class documentation.

## Example

If there is library like this:
```python
from pathlib import Path

from robotlibcore import DynamicCore, keyword

class SmallLibrary(DynamicCore):
    """Library documentation."""

    def __init__(self, translation: Path):
        """__init__ documentation."""
        DynamicCore.__init__(self, [], translation.absolute())

    @keyword(tags=["tag1", "tag2"])
    def normal_keyword(self, arg: int, other: str) -> str:
        """I have doc

        Multiple lines.
        Other line.
        """
        data = f"{arg} {other}"
        print(data)
        return data

    def not_keyword(self, data: str) -> str:
        print(data)
        return data

    @keyword(name="This Is New Name", tags=["tag1", "tag2"])
    def name_changed(self, some: int, other: int) -> int:
        """This one too"""
        print(f"{some} {type(some)}, {other} {type(other)}")
        return some + other
```

And when there is translation file like:
```json
{
    "normal_keyword": {
        "name": "other_name",
        "doc": "This is new doc"
    },
    "name_changed": {
        "name": "name_changed_again",
        "doc": "This is also replaced.\n\nnew line."
    },
    "__init__": {
        "name": "__init__",
        "doc": "Replaces init docs with this one."
    },
    "__intro__": {
        "name": "__intro__",
        "doc": "New __intro__ documentation is here."
    },
}
```
Then `normal_keyword` is translated to `other_name`. Also this keyword documentions is
translted to `This is new doc`. The keyword is `name_changed` is translted to
`name_changed_again` keyword and keyword documentation is translted to
`This is also replaced.\n\nnew line.`. The library class documentation is translated
to `Replaces init docs with this one.` and class documentation is translted to
`New __intro__ documentation is here.`

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/robotframework/PythonLibCore",
    "name": "robotframework-pythonlibcore",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4,>=3.8",
    "maintainer_email": null,
    "keywords": "robotframework testing testautomation library development",
    "author": "Tatu Aalto",
    "author_email": "aalto.tatu@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/71/89/5dc8c8186c897ee4b7d0b2631ebc90e679e8c8f04ea85505f96ad38aad64/robotframework-pythonlibcore-4.4.1.tar.gz",
    "platform": "any",
    "description": "# Python Library Core\n\nTools to ease creating larger test libraries for [Robot\nFramework](http://robotframework.org) using Python. The Robot Framework\n[hybrid](https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#hybrid-library-api)\nand [dynamic library\nAPI](https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#dynamic-library-api)\ngives more flexibility for library than the static library API, but they\nalso sets requirements for libraries which needs to be implemented in\nthe library side. PythonLibCore eases the problem by providing simpler\ninterface and handling all the requirements towards the Robot Framework\nlibrary APIs.\n\nCode is stable and is already used by\n[SeleniumLibrary](https://github.com/robotframework/SeleniumLibrary/)\nand\n[Browser library](https://github.com/MarketSquare/robotframework-browser/).\nProject supports two latest version of Robot Framework.\n\n[![Version](https://img.shields.io/pypi/v/robotframework-pythonlibcore.svg)](https://pypi.python.org/pypi/robotframework-pythonlibcore/)\n[![Actions Status](https://github.com/robotframework/PythonLibCore/workflows/CI/badge.svg)](https://github.com/robotframework/PythonLibCore/actions)\n[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)\n\n## Usage\n\nThere are two ways to use PythonLibCore, either by\n`HybridCore` or by using `DynamicCore`. `HybridCore` provides support for\nthe hybrid library API and `DynamicCore` provides support for dynamic library API.\nConsult the Robot Framework [User\nGuide](https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#creating-test-libraries),\nfor choosing the correct API for library.\n\nRegardless which library API is chosen, both have similar requirements.\n\n1)  Library must inherit either the `HybridCore` or `DynamicCore`.\n2)  Library keywords must be decorated with Robot Framework\n    [\\@keyword](https://github.com/robotframework/robotframework/blob/master/src/robot/api/deco.py)\n    decorator.\n3)  Provide a list of class instances implementing keywords to\n    `library_components` argument in the `HybridCore` or `DynamicCore` `__init__`.\n\nIt is also possible implement keywords in the library main class, by marking method with\n`@keyword` as keywords. It is not required pass main library instance in the\n`library_components` argument.\n\nAll keyword, also keywords implemented in the classes outside of the\nmain library are available in the library instance as methods. This\nautomatically publish library keywords in as methods in the Python\npublic API.\n\nThe example in below demonstrates how the PythonLibCore can be used with\na library.\n\n# Example\n\n``` python\n\"\"\"Main library.\"\"\"\n\nfrom robotlibcore import DynamicCore\n\nfrom mystuff import Library1, Library2\n\n\nclass MyLibrary(DynamicCore):\n    \"\"\"General library documentation.\"\"\"\n\n    def __init__(self):\n        libraries = [Library1(), Library2()]\n        DynamicCore.__init__(self, libraries)\n\n    @keyword\n    def keyword_in_main(self):\n        pass\n```\n\n``` python\n\"\"\"Library components.\"\"\"\n\nfrom robotlibcore import keyword\n\n\nclass Library1(object):\n\n    @keyword\n    def example(self):\n        \"\"\"Keyword documentation.\"\"\"\n        pass\n\n    @keyword\n    def another_example(self, arg1, arg2='default'):\n        pass\n\n    def not_keyword(self):\n        pass\n\n\nclass Library2(object):\n\n    @keyword('Custom name')\n    def this_name_is_not_used(self):\n        pass\n\n    @keyword(tags=['tag', 'another'])\n    def tags(self):\n        pass\n```\n\n# Plugin API\n\nIt is possible to create plugin API to a library by using PythonLibCore.\nThis allows extending library with external Python classes. Plugins can\nbe imported during library import time, example by defining argumet in\nlibrary [\\_\\_init\\_\\_]{.title-ref} which allows defining the plugins. It\nis possible to define multiple plugins, by seperating plugins with with\ncomma. Also it is possible to provide arguments to plugin by seperating\narguments with semicolon.\n\n``` python\nfrom robot.api.deco import keyword  # noqa F401\n\nfrom robotlibcore import DynamicCore, PluginParser\n\nfrom mystuff import Library1, Library2\n\n\nclass PluginLib(DynamicCore):\n\n    def __init__(self, plugins):\n        plugin_parser = PluginParser()\n        libraries = [Library1(), Library2()]\n        parsed_plugins = plugin_parser.parse_plugins(plugins)\n        libraries.extend(parsed_plugins)\n        DynamicCore.__init__(self, libraries)\n```\n\nWhen plugin class can look like this:\n\n``` python\nclass MyPlugi:\n\n    @keyword\n    def plugin_keyword(self):\n        return 123\n```\n\nThen Library can be imported in Robot Framework side like this:\n\n``` robotframework\nLibrary    ${CURDIR}/PluginLib.py    plugins=${CURDIR}/MyPlugin.py\n```\n\n# Translation\n\nPLC supports translation of keywords names and documentation, but arguments names, tags and types\ncan not be currently translated. Translation is provided as a file containing\n[Json](https://www.json.org/json-en.html) and as a\n[Path](https://docs.python.org/3/library/pathlib.html) object. Translation is provided in\n`translation` argument in the `HybridCore` or `DynamicCore` `__init__`. Providing translation\nfile is optional, also it is not mandatory to provide translation to all keyword.\n\nThe keys of json are the methods names, not the keyword names, which implements keyword. Value\nof key is json object which contains two keys: `name` and `doc`. `name` key contains the keyword\ntranslated name and `doc` contains keyword translated documentation. Providing\n`doc` and `name` is optional, example translation json file can only provide translations only\nto keyword names or only to documentatin. But it is always recomended to provide translation to\nboth `name` and `doc`.\n\nLibrary class documentation and instance documetation has special keys, `__init__` key will\nreplace instance documentation and `__intro__` will replace libary class documentation.\n\n## Example\n\nIf there is library like this:\n```python\nfrom pathlib import Path\n\nfrom robotlibcore import DynamicCore, keyword\n\nclass SmallLibrary(DynamicCore):\n    \"\"\"Library documentation.\"\"\"\n\n    def __init__(self, translation: Path):\n        \"\"\"__init__ documentation.\"\"\"\n        DynamicCore.__init__(self, [], translation.absolute())\n\n    @keyword(tags=[\"tag1\", \"tag2\"])\n    def normal_keyword(self, arg: int, other: str) -> str:\n        \"\"\"I have doc\n\n        Multiple lines.\n        Other line.\n        \"\"\"\n        data = f\"{arg} {other}\"\n        print(data)\n        return data\n\n    def not_keyword(self, data: str) -> str:\n        print(data)\n        return data\n\n    @keyword(name=\"This Is New Name\", tags=[\"tag1\", \"tag2\"])\n    def name_changed(self, some: int, other: int) -> int:\n        \"\"\"This one too\"\"\"\n        print(f\"{some} {type(some)}, {other} {type(other)}\")\n        return some + other\n```\n\nAnd when there is translation file like:\n```json\n{\n    \"normal_keyword\": {\n        \"name\": \"other_name\",\n        \"doc\": \"This is new doc\"\n    },\n    \"name_changed\": {\n        \"name\": \"name_changed_again\",\n        \"doc\": \"This is also replaced.\\n\\nnew line.\"\n    },\n    \"__init__\": {\n        \"name\": \"__init__\",\n        \"doc\": \"Replaces init docs with this one.\"\n    },\n    \"__intro__\": {\n        \"name\": \"__intro__\",\n        \"doc\": \"New __intro__ documentation is here.\"\n    },\n}\n```\nThen `normal_keyword` is translated to `other_name`. Also this keyword documentions is\ntranslted to `This is new doc`. The keyword is `name_changed` is translted to\n`name_changed_again` keyword and keyword documentation is translted to\n`This is also replaced.\\n\\nnew line.`. The library class documentation is translated\nto `Replaces init docs with this one.` and class documentation is translted to\n`New __intro__ documentation is here.`\n",
    "bugtrack_url": null,
    "license": "Apache License 2.0",
    "summary": "Tools to ease creating larger test libraries for Robot Framework using Python.",
    "version": "4.4.1",
    "project_urls": {
        "Homepage": "https://github.com/robotframework/PythonLibCore"
    },
    "split_keywords": [
        "robotframework",
        "testing",
        "testautomation",
        "library",
        "development"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ac6447d8403c7c0af89b46461640a2f67e49a5778062b8dd6eb3e128aa3c50cc",
                "md5": "51853f6f316e9cc52b8266f184a5d8fe",
                "sha256": "e0517129522aaa039eb2a28fd3d9720b7a0be0b90d0cbcb153a6c8016bb9e973"
            },
            "downloads": -1,
            "filename": "robotframework_pythonlibcore-4.4.1-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "51853f6f316e9cc52b8266f184a5d8fe",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": "<4,>=3.8",
            "size": 12452,
            "upload_time": "2024-04-05T22:27:10",
            "upload_time_iso_8601": "2024-04-05T22:27:10.353916Z",
            "url": "https://files.pythonhosted.org/packages/ac/64/47d8403c7c0af89b46461640a2f67e49a5778062b8dd6eb3e128aa3c50cc/robotframework_pythonlibcore-4.4.1-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "71895dc8c8186c897ee4b7d0b2631ebc90e679e8c8f04ea85505f96ad38aad64",
                "md5": "13924c9fbbf8b26259badcb3f22d79ee",
                "sha256": "2d695b2ea906f5815179643e29182466675e682d82c5fa9d1009edfae2f84b16"
            },
            "downloads": -1,
            "filename": "robotframework-pythonlibcore-4.4.1.tar.gz",
            "has_sig": false,
            "md5_digest": "13924c9fbbf8b26259badcb3f22d79ee",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4,>=3.8",
            "size": 12835,
            "upload_time": "2024-04-05T22:27:12",
            "upload_time_iso_8601": "2024-04-05T22:27:12.049234Z",
            "url": "https://files.pythonhosted.org/packages/71/89/5dc8c8186c897ee4b7d0b2631ebc90e679e8c8f04ea85505f96ad38aad64/robotframework-pythonlibcore-4.4.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-05 22:27:12",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "robotframework",
    "github_project": "PythonLibCore",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "robotframework-pythonlibcore"
}
        
Elapsed time: 0.23245s