Skip to content

Commit

Permalink
WIP Add patching guide
Browse files Browse the repository at this point in the history
  • Loading branch information
OmeGak committed Nov 11, 2023
1 parent 1bcb4e3 commit 7406e8a
Show file tree
Hide file tree
Showing 5 changed files with 367 additions and 2 deletions.
111 changes: 109 additions & 2 deletions doc/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,110 @@
# How to use Indico Patcher
# Indico Patching Guide

<!-- TODO -->
This guide describes how to use `indico-patcher` for multiple use cases and considerations to keep in mind when patching Indico. In this page, you can read general information applicable in all the use cases described in the rest of the guide.

- [Terminology](#terminology)
- [Usage](#usage)
- [FAQs](#faqs)
- [Keep in mind](#keep-in-mind)

The guide covers in detail the following use cases in dedicated pages:

1. [Patching Indico classes](./classes.md)
2. [Patching SQLAlchemy models](./models.md)
3. [Patching WTForms forms](./forms.md)
4. [Patching Enums](./enums.md)

## Terminology

### Patch class

A class that is decorated with the `@patch` decorator. It is uniquely used to patch an existing class in Indico and never to be imported and used directly.

### Original class

The Indico class that is being patched by a patch class, passed as argument to the `@patch()` decorator.

### Patched class

The class that results from the application of a patch class to an original class.

### Patch

A collection of attributes, methods and properties defined in a patch class that are applied to the original class. A patch is applied when the patch class is imported.

## Usage

Import the `patch` decorator from the `indico_patcher` module:

```python
from indico_patcher import patch
```

Import also the original class to be patched:

```python
from indico.modules.users.models.users import User
from indico.modules.users.models.user_titles import UserTitle
```

Define a patch class and decorate it with the `@patch()` decorator. The original class is passed as argument to the decorator. Since the patch class is not meant to be used directly, it is recommended to prefix its name with an underscore:

```python
@patch(User)
class _User:
...
```

This differs a bit in the case of enums, as the patch class must inherit from `Enum` or a subclass of it:

```python
@patch(UserTitle)
class _UserTitle(RichIntEnum):
...
```

Once the patch class is imported in the plugin, the original class will be patched. New members can be accessed as if they had been defined in the original class. All calls to existing members will be redirected to the ones defined in the patch class.

> [!NOTE]
> For more specific usage details, please refer to the different pages of the guide.
## FAQs

### When are patches applied?

The patches are applied when the patch classes are imported. This means that you need to import the patch classes in your plugin's `__init__.py` file or in any other file that is imported by it.

### Can the same class be patched multiple times?

Yes, this is possible and it is useful or unavoidable in some cases. For instance, you may want to patch the same class in two different modules of your plugin. Or you may enable two different plugins that patch the same class. In both cases, the patches will be applied in the order in which the patch classes are imported. This means that if multiple patches are overriding the same class member, the last one will be the one that is applied.

### What are some built-in tools to avoid patching Indico?

Indico provides many signals that can be used to extend its functionality without patching it. You can find a list of all the available signals in [`indico/core/signals`](https://github.com/indico/indico/tree/v3.2.8/indico/core/signals). A particularly useful one is [`interceptable_function`](https://github.com/indico/indico/blob/v3.2.8/indico/core/signals/plugin.py#L121). You may also want to check [Flask signals](https://flask.palletsprojects.com/en/2.0.x/api/#signals) and [SQLAlchemy event hooks](https://docs.sqlalchemy.org/en/14/core/event.html).

### Why is the `interceptable_function` signal so useful?

The `interceptable_function` signal allows for intercepting the call of any function in Indico, not only methods, and modify its arguments and return values. This gives the same flexibility as patching a method but more reliably.

It is also more reliable than patching methods because functions decorated with `@make_interceptable` in Indico can be considered as somewhat official API. If an interceptable function is removed or its signature is changed, the maintainers will either keep it backwards compatible, show deprecation warnings in the logs or announce it in the changelog.

This is an example of how to use the `interceptable_function` signal to intercept the call of the `make_email()` function in Indico:

```python
from indico.core import signals
from indico.core.notifications import make_email
from indico.util.signals import interceptable_sender

@signals.plugin.interceptable_function.connect_via(interceptable_sender(make_email))
def _make_email(sender, func, args, **kwargs):
if func(*args.args, **args.kwargs):
return True
```

## Keep in mind

> [!WARNING]
> Remember! With great power comes great responsibility. Patch Indico as little as possible and only when it is absolutely necessary. Always try to find a way to achieve what you want without patching Indico.
> [!NOTE]
> The code examples and links to APIs in this guide make reference to Indico classes and dependencies as they are defined in the `v3.2.8` of the codebase. Please note that some class names, class locations and APIs may differ if you are using a different version of Indico.
128 changes: 128 additions & 0 deletions doc/classes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# Patching Indico classes

This page of the guide explains how to generally patch Indico classes to add and override attributes, methods and properties. For more specific use cases, like patching [SQLAlchemy models](./models.md) or [WTForms forms](./forms.md), please refer to their respective pages of the guide.

- [Adding and overriding attributes](#adding-and-overriding-attributes)
- [Adding and overriding methods](#adding-and-overriding-methods)
- [Adding and overriding properties](#adding-and-overriding-properties)

## Adding and overriding attributes

It is possible to add a new attribute to the original class by simply defining it in the patch class. For instance, you could add an `error_message` attribute to `RHUserBlock` that will be used in overridden methods in a section below.

```python
@patch(RHUserBlock)
class _RHUserBlock:
# Adds new attribute
error_message = L_('Action not allowed on this user.')
```

It is also possible to override an existing attribute in the original class by assigning a new value to it in the patch class. In this example, a plugin-defined form is assigned to be used by `LocalRegistrationHandler` instead of the original `LocalRegistrationForm`.

```python
@patch(LocalRegistrationHandler)
class _LocalRegistrationHandler:
# Overrides existing attribute
form = CustomLocalRegistrationForm
```

## Adding and overriding methods

Similarly, it is possible to add a new method to the original class by defining it in the patch class. For instance, you could add a `_can_block_user` method to `RHUserBlock` that will be used in overridden methods.

```python
@patch(RHUserBlock)
class _RHUserBlock:
# Adds new method
def _can_block_user(self):
return not self.user.merged_into_user
```

Defining a method on the patch class with the same name as an existing method in the original class will override the original method. Also, it's possible to call the original method from the patch class by using `super()`.

In this example, the PUT and DELETE process methods of the class `RHUserBlock` are intercepted to check if the user can be blocked with the newly added `_can_block_user()`. If not, an exception is raised with `error_message`. Otherwise, the original method is called.

```python
@patch(RHUserBlock)
class _RHUserBlock:
# Adds new attribute
error_message = L_('Action not allowed on this user')

# Adds new method
def _can_block_user(self):
return not self.user.merged_into_user

# Overrides existing method
def _process_PUT(self):
if self._can_block_user():
raise Forbidden(self.error_message)
return super()._process_PUT()

# Overrides existing method
def _process_DELETE(self):
if self._can_block_user():
raise Forbidden(self.error_message)
return super()._process_PUT()
```

This works as well for `@staticmethod` and `@classmethod`:

```python
@patch(User)
class _User:
# Adds new classmethod
@classmethod
def get_academic_users(cls):
return cls.query.filter(cls.title.in_({UserTitle.dr, UserTitle.prof}))

# Overrides existing staticmethod
@staticmethod
def get_system_user():
system_user = super().get_system_user()
logger.info('System user was retrieved.')
return system_user
```

> [!IMPORTANT]
> Overriding a method in the original class is fragile and can break in future versions of Indico if the original method is changed. A more reliable way to override a method in the original class is to intercept calls to it via `interceptable_function` signal.
## Adding and overriding properties

Adding a new property to the original class is also supported by defining it in the patch class. As an example, you could add a new `is_academic` property to `User` that will be check if a user's title is either Doc. or Prof.

```python
@patch(User)
class _User:
# Adds new property
@property
def is_academic(self):
return self.title in {UserTitle.dr, UserTitle.prof}
```

As expected, overriding a property as well as its setter and deleter descriptor methods is supported. Like in the case of methods, it's possible to call the original property descriptor method from the patch class by using `super()`.

For example, here below the `data` property of `Identity` is overridden to add a new key to the dictionary returned by the original property. The setter and deleter descriptors are also overridden to raise an exception if they are called.

```python
@patch(Identity)
class _Identity:
# Overrides existing property descriptor method
@property
def data(self):
data = super().data
data.update({'tag': 'plugin'})
return data

# Overrides existing property descriptor method
@data.setter
def data(self, value):
raise RuntimeError('Cannot set data on identity')

# Adds new property descriptor method
@data.deleter
def data(self):
raise RuntimeError('Cannot delete data on identity')
```

> [!NOTE]
> It is not currently possible to call `super()` on the `setter` and `deleter` descriptor methods of properties.
1 change: 1 addition & 0 deletions doc/enums.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Patching Enums
1 change: 1 addition & 0 deletions doc/forms.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Patching WTForm forms
128 changes: 128 additions & 0 deletions doc/models.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# Patching SQLAlchemy models

Indico defines its database schema using the [SQLAlchemy](https://www.sqlalchemy.org/) library.This page of the guide explains how to patch SQLAlchemy models defined in Indico. This is useful, for instance, when you want to add new columns or relationships to an already existing model, or when you want to add new constraints to an already existing table. This is illustrated with some examples of common use cases.

- [Adding new columns and relationships](#adding-new-columns-and-relationships)
- [Adding and modifying hybrid properties](#adding-and-modifying-hybrid-properties)
- [Adding, removing and replacing table constraints](#adding-removing-and-replacing-table-constraints)
- [Generating Alembic migration scripts for patched models](#generating-alembic-migration-scripts-for-patched-models)

## Adding new columns and relationships

As an example, you can add a new `credit_card_id` column to the `User` model in Indico, and a `credit_card` relationship to a hypothetical `CreditCard` model.

```python
@patch(User)
class _User:
credit_card_id = db.Column(db.String, ForeignKey('credit_cards.id'))
credit_card = db.relationship('CreditCard', backref=backref('user'))
```

You can then use the new column and relationship to insert, update and query rows in the database as if they were defined in the original model.

```python
user = User.query.filter_by(id=1).one()
user.credit_card = CreditCard('XXXX-XXXX-XXXX-XXXX')
```

```python
users = User.query.filter(User.credit_card == None).all()
```

> [!IMPORTANT]
> Please note that for this to work as expected, you will still need to add the new column to the database schema via Alembic migration script. How to do this is explained in a [section below](#generating-alembic-migration-scripts-for-patched-models).
## Adding and modifying hybrid properties

Adding a new hybrid property is as simple as defining a new method and decorating it with the `@hybrid_property` decorator as you would in any model.

```python
@patch(Event)
class _Event:
@hybrid_property
def is_in_series(self):
return self.series_id is not None

@is_in_series.expression
def is_in_series(self):
return ~self.series_id.is_(None)
```

Replacing an existing hybrid property is also simple. Just define a new method with the same name as the original property and decorate it with the `@hybrid_property` decorator. The original hybrid property will be replaced with the new one.

```python
@patch(Event)
class _Event:
@hybrid_property
def event_message(self):
return ''

@event_message.setter
def event_message(self, value):
pass

@event_message.expression
def event_message(self):
return ''
```

## Adding, removing and replacing table constraints

Adding new constraints is easy, as it only requires calling the `append_constraint` method of the `__table__` attribute of an original model.

```python
@patch(RegistrationForm)
class _RegistrationForm:
RegistrationForm.__table__.append_constraint(
db.CheckConstraint(...)
)
```

Removing constraints is a bit more involved, as it requires to first find the constraint you want to remove from the set of table constraints. The easiest way to find the name of the constraint to remove is to inspect the database schema directly (e.g. via `psql` command).

```python
@patch(RegistrationForm)
class _RegistrationForm:
RegistrationForm.__table__.constraints -= {
c for c in RegistrationForm.__table__.constraints
if c.name == '<constraint_name>'
}
```

Finally, replacing a constraint requires both previous steps. First, remove the old constraint and, then, add the new one.

```python
@patch(RegistrationForm)
class _RegistrationForm:
RegistrationForm.__table__.constraints -= {
c for c in RegistrationForm.__table__.constraints
if c.name == '<constraint_name>'
}
RegistrationForm.__table__.append_constraint(
db.CheckConstraint(..., '<constraint_name>')
)
```

> [!IMPORTANT]
> Please note that for the new constraint definitions to take effect, you still need to apply them to the database schema via Alembic migration script. How to do this is explained in a [section below](#generating-alembic-migration-scripts-for-patched-models).
> [!NOTE]
> It's worth noting that patching for modifying table constraints is not necessary, as you are calling `__table__` directly the original model. It may still be a good idea to do it within a patch class as a way to keep all modifications of an Indico model under the same class.
## Generating Alembic migration scripts for patched models

Once patches are defined, you need to generate the Alembic migration script that will perform the updates to the database schema.

Typically, for plugin-defined tables, this is done by invoking `indico db --plugin <plugin-name> migrate`. However, since the new columns and table constraints are defined in Indico core classes, the migration script needs to be generated with this instead:

```sh
indico db migrate
```

This will generate a new Alembic migration script in the `indico/migrations/versions/` directory of the `indico` package. This will not be its final location, as you shouldn't add a new migration script to the Indico core. Instead, move the script to the migrations directory of the plugin and adjust its `down_revision` to point to the latest revision.

Finally, once the migration script is in the plugin's migrations directory, and manually adjusted, you can apply it to the database schema with:

```sh
indico db --plugin <plugin-name> upgrade
```

0 comments on commit 7406e8a

Please sign in to comment.