[base] create enum in base and use it in calendar and housing

This commit is contained in:
Bezleputh 2015-03-05 18:16:34 +01:00
commit a3d351eb88
3 changed files with 17 additions and 24 deletions

View file

@ -34,6 +34,21 @@ __all__ = ['UserError', 'FieldNotFound', 'NotAvailable',
'empty', 'BaseObject'] 'empty', 'BaseObject']
def enum(**enums):
_values = enums.values()
_keys = enums.keys()
_items = enums.items()
_types = list((type(value) for value in enums.values()))
_index = dict((value if not isinstance(value, dict) else value.itervalues().next(), i) for i, value in enumerate(enums.values()))
enums['keys'] = _keys
enums['values'] = _values
enums['items'] = _items
enums['index'] = _index
enums['types'] = _types
return type('Enum', (), enums)
def empty(value): def empty(value):
""" """
Checks if a value is empty (None, NotLoaded or NotAvailable). Checks if a value is empty (None, NotLoaded or NotAvailable).

View file

@ -17,7 +17,7 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>. # along with weboob. If not, see <http://www.gnu.org/licenses/>.
from .base import BaseObject, StringField, IntField, FloatField, Field from .base import BaseObject, StringField, IntField, FloatField, Field, enum
from .collection import CapCollection, CollectionNotFound, Collection from .collection import CapCollection, CollectionNotFound, Collection
from .date import DateField from .date import DateField
@ -27,17 +27,6 @@ from weboob.tools.date import parse_date
__all__ = ['BaseCalendarEvent', 'CapCalendarEvent'] __all__ = ['BaseCalendarEvent', 'CapCalendarEvent']
def enum(**enums):
_values = enums.values()
_items = enums.items()
_index = dict((value, i) for i, value in enumerate(enums.values()))
_types = list((type(value) for value in enums.values()))
enums['values'] = _values
enums['items'] = _items
enums['index'] = _index
enums['types'] = _types
return type('Enum', (), enums)
CATEGORIES = enum(CONCERT=u'Concert', CINE=u'Cinema', THEATRE=u'Theatre', TELE=u'Television', CONF=u'Conference') CATEGORIES = enum(CONCERT=u'Concert', CINE=u'Cinema', THEATRE=u'Theatre', TELE=u'Television', CONF=u'Conference')
#the following elements deal with ICalendar stantdards #the following elements deal with ICalendar stantdards

View file

@ -19,7 +19,7 @@
from .base import Capability, BaseObject, Field, IntField, DecimalField, \ from .base import Capability, BaseObject, Field, IntField, DecimalField, \
StringField, BytesField StringField, BytesField, enum
from .date import DateField from .date import DateField
__all__ = ['HousingPhoto', 'Housing', 'Query', 'City', 'CapHousing'] __all__ = ['HousingPhoto', 'Housing', 'Query', 'City', 'CapHousing']
@ -71,17 +71,6 @@ class Query(BaseObject):
TYPE_RENT = 0 TYPE_RENT = 0
TYPE_SALE = 1 TYPE_SALE = 1
def enum(**enums):
_values = enums.values()
_items = enums.items()
_index = dict((value, i) for i, value in enumerate(enums.values()))
_types = list((type(value) for value in enums.values()))
enums['values'] = _values
enums['items'] = _items
enums['index'] = _index
enums['types'] = _types
return type('Enum', (), enums)
HOUSE_TYPES = enum(APART=u'Apartment', HOUSE_TYPES = enum(APART=u'Apartment',
HOUSE=u'House', HOUSE=u'House',
PARKING=u'Parking', PARKING=u'Parking',