# AddFile.py -- Pamhyr # Copyright (C) 2024 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 . # -*- coding: utf-8 -*- from functools import reduce from tools import trace, timer from Model.Tools.PamhyrDB import SQLSubModel from Model.Except import NotImplementedMethodeError from Model.Scenario import Scenario class AddFile(SQLSubModel): _sub_classes = [] def __init__(self, id: int = -1, enabled=True, name="", path="", text="", status=None): super(AddFile, self).__init__(id) self._status = status self._enabled = enabled self._name = f"File #{self._pamhyr_id}" if name == "" else name self._path = path self._text = text def __getitem__(self, key): value = None if key == "enabled": value = self._enabled elif key == "name": value = self._name elif key == "path": value = self._path elif key == "text": value = self._text return value def __setitem__(self, key, value): if key == "enabled": self._enabled = value elif key == "name": self._name = value elif key == "path": self._path = value elif key == "text": self._text = value self._status.modified() @property def enabled(self): return self._enabled @enabled.setter def enabled(self, enabled): self._enabled = enabled self._status.modified() def is_enabled(self): return self._enabled @property def name(self): return self._name @name.setter def name(self, name): self._name = name self._status.modified() @property def path(self): return self._path @path.setter def path(self, path): self._path = path self._status.modified() @property def text(self): return self._text @text.setter def text(self, text): self._text = text self._status.modified() @classmethod def _db_create(cls, execute, ext=""): execute(f""" CREATE TABLE additional_files{ext} ( {cls.create_db_add_pamhyr_id()}, enabled BOOLEAN NOT NULL, name TEXT NOT NULL, path TEXT NOT NULL, text TEXT NOT NULL, {Scenario.create_db_add_scenario()}, {Scenario.create_db_add_scenario_fk()}, PRIMARY KEY(pamhyr_id, scenario) ) """) return cls._create_submodel(execute) @classmethod def _db_update(cls, execute, version, data=None): major, minor, release = version.strip().split(".") if major == minor == "0": release = int(release) if release < 8: cls._db_create(execute) if 8 < release < 13: cls._db_update_to_0_0_13(execute, data) return True @classmethod def _db_update_to_0_0_13(cls, execute, data): table = "additional_files" cls.update_db_add_pamhyr_id(execute, table, data) Scenario.update_db_add_scenario(execute, table) cls._db_create(execute, ext="_tmp") execute( "INSERT INTO additional_files_tmp " + "(pamhyr_id, enabled, name, path, text, scenario) " + "SELECT pamhyr_id, enabled, name, path, text, scenario " + "FROM additional_files" ) execute(f"DROP TABLE {table}") execute(f"ALTER TABLE {table}_tmp RENAME TO {table}") @classmethod def _db_load(cls, execute, data=None): new = [] table = execute( "SELECT pamhyr_id, enabled, name, path, text " + "FROM additional_files" ) for row in table: it = iter(row) id = next(it) enabled = (next(it) == 1) name = next(it) path = next(it) text = next(it) f = cls( id=id, enabled=enabled, name=name, path=path, text=text, status=data['status'] ) new.append(f) return new def _db_save(self, execute, data=None): sql = ( "INSERT INTO " + "additional_files(pamhyr_id, enabled, name, path, text) " + "VALUES (" + f"{self._pamhyr_id}, {self._enabled}, " + f"'{self._db_format(self._name)}', " + f"'{self._db_format(self._path)}', " + f"'{self._db_format(self._text)}'" + ")" ) execute(sql) return True