home · contact · privacy
Some work on tests, kinda unfinished.
[plomtask] / plomtask / processes.py
1 """Collecting Processes and Process-related items."""
2 from __future__ import annotations
3 from dataclasses import dataclass
4 from typing import Set, Any
5 from sqlite3 import Row
6 from plomtask.db import DatabaseConnection, BaseModel
7 from plomtask.versioned_attributes import VersionedAttribute
8 from plomtask.conditions import Condition, ConditionsRelations
9 from plomtask.exceptions import (NotFoundException, BadFormatException,
10                                  HandledException)
11
12
13 @dataclass
14 class ProcessStepsNode:
15     """Collects what's useful to know for ProcessSteps tree display."""
16     process: Process
17     parent_id: int | None
18     is_explicit: bool
19     steps: dict[int, ProcessStepsNode]
20     seen: bool = False
21     is_suppressed: bool = False
22
23
24 class Process(BaseModel[int], ConditionsRelations):
25     """Template for, and metadata for, Todos, and their arrangements."""
26     # pylint: disable=too-many-instance-attributes
27     table_name = 'processes'
28     to_save = ['calendarize']
29     to_save_versioned = ['title', 'description', 'effort']
30     to_save_relations = [('process_conditions', 'process', 'conditions', 0),
31                          ('process_blockers', 'process', 'blockers', 0),
32                          ('process_enables', 'process', 'enables', 0),
33                          ('process_disables', 'process', 'disables', 0),
34                          ('process_step_suppressions', 'process',
35                           'suppressed_steps', 0)]
36     to_search = ['title.newest', 'description.newest']
37
38     def __init__(self, id_: int | None, calendarize: bool = False) -> None:
39         BaseModel.__init__(self, id_)
40         ConditionsRelations.__init__(self)
41         self.title = VersionedAttribute(self, 'process_titles', 'UNNAMED')
42         self.description = VersionedAttribute(self, 'process_descriptions', '')
43         self.effort = VersionedAttribute(self, 'process_efforts', 1.0)
44         self.explicit_steps: list[ProcessStep] = []
45         self.suppressed_steps: list[ProcessStep] = []
46         self.calendarize = calendarize
47         self.n_owners: int | None = None  # only set by from_table_row
48
49     @property
50     def as_dict(self) -> dict[str, object]:
51         """Return self as (json.dumps-coompatible) dict."""
52         d = super().as_dict
53         d['explicit_steps'] = [s.as_dict for s in self.explicit_steps]
54         d['suppressed_steps'] = [s.as_dict for s in self.suppressed_steps]
55         return d
56
57     @classmethod
58     def from_table_row(cls, db_conn: DatabaseConnection,
59                        row: Row | list[Any]) -> Process:
60         """Make from DB row, with dependencies."""
61         process = super().from_table_row(db_conn, row)
62         assert isinstance(process.id_, int)
63         for name in ('title', 'description', 'effort'):
64             table = f'process_{name}s'
65             for row_ in db_conn.row_where(table, 'parent', process.id_):
66                 getattr(process, name).history_from_row(row_)
67         for name in ('conditions', 'blockers', 'enables', 'disables'):
68             table = f'process_{name}'
69             assert isinstance(process.id_, int)
70             for c_id in db_conn.column_where(table, 'condition',
71                                              'process', process.id_):
72                 target = getattr(process, name)
73                 target += [Condition.by_id(db_conn, c_id)]
74         for row_ in db_conn.row_where('process_steps', 'owner', process.id_):
75             step = ProcessStep.from_table_row(db_conn, row_)
76             process.explicit_steps += [step]
77         for row_ in db_conn.row_where('process_step_suppressions', 'process',
78                                       process.id_):
79             step = ProcessStep.by_id(db_conn, row_[1])
80             process.suppressed_steps += [step]
81         process.n_owners = len(process.used_as_step_by(db_conn))
82         return process
83
84     def used_as_step_by(self, db_conn: DatabaseConnection) -> list[Process]:
85         """Return Processes using self for a ProcessStep."""
86         if not self.id_:
87             return []
88         owner_ids = set()
89         for id_ in db_conn.column_where('process_steps', 'owner',
90                                         'step_process', self.id_):
91             owner_ids.add(id_)
92         return [self.__class__.by_id(db_conn, id_) for id_ in owner_ids]
93
94     def get_steps(self, db_conn: DatabaseConnection, external_owner:
95                   Process | None = None) -> dict[int, ProcessStepsNode]:
96         """Return tree of depended-on explicit and implicit ProcessSteps."""
97
98         def make_node(step: ProcessStep, suppressed: bool) -> ProcessStepsNode:
99             is_explicit = False
100             if external_owner is not None:
101                 is_explicit = step.owner_id == external_owner.id_
102             process = self.__class__.by_id(db_conn, step.step_process_id)
103             step_steps = {}
104             if not suppressed:
105                 step_steps = process.get_steps(db_conn, external_owner)
106             return ProcessStepsNode(process, step.parent_step_id,
107                                     is_explicit, step_steps, False, suppressed)
108
109         def walk_steps(node_id: int, node: ProcessStepsNode) -> None:
110             node.seen = node_id in seen_step_ids
111             seen_step_ids.add(node_id)
112             if node.is_suppressed:
113                 return
114             explicit_children = [s for s in self.explicit_steps
115                                  if s.parent_step_id == node_id]
116             for child in explicit_children:
117                 assert isinstance(child.id_, int)
118                 node.steps[child.id_] = make_node(child, False)
119             for id_, step in node.steps.items():
120                 walk_steps(id_, step)
121
122         steps: dict[int, ProcessStepsNode] = {}
123         seen_step_ids: Set[int] = set()
124         if external_owner is None:
125             external_owner = self
126         for step in [s for s in self.explicit_steps
127                      if s.parent_step_id is None]:
128             assert isinstance(step.id_, int)
129             new_node = make_node(step, step in external_owner.suppressed_steps)
130             steps[step.id_] = new_node
131         for step_id, step_node in steps.items():
132             walk_steps(step_id, step_node)
133         return steps
134
135     def set_step_suppressions(self, db_conn: DatabaseConnection,
136                               step_ids: list[int]) -> None:
137         """Set self.suppressed_steps from step_ids."""
138         assert isinstance(self.id_, int)
139         db_conn.delete_where('process_step_suppressions', 'process', self.id_)
140         self.suppressed_steps = [ProcessStep.by_id(db_conn, s)
141                                  for s in step_ids]
142
143     def set_steps(self, db_conn: DatabaseConnection,
144                   steps: list[ProcessStep]) -> None:
145         """Set self.explicit_steps in bulk.
146
147         Checks against recursion, and turns into top-level steps any of
148         unknown or non-owned parent.
149         """
150         def walk_steps(node: ProcessStep) -> None:
151             if node.step_process_id == self.id_:
152                 raise BadFormatException('bad step selection causes recursion')
153             step_process = self.by_id(db_conn, node.step_process_id)
154             for step in step_process.explicit_steps:
155                 walk_steps(step)
156
157         assert isinstance(self.id_, int)
158         for step in [s for s in self.explicit_steps if s not in steps]:
159             step.remove(db_conn)
160         for step in [s for s in steps if s not in self.explicit_steps]:
161             if step.parent_step_id is not None:
162                 try:
163                     parent_step = ProcessStep.by_id(db_conn,
164                                                     step.parent_step_id)
165                     if parent_step.owner_id != self.id_:
166                         step.parent_step_id = None
167                 except NotFoundException:
168                     step.parent_step_id = None
169             walk_steps(step)
170             step.save(db_conn)
171
172     def set_owners(self, db_conn: DatabaseConnection,
173                    owner_ids: list[int]) -> None:
174         """Re-set owners to those identified in owner_ids."""
175         owners_old = self.used_as_step_by(db_conn)
176         losers = [o for o in owners_old if o.id_ not in owner_ids]
177         owners_old_ids = [o.id_ for o in owners_old]
178         winners = [Process.by_id(db_conn, id_) for id_ in owner_ids
179                    if id_ not in owners_old_ids]
180         steps_to_remove = []
181         for loser in losers:
182             steps_to_remove += [s for s in loser.explicit_steps
183                                 if s.step_process_id == self.id_]
184         for step in steps_to_remove:
185             step.remove(db_conn)
186         for winner in winners:
187             assert isinstance(winner.id_, int)
188             assert isinstance(self.id_, int)
189             new_step = ProcessStep(None, winner.id_, self.id_, None)
190             new_explicit_steps = winner.explicit_steps + [new_step]
191             winner.set_steps(db_conn, new_explicit_steps)
192
193     def save(self, db_conn: DatabaseConnection) -> None:
194         """Add (or re-write) self and connected items to DB."""
195         super().save(db_conn)
196         assert isinstance(self.id_, int)
197         db_conn.delete_where('process_steps', 'owner', self.id_)
198         for step in self.explicit_steps:
199             step.save(db_conn)
200
201     def remove(self, db_conn: DatabaseConnection) -> None:
202         """Remove from DB, with dependencies.
203
204         Guard against removal of Processes in use.
205         """
206         assert isinstance(self.id_, int)
207         for _ in db_conn.row_where('process_steps', 'step_process', self.id_):
208             raise HandledException('cannot remove Process in use')
209         for _ in db_conn.row_where('todos', 'process', self.id_):
210             raise HandledException('cannot remove Process in use')
211         for step in self.explicit_steps:
212             step.remove(db_conn)
213         super().remove(db_conn)
214
215
216 class ProcessStep(BaseModel[int]):
217     """Sub-unit of Processes."""
218     table_name = 'process_steps'
219     to_save = ['owner_id', 'step_process_id', 'parent_step_id']
220
221     def __init__(self, id_: int | None, owner_id: int, step_process_id: int,
222                  parent_step_id: int | None) -> None:
223         super().__init__(id_)
224         self.owner_id = owner_id
225         self.step_process_id = step_process_id
226         self.parent_step_id = parent_step_id
227
228     def save(self, db_conn: DatabaseConnection) -> None:
229         """Update into DB/cache, and owner's .explicit_steps."""
230         super().save(db_conn)
231         owner = Process.by_id(db_conn, self.owner_id)
232         if self not in owner.explicit_steps:
233             for s in [s for s in owner.explicit_steps if s.id_ == self.id_]:
234                 s.remove(db_conn)
235             owner.explicit_steps += [self]
236         owner.explicit_steps.sort(key=hash)
237
238     def remove(self, db_conn: DatabaseConnection) -> None:
239         """Remove from DB, and owner's .explicit_steps."""
240         owner = Process.by_id(db_conn, self.owner_id)
241         owner.explicit_steps.remove(self)
242         super().remove(db_conn)