1 # This file is part of Indico.
2 # Copyright (C) 2002 - 2015 European Organization for Nuclear Research (CERN).
4 # Indico is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License as
6 # published by the Free Software Foundation; either version 3 of the
7 # License, or (at your option) any later version.
9 # Indico is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with Indico; if not, see <http://www.gnu.org/licenses/>.
19 from indico
.core
.errors
import IndicoError
20 from indico
.core
.logger
import Logger
23 class Serializer(object):
26 def to_serializable(self
, attr
='__public__', converters
=None):
28 if converters
is None:
30 for k
in getattr(self
, attr
):
32 if isinstance(k
, tuple):
38 if callable(v
): # to make it generic, we can get rid of it by properties
40 if isinstance(v
, Serializer
):
41 v
= v
.to_serializable()
42 elif isinstance(v
, list):
43 v
= [e
.to_serializable() for e
in v
]
44 elif isinstance(v
, dict):
45 v
= dict((k
, vv
.to_serializable() if isinstance(vv
, Serializer
) else vv
)
46 for k
, vv
in v
.iteritems())
47 elif isinstance(v
, Enum
):
49 if type(v
) in converters
:
50 v
= converters
[type(v
)](v
)
51 serializable
[name
] = v
53 msg
= 'Could not retrieve {}.{}.'.format(self
.__class
__.__name
__, k
)
54 Logger
.get('Serializer{}'.format(self
.__class
__.__name
__)).exception(msg
)
55 raise IndicoError(msg
)