Package horizons :: Package component
[hide private]
[frames] | no frames]

Source Code for Package horizons.component

  1  # ################################################### 
  2  # Copyright (C) 2008-2017 The Unknown Horizons Team 
  3  # team@unknown-horizons.org 
  4  # This file is part of Unknown Horizons. 
  5   
  6  # Unknown Horizons is free software; you can redistribute it and/or modify 
  7  # it under the terms of the GNU General Public License as published by 
  8  # the Free Software Foundation; either version 2 of the License, or 
  9  # (at your option) any later version. 
 10  # 
 11  # This program is distributed in the hope that it will be useful, 
 12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  # GNU General Public License for more details. 
 15  # 
 16  # You should have received a copy of the GNU General Public License 
 17  # along with this program; if not, write to the 
 18  # Free Software Foundation, Inc., 
 19  # 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 20  # ################################################### 
 21   
 22  from typing import List 
23 24 25 -class Component:
26 """ 27 Base class for all components. Something like an interface. 28 29 TUTORIAL: 30 This is what all components share, basically only set up and tear down. 31 It would be advisable to look through all methods here, 32 and especially to see the difference between loading components (from 33 a savegame) and setting them up in a normal game. 34 35 Once you know how it works, please proceed to horizons/world/ingametype.py 36 where you'll see how the actual things in Unknown Horizons are created. 37 """ 38 39 # Store the name of this component. This has to be overwritten in subclasses 40 NAME = None # type: str 41 42 # Store dependencies to other components here 43 DEPENDENCIES = [] # type: List[str] 44
45 - def __init__(self):
46 """ 47 Used for initialization code that does not require any other components. 48 This is always called first, on construction and on load.""" 49 super().__init__() 50 self.instance = None # Has to be set by the componentholder
51 52 @property
53 - def session(self):
54 return self.instance.session
55
56 - def initialize(self):
57 """ 58 This is called by the ComponentHolder after it set the instance. 59 Use this to initialize any needed infrastructure. 60 When this is called, it is guaranteed that all other components this one 61 has a dependency on have been added, but initalize may not have been called 62 on them, only __init__. It is only called after construction, not on load(). 63 """ 64 pass
65
66 - def remove(self):
67 """ 68 Removes component and reference to instance 69 """ 70 self.instance = None
71
72 - def save(self, db):
73 """ 74 Will do nothing, but will be always called in componentholder code, even if not implemented. 75 """ 76 pass
77
78 - def load(self, db, worldid):
79 """ 80 This does on load what __init() and initalize() together do on constructions at runtime. 81 Has to set up everything that is not setup in __init__(). Note that on loading 82 __init__() is called with the data needed by the component through get_instance(), 83 but initialize() is not, so any work needed for loading as well should be moved 84 to a separate method and called here. 85 """ 86 pass
87 88 @classmethod
89 - def get_instance(cls, arguments=None):
90 """ 91 This function is used to instantiate classes from yaml data. Override this if 92 the component has more than just a basic constructor with primitive types 93 (takes custom classes as arguments, e.g. Storages) 94 """ 95 arguments = arguments or {} 96 return cls(**arguments)
97
98 - def __gt__(self, other):
99 return other.__class__ in self.DEPENDENCIES
100
101 - def __lt__(self, other):
102 return not self.__gt__(other)
103