Skip to content

Respo create CLI

CLI

Respo CLI command is based on click library. It is not included in module by default, you can install it using pip install respo[cli] or the best in development, pip install respo[all].

We will use respo_model.yml file generated in provious section.

Respo create

Ok, we already declared resource policies in yml file. There are few steps before we can use it in Python code, we must:

  • read
  • validate
  • resolve complex rules and nested roles
  • save

Note

To get option preview, use respo --help and respo create --help

You can also change default folder paths of generated files below, using environment variables.

Every part will be covered in another section, now let's use respo create command.

1
2
3
4
5
6
7
$ respo create respo_model.yml

INFO: Validating respo model from respo_model.yml...
INFO: Saved binary file to .respo_cache/__auto__respo_model.bin
INFO: Saved python file to respo_model.py
INFO: Processed in 0.0239s. Bin file size: 0.0013 mb.
INFO: Success!

Your folder structure after success should look like:

1
2
3
4
5
6
.
├── respo_model.yml
├── respo_model.py # new Python file
|
├── .respo_cache # read-only, processed model files
│   ├── __auto__respo_model.bin

Pickled, resolved input file was saved by default to .respo_cache folder. It should not be included in .gitignore. It allows better performence when reading policy, no need to validate and resolve input yml file every time on app startup and prevents developer mistake.

But there is also another file, respo_model.py with following content:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"""
Auto generated using respo create command
Docs: https://rafsaf.github.io/respo/
"""

import typing

import respo


class RespoModel(respo.RespoModel):
    if typing.TYPE_CHECKING:

        class _ROLES(respo.ROLESContainer):
            ADMIN: str
            DEFAULT: str
            PRO_USER: str
            SUPERADMIN: str

        class _PERMS(respo.PERMSContainer):
            BOOK__ALL: str
            BOOK__BUY: str
            BOOK__LIST: str
            BOOK__READ: str
            BOOK__SELL: str
            USER__ALL: str
            USER__READ_ALL: str
            USER__READ_ALL_BETTER: str
            USER__READ_BASIC: str
            USER__UPDATE: str

        PERMS: _PERMS
        ROLES: _ROLES

        @staticmethod
        def get_respo_model() -> "RespoModel":
            return respo.RespoModel.get_respo_model()  # type: ignore

This auto-generated file provides best autocompletion support possible in your Python code, note whole logic is wrapped in typing.TYPE_CHECKING, it will be understood by your IDE, but generates no additional overhead on the runtime.