Package schlachtfeld :: Package amov :: Package Skripte :: Package Kernskripte :: Module Objekt
[hide private]
[frames] | no frames]

Source Code for Module schlachtfeld.amov.Skripte.Kernskripte.Objekt

  1  #!/bin/env python 
  2  # encoding: utf-8 
  3  """ 
  4  Read out and write out an Object from/to a yaml-file. 
  5   
  6  This is a base class which gets used by other classes.  
  7   
  8  Just call it with an ID-dict and a basic template (object or dict) and get the object or dict via Objekt.objekt, or write it via Objekt.write(<dict>).  
  9  """ 
 10   
 11   
 12  # Charakterverwaltung - Verwalte Charaktere im lesbaren YAML Format 
 13  # Copyright © 2007 - 2007 Arne Babenhauserheide 
 14   
 15  # This program is free software; you can redistribute it and/or modify 
 16  # it under the terms of the GNU General Public License as published by 
 17  # the Free Software Foundation; either version 2 of the License, or 
 18  # (at your option) any later version. 
 19   
 20  # This program is distributed in the hope that it will be useful, 
 21  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 22  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 23  # GNU General Public License for more details. 
 24   
 25  # You should have received a copy of the GNU General Public License 
 26  # along with this program; if not, write to the Free Software 
 27  # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, 
 28  # MA 02110-1301 USA 
 29   
 30  # Diese Datei ist ein Container für die klasse Charaktere 
 31   
 32  ### Imports ### 
 33   
 34  # Für das Dateiformat brauchen wir yaml 
 35  import yaml 
 36   
 37  # Für die Übersetzung von IDs in Dateinamen brauchen wir noch tag_zu_datei 
 38   
 39  from ein_und_ausgabe import tag_zu_datei 
 40   
 41  # Um Charaktere per Kommandozeile übergeben zu können brauchen wir sys 
 42  import sys 
 43   
 44  # Für die Prüfung ob Charakterdateien schon existieren brauchen wir außerdem os.path 
 45   
 46  import os 
 47   
 48  ### Imports ###  
 49   
 50  ### Classes ### 
 51   
52 -class Objekt:
53 - def __init__(self, ID=yaml.load("""ID: tag:draketo.de,2007:Sskreszta 54 Version: 0.15 55 Kategorie: Charaktere 56 """), template=yaml.load("""- Name: " " 57 - Grunddaten: 58 Beschreibung: " " 59 Herkunft: 60 Sprache: ' ' 61 Region: ' ' 62 Stimmung: ' ' 63 - Werte: 64 - Eigenschaften: " " 65 - Fertigkeiten: 66 Nahkampf: &id001 67 Zahlenwert: 12 68 - Merkmale: " " 69 - Ausrüstung: 70 Waffen: 71 Waffenlos: &id002 72 Name: Waffenlos 73 Schaden: 1 74 Rüestung: 75 Stoffkleidung: &hauptruestung 76 Name: Stoffkleidung 77 Schutz: 1 78 - Kampfwerte: 79 Hauptwaffe: 80 Kampffertigkeit: *id001 81 Waffe: *id002 82 Trefferpunkte: 24 83 Wundschwelle: 4 84 Hauptrüstung: *hauptruestung""")):
85 self.ID = ID 86 self.template = template 87 self.template_yaml = yaml.dump(self.template, default_flow_style=False, allow_unicode=True) 88 self.objekt = self.laden() 89
90 - def laden(self):
91 """Load an object from a yaml-file.""" 92 # print "Charakterbogen aus Datei", self.dateiname(), "wird geladen" 93 if os.path.exists(self.dateiname()): 94 datei = open(self.dateiname(), "r") 95 charakter = yaml.load(datei.read()) 96 datei.close() 97 return charakter 98 elif not os.path.isdir("./" + self.kategorie() + "/"): 99 print "Kategorie", self.kategorie(), "existiert nicht. Kategorie und Datei werden erstellt." 100 os.mkdir("./" + self.kategorie() + "/") 101 datei = open(self.dateiname(), "w") 102 datei.write(self.template_yaml) 103 datei.close() 104 return self.laden() 105 else: 106 print "Datei", self.dateiname(), "existiert nicht. Wird erstellt." 107 datei = open(self.dateiname(), "w") 108 datei.write(self.template_yaml) 109 datei.close() 110 return self.laden()
111
112 - def dateiname(self):
113 """Return path and filename based on Cathegory, Version and Tag.""" 114 tag_zu_dat = tag_zu_datei.Datei(self.ID) 115 return tag_zu_dat.dateiname_ausgeben() 116
117 - def kategorie(self):
118 """Return the cathegory of the Object""" 119 tag_zu_dat = tag_zu_datei.Datei(self.ID) 120 return tag_zu_dat.kategorie_ausgeben()
121
122 - def write(self):
123 print u"öffne", self.dateiname() 124 datei = open(self.dateiname(), "w") 125 print u"Schreibe", self.kategorie(), self.objekt_name() 126 datei.write(self.yaml()) 127 datei.close() 128
129 - def yaml(self):
130 print self.objekt 131 print yaml.dump(self.objekt, default_flow_style=False, allow_unicode=True) 132 return yaml.dump(self.objekt, default_flow_style=False, allow_unicode=True) 133
134 - def objekt_name(self):
135 tag_zu_dat = tag_zu_datei.Datei(self.ID) 136 return tag_zu_dat.name_ausgeben() 137 138 ### Classes ### 139 140 ### Selt-Test ### 141 142 if __name__ == '__main__': 143 objekt = Objekt() 144 print objekt 145 print yaml.dump(objekt.objekt) 146