How to use LookupWithType method in autotest

Best Python code snippet using autotest_python

jsontemplate.py

Source:jsontemplate.py Github

copy

Full Screen

...106 args: Extra arguments to be passed to the function at expansion time107 Should be None to pass NO arguments, since it can pass a 0-tuple too.108 """109 raise NotImplementedError110 def LookupWithType(self, user_str):111 func, args = self.Lookup(user_str)112 # If users need the complexity of FunctionRegistry, then they get the113 # 3-arguments formatter signature (value, context, args)114 return func, args, ENHANCED_FUNC115def _DecideFuncType(user_str):116 """117 By default, formatters/predicates which start with a non-lowercase letter take118 contexts rather than just the cursor.119 """120 if user_str[0].islower():121 return SIMPLE_FUNC122 else:123 return ENHANCED_FUNC124class DictRegistry(FunctionRegistry):125 """Look up functions in a simple dictionary."""126 def __init__(self, func_dict):127 self.func_dict = func_dict128 def LookupWithType(self, user_str):129 return self.func_dict.get(user_str), None, _DecideFuncType(user_str)130class CallableRegistry(FunctionRegistry):131 """Look up functions in a (higher-order) function."""132 def __init__(self, func):133 self.func = func134 def LookupWithType(self, user_str):135 return self.func(user_str), None, _DecideFuncType(user_str)136class PrefixRegistry(FunctionRegistry):137 """Lookup functions with arguments.138 139 The function name is identified by a prefix. The character after the prefix,140 usually a space, is considered the argument delimiter (similar to sed/perl's141 s/foo/bar s|foo|bar syntax).142 """143 def __init__(self, functions):144 """145 Args:146 functions: List of 2-tuples (prefix, function), e.g.147 [('pluralize', _Pluralize), ('cycle', _Cycle)]148 """149 self.functions = functions150 def Lookup(self, user_str):151 for prefix, func in self.functions:152 if user_str.startswith(prefix):153 i = len(prefix)154 # Usually a space, but could be something else155 try:156 splitchar = user_str[i]157 except IndexError:158 args = () # No arguments159 else:160 args = user_str.split(splitchar)[1:]161 return func, args162 return None, ()163class _TemplateRef(object):164 """A reference from one template to another.165 166 The _TemplateRef sits statically in the program tree as one of the formatters.167 At runtime, _DoSubstitute calls Resolve() with the group being used.168 """169 def __init__(self, name=None, template=None):170 self.name = name171 self.template = template # a template that's already been resolved172 def Resolve(self, context):173 if self.template:174 return self.template175 if context.group:176 return context.group.get(self.name)177 else:178 raise EvaluationError(179 "Couldn't find template with name %r (create a template group?)"180 % self.name)181class _TemplateRegistry(FunctionRegistry):182 """Each template owns a _TemplateRegistry.183 LookupWithType always returns a TemplateRef to the template compiler184 (_ProgramBuilder). At runtime, which may be after MakeTemplateGroup is185 called, the names can be resolved.186 """187 def __init__(self, owner):188 """189 Args:190 owner: The Template instance that owns this formatter. (There should be191 exactly one)192 """193 self.owner = owner194 def LookupWithType(self, user_str):195 """196 Returns:197 ref: Either a template instance (itself) or _TemplateRef198 """199 prefix = 'template '200 ref = None # fail the lookup by default201 if user_str.startswith(prefix):202 name = user_str[len(prefix):]203 if name == 'SELF':204 # we can resolve this right away205 ref = _TemplateRef(template=self.owner) # special value206 else:207 ref = _TemplateRef(name)208 return ref, (), TEMPLATE_FORMATTER209class ChainedRegistry(FunctionRegistry):210 """Look up functions in chain of other FunctionRegistry instances."""211 def __init__(self, registries):212 self.registries = registries213 def LookupWithType(self, user_str):214 for registry in self.registries:215 func, args, func_type = registry.LookupWithType(user_str)216 if func:217 return func, args, func_type218 # Nothing found219 return None, None, SIMPLE_FUNC220class _ProgramBuilder(object):221 """222 Receives method calls from the parser, and constructs a tree of _Section()223 instances.224 """225 def __init__(self, formatters, predicates, template_registry):226 """227 Args:228 formatters: See docstring for _CompileTemplate229 predicates: See docstring for _CompileTemplate230 """231 self.current_section = _Section(None)232 self.stack = [self.current_section]233 # Passing a dictionary or a function is often more convenient than making a234 # FunctionRegistry235 if isinstance(formatters, dict):236 formatters = DictRegistry(formatters)237 elif callable(formatters):238 formatters = CallableRegistry(formatters)239 # default formatters with arguments240 default_formatters = PrefixRegistry([241 ('pluralize', _Pluralize),242 ('cycle', _Cycle),243 # These have to go first because 'strftime' is a prefix of244 # strftime-local/gm!245 ('strftime-local', _StrftimeLocal), # local246 ('strftime-gm', _StrftimeGm), # world247 ('strftime', _StrftimeLocal), # local by default248 ])249 # First consult user formatters, then templates enabled by250 # MakeTemplateGroup, then the default formatters251 self.formatters = ChainedRegistry([252 formatters,253 template_registry, # returns _TemplateRef instances254 DictRegistry(_DEFAULT_FORMATTERS),255 default_formatters])256 # Same for predicates257 if isinstance(predicates, dict):258 predicates = DictRegistry(predicates)259 elif callable(predicates):260 predicates = CallableRegistry(predicates)261 # default predicates with arguments262 default_predicates = PrefixRegistry([263 ('test', _TestAttribute),264 ('template', _TemplateExists),265 ])266 self.predicates = ChainedRegistry(267 [predicates, DictRegistry(_DEFAULT_PREDICATES), default_predicates])268 def Append(self, statement):269 """270 Args:271 statement: Append a literal272 """273 self.current_section.Append(statement)274 def _GetFormatter(self, format_str):275 """276 The user's formatters are consulted first, then the default formatters.277 """278 formatter, args, func_type = self.formatters.LookupWithType(format_str)279 if formatter:280 return formatter, args, func_type281 else:282 raise BadFormatter('%r is not a valid formatter' % format_str)283 def _GetPredicate(self, pred_str, test_attr=False):284 """285 The user's predicates are consulted first, then the default predicates.286 """287 predicate, args, func_type = self.predicates.LookupWithType(pred_str)288 if predicate:289 pred = predicate, args, func_type290 else:291 # Nicer syntax, {.debug?} is shorthand for {.if test debug}.292 # Currently there is not if/elif chain; just use293 # {.if test debug} {.or test release} {.or} {.end}294 if test_attr:295 assert pred_str.endswith('?')296 # func, args, func_type297 pred = (_TestAttribute, (pred_str[:-1],), ENHANCED_FUNC)298 else:299 raise BadPredicate('%r is not a valid predicate' % pred_str)300 return pred301 def AppendSubstitution(self, name, formatters):...

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