Skip to content

_FLObject

FLObject is the core of PyFLP's object model. It handles parsing of events dispatched by Parser and saving them back by calling dump methods of Event. Parser will use the EventID class inside a _FLObject to instantiate it based upon the event ID.

Bases: abc.ABC

ABC for the FLP object model.

Source code in pyflp/_flobject.py
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
class _FLObject(abc.ABC):
    """ABC for the FLP object model."""

    @enum.unique
    class EventID(enum.IntEnum):
        """Stores event IDs used by `parse_event` delegates."""

    def __repr__(self) -> str:
        reprs = []
        for k in vars(type(self)).keys():
            prop = getattr(type(self), k)
            attr = getattr(self, k)
            if isinstance(prop, _Property):
                reprs.append(f"{k}={attr!r}")
        return f"<{type(self).__name__} {', '.join(reprs)}>"

    def _setprop(self, n: str, v: Any) -> None:
        """Dumps a property value to the underlying event
        provided `_events` has a key with name `n`."""

        ev = self._events.get(n)
        if ev is not None:
            ev.dump(v)

    # * Parsing logic
    def parse_event(self, event: EventType) -> None:
        """Adds and parses an event from the event store.

        Note: Delegates
            Uses delegate methods `_parse_byte_event`, `_parse_word_event`,
            `_parse_dword_event`, `_parse_text_event` and `_parse_data_event`.

        Tip: Overriding
            Can be overriden when a derived class contains properties
            holding `FLObject` derived classes, *for e.g.* `Insert.slots` holds
            `List[InsertSlot]` and whenever the event ID belongs to
            `InsertSlot.EventID`, it is passed to the slot's `parse_event`
            method directly.

        Args:
            event (Event): Event to dispatch to `self._parseprop`."""

        # Convert event.id from an int to a member of the class event ID
        try:
            event.id_ = self.EventID(event.id_)
        except ValueError:
            # The delegates below should assign the proper value
            pass

        id = event.id_

        if id >= BYTE and id < WORD:
            self._parse_byte_event(event)
        elif id >= WORD and id < DWORD:
            self._parse_word_event(event)
        elif id >= DWORD and id < TEXT:
            self._parse_dword_event(event)
        elif (id >= TEXT and id < DATA) or id in DATA_TEXT_EVENTS:
            self._parse_text_event(event)
        else:
            self._parse_data_event(event)

    def _parse_byte_event(self, _: ByteEventType) -> None:  # pragma: no cover
        pass

    def _parse_word_event(self, _: WordEventType) -> None:  # pragma: no cover
        pass

    def _parse_dword_event(self, _: DWordEventType) -> None:  # pragma: no cover
        pass

    def _parse_text_event(self, _: TextEventType) -> None:  # pragma: no cover
        pass

    def _parse_data_event(self, _: DataEventType) -> None:  # pragma: no cover
        pass

    # * Property parsing logic
    def _parseprop(self, event: EventType, key: str, value: Any) -> None:
        """Reduces boilerplate for `parse_event()` delegate methods.

        Not to be used unless helper `_parse_*` methods aren't useful.
        """
        self._events[key] = event
        setattr(self, "_" + key, value)

    def _parse_bool(self, event: ByteEventType, key: str) -> None:
        """`self._parseprop` for bool properties."""
        self._parseprop(event, key, event.to_bool())

    def _parse_B(self, event: ByteEventType, key: str) -> None:  # noqa
        """`self._parseprop` for uint8 properties."""
        self._parseprop(event, key, event.to_uint8())

    def _parse_b(self, event: ByteEventType, key: str) -> None:
        """`self._parseprop` for int8 properties."""
        self._parseprop(event, key, event.to_int8())

    def _parse_H(self, event: WordEventType, key: str) -> None:  # noqa
        """`self._parseprop` for uint16 properties."""
        self._parseprop(event, key, event.to_uint16())

    def _parse_h(self, event: WordEventType, key: str) -> None:
        """`self._parseprop` for int16 properties."""
        self._parseprop(event, key, event.to_int16())

    def _parse_I(self, event: DWordEventType, key: str) -> None:  # noqa
        """`self._parseprop` for uint32 properties."""
        self._parseprop(event, key, event.to_uint32())

    def _parse_i(self, event: DWordEventType, key: str) -> None:
        """`self._parseprop` for int32 properties."""
        self._parseprop(event, key, event.to_int32())

    def _parse_s(self, event: TextEventType, key: str) -> None:
        """`self._parseprop` for string properties."""
        self._parseprop(event, key, event.to_str())

    def _parse_color(self, event: ColorEventType, key: str = "color") -> None:
        """`self._parseprop` for Color properties."""
        self._parseprop(event, key, event.to_color())

    def _parse_flobject(self, event: EventType, key: str, value: Any) -> None:
        """`self._parseprop` for `FLObject` properties.

        e.g `Channel.delay` is of type `ChannelDelay` which is itself an
        `FLObject` subclass. This method works only for classes which work on
        a single event and occur once inside the container class!
        """
        if not hasattr(self, "_" + key):
            self._parseprop(event, key, value)
        obj: _FLObject = getattr(self, "_" + key)
        obj.parse_event(event)

    def _save(self) -> List[EventType]:
        """Dumps pending changes into the events and returns them a list."""
        return list(self._events.values())

    def __init__(
        self,
        project: "Optional[Project]" = None,
        max_instances: Optional[MaxInstances] = None,
    ) -> None:
        self._project = project
        self._max_instances = max_instances
        self._events: Dict[str, EventType] = {}

