Skip to main content

A simple permission rule parsing library.

Project description

permtab

A simple permission rule parsing library.

Installation

Install permtab via pip:

pip install permtab

Format

Permtab uses a syntax similar to crontab, although it is actually a reversed version. Actually, the shlex is used for parsing.

# *.permtab
<rulename> <filter...>

Here filter has the same meaning with rule and condition.

NOTE: Leading a rule to True only needs one filter that returns True.

Here is an example:

myrule1     owner_1234  operator_8525   operator_8390
myrule2     text_hello  text_world      "text_hello world!"
*           user_any
# The tab rule with name '*' is considered a base rule.
# If not defined, the default rule will pass everything to other rules.

Rule factory

Rule factory is a function which exports a checker function.

This is a generic definition:

import permtab

def factory(*args, **kwargs):
    def _checker(*args, **kwargs):
        return CONDITION
    return _checker

permtab.register_rulefactory(REGEX_FOR_THE_FACTORY, factory)

Exactly, the parameters for rule factory and checker depend on their use.

The parameters for rule factory is decided by groups in provided regex. All groups that matched are unpacked to positional arguments, and all named groups that matched are unpacked to keyword arguments.

Checkers' parameters are decided by where the checker called.

NOTE: Parameters provided by named groups are all in common groups.

NOTE: Dependency injection will not work by default, so you must patch some functions to export new functions that support dependency injection if needed.

Loading and using permtab

This step is quite easy:

import permtab

permtab.load("/path/to/your.permtab")  # load "/path/to/your.permtab"

rule = permtab.find_rule("your_rule_name")  # get rule function, then use it

def f():
    if rule(...):
        ...  # run if the condition is satisfied
    else:
        ...  # otherwise

Editing a permtab

Editing uses a + and - marker on a rule to control changes.

Here is an example:

import permtab

parsed = permtab.parse("test.permtab")
# DO NOT parse and edit the SAME FILE at the same time
# Otherwise your data may be lost

with open("test.permtab", "w") as f:
    f.writelines(
        permtab.edit(
            parsed,
            {
                "myrule1": ("-t_aeiou", "-t_hello world!"),
                # add a `-` to remove an existing rule
                "myrule2": ("+t_hellp",),
                # add a `+` to add a new rule
                "myrule3": ("+t_foo", "+t_bar", "-t_foo")
                # `+` and `-` operation can take effect at the same time
                # duplicate changes will be evaluated
            }
        )
    )

permtab.reset_rule()  # remove rules previously loaded
permtab.load("test.permtab")  # reload new permtab

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

permtab-0.1.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

permtab-0.1.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file permtab-0.1.0.tar.gz.

File metadata

  • Download URL: permtab-0.1.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for permtab-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e65b62f2cf97b58ac47162df1d167307eb67487d2ed135d7fc4cbabc1c751f76
MD5 bcb99bd280152806934ad8588324a68f
BLAKE2b-256 635149c7bbc495ed60066e653d6ccb68ea76f38c4c00c6b3e2df57ec755c2503

See more details on using hashes here.

File details

Details for the file permtab-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: permtab-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for permtab-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 970f0ccd8e18359877e3f49299ef9dc0e6c9d9a0e804533fe8c2f4be84dcca8a
MD5 390d953b6f46453dc522a634e561de7f
BLAKE2b-256 2170064ad37e7bd48e197e3e65e20efd554a6ed2d9358b1b8acf6fd4ee864b1c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page