Gnumed.timelinelib.canvas.data.base

Package Gnumed :: Package timelinelib :: Package canvas :: Package data :: Module base |
[frames] | no frames]

# Source Code for Module Gnumed.timelinelib.canvas.data.base

     1  # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018  Rickard Lindberg, Roger Lindberg
     2  #
     3  # This file is part of Timeline.
     4  #
     5  # Timeline is free software: you can redistribute it and/or modify
     6  # it under the terms of the GNU General Public License as published by
     7  # the Free Software Foundation, either version 3 of the License, or
     8  # (at your option) any later version.
     9  #
    10  # Timeline is distributed in the hope that it will be useful,
    11  # but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  # GNU General Public License for more details.
    14  #
    15  # You should have received a copy of the GNU General Public License
    16  # along with Timeline.  If not, see <http://www.gnu.org/licenses/>.
    17
    18
    19 -class ItemBase(object):
    20
    21 -    def __init__(self, db, id_, immutable_value):
    22          self._db = db
    23          self._id = id_
    24          self._immutable_value = immutable_value
    25
    26 -    def duplicate(self, target_db=None):
    27          if target_db is None:
    28              db = self.db
    29          else:
    30              db = target_db
    31          return self.__class__(db=db, immutable_value=self._immutable_value)
    32
    33      @property
    34 -    def db(self):
    35          return self._db
    36
    37      @db.setter
    38 -    def db(self, value):
    39          if self._db is None:
    40              self._db = value
    41          elif self._db is not value:
    42              raise ValueError("Can't change db")
    43
    44      @property
    45 -    def id(self):
    46          return self._id
    47
    48      @id.setter
    49 -    def id(self, value):
    50          self._id = value
    51
    52 -    def get_id(self):
    53          return self.id
    54
    55 -    def has_id(self):
    56          return self.id is not None
    57
    58 -    def set_id(self, id_):
    59          self.id = id_
    60          return self
    61
    62 -    def ensure_id(self):
    63          if self.id is None:
    64              self.id = self.db.next_id()
    65          return self.id
    66
    67
    68 -def create_noop_property(klass, name, value):
    69      def getter(self):
    70          return value
    71
    72      def setter(self, value):
    73          return self
    74      setattr(klass, "get_{0}".format(name), getter)
    75      setattr(klass, "set_{0}".format(name), setter)
    76      setattr(klass, "{0}".format(name), property(getter, setter))
    77