EventID

Bases: enum.IntEnum

Stores event IDs used by parse_event delegates.

Source code in pyflp/_flobject.py
47
48
49
@enum.unique
class EventID(enum.IntEnum):
    """Stores event IDs used by `parse_event` delegates."""

__init__(project=None, max_instances=None)

Source code in pyflp/_flobject.py
182
183
184
185
186
187
188
189
def __init__(
    self,
    project: "Optional[Project]" = None,
    max_instances: Optional[MaxInstances] = None,
) -> None:
    self._project = project
    self._max_instances = max_instances
    self._events: Dict[str, EventType] = {}

__repr__()

Source code in pyflp/_flobject.py
51
52
53
54
55
56
57
58
def __repr__(self) -> str:
    reprs = []
    for k in vars(type(self)).keys():
        prop = getattr(type(self), k)
        attr = getattr(self, k)
        if isinstance(prop, _Property):
            reprs.append(f"{k}={attr!r}")
    return f"<{type(self).__name__} {', '.join(reprs)}>"

parse_event(event)

Adds and parses an event from the event store.

Delegates

Uses delegate methods _parse_byte_event, _parse_word_event, _parse_dword_event, _parse_text_event and _parse_data_event.

Overriding

Can be overriden when a derived class contains properties holding FLObject derived classes, for e.g. Insert.slots holds List[InsertSlot] and whenever the event ID belongs to InsertSlot.EventID, it is passed to the slot's parse_event method directly.

Parameters:

Name Type Description Default
event Event

Event to dispatch to self._parseprop.

required
Source code in pyflp/_flobject.py
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
def parse_event(self, event: EventType) -> None:
    """Adds and parses an event from the event store.

    Note: Delegates
        Uses delegate methods `_parse_byte_event`, `_parse_word_event`,
        `_parse_dword_event`, `_parse_text_event` and `_parse_data_event`.

    Tip: Overriding
        Can be overriden when a derived class contains properties
        holding `FLObject` derived classes, *for e.g.* `Insert.slots` holds
        `List[InsertSlot]` and whenever the event ID belongs to
        `InsertSlot.EventID`, it is passed to the slot's `parse_event`
        method directly.

    Args:
        event (Event): Event to dispatch to `self._parseprop`."""

    # Convert event.id from an int to a member of the class event ID
    try:
        event.id_ = self.EventID(event.id_)
    except ValueError:
        # The delegates below should assign the proper value
        pass

    id = event.id_

    if id >= BYTE and id < WORD:
        self._parse_byte_event(event)
    elif id >= WORD and id < DWORD:
        self._parse_word_event(event)
    elif id >= DWORD and id < TEXT:
        self._parse_dword_event(event)
    elif (id >= TEXT and id < DATA) or id in DATA_TEXT_EVENTS:
        self._parse_text_event(event)
    else:
        self._parse_data_event(event)