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