def parse_selected_at(value_expr, selected_at_expr_string): index = parse_function_arg(SELECTED_AT, selected_at_expr_string) try: index = int(index) except ValueError: return Literal('Error: selected-at index must be an integer: {}'.format(selected_at_expr_string)) return Apply(Reference(SELECTED_AT), value_expr, Literal(index))
def attachment_url(val): if not val: return None from commcare_export.minilinq import Apply, Reference, Literal return Apply(Reference('template'), Literal('{}/a/{}/api/form/attachment/{}/{}'), Reference('commcarehq_base_url'), Reference('$.domain'), Reference('$.id'), Literal(val))
def get_locations_query(lp): location_types = lp.location_types # For test stability and clarity, we order location types from deepest # to shallowest. depth = {} def set_depth(lt): if lt not in depth: parent = location_types[lt]['parent'] if parent is not None: set_depth(parent) depth[lt] = depth[parent] + 1 else: depth[lt] = 0 for lt in location_types: set_depth(lt) ordered_location_types = sorted(location_types.values(), key=lambda lt: -depth[lt['resource_uri']]) location_codes = [lt['code'] for lt in ordered_location_types] # The input names are codes produced by Django's slugify utility # method. Replace hyphens with underscores to be easier to use in SQL. def sql_column_name(code): return re.sub('-', '_', code) location_columns = [ Column('id', 'id'), Column('created_at', 'created_at', 'str2date'), Column('domain', 'domain'), Column('external_id', 'external_id'), Column('last_modified', 'last_modified', 'str2date'), Column('latitude', 'latitude'), Column('location_data', 'location_data', 'json2str'), Column('location_id', 'location_id'), Column('location_type', 'location_type'), Column('longitude', 'longitude'), Column('name', 'name'), Column('parent', 'parent'), Column('resource_uri', 'resource_uri'), Column('site_code', 'site_code'), Column('location_type_administrative', 'location_type', 'get_location_info', Literal('administrative')), Column('location_type_code', 'location_type', 'get_location_info', Literal('code')), Column('location_type_name', 'location_type', 'get_location_info', Literal('name')), Column('location_type_parent', 'location_type', 'get_location_info', Literal('parent')), ] + [ Column(sql_column_name(code), 'resource_uri', 'get_location_ancestor', Literal(code)) for code in location_codes ] return compile_query(location_columns, 'location', LOCATIONS_TABLE_NAME)
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)
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))
def compile_query(columns, data_source, table_name): source = Apply(Reference('api_data'), Literal(data_source), Reference('checkpoint_manager')) part = excel_query.SheetParts( table_name, [c.name for c in columns], source, List([c.mapped_source_field for c in columns]), None) return excel_query.compile_queries([part], None, False)[0]
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
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 test_parse_template_no_template(self): expected = Literal( 'Error: template function requires the format template: template()' ) assert parse_template('form.question1', 'template()') == expected
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
def __init__(self, name, source, map_function=None, *extra_args): self.name = Literal(name) self.source = source self.map_function = map_function self.extra_args = 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))