Package fife :: Package extensions :: Package pychan :: Module attrs
[hide private]
[frames] | no frames]

Source Code for Module fife.extensions.pychan.attrs

  1  # -*- coding: utf-8 -*- 
  2   
  3  # #################################################################### 
  4  #  Copyright (C) 2005-2011 by the FIFE team 
  5  #  http://www.fifengine.net 
  6  #  This file is part of FIFE. 
  7  # 
  8  #  FIFE is free software; you can redistribute it and/or 
  9  #  modify it under the terms of the GNU Lesser General Public 
 10  #  License as published by the Free Software Foundation; either 
 11  #  version 2.1 of the License, or (at your option) any later version. 
 12  # 
 13  #  This library is distributed in the hope that it will be useful, 
 14  #  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 15  #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
 16  #  Lesser General Public License for more details. 
 17  # 
 18  #  You should have received a copy of the GNU Lesser General Public 
 19  #  License along with this library; if not, write to the 
 20  #  Free Software Foundation, Inc., 
 21  #  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA 
 22  # #################################################################### 
 23   
 24  """ 
 25  Simple error checking attributes. 
 26   
 27  This module defines a set of Attribute classes 
 28  which you can use to define possible values 
 29  an attribute of an object accepts. 
 30   
 31  Usage:: 
 32    class SomeObject: 
 33        nameAttr, posAttr = [ Attr("name"), PointAttr("pos") ] 
 34   
 35    obj =  SomeObject() 
 36    obj.nameAttr.set(obj,"newName") 
 37    obj.posAttr.set(obj,"89,89") 
 38   
 39  This is most useful for error checking parsing and defining 
 40  accepted attributes in classes and is used by pychan internally. 
 41   
 42  """ 
 43   
 44  from exceptions import ParserError 
 45   
46 -class Attr(object):
47 """ 48 A simple text attribute. 49 """
50 - def __init__(self,name):
51 self.name = name
52
53 - def set(self,obj,value):
54 """ 55 Parses the given value with the L{parse} method 56 and sets it on the given instance with C{setattr}. 57 """ 58 value = self.parse(value) 59 setattr(obj,self.name,value)
60
61 - def parse(self,value):
62 """ 63 Parses a value and checks for errors. 64 Override with specialiced behaviour. 65 """ 66 return str(value)
67
68 -class UnicodeAttr(Attr):
69 - def parse(self,value):
70 """ 71 Parses a value and checks for errors. 72 Override with specialiced behaviour. 73 """ 74 return unicode(value)
75 76
77 -class PointAttr(Attr):
78 - def parse(self,value):
79 try: 80 x,y = tuple(map(int,str(value).split(','))) 81 return x,y 82 except: 83 raise ParserError("Expected a comma separated list of two integers.")
84
85 -class ColorAttr(Attr):
86 - def parse(self,value):
87 a = 255 88 try: 89 try: 90 r,g,b,a = tuple(map(int,str(value).split(','))) 91 for c in (r,g,b,a): 92 if not 0 <= c < 256: raise ParserError("Expected a color (Failed: 0 <= %d <= 255)" %c) 93 except: 94 r,g,b = tuple(map(int,str(value).split(','))) 95 for c in (r,g,b): 96 if not 0 <= c < 256: raise ParserError("Expected a color (Failed: 0 <= %d <= 255)" %c) 97 except: 98 raise ParserError("Expected a color.") 99 100 return r,g,b,a
101
102 -class IntAttr(Attr):
103 - def parse(self,value):
104 try: 105 return int(value) 106 except: 107 raise ParserError("Expected a single integer.")
108
109 -class BoolAttr(Attr):
110 - def parse(self,value):
111 try: 112 value = int(value) 113 if value not in (0,1): 114 raise ParserError("Expected a 0 or 1.") 115 return value 116 except: 117 raise ParserError("Expected a 0 or 1.")
118
119 -class FloatAttr(Attr):
120 - def parse(self, value):
121 try: 122 return float(value) 123 except: 124 raise ParseError("Expected a float.")
125