X-Git-Url: https://plomlompom.com/repos/day?a=blobdiff_plain;f=plomtask%2Fprocesses.py;h=bb1de3a4a3356415473bc652d650e202886eb01b;hb=HEAD;hp=4ad6e75ca205099f6afc9b492097b8ca1fd11832;hpb=0952d4a17e7df265cf0c50b66df1e1391075b821;p=plomtask diff --git a/plomtask/processes.py b/plomtask/processes.py index 4ad6e75..23eb624 100644 --- a/plomtask/processes.py +++ b/plomtask/processes.py @@ -1,8 +1,8 @@ """Collecting Processes and Process-related items.""" from __future__ import annotations -from dataclasses import dataclass from typing import Set, Any from sqlite3 import Row +from plomtask.misc import DictableNode from plomtask.db import DatabaseConnection, BaseModel from plomtask.versioned_attributes import VersionedAttribute from plomtask.conditions import Condition, ConditionsRelations @@ -10,51 +10,57 @@ from plomtask.exceptions import (NotFoundException, BadFormatException, HandledException) -@dataclass -class ProcessStepsNode: +class ProcessStepsNode(DictableNode): """Collects what's useful to know for ProcessSteps tree display.""" + # pylint: disable=too-few-public-methods + step: ProcessStep process: Process - parent_id: int | None is_explicit: bool - steps: dict[int, ProcessStepsNode] - seen: bool + steps: list[ProcessStepsNode] + seen: bool = False + is_suppressed: bool = False + _to_dict = ['step', 'process', 'is_explicit', 'steps', 'seen', + 'is_suppressed'] class Process(BaseModel[int], ConditionsRelations): """Template for, and metadata for, Todos, and their arrangements.""" # pylint: disable=too-many-instance-attributes table_name = 'processes' - to_save = ['calendarize'] - to_save_versioned = ['title', 'description', 'effort'] + to_save_simples = ['calendarize'] to_save_relations = [('process_conditions', 'process', 'conditions', 0), ('process_blockers', 'process', 'blockers', 0), ('process_enables', 'process', 'enables', 0), - ('process_disables', 'process', 'disables', 0)] + ('process_disables', 'process', 'disables', 0), + ('process_step_suppressions', 'process', + 'suppressed_steps', 0)] + add_to_dict = ['explicit_steps'] + versioned_defaults = {'title': 'UNNAMED', 'description': '', 'effort': 1.0} to_search = ['title.newest', 'description.newest'] + can_create_by_id = True + sorters = {'steps': lambda p: len(p.explicit_steps), + 'owners': lambda p: p.n_owners, + 'effort': lambda p: p.effort.newest, + 'title': lambda p: p.title.newest} def __init__(self, id_: int | None, calendarize: bool = False) -> None: BaseModel.__init__(self, id_) ConditionsRelations.__init__(self) - self.title = VersionedAttribute(self, 'process_titles', 'UNNAMED') - self.description = VersionedAttribute(self, 'process_descriptions', '') - self.effort = VersionedAttribute(self, 'process_efforts', 1.0) + for name in ['title', 'description', 'effort']: + attr = VersionedAttribute(self, f'process_{name}s', + self.versioned_defaults[name]) + setattr(self, name, attr) self.explicit_steps: list[ProcessStep] = [] + self.suppressed_steps: list[ProcessStep] = [] self.calendarize = calendarize + self.n_owners: int | None = None # only set by from_table_row @classmethod def from_table_row(cls, db_conn: DatabaseConnection, row: Row | list[Any]) -> Process: """Make from DB row, with dependencies.""" process = super().from_table_row(db_conn, row) - assert isinstance(process.id_, int) - for name in ('title', 'description', 'effort'): - table = f'process_{name}s' - for row_ in db_conn.row_where(table, 'parent', process.id_): - getattr(process, name).history_from_row(row_) - for row_ in db_conn.row_where('process_steps', 'owner', - process.id_): - step = ProcessStep.from_table_row(db_conn, row_) - process.explicit_steps += [step] # pylint: disable=no-member + assert process.id_ is not None for name in ('conditions', 'blockers', 'enables', 'disables'): table = f'process_{name}' assert isinstance(process.id_, int) @@ -62,6 +68,14 @@ class Process(BaseModel[int], ConditionsRelations): 'process', process.id_): target = getattr(process, name) target += [Condition.by_id(db_conn, c_id)] + for row_ in db_conn.row_where('process_steps', 'owner', process.id_): + step = ProcessStep.from_table_row(db_conn, row_) + process.explicit_steps += [step] + for row_ in db_conn.row_where('process_step_suppressions', 'process', + process.id_): + step = ProcessStep.by_id(db_conn, row_[1]) + process.suppressed_steps += [step] + process.n_owners = len(process.used_as_step_by(db_conn)) return process def used_as_step_by(self, db_conn: DatabaseConnection) -> list[Process]: @@ -75,50 +89,94 @@ class Process(BaseModel[int], ConditionsRelations): return [self.__class__.by_id(db_conn, id_) for id_ in owner_ids] def get_steps(self, db_conn: DatabaseConnection, external_owner: - Process | None = None) -> dict[int, ProcessStepsNode]: + Process | None = None) -> list[ProcessStepsNode]: """Return tree of depended-on explicit and implicit ProcessSteps.""" - def make_node(step: ProcessStep) -> ProcessStepsNode: + def make_node(step: ProcessStep, suppressed: bool) -> ProcessStepsNode: is_explicit = False if external_owner is not None: is_explicit = step.owner_id == external_owner.id_ process = self.__class__.by_id(db_conn, step.step_process_id) - step_steps = process.get_steps(db_conn, external_owner) - return ProcessStepsNode(process, step.parent_step_id, - is_explicit, step_steps, False) + step_steps = [] + if not suppressed: + step_steps = process.get_steps(db_conn, external_owner) + return ProcessStepsNode(step, process, is_explicit, step_steps, + False, suppressed) - def walk_steps(node_id: int, node: ProcessStepsNode) -> None: + def walk_steps(node: ProcessStepsNode) -> None: + node.seen = node.step.id_ in seen_step_ids + assert isinstance(node.step.id_, int) + seen_step_ids.add(node.step.id_) + if node.is_suppressed: + return explicit_children = [s for s in self.explicit_steps - if s.parent_step_id == node_id] + if s.parent_step_id == node.step.id_] for child in explicit_children: - assert isinstance(child.id_, int) - node.steps[child.id_] = make_node(child) - # # ensure that one (!) explicit step of process replaces - # # one (!) implicit step of same process - # for i in [i for i, s in node.steps.items() - # if not s.process_step.owner_id == child.id_ - # and s.process.id_ == child.step_process_id]: - # del node.steps[i] - # break - node.seen = node_id in seen_step_ids - seen_step_ids.add(node_id) - for id_, step in node.steps.items(): - walk_steps(id_, step) - - steps: dict[int, ProcessStepsNode] = {} + node.steps += [make_node(child, False)] + for step in node.steps: + walk_steps(step) + + step_nodes: list[ProcessStepsNode] = [] seen_step_ids: Set[int] = set() if external_owner is None: external_owner = self for step in [s for s in self.explicit_steps if s.parent_step_id is None]: assert isinstance(step.id_, int) - steps[step.id_] = make_node(step) - for step_id, step_node in steps.items(): - walk_steps(step_id, step_node) - return steps + new_node = make_node(step, step in external_owner.suppressed_steps) + step_nodes += [new_node] + for step_node in step_nodes: + walk_steps(step_node) + return step_nodes + + def set_step_relations(self, + db_conn: DatabaseConnection, + owners: list[int], + suppressions: list[int], + owned_steps: list[ProcessStep] + ) -> None: + """Set step owners, suppressions, and owned steps.""" + self._set_owners(db_conn, owners) + self._set_step_suppressions(db_conn, suppressions) + self.set_steps(db_conn, owned_steps) + + def _set_step_suppressions(self, + db_conn: DatabaseConnection, + step_ids: list[int] + ) -> None: + """Set self.suppressed_steps from step_ids.""" + assert isinstance(self.id_, int) + db_conn.delete_where('process_step_suppressions', 'process', self.id_) + self.suppressed_steps = [ProcessStep.by_id(db_conn, s) + for s in step_ids] + + def _set_owners(self, + db_conn: DatabaseConnection, + owner_ids: list[int] + ) -> None: + """Re-set owners to those identified in owner_ids.""" + owners_old = self.used_as_step_by(db_conn) + losers = [o for o in owners_old if o.id_ not in owner_ids] + owners_old_ids = [o.id_ for o in owners_old] + winners = [Process.by_id(db_conn, id_) for id_ in owner_ids + if id_ not in owners_old_ids] + steps_to_remove = [] + for loser in losers: + steps_to_remove += [s for s in loser.explicit_steps + if s.step_process_id == self.id_] + for step in steps_to_remove: + step.remove(db_conn) + for winner in winners: + assert isinstance(winner.id_, int) + assert isinstance(self.id_, int) + new_step = ProcessStep(None, winner.id_, self.id_, None) + new_explicit_steps = winner.explicit_steps + [new_step] + winner.set_steps(db_conn, new_explicit_steps) - def set_steps(self, db_conn: DatabaseConnection, - steps: list[ProcessStep]) -> None: + def set_steps(self, + db_conn: DatabaseConnection, + steps: list[ProcessStep] + ) -> None: """Set self.explicit_steps in bulk. Checks against recursion, and turns into top-level steps any of @@ -132,12 +190,9 @@ class Process(BaseModel[int], ConditionsRelations): walk_steps(step) assert isinstance(self.id_, int) - for step in self.explicit_steps: - step.uncache() - self.explicit_steps = [] - db_conn.delete_where('process_steps', 'owner', self.id_) - for step in steps: - step.save(db_conn) + for step in [s for s in self.explicit_steps if s not in steps]: + step.remove(db_conn) + for step in [s for s in steps if s not in self.explicit_steps]: if step.parent_step_id is not None: try: parent_step = ProcessStep.by_id(db_conn, @@ -147,7 +202,7 @@ class Process(BaseModel[int], ConditionsRelations): except NotFoundException: step.parent_step_id = None walk_steps(step) - self.explicit_steps += [step] + step.save(db_conn) def save(self, db_conn: DatabaseConnection) -> None: """Add (or re-write) self and connected items to DB.""" @@ -175,7 +230,7 @@ class Process(BaseModel[int], ConditionsRelations): class ProcessStep(BaseModel[int]): """Sub-unit of Processes.""" table_name = 'process_steps' - to_save = ['owner_id', 'step_process_id', 'parent_step_id'] + to_save_simples = ['owner_id', 'step_process_id', 'parent_step_id'] def __init__(self, id_: int | None, owner_id: int, step_process_id: int, parent_step_id: int | None) -> None: @@ -184,6 +239,16 @@ class ProcessStep(BaseModel[int]): self.step_process_id = step_process_id self.parent_step_id = parent_step_id + def save(self, db_conn: DatabaseConnection) -> None: + """Update into DB/cache, and owner's .explicit_steps.""" + super().save(db_conn) + owner = Process.by_id(db_conn, self.owner_id) + if self not in owner.explicit_steps: + for s in [s for s in owner.explicit_steps if s.id_ == self.id_]: + s.remove(db_conn) + owner.explicit_steps += [self] + owner.explicit_steps.sort(key=hash) + def remove(self, db_conn: DatabaseConnection) -> None: """Remove from DB, and owner's .explicit_steps.""" owner = Process.by_id(db_conn, self.owner_id)