| Home | Trees | Indices | Help |
|---|
|
|
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
47 """
48 A simple text attribute.
49 """
52
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
62 """
63 Parses a value and checks for errors.
64 Override with specialiced behaviour.
65 """
66 return str(value)
67
70 """
71 Parses a value and checks for errors.
72 Override with specialiced behaviour.
73 """
74 return unicode(value)
75
76
84
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
108
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
125
| Home | Trees | Indices | Help |
|---|
| Generated by Epydoc 3.0.1 on Tue May 22 00:00:40 2012 | http://epydoc.sourceforge.net |