Pamhyr2/src/Model/Stricklers/StricklersList.py

117 lines
3.0 KiB
Python

# StricklersList.py -- Pamhyr
# Copyright (C) 2023 INRAE
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# -*- coding: utf-8 -*-
from tools import trace, timer
from Model.DB import SQLSubModel
from Model.Saved import SavedStatus
from Model.Stricklers.Stricklers import Stricklers
class StricklersList(SQLSubModel):
_sub_classes = [
Stricklers,
]
def __init__(self, status = None):
if status is None:
status = SavedStatus()
self._status = status
self._stricks = []
@classmethod
def _sql_create(cls, execute):
return cls._create_submodel(execute)
@classmethod
def _sql_update(cls, execute, version):
return cls._update_submodel(execute, version)
@classmethod
def _sql_load(cls, execute, data = None):
new = cls(status = data["status"])
new._stricks = Stricklers._sql_load(
execute,
data = data
)
return new
def _sql_save(self, execute, data = None):
execute("DELETE FROM stricklers")
objs = self._stricks
return self._save_submodel(execute, objs, data)
def __len__(self):
return len(self._stricks)
@property
def stricklers(self):
return self._stricks.copy()
def get(self, index):
if 0 <= index < len(self._stricks):
return self._stricks[index]
return None
def insert(self, index, strick):
self._stricks.insert(index, strick)
self._status.modified()
def add(self, index):
s = Stricklers(status = self._status)
self.insert(index, s)
return s
def delete(self, stricks):
self._stricks = list(
filter(
lambda s: s not in stricks,
self._stricks
)
)
self._status.modified()
def delete_i(self, indexes):
stricks = set(
map(
lambda e: e[1],
filter(
lambda e: e[0] in indexes,
enumerate(self._stricks)
)
)
)
self.delete(stricks)
@timer
def sort(self, reverse:bool = False, key=None):
f = lambda st: st.name
if key is not None:
f = key
self._stricks = sorted(
self._stricks,
key = f,
reverse = reverse,
)
self._status.modified()