1# Copyright 2011 Google Inc. All Rights Reserved. 2"""Module to represent a Field in an experiment file.""" 3 4 5class Field(object): 6 """Class representing a Field in an experiment file.""" 7 8 def __init__(self, name, required, default, inheritable, description): 9 self.name = name 10 self.required = required 11 self.assigned = False 12 self.default = default 13 self._value = default 14 self.inheritable = inheritable 15 self.description = description 16 17 def Set(self, value, parse=True): 18 if parse: 19 self._value = self._Parse(value) 20 else: 21 self._value = value 22 self.assigned = True 23 24 def Append(self, value): 25 self._value += self._Parse(value) 26 self.assigned = True 27 28 def _Parse(self, value): 29 return value 30 31 def Get(self): 32 return self._value 33 34 def GetString(self): 35 return str(self._value) 36 37 38class TextField(Field): 39 """Class of text field.""" 40 41 def __init__(self, 42 name, 43 required=False, 44 default='', 45 inheritable=False, 46 description=''): 47 super(TextField, self).__init__(name, required, default, inheritable, 48 description) 49 50 def _Parse(self, value): 51 return str(value) 52 53 54class BooleanField(Field): 55 """Class of boolean field.""" 56 57 def __init__(self, 58 name, 59 required=False, 60 default=False, 61 inheritable=False, 62 description=''): 63 super(BooleanField, self).__init__(name, required, default, inheritable, 64 description) 65 66 def _Parse(self, value): 67 if value.lower() == 'true': 68 return True 69 elif value.lower() == 'false': 70 return False 71 raise TypeError("Invalid value for '%s'. Must be true or false." % 72 self.name) 73 74 75class IntegerField(Field): 76 """Class of integer field.""" 77 78 def __init__(self, 79 name, 80 required=False, 81 default=0, 82 inheritable=False, 83 description=''): 84 super(IntegerField, self).__init__(name, required, default, inheritable, 85 description) 86 87 def _Parse(self, value): 88 return int(value) 89 90 91class FloatField(Field): 92 """Class of float field.""" 93 94 def __init__(self, 95 name, 96 required=False, 97 default=0, 98 inheritable=False, 99 description=''): 100 super(FloatField, self).__init__(name, required, default, inheritable, 101 description) 102 103 def _Parse(self, value): 104 return float(value) 105 106 107class ListField(Field): 108 """Class of list field.""" 109 110 def __init__(self, 111 name, 112 required=False, 113 default=None, 114 inheritable=False, 115 description=''): 116 super(ListField, self).__init__(name, required, default, inheritable, 117 description) 118 119 def _Parse(self, value): 120 return value.split() 121 122 def GetString(self): 123 return ' '.join(self._value) 124 125 def Append(self, value): 126 v = self._Parse(value) 127 if not self._value: 128 self._value = v 129 else: 130 self._value += v 131 self.assigned = True 132 133 134class EnumField(Field): 135 """Class of enum field.""" 136 137 def __init__(self, 138 name, 139 options, 140 required=False, 141 default='', 142 inheritable=False, 143 description=''): 144 super(EnumField, self).__init__(name, required, default, inheritable, 145 description) 146 self.options = options 147 148 def _Parse(self, value): 149 if value not in self.options: 150 raise TypeError("Invalid enum value for field '%s'. Must be one of (%s)" % 151 (self.name, ', '.join(self.options))) 152 return str(value) 153