# coding: utf-8
#
# Copyright 2013 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS-IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Classes relating to Oppia parameters."""

__author__ = 'Sean Lip'

import utils


class Parameter(object):
    """Value object for a parameter.

    A parameter consists of a name, an obj_type, a list of values
    and an optional description. Each element in the list of values
    must be of the given obj_type.
    """
    name = None
    values = None
    obj_type = None
    description = ''

    def __init__(self, name, values, obj_type, description=''):
        self.name = name
        self.values = values
        self.obj_type = obj_type
        self.description = description
        assert self.values and self.obj_type and self.name

    @property
    def value(self):
        """Picks one of the values in the list at random."""
        if not self.values:
            return None
        return utils.get_random_choice(self.values)

@@ -679,16 +679,54 @@
 n Lip'%0A%0A
+from oppia.domain import obj_services%0A
 import u
@@ -963,16 +963,75 @@
 bj_type.
+%0A%0A    Note that the obj_type must be set before the values.
 %0A    %22%22%22
@@ -1051,22 +1051,24 @@
 one%0A    
-values
+obj_type
  = None%0A
@@ -1067,32 +1067,30 @@
  = None%0A    
-obj_type
+values
  = None%0A    
@@ -1121,12 +1121,15 @@
 f __
-init
+setattr
 __(s
@@ -1148,37 +1148,266 @@
 alue
-s, obj_type, description=''):
+):%0A        if name == 'name':%0A            assert value%0A        elif name == 'obj_type':%0A            # Later, accept actual obj_types (not the string for the name).%0A            # Or maybe accept both?%0A            assert value and isinstance(value, basestring)
 %0A   
@@ -1415,62 +1415,146 @@
     
-s
 el
-f.
+if 
 name =
- name%0A        self.values = values%0A
+= 'values':%0A            assert value and isinstance(value, list)%0A            value = %5Bobj_services.get_object_class(%0A  
         
 self
@@ -1545,24 +1545,30 @@
 (%0A          
+      
 self.obj_typ
@@ -1572,46 +1572,149 @@
 type
- = obj_type%0A        self.description =
+).normalize(item) for item in value%5D%0A%0A        super(Parameter, self).__setattr__(name, value)%0A%0A    def __init__(self, name, obj_type, values,
  des
@@ -1721,16 +1721,21 @@
 cription
+=''):
 %0A       
@@ -1739,58 +1739,125 @@
     
-assert self.values and self.obj_type and self.name
+self.name = name%0A        self.obj_type = obj_type%0A        self.values = values%0A        self.description = description
 %0A%0A  

Add validation to the parameter domain module.