def validate_parameter_xml_json(parameter, state = None): if parameter is None: return None, None state = conv.add_ancestor_to_state(state, parameter) validated_parameter, errors = conv.pipe( conv.test_isinstance(dict), conv.struct( dict( code = conv.pipe( conv.test_isinstance(basestring), conv.cleanup_line, conv.not_none, ), description = conv.pipe( conv.test_isinstance(basestring), conv.cleanup_line, ), format = conv.pipe( conv.test_isinstance(basestring), conv.input_to_slug, conv.test_in(xml_json_formats), ), tail = conv.pipe( conv.test_isinstance(basestring), conv.cleanup_text, ), taille = conv.pipe( conv.test_isinstance(basestring), conv.test_in([ 'moinsde20', 'plusde20', ]), ), text = conv.pipe( conv.test_isinstance(basestring), conv.cleanup_text, ), type = conv.pipe( conv.test_isinstance(basestring), conv.input_to_slug, conv.test_in(json_unit_by_xml_json_type), ), VALUE = conv.pipe( conv.test_isinstance(list), conv.uniform_sequence( validate_value_xml_json, drop_none_items = True, ), make_validate_values_xml_json_dates(require_consecutive_dates = True), conv.empty_to_none, conv.not_none, ), ), constructor = collections.OrderedDict, drop_none_values = 'missing', keep_value_order = True, ), )(parameter, state = state) conv.remove_ancestor_from_state(state, parameter) return validated_parameter, errors
def validate_scale_xml_json(scale, state = None): if scale is None: return None, None state = conv.add_ancestor_to_state(state, scale) validated_scale, errors = conv.pipe( conv.test_isinstance(dict), conv.struct( dict( code = conv.pipe( conv.test_isinstance(basestring), conv.cleanup_line, conv.not_none, ), description = conv.pipe( conv.test_isinstance(basestring), conv.cleanup_line, ), option = conv.pipe( conv.test_isinstance(basestring), conv.input_to_slug, conv.test_in(( 'contrib', 'main-d-oeuvre', 'noncontrib', )), ), TRANCHE = conv.pipe( conv.test_isinstance(list), conv.uniform_sequence( validate_slice_xml_json, drop_none_items = True, ), validate_slices_xml_json_dates, conv.empty_to_none, conv.not_none, ), tail = conv.pipe( conv.test_isinstance(basestring), conv.cleanup_text, ), text = conv.pipe( conv.test_isinstance(basestring), conv.cleanup_text, ), type = conv.pipe( conv.test_isinstance(basestring), conv.input_to_slug, conv.test_in(( 'monetary', )), ), ), constructor = collections.OrderedDict, drop_none_values = 'missing', keep_value_order = True, ), )(scale, state = state) conv.remove_ancestor_from_state(state, scale) return validated_scale, errors
def input_to_dated_python(self): enum = self.variable.possible_values if enum is None: return conv.test_isinstance(basestring_type) return conv.pipe( # Verify that item index belongs to enumeration. conv.test_in([item.name for item in list(enum)]))
def validate_dated_value_json(value, state = None): if value is None: return None, None container = state.ancestors[-1] value_converter = dict( boolean = conv.condition( conv.test_isinstance(int), conv.test_in((0, 1)), conv.test_isinstance(bool), ), float = conv.condition( conv.test_isinstance(int), conv.anything_to_float, conv.test_isinstance(float), ), integer = conv.condition( conv.test_isinstance(float), conv.pipe( conv.test(lambda number: round(number) == number), conv.function(int), ), conv.test_isinstance(int), ), rate = conv.condition( conv.test_isinstance(int), conv.anything_to_float, conv.test_isinstance(float), ), )[container.get('format') or 'float'] # Only parameters have a "format". return value_converter(value, state = state or conv.default_state)
def validate_dated_value_json(value, state=None): if value is None: return None, None container = state.ancestors[-1] value_converter = dict( boolean=conv.condition( conv.test_isinstance(int), conv.test_in((0, 1)), conv.test_isinstance(bool), ), float=conv.condition( conv.test_isinstance(int), conv.anything_to_float, conv.test_isinstance(float), ), integer=conv.condition( conv.test_isinstance(float), conv.pipe( conv.test(lambda number: round(number) == number), conv.function(int), ), conv.test_isinstance(int), ), rate=conv.condition( conv.test_isinstance(int), conv.anything_to_float, conv.test_isinstance(float), ), )[container.get('format') or 'float'] # Only parameters have a "format". return value_converter(value, state=state or conv.default_state)
def validate_node_json(node, state=None): if node is None: return None, None if state is None: state = conv.default_state validated_node, errors = conv.pipe( conv.condition( conv.test_isinstance(list), conv.uniform_sequence( validate_node_json, drop_none_items=True, ), conv.pipe( conv.condition( conv.test_isinstance(basestring_type), conv.function(lambda code: dict(code=code)), conv.test_isinstance(dict), ), conv.struct( dict( children=conv.pipe( conv.test_isinstance(list), conv.uniform_sequence( validate_node_json, drop_none_items=True, ), conv.empty_to_none, ), code=conv.pipe( conv.test_isinstance(basestring_type), conv.cleanup_line, ), ), constructor=collections.OrderedDict, default=conv.noop, drop_none_values='missing', keep_value_order=True, ), ), ), conv.empty_to_none, )(node, state=state) if validated_node is None or errors is not None: return validated_node, errors if isinstance(validated_node, dict) and not validated_node.get('children'): validated_node, errors = conv.struct( dict(code=conv.pipe( conv.test_in(tax_benefit_system.variables), conv.not_none, ), ), default=conv.noop, )(validated_node, state=state) return validated_node, errors
def make_json_or_python_to_axes(tax_benefit_system): column_by_name = tax_benefit_system.variables return conv.pipe( conv.test_isinstance(list), conv.uniform_sequence( conv.pipe( conv.item_or_sequence( conv.pipe( conv.test_isinstance(dict), conv.struct( dict( count=conv.pipe( conv.test_isinstance(int), conv.test_greater_or_equal(1), conv.not_none, ), index=conv.pipe( conv.test_isinstance(int), conv.test_greater_or_equal(0), conv.default(0), ), max=conv.pipe( conv.test_isinstance((float, int)), conv.not_none, ), min=conv.pipe( conv.test_isinstance((float, int)), conv.not_none, ), name=conv.pipe( conv.test_isinstance(basestring_type), conv.test_in(column_by_name), conv.test( lambda column_name: tax_benefit_system. get_variable(column_name).dtype in (np.float32, np.int16, np.int32), error=N_( 'Invalid type for axe: integer or float expected' )), conv.not_none, ), period=conv.function(periods.period), ), ), ), drop_none_items=True, ), conv.make_item_to_singleton(), ), drop_none_items=True, ), conv.empty_to_none, )
def json_to_dated_python(self): enum = self.variable.possible_values possible_names = [item.name for item in list(enum)] if enum is None: return conv.pipe(conv.test_isinstance(basestring_type)) return conv.pipe( conv.test_isinstance(basestring_type), conv.pipe( # Verify that item belongs to enumeration. conv.test_in(possible_names), # Transform that item into enum object. conv.function(lambda enum_name: enum[enum_name])))
def validate_value_json(value, state = None): if value is None: return None, None container = state.ancestors[-1] value_converter = dict( boolean = conv.condition( conv.test_isinstance(int), conv.test_in((0, 1)), conv.test_isinstance(bool), ), float = conv.condition( conv.test_isinstance(int), conv.anything_to_float, conv.test_isinstance(float), ), integer = conv.condition( conv.test_isinstance(float), conv.pipe( conv.test(lambda number: round(number) == number), conv.function(int), ), conv.test_isinstance(int), ), rate = conv.condition( conv.test_isinstance(int), conv.anything_to_float, conv.test_isinstance(float), ), )[container.get('format') or 'float'] # Only parameters have a "format". state = conv.add_ancestor_to_state(state, value) validated_value, errors = conv.pipe( conv.test_isinstance(dict), conv.struct( { u'comment': conv.pipe( conv.test_isinstance(basestring), conv.cleanup_text, ), u'from': conv.pipe( conv.test_isinstance(basestring), conv.iso8601_input_to_date, conv.date_to_iso8601_str, conv.not_none, ), u'to': conv.pipe( conv.test_isinstance(basestring), conv.iso8601_input_to_date, conv.date_to_iso8601_str, conv.not_none, ), u'value': conv.pipe( value_converter, conv.not_none, ), }, constructor = collections.OrderedDict, drop_none_values = 'missing', keep_value_order = True, ), )(value, state = state) conv.remove_ancestor_from_state(state, value) return validated_value, errors
def validate_node_json(node, state = None): if node is None: return None, None state = conv.add_ancestor_to_state(state, node) validated_node, error = conv.test_isinstance(dict)(node, state = state) if error is not None: conv.remove_ancestor_from_state(state, node) return validated_node, error validated_node, errors = conv.struct( { '@context': conv.pipe( conv.test_isinstance(basestring), conv.make_input_to_url(full = True), conv.test_equals(u'http://openfisca.fr/contexts/legislation.jsonld'), ), '@type': conv.pipe( conv.test_isinstance(basestring), conv.cleanup_line, conv.test_in((u'Node', u'Parameter', u'Scale')), conv.not_none, ), 'comment': conv.pipe( conv.test_isinstance(basestring), conv.cleanup_text, ), 'description': conv.pipe( conv.test_isinstance(basestring), conv.cleanup_line, ), }, constructor = collections.OrderedDict, default = conv.noop, drop_none_values = 'missing', keep_value_order = True, )(validated_node, state = state) if errors is not None: conv.remove_ancestor_from_state(state, node) return validated_node, errors validated_node.pop('@context', None) # Remove optional @context everywhere. It will be added to root node later. node_converters = { '@type': conv.noop, 'comment': conv.noop, 'description': conv.noop, } node_type = validated_node['@type'] if node_type == u'Node': node_converters.update(dict( children = conv.pipe( conv.test_isinstance(dict), conv.uniform_mapping( conv.pipe( conv.test_isinstance(basestring), conv.cleanup_line, conv.not_none, ), conv.pipe( validate_node_json, conv.not_none, ), ), conv.empty_to_none, conv.not_none, ), )) elif node_type == u'Parameter': node_converters.update(dict( format = conv.pipe( conv.test_isinstance(basestring), conv.input_to_slug, conv.test_in([ 'boolean', 'float', 'integer', 'rate', ]), ), unit = conv.pipe( conv.test_isinstance(basestring), conv.input_to_slug, conv.test_in(units), ), values = conv.pipe( conv.test_isinstance(list), conv.uniform_sequence( validate_value_json, drop_none_items = True, ), make_validate_values_json_dates(require_consecutive_dates = True), conv.empty_to_none, conv.not_none, ), )) else: assert node_type == u'Scale' node_converters.update(dict( option = conv.pipe( conv.test_isinstance(basestring), conv.input_to_slug, conv.test_in(( 'contrib', 'main-d-oeuvre', 'noncontrib', )), ), slices = conv.pipe( conv.test_isinstance(list), conv.uniform_sequence( validate_slice_json, drop_none_items = True, ), validate_slices_json_dates, conv.empty_to_none, conv.not_none, ), unit = conv.pipe( conv.test_isinstance(basestring), conv.input_to_slug, conv.test_in(( 'currency', )), ), )) validated_node, errors = conv.struct( node_converters, constructor = collections.OrderedDict, drop_none_values = 'missing', keep_value_order = True, )(validated_node, state = state) conv.remove_ancestor_from_state(state, node) return validated_node, errors
def validate_value_xml_json(value, state = None): if value is None: return None, None container = state.ancestors[-1] value_converter = dict( bool = conv.pipe( conv.test_isinstance(basestring), conv.cleanup_line, conv.test_in([u'0', u'1']), ), float = conv.pipe( conv.test_isinstance(basestring), conv.cleanup_line, conv.test_conv(conv.anything_to_float), ), integer = conv.pipe( conv.test_isinstance(basestring), conv.cleanup_line, conv.test_conv(conv.anything_to_strict_int), ), percent = conv.pipe( conv.test_isinstance(basestring), conv.cleanup_line, conv.test_conv(conv.anything_to_float), ), date = conv.pipe( conv.test_isinstance(basestring), conv.cleanup_line, #TODO: add a new conv ? conv.test_conv(conv.anything_to_strict_int), ), )[container.get('format') or 'float'] # Only CODE have a "format". state = conv.add_ancestor_to_state(state, value) validated_value, errors = conv.pipe( conv.test_isinstance(dict), conv.struct( dict( code = conv.pipe( conv.test_isinstance(basestring), conv.cleanup_line, ), deb = conv.pipe( conv.test_isinstance(basestring), conv.iso8601_input_to_date, conv.date_to_iso8601_str, conv.not_none, ), fin = conv.pipe( conv.test_isinstance(basestring), conv.iso8601_input_to_date, conv.date_to_iso8601_str, conv.not_none, ), format = conv.pipe( conv.test_isinstance(basestring), conv.input_to_slug, conv.test_in(xml_json_formats), conv.test_equals(container.get('format')), ), tail = conv.pipe( conv.test_isinstance(basestring), conv.cleanup_text, ), text = conv.pipe( conv.test_isinstance(basestring), conv.cleanup_text, ), type = conv.pipe( conv.test_isinstance(basestring), conv.input_to_slug, conv.test_in([ 'age', 'days', 'hours', 'monetary', 'months', ]), conv.test_equals(container.get('type')), ), valeur = conv.pipe( value_converter, conv.not_none, ), ), constructor = collections.OrderedDict, drop_none_values = 'missing', keep_value_order = True, ), )(value, state = state) conv.remove_ancestor_from_state(state, value) return validated_value, errors
def validate_value_json(value, state=None): if value is None: return None, None container = state.ancestors[-1] value_converter = dict( boolean=conv.condition( conv.test_isinstance(int), conv.test_in((0, 1)), conv.test_isinstance(bool), ), float=conv.condition( conv.test_isinstance(int), conv.anything_to_float, conv.test_isinstance(float), ), integer=conv.condition( conv.test_isinstance(float), conv.pipe( conv.test(lambda number: round(number) == number), conv.function(int), ), conv.test_isinstance(int), ), rate=conv.condition( conv.test_isinstance(int), conv.anything_to_float, conv.test_isinstance(float), ), )[container.get('format') or 'float'] # Only parameters have a "format". state = conv.add_ancestor_to_state(state, value) validated_value, errors = conv.pipe( conv.test_isinstance(dict), conv.struct( { u'comment': conv.pipe( conv.test_isinstance(basestring), conv.cleanup_text, ), u'from': conv.pipe( conv.test_isinstance(basestring), conv.iso8601_input_to_date, conv.date_to_iso8601_str, conv.not_none, ), u'to': conv.pipe( conv.test_isinstance(basestring), conv.iso8601_input_to_date, conv.date_to_iso8601_str, conv.not_none, ), u'value': conv.pipe( value_converter, conv.not_none, ), }, constructor=collections.OrderedDict, drop_none_values='missing', keep_value_order=True, ), )(value, state=state) conv.remove_ancestor_from_state(state, value) return validated_value, errors
def validate_node_json(node, state=None): if node is None: return None, None state = conv.add_ancestor_to_state(state, node) validated_node, error = conv.test_isinstance(dict)(node, state=state) if error is not None: conv.remove_ancestor_from_state(state, node) return validated_node, error validated_node, errors = conv.struct( { '@context': conv.pipe( conv.test_isinstance(basestring), conv.make_input_to_url(full=True), conv.test_equals( u'http://openfisca.fr/contexts/legislation.jsonld'), ), '@type': conv.pipe( conv.test_isinstance(basestring), conv.cleanup_line, conv.test_in((u'Node', u'Parameter', u'Scale')), conv.not_none, ), 'comment': conv.pipe( conv.test_isinstance(basestring), conv.cleanup_text, ), 'description': conv.pipe( conv.test_isinstance(basestring), conv.cleanup_line, ), }, constructor=collections.OrderedDict, default=conv.noop, drop_none_values='missing', keep_value_order=True, )(validated_node, state=state) if errors is not None: conv.remove_ancestor_from_state(state, node) return validated_node, errors validated_node.pop( '@context', None ) # Remove optional @context everywhere. It will be added to root node later. node_converters = { '@type': conv.noop, 'comment': conv.noop, 'description': conv.noop, } node_type = validated_node['@type'] if node_type == u'Node': node_converters.update( dict(children=conv.pipe( conv.test_isinstance(dict), conv.uniform_mapping( conv.pipe( conv.test_isinstance(basestring), conv.cleanup_line, conv.not_none, ), conv.pipe( validate_node_json, conv.not_none, ), ), conv.empty_to_none, conv.not_none, ), )) elif node_type == u'Parameter': node_converters.update( dict( format=conv.pipe( conv.test_isinstance(basestring), conv.input_to_slug, conv.test_in([ 'boolean', 'float', 'integer', 'rate', ]), ), unit=conv.pipe( conv.test_isinstance(basestring), conv.input_to_slug, conv.test_in(units), ), values=conv.pipe( conv.test_isinstance(list), conv.uniform_sequence( validate_value_json, drop_none_items=True, ), make_validate_values_json_dates( require_consecutive_dates=True), conv.empty_to_none, conv.not_none, ), )) else: assert node_type == u'Scale' node_converters.update( dict( option=conv.pipe( conv.test_isinstance(basestring), conv.input_to_slug, conv.test_in(( 'contrib', 'main-d-oeuvre', 'noncontrib', )), ), slices=conv.pipe( conv.test_isinstance(list), conv.uniform_sequence( validate_slice_json, drop_none_items=True, ), validate_slices_json_dates, conv.empty_to_none, conv.not_none, ), unit=conv.pipe( conv.test_isinstance(basestring), conv.input_to_slug, conv.test_in(('currency', )), ), )) validated_node, errors = conv.struct( node_converters, constructor=collections.OrderedDict, drop_none_values='missing', keep_value_order=True, )(validated_node, state=state) conv.remove_ancestor_from_state(state, node) return validated_node, errors
def validate_node_xml_json(node, state=None): validated_node, errors = conv.pipe( conv.test_isinstance(dict), conv.struct( dict( code=conv.pipe( conv.test_isinstance(basestring_type), conv.cleanup_line, conv.not_none, ), color=conv.pipe( conv.test_isinstance(basestring_type), conv.function(lambda colors: colors.split(',')), conv.uniform_sequence( conv.pipe( conv.input_to_int, conv.test_between(0, 255), conv.not_none, ), ), conv.test( lambda colors: len(colors) == 3, error=N_('Wrong number of colors in triplet.')), conv.function(lambda colors: ','.join( to_unicode(color) for color in colors)), ), desc=conv.pipe( conv.test_isinstance(basestring_type), conv.cleanup_line, conv.not_none, ), NODE=conv.pipe( conv.test_isinstance(list), conv.uniform_sequence( validate_node_xml_json, drop_none_items=True, ), conv.empty_to_none, ), shortname=conv.pipe( conv.test_isinstance(basestring_type), conv.cleanup_line, conv.not_none, ), tail=conv.pipe( conv.test_isinstance(basestring_type), conv.cleanup_text, ), text=conv.pipe( conv.test_isinstance(basestring_type), conv.cleanup_text, ), typevar=conv.pipe( conv.test_isinstance(basestring_type), conv.input_to_int, conv.test_equals(2), ), ), constructor=collections.OrderedDict, drop_none_values='missing', keep_value_order=True, ), )(node, state=state or conv.default_state) if errors is not None: return validated_node, errors if not validated_node.get('NODE'): validated_node, errors = conv.struct( dict(code=conv.test_in(tax_benefit_system.variables), ), default=conv.noop, )(validated_node, state=state) return validated_node, errors