def test_parse_template_args_long(self):
     expected = Apply(
         Reference('template'),
         Literal('https://www.commcarehq.org/a/{}/reports/form_data/{}/'),
         Reference('$.domain'),
         Reference('$.id'),
     )
     assert parse_template('form.id', 'template(https://www.commcarehq.org/a/{}/reports/form_data/{}/, $.domain, $.id)') == expected
Exemple #2
0
def _doc_url(url_path):
    from commcare_export.minilinq import Apply, Reference, Literal
    return Apply(
        Reference('template'),
        Literal('{}/a/{}/reports/' + url_path + '/{}/'),
        Reference('commcarehq_base_url'),
        Reference('$.domain'),
        Reference('$.id'),
    )
def compile_map_format_via(value_expr, map_format_expression_string):
    fn_name = map_format_expression_string.split('(')[0]
    parser = MAP_FORMAT_PARSERS.get(fn_name)
    if parser:
        try:
            return parser(value_expr, map_format_expression_string)
        except ParsingException as e:
            return Literal(e.message)

    return Apply(Reference(map_format_expression_string), value_expr)
def parse_template(value_expr, format_expr_string):
    args_string = parse_function_arg(TEMPLATE, format_expr_string)
    args = [arg.strip() for arg in args_string.split(',') if arg.strip()]
    if len(args) < 1:
        return Literal('Error: template function requires the format template: {}'.format(format_expr_string))
    template = args.pop(0)
    if args:
        args = [Reference(arg) for arg in args]
    else:
        args = [value_expr]
    return Apply(Reference(TEMPLATE), Literal(template), *args)
Exemple #5
0
def parse_substr(value_expr, substr_expr_string):
    args_string = parse_function_arg(SUBSTR, substr_expr_string)
    regex = r'^\s*(\d+)\s*,\s*(\d+)\s*$'
    matches = re.match(regex, args_string)
    if not matches or len(matches.groups()) != 2:
        raise ParsingException('Error: both substr arguments must be non-negative integers: {}'.format(substr_expr_string))

    # These conversions should always succeed after a pattern match.
    start = int(matches.groups()[0])
    end = int(matches.groups()[1])

    return Apply(Reference(SUBSTR), value_expr, Literal(start), Literal(end))
Exemple #6
0
 def test_parse_template_args(self):
     expected = Apply(Reference('template'), Literal('my name is {}'),
                      Reference('form.question2'))
     assert parse_template(
         'form.question1',
         'template(my name is {}, form.question2)') == expected
Exemple #7
0
 def mapped_source_field(self):
     if not self.map_function:
         return Reference(self.source)
     else:
         return Apply(Reference(self.map_function), Reference(self.source),
                      *self.extra_args)
def parse_selected(value_expr, selected_expr_string):
    ref_val = parse_function_arg(SELECTED, selected_expr_string)
    return Apply(Reference(SELECTED), value_expr, Literal(ref_val))