# -*- coding: utf-8 -*-

from __future__ import unicode_literals, print_function, division, absolute_import
from openfisca_core.commons import to_unicode


def build_entities(tax_benefit_system):
    entities = {
        entity.key: build_entity(entity)
        for entity in tax_benefit_system.entities
        }
    return entities


def build_entity(entity):

    entity_formated = {
        'plural': entity.plural,
        'description': to_unicode(entity.doc)
        }
    if hasattr(entity, 'roles'):
        entity_formated['roles'] = \
            {
            role.key: build_role(role)
            for role in entity.roles
            }
    return entity_formated


def build_role(role):
    role_formated = {
        'plural': role.plural,
        'description': role.doc
        }

    if role.max:
        role_formated['max'] = role.max
    if role.subroles:
        role_formated['max'] = len(role.subroles)

    role_formated['mandatory'] = True if role_formated.get('max') else False
    return role_formated

@@ -360,31 +360,32 @@
 ):%0A%0A    
-entity_
 format
+t
 ed
+_entity
  = %7B%0A   
@@ -436,16 +436,67 @@
 iption':
+ to_unicode(entity.label),%0A        'documentation':
  to_unic
@@ -562,31 +562,32 @@
         
-entity_
 format
+t
 ed
+_entity
 %5B'roles'
@@ -593,22 +593,8 @@
 '%5D =
- %5C%0A           
  %7B%0A 
@@ -697,23 +697,24 @@
 urn 
-entity_
 format
+t
 ed
+_entity
 %0A%0A%0Ad
@@ -738,29 +738,30 @@
 e):%0A    
-role_
 format
+t
 ed
+_role
  = %7B%0A   
@@ -852,37 +852,38 @@
 ax:%0A        
-role_
 format
+t
 ed
+_role
 %5B'max'%5D = ro
@@ -915,37 +915,38 @@
 es:%0A        
-role_
 format
+t
 ed
+_role
 %5B'max'%5D = le
@@ -967,29 +967,30 @@
 s)%0A%0A    
-role_
 format
+t
 ed
+_role
 %5B'mandat
@@ -1005,29 +1005,30 @@
 True if 
-role_
 format
+t
 ed
+_role
 .get('ma
@@ -1057,18 +1057,19 @@
 urn 
-role_
 format
+t
 ed
+_role
 %0A

Make variable names more explicit