How to use _MatchDirective method in autotest

Best Python code snippet using autotest_python

jsontemplate.py

Source:jsontemplate.py Github

copy

Full Screen

...545 ALTERNATES_TOKEN, # {.or}546 OR_TOKEN, # {.or}547 END_TOKEN, # {.end}548 ) = list(range(8))549def _MatchDirective(token):550 """Helper function for matching certain directives."""551 if token.startswith('.'):552 token = token[1:]553 else:554 return None, None # Must start with .555 if token == 'alternates with':556 return ALTERNATES_TOKEN, token557 if token.startswith('or'):558 if token.strip() == 'or':559 return OR_TOKEN, None560 else:561 pred_str = token[2:].strip()562 return OR_TOKEN, pred_str563 if token == 'end':564 return END_TOKEN, None565 match = _SECTION_RE.match(token)566 if match:567 repeated, section_name = match.groups()568 if repeated:569 return REPEATED_SECTION_TOKEN, section_name570 else:571 return SECTION_TOKEN, section_name572 # {.if plural?} and {.plural?} are synonyms. The ".if" will read better for573 # expressions, for people who like that kind of dirty thing...574 if token.startswith('if '):575 return PREDICATE_TOKEN, token[3:].strip()576 if token.endswith('?'):577 return PREDICATE_TOKEN, token578 return None, None # no match579def _Tokenize(template_str, meta_left, meta_right):580 """Yields tokens, which are 2-tuples (TOKEN_TYPE, token_string)."""581 trimlen = len(meta_left)582 token_re = MakeTokenRegex(meta_left, meta_right)583 for line in template_str.splitlines(True): # retain newlines584 tokens = token_re.split(line)585 # Check for a special case first. If a comment or "block" directive is on a586 # line by itself (with only space surrounding it), then the space is587 # omitted. For simplicity, we don't handle the case where we have 2588 # directives, say '{.end} # {#comment}' on a line.589 if len(tokens) == 3:590 # ''.isspace() == False, so work around that591 if (tokens[0].isspace() or not tokens[0]) and \592 (tokens[2].isspace() or not tokens[2]):593 token = tokens[1][trimlen : -trimlen]594 if token.startswith('#'):595 continue # The whole line is omitted596 token_type, token = _MatchDirective(token)597 if token_type is not None:598 yield token_type, token # Only yield the token, not space599 continue600 # The line isn't special; process it normally.601 for i, token in enumerate(tokens):602 if i % 2 == 0:603 yield LITERAL_TOKEN, token604 else: # It's a "directive" in metachracters605 assert token.startswith(meta_left), repr(token)606 assert token.endswith(meta_right), repr(token)607 token = token[trimlen : -trimlen]608 # It's a comment609 if token.startswith('#'):610 continue611 if token.startswith('.'):612 literal = {613 '.meta-left': meta_left,614 '.meta-right': meta_right,615 '.space': ' ',616 '.tab': '\t',617 '.newline': '\n',618 }.get(token)619 if literal is not None:620 yield LITERAL_TOKEN, literal621 continue622 token_type, token = _MatchDirective(token)623 if token_type is not None:624 yield token_type, token625 else: # Now we know the directive is a substitution.626 yield SUBSTITUTION_TOKEN, token627def CompileTemplate(628 template_str, builder=None, meta='{}', format_char='|',629 more_formatters=lambda x: None, more_predicates=lambda x: None,630 default_formatter='str'):631 """Compile the template string, calling methods on the 'program builder'.632 Args:633 template_str: The template string. It should not have any compilation634 options in the header -- those are parsed by FromString/FromFile635 builder: The interface of _ProgramBuilder isn't fixed. Use at your own636 risk....

Full Screen

Full Screen

_jsontemplate.py

Source:_jsontemplate.py Github

copy

Full Screen

...303 REPEATED_SECTION_TOKEN, # {.repeated section name}304 CLAUSE_TOKEN, # {.or}305 END_TOKEN, # {.end}306 ) = range(6)307def _MatchDirective(token):308 """Helper function for matching certain directives."""309 if token in ('.or', '.alternates with'):310 return CLAUSE_TOKEN, token[1:]311 if token == '.end':312 return END_TOKEN, None313 match = _SECTION_RE.match(token[1:])314 if match:315 repeated, section_name = match.groups()316 if repeated:317 return REPEATED_SECTION_TOKEN, section_name318 else:319 return SECTION_TOKEN, section_name320 return None, None # no match321def _Tokenize(template_str, meta_left, meta_right):322 """Yields tokens, which are 2-tuples (TOKEN_TYPE, token_string)."""323 trimlen = len(meta_left)324 token_re = MakeTokenRegex(meta_left, meta_right)325 for line in template_str.splitlines(True): # retain newlines326 tokens = token_re.split(line)327 # Check for a special case first. If a comment or "block" directive is on a328 # line by itself (with only space surrounding it), then the space is329 # omitted. For simplicity, we don't handle the case where we have 2330 # directives, say '{.end} # {#comment}' on a line.331 if len(tokens) == 3:332 # ''.isspace() == False, so work around that333 if (tokens[0].isspace() or not tokens[0]) and \334 (tokens[2].isspace() or not tokens[2]):335 token = tokens[1][trimlen : -trimlen]336 if token.startswith('#'):337 continue # The whole line is omitted338 token_type, token = _MatchDirective(token)339 if token_type is not None:340 yield token_type, token # Only yield the token, not space341 continue342 # The line isn't special; process it normally.343 for i, token in enumerate(tokens):344 if i % 2 == 0:345 yield LITERAL_TOKEN, token346 else: # It's a "directive" in metachracters347 assert token.startswith(meta_left), repr(token)348 assert token.endswith(meta_right), repr(token)349 token = token[trimlen : -trimlen]350 # It's a comment351 if token.startswith('#'):352 continue353 if token.startswith('.'):354 literal = {355 '.meta-left': meta_left,356 '.meta-right': meta_right,357 '.space': ' ',358 '.tab': '\t',359 '.newline': '\n',360 }.get(token)361 if literal is not None:362 yield LITERAL_TOKEN, literal363 continue364 token_type, token = _MatchDirective(token)365 if token_type is not None:366 yield token_type, token367 else: # Now we know the directive is a substitution.368 yield SUBSTITUTION_TOKEN, token369def CompileTemplate(370 template_str, builder=None, meta='{}', format_char='|',371 more_formatters=lambda x: None, default_formatter='str'):372 """Compile the template string, calling methods on the 'program builder'.373 Args:374 template_str: The template string. It should not have any compilation375 options in the header -- those are parsed by FromString/FromFile376 builder: Something with the interface of _ProgramBuilder377 meta: The metacharacters to use378 more_formatters: A function which maps format strings to...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful