lockable


Namelockable JSON
Version 0.11.1 PyPI version JSON
download
home_pagehttps://github.com/jupe/py-lockable
Summarylockable resource module
upload_time2024-06-13 10:50:56
maintainerNone
docs_urlNone
authorJussi Vatjus-Anttila
requires_python<4,>=3.8
licenseNone
keywords py.test pytest lockable resource
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            # lockable

[![CircleCI](https://circleci.com/gh/jupe/py-lockable/tree/master.svg?style=svg)](https://circleci.com/gh/jupe/py-lockable/tree/master)
[![PyPI version](https://badge.fury.io/py/lockable.svg)](https://pypi.org/project/lockable/)
[![Coverage Status](https://coveralls.io/repos/github/jupe/pytest-lockable/badge.svg)](https://coveralls.io/github/jupe/py-lockable)

Resource locking module for python.

Originally designed for following projects:
* [pytest-lockable](https://github.com/jupe/pytest-lockable)
* [robot-lockable](https://github.com/jupe/robot-lockable)


Module provides python API and simple CLI interface.

Resource is released in following cases:
* process ends
* when context ends when `lockable.auto_lock(..)` is used
* allocation.unlock() is called
* lockable.unlock(<allocation>) is called

Resources data provider support following mechanisms:
* `resources.json` file in file system
* python list of dictionaries
* http uri which points to API and is used with HTTP GET method. API should provide `resources.json` data as json object.

# CLI interface

```
% lockable --help
usage: lockable [-h] [--validate-only] [--lock-folder LOCK_FOLDER] [--resources RESOURCES]
                [--timeout TIMEOUT] [--hostname HOSTNAME]
                [--requirements REQUIREMENTS]
                [command [command ...]]

run given command while suitable resource is allocated.
Usage example: lockable --requirements {"online":true} echo using resource: $ID

positional arguments:
  command               Command to be execute during device allocation

optional arguments:
  -h, --help            show this help message and exit
  --validate-only       Only validate resources.json
  --lock-folder LOCK_FOLDER
                        lock folder
  --resources RESOURCES
                        Resources file (utf-8) or http uri
  --timeout TIMEOUT     Timeout for trying allocate suitable resource
  --hostname HOSTNAME   Hostname
  --requirements REQUIREMENTS
                        requirements as json string

```

# API's

Constructor
```python
lockable = Lockable([hostname], [resource_list_file], [resource_list], [lock_folder])
```

Allocation
```python
allocation_context = lockable.lock(requirements, [timeout_s])
print(allocation_context.resource_info)
print(allocation_context.resource_id)
allocation_context.unlock()
# or using resource info
lockable.unlock(allocation_context)
```

Allocation context contains following API:
* `requirements: dict` Original requirements for allocation
* `resource_info: dict` Allocated resource information
* `unlock(): func`  release resource lock function
* `allocation_queue_time: timedelta` How long waited before allocation
* `allocation_start_time: datetime` when allocation was started
* `release_time: datetime` when allocation was ended
* `alloc_id: str` allocation id
* `allocation_durations: timedelta` how long time allocation takes

or using context manager which unlock automatically
```python
with lockable.auto_lock(requirements, [timeout_s]) as allocation:
    print(allocation.resource_info)
```

**Tips:**

You can allocate also offline devices by set requirements `"online": None` . 
You can ignore also `hostname` same same way by  setting it to  None`



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/jupe/py-lockable",
    "name": "lockable",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4,>=3.8",
    "maintainer_email": null,
    "keywords": "py.test pytest lockable resource",
    "author": "Jussi Vatjus-Anttila",
    "author_email": "jussiva@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/59/73/f737a5e57d01b8c3c362adcd7de296362b251b3d5949da66aa95ae276426/lockable-0.11.1.tar.gz",
    "platform": null,
    "description": "# lockable\n\n[![CircleCI](https://circleci.com/gh/jupe/py-lockable/tree/master.svg?style=svg)](https://circleci.com/gh/jupe/py-lockable/tree/master)\n[![PyPI version](https://badge.fury.io/py/lockable.svg)](https://pypi.org/project/lockable/)\n[![Coverage Status](https://coveralls.io/repos/github/jupe/pytest-lockable/badge.svg)](https://coveralls.io/github/jupe/py-lockable)\n\nResource locking module for python.\n\nOriginally designed for following projects:\n* [pytest-lockable](https://github.com/jupe/pytest-lockable)\n* [robot-lockable](https://github.com/jupe/robot-lockable)\n\n\nModule provides python API and simple CLI interface.\n\nResource is released in following cases:\n* process ends\n* when context ends when `lockable.auto_lock(..)` is used\n* allocation.unlock() is called\n* lockable.unlock(<allocation>) is called\n\nResources data provider support following mechanisms:\n* `resources.json` file in file system\n* python list of dictionaries\n* http uri which points to API and is used with HTTP GET method. API should provide `resources.json` data as json object.\n\n# CLI interface\n\n```\n% lockable --help\nusage: lockable [-h] [--validate-only] [--lock-folder LOCK_FOLDER] [--resources RESOURCES]\n                [--timeout TIMEOUT] [--hostname HOSTNAME]\n                [--requirements REQUIREMENTS]\n                [command [command ...]]\n\nrun given command while suitable resource is allocated.\nUsage example: lockable --requirements {\"online\":true} echo using resource: $ID\n\npositional arguments:\n  command               Command to be execute during device allocation\n\noptional arguments:\n  -h, --help            show this help message and exit\n  --validate-only       Only validate resources.json\n  --lock-folder LOCK_FOLDER\n                        lock folder\n  --resources RESOURCES\n                        Resources file (utf-8) or http uri\n  --timeout TIMEOUT     Timeout for trying allocate suitable resource\n  --hostname HOSTNAME   Hostname\n  --requirements REQUIREMENTS\n                        requirements as json string\n\n```\n\n# API's\n\nConstructor\n```python\nlockable = Lockable([hostname], [resource_list_file], [resource_list], [lock_folder])\n```\n\nAllocation\n```python\nallocation_context = lockable.lock(requirements, [timeout_s])\nprint(allocation_context.resource_info)\nprint(allocation_context.resource_id)\nallocation_context.unlock()\n# or using resource info\nlockable.unlock(allocation_context)\n```\n\nAllocation context contains following API:\n* `requirements: dict` Original requirements for allocation\n* `resource_info: dict` Allocated resource information\n* `unlock(): func`  release resource lock function\n* `allocation_queue_time: timedelta` How long waited before allocation\n* `allocation_start_time: datetime` when allocation was started\n* `release_time: datetime` when allocation was ended\n* `alloc_id: str` allocation id\n* `allocation_durations: timedelta` how long time allocation takes\n\nor using context manager which unlock automatically\n```python\nwith lockable.auto_lock(requirements, [timeout_s]) as allocation:\n    print(allocation.resource_info)\n```\n\n**Tips:**\n\nYou can allocate also offline devices by set requirements `\"online\": None` . \nYou can ignore also `hostname` same same way by  setting it to  None`\n\n\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "lockable resource module",
    "version": "0.11.1",
    "project_urls": {
        "Bug Reports": "https://github.com/jupe/pytest-lockable",
        "Homepage": "https://github.com/jupe/py-lockable",
        "Source": "https://github.com/jupe/pytest-lockable/"
    },
    "split_keywords": [
        "py.test",
        "pytest",
        "lockable",
        "resource"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b4e393bc23e87bd584457aaebc50af1b6a3136fcc217cc270daad05a87044c03",
                "md5": "925dff190756a28a14657e243f684212",
                "sha256": "ce5ae1e9bdbecba33c26c1cf7e0618c9f80f3a147fb598cb015283d48ed2db8f"
            },
            "downloads": -1,
            "filename": "lockable-0.11.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "925dff190756a28a14657e243f684212",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4,>=3.8",
            "size": 13548,
            "upload_time": "2024-06-13T10:50:54",
            "upload_time_iso_8601": "2024-06-13T10:50:54.130701Z",
            "url": "https://files.pythonhosted.org/packages/b4/e3/93bc23e87bd584457aaebc50af1b6a3136fcc217cc270daad05a87044c03/lockable-0.11.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5973f737a5e57d01b8c3c362adcd7de296362b251b3d5949da66aa95ae276426",
                "md5": "073837e7db3f5d576924132150a1a06d",
                "sha256": "e75a45466e8484413ac0d78bb15529fa46e49d4296abe371359db57f6cfa2619"
            },
            "downloads": -1,
            "filename": "lockable-0.11.1.tar.gz",
            "has_sig": false,
            "md5_digest": "073837e7db3f5d576924132150a1a06d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4,>=3.8",
            "size": 24014,
            "upload_time": "2024-06-13T10:50:56",
            "upload_time_iso_8601": "2024-06-13T10:50:56.542339Z",
            "url": "https://files.pythonhosted.org/packages/59/73/f737a5e57d01b8c3c362adcd7de296362b251b3d5949da66aa95ae276426/lockable-0.11.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-06-13 10:50:56",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "jupe",
    "github_project": "py-lockable",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "circle": true,
    "requirements": [],
    "lcname": "lockable"
}
        
Elapsed time: 4.07693s