Package mobi :: Package mtld :: Package da :: Module property
[hide private]
[frames] | no frames]

Source Code for Module mobi.mtld.da.property

  1  #!/usr/bin/env python 
  2   
  3  """ 
  4  @copyright: 
  5   Copyright (c) 2014 by mTLD Top Level Domain Limited. All rights reserved.\n 
  6   Portions copyright (c) 2008 by Argo Interactive Limited.\n 
  7   Portions copyright (c) 2008 by Nokia Inc.\n 
  8   Portions copyright (c) 2008 by Telecom Italia Mobile S.p.A.\n 
  9   Portions copyright (c) 2008 by Volantis Systems Limited.\n 
 10   Portions copyright (c) 2002-2008 by Andreas Staeding.\n 
 11   Portions copyright (c) 2008 by Zandan.\n 
 12  @author: dotMobi 
 13  """ 
 14   
 15  from mobi.mtld.da.exception.incorrect_property_type_exception import IncorrectPropertyTypeException 
 16  from mobi.mtld.da.data_type import DataType 
17 18 -class Property(object):
19 """ 20 Contains a property value and data type id. The value can be fetched as a 21 generic Object or one of the convenience str(), int(), bool() methods can be 22 used to get the value in a specific type. 23 """ 24 25 __value = '' 26 __data_type_id = 0 27 __data_type_name = 'Boolean' 28
29 - def __init__(self, value, data_type):
30 """ 31 Creates a new Property with a value and data type 32 @param value: is the value to store 33 @param data_type: is a byte or char that represents the data type of the value to 34 store 35 """ 36 if isinstance(data_type, basestring): 37 38 if data_type == 'b': 39 self.__data_type_id = DataType.BOOLEAN 40 self.__value = value 41 elif data_type == 'i': 42 self.__data_type_id = DataType.INTEGER 43 self.__value = int(value) 44 elif data_type == 's': 45 self.__data_type_id = DataType.STRING 46 if isinstance(value, list): 47 self.__value = ",".join(value) 48 else: 49 self.__value = str(value) 50 elif data_type == 'd': 51 self.__data_type_id = DataType.DOUBLE 52 self.__value = value 53 else: 54 self.__data_type_id = DataType.UNKNOWN 55 self.__value = value 56 57 elif isinstance(data_type, int): 58 59 self.__data_type_id = data_type 60 61 if data_type == DataType.BOOLEAN: 62 self.__value = value 63 elif data_type == DataType.INTEGER: 64 self.__value = int(value) 65 elif data_type == DataType.STRING: 66 if isinstance(value, list): 67 self.__value = ",".join(value) 68 else: 69 self.__value = str(value) 70 elif data_type == DataType.DOUBLE: 71 self.__value = value 72 else: 73 self.__data_type_id = DataType.UNKNOWN 74 self.__value = value 75 76 self.__data_type_name = DataType.name(self.__data_type_id)
77 78 @property
79 - def value(self):
80 return self.__value
81 82 @property
83 - def data_type_id(self):
84 return self.__data_type_id
85
86 - def data_type(self):
87 """ 88 Get data type name. 89 It returns a String with the data type name 90 """ 91 return self.__data_type_name
92
93 - def __iter__(self):
94 """ 95 Gets a set of possible values for this property. This is typically only 96 used when it is known that a given property name can have multiple 97 possible values. All items in the set will have the same data type. 98 It returns the array value of the property 99 """ 100 if isinstance(self.__value, basestring) and self.__value.contains(","): 101 values = self.__value.split(",") 102 else: 103 values = self.__value 104 105 class iterator(object): 106 def __init__(self, obj): 107 self.obj = obj 108 self.index = -1
109 def __iter__(self): 110 return self
111 def next(self): 112 if self.index < len(values) - 1: 113 self.index += 1 114 return self.obj[self.index] 115 else: 116 raise StopIteration 117 return iterator(self.__value) 118 119
120 - def __bool__(self):
121 """ 122 Get the value of the property as a boolean (Python 3.x) 123 """ 124 return self.__nonzero__()
125
126 - def __nonzero__(self): # Python 2.x
127 128 if(self.__data_type_id != DataType.BOOLEAN): 129 raise IncorrectPropertyTypeException('Property is not convertible to a ' + 130 'boolean') 131 132 return self.__value == 1 or self.__value == '1' or self.__value is True 133
134 - def __int__(self):
135 """ 136 Get the value of the property as an integer. 137 """ 138 if(self.__data_type_id != DataType.BYTE and 139 self.__data_type_id != DataType.SHORT and 140 self.__data_type_id != DataType.INTEGER): 141 raise IncorrectPropertyTypeException('Property is not convertible to an int') 142 143 return int(self.__value)
144
145 - def __str__(self):
146 """ 147 Gets the value of the property as string. If a property has multiple possible 148 values then the values are concatenated with a comma. 149 """ 150 if isinstance(self.__value, list): 151 for i in range(0, len(self.__value)): 152 if isinstance(self.__value[i], bytes): 153 self.__value[i] = self.__value[i].decode("utf-8") 154 return ",".join(self.__value) 155 156 if isinstance(self.__value, bytes): 157 try: 158 self.__value = self.__value.decode("utf-8") 159 except: 160 pass 161 162 return str(self.__value)
163
164 - def __eq__(self, other):
165 """ 166 Compare two instances of this class. 167 If both have equal values and data type then returns true. 168 """ 169 return (type(self) == type(other) and 170 self.data_typeName == other.data_type() and 171 self.__value == other.value)
172
173 - def __ne__(self, other):
174 """ 175 Opposite to __eq__() 176 """ 177 return (type(self) != type(other) or 178 self.data_typeName != other.data_type() or 179 self.__value != other.value)
180