MCPcopy Index your code
hub / github.com/httprunner/httprunner / parse_string

Function parse_string

httprunner/parser.py:294–398  ·  view source on GitHub ↗

parse string content with variables and functions mapping. Args: raw_string: raw string content to be parsed. variables_mapping: variables mapping. functions_mapping: functions mapping. Returns: str: parsed string content. Examples: >>> raw_stri

(
    raw_string: Text,
    variables_mapping: VariablesMapping,
    functions_mapping: FunctionsMapping,
)

Source from the content-addressed store, hash-verified

292
293
294def parse_string(
295 raw_string: Text,
296 variables_mapping: VariablesMapping,
297 functions_mapping: FunctionsMapping,
298) -> Any:
299 """parse string content with variables and functions mapping.
300
301 Args:
302 raw_string: raw string content to be parsed.
303 variables_mapping: variables mapping.
304 functions_mapping: functions mapping.
305
306 Returns:
307 str: parsed string content.
308
309 Examples:
310 >>> raw_string = "abc${add_one($num)}def"
311 >>> variables_mapping = {"num": 3}
312 >>> functions_mapping = {"add_one": lambda x: x + 1}
313 >>> parse_string(raw_string, variables_mapping, functions_mapping)
314 "abc4def"
315
316 """
317 try:
318 match_start_position = raw_string.index("$", 0)
319 parsed_string = raw_string[0:match_start_position]
320 except ValueError:
321 parsed_string = raw_string
322 return parsed_string
323
324 while match_start_position < len(raw_string):
325
326 # Notice: notation priority
327 # $$ > ${func($a, $b)} > $var
328
329 # search $$
330 dollar_match = dolloar_regex_compile.match(raw_string, match_start_position)
331 if dollar_match:
332 match_start_position = dollar_match.end()
333 parsed_string += "$"
334 continue
335
336 # search function like ${func($a, $b)}
337 func_match = function_regex_compile.match(raw_string, match_start_position)
338 if func_match:
339 func_name = func_match.group(1)
340 func = get_mapping_function(func_name, functions_mapping)
341
342 func_params_str = func_match.group(2)
343 function_meta = parse_function_params(func_params_str)
344 args = function_meta["args"]
345 kwargs = function_meta["kwargs"]
346 parsed_args = parse_data(args, variables_mapping, functions_mapping)
347 parsed_kwargs = parse_data(kwargs, variables_mapping, functions_mapping)
348
349 try:
350 func_eval_value = func(*parsed_args, **parsed_kwargs)
351 except Exception as ex:

Callers 2

parse_dataFunction · 0.85
parse_stringMethod · 0.85

Calls 5

get_mapping_functionFunction · 0.85
parse_function_paramsFunction · 0.85
parse_dataFunction · 0.85
funcFunction · 0.85
get_mapping_variableFunction · 0.85

Tested by

no test coverage detected