def replace_string(self, string, splitted=None, ignore_errors=False): """Replaces variables from a string. Result is always a string.""" if self._cannot_have_variables(string): return utils.unescape(string) result = [] if splitted is None: splitted = VariableSplitter(string, self._identifiers) while True: if splitted.identifier is None: result.append(utils.unescape(string)) break result.append(utils.unescape(string[:splitted.start])) try: value = self._get_variable(splitted) except DataError: if not ignore_errors: raise value = string[splitted.start:splitted.end] if not isinstance(value, unicode): value = utils.unic(value) result.append(value) string = string[splitted.end:] splitted = VariableSplitter(string, self._identifiers) result = ''.join(result) return result
def _yield_replaced(self, string, splitter, ignore_errors=False): while splitter.identifier: yield unescape(string[: splitter.start]) try: value = self._get_variable(splitter) except DataError: if not ignore_errors: raise value = string[splitter.start : splitter.end] yield unic(value) string = string[splitter.end :] splitter = VariableSplitter(string) yield unescape(string)
def _yield_replaced(self, string, splitter, ignore_errors=False): while splitter.identifier: yield unescape(string[:splitter.start]) try: value = self._get_variable(splitter) except DataError: if not ignore_errors: raise value = string[splitter.start:splitter.end] yield unic(value) string = string[splitter.end:] splitter = VariableSplitter(string) yield unescape(string)
def UserLibrary(path): """Create a user library instance from given resource file. This is used by Libdoc. """ from robot import utils from .arguments import ArgumentSpec from .userkeyword import UserLibrary as RuntimeUserLibrary resource = ResourceFileBuilder().build(path) ret = RuntimeUserLibrary(resource.keywords, path) for handler in ret.handlers: if handler.type != 'error': handler.doc = utils.unescape(handler._doc) ret.doc = utils.unescape(resource.doc) return ret
def replace_string(self, string, ignore_errors=False): """Replaces variables from a string. Result is always a string.""" if not is_string(string): return unic(string) if self._cannot_have_variables(string): return unescape(string) return self._replace_string(string, ignore_errors=ignore_errors)
def UserLibrary(path): """Create a user library instance from given resource file. This is used by Libdoc. """ from robot import utils from .arguments import ArgumentSpec from .userkeyword import UserLibrary as RuntimeUserLibrary resource = ResourceFileBuilder().build(path) ret = RuntimeUserLibrary(resource.keywords, path) for handler in ret.handlers: if handler.type != "error": handler.doc = utils.unescape(handler._doc) ret.doc = utils.unescape(resource.doc) return ret
def _replace_list(self, items, ignore_errors): for item in items: if self._cannot_have_variables(item): yield unescape(item) else: for value in self._replace_list_item(item, ignore_errors): yield value
def _replace_scalar(self, item, splitter=None): if not splitter: splitter = VariableSplitter(item) if not splitter.identifier: return unescape(item) if splitter.is_variable(): return self._get_variable(splitter) return self._replace_string(item, splitter)
def _replace_list_item(self, item, ignore_errors): match = search_variable(item, ignore_errors=ignore_errors) if not match: return [unescape(match.string)] value = self.replace_scalar(match, ignore_errors) if match.is_list_variable() and is_list_like(value): return value return [value]
def replace_string(self, item, ignore_errors=False): """Replaces variables from a string. Result is always a string. Input can also be an already found VariableMatch. """ match = self._search_variable(item, ignore_errors=ignore_errors) if not match: return unic(unescape(match.string)) return ''.join(self._yield_replaced(match, ignore_errors))
def replace_scalar(self, item, ignore_errors=False): """Replaces variables from a scalar item. If the item is not a string it is returned as is. If it is a ${scalar} variable its value is returned. Otherwise variables are replaced with 'replace_string'. Result may be any object. """ if self._cannot_have_variables(item): return unescape(item) return self._replace_scalar(item, ignore_errors=ignore_errors)
def replace_scalar(self, item, ignore_errors=False): """Replaces variables from a scalar item. If the item is not a string it is returned as is. If it is a variable, its value is returned. Otherwise possible variables are replaced with 'replace_string'. Result may be any object. """ match = self._search_variable(item, ignore_errors=ignore_errors) if not match: return unescape(match.string) return self._replace_scalar(match, ignore_errors)
def _replace_list(self, items): for item in items: if self._cannot_have_variables(item): yield unescape(item) else: splitter = VariableSplitter(item) value = self._replace_scalar(item, splitter) if splitter.is_list_variable(): for v in value: yield v else: yield value
def _get_variable_value(self, match, ignore_errors): if match.identifier not in '$@&%': return self._get_reserved_variable(match) try: name = match.get_variable(self) value = self._variables[name] if match.items: value = self._get_variable_item(name, value, match) except DataError: if not ignore_errors: raise value = unescape(match.match) return value
def _replace_scalar(self, item, splitter=None, ignore_errors=False): if not splitter: splitter = VariableSplitter(item) if not splitter.identifier: return unescape(item) if not splitter.is_variable(): return self._replace_string(item, splitter, ignore_errors) try: return self._get_variable(splitter) except DataError: if ignore_errors: return item raise
def replace_scalar(self, item): """Replaces variables from a scalar item. If the item is not a string it is returned as is. If it is a ${scalar} variable its value is returned. Otherwise variables are replaced with 'replace_string'. Result may be any object. """ if self._cannot_have_variables(item): return utils.unescape(item) var = VariableSplitter(item, self._identifiers) if var.identifier and var.base and var.start == 0 and var.end == len(item): return self._get_variable(var) return self.replace_string(item, var)
def _get_variable_value(self, match, ignore_errors): match.resolve_base(self, ignore_errors) # TODO: Do we anymore need to reserve `*{var}` syntax for anything? if match.identifier == '*': logger.warn(r"Syntax '%s' is reserved for future use. Please " r"escape it like '\%s'." % (match, match)) return unic(match) try: value = self._variables[match] if match.items: value = self._get_variable_item(match, value) except DataError: if not ignore_errors: raise value = unescape(match.match) return value
def UserLibrary(path): """Create a user library instance from given resource file. This is used at least by libdoc.py.""" from robot.parsing import ResourceFile from robot import utils from arguments import UserKeywordArguments from userkeyword import UserLibrary as RuntimeUserLibrary resource = ResourceFile(path).populate() ret = RuntimeUserLibrary(resource.keyword_table.keywords, path) for handler in ret.handlers.values(): # This is done normally only at runtime. handler.arguments = UserKeywordArguments(handler._keyword_args, handler.longname) handler.doc = utils.unescape(handler._doc) ret.doc = resource.setting_table.doc.value return ret
def UserLibrary(path): """Create a user library instance from given resource file. This is used at least by libdoc.py.""" from robot.parsing import ResourceFile from robot import utils from .arguments import UserKeywordArguments from .userkeyword import UserLibrary as RuntimeUserLibrary resource = ResourceFile(path).populate() ret = RuntimeUserLibrary(resource.keyword_table.keywords, path) for handler in ret.handlers.values( ): # This is done normally only at runtime. handler.arguments = UserKeywordArguments(handler._keyword_args, handler.longname) handler.doc = utils.unescape(handler._doc) ret.doc = resource.setting_table.doc.value return ret
def UserLibrary(path): """Create a user library instance from given resource file. This is used by Libdoc. """ from robot import utils from .arguments import ArgumentSpec from .userkeyword import UserLibrary as RuntimeUserLibrary resource = ResourceFileBuilder().build(path) ret = RuntimeUserLibrary(resource.keywords, path) for handler in ret.handlers: if handler.type != 'error': handler.doc = utils.unescape(handler._doc) else: handler.arguments = ArgumentSpec(handler.longname) handler.doc = '*Creating keyword failed: %s*' % handler.error ret.doc = resource.doc return ret
def UserLibrary(path): """Create a user library instance from given resource file. This is used at least by libdoc.py.""" from robot.parsing import ResourceFile from robot import utils from .arguments import UserKeywordArguments from .userkeyword import UserLibrary as RuntimeUserLibrary resource = ResourceFile(path).populate() ret = RuntimeUserLibrary(resource.keyword_table.keywords, path) for handler in ret.handlers.values(): if handler.type != "error": handler.arguments = UserKeywordArguments(handler._keyword_args, handler.longname) handler.doc = utils.unescape(handler._doc) else: handler.arguments = UserKeywordArguments([], handler.longname) handler.doc = "*Creating keyword failed: %s*" % handler.error ret.doc = resource.setting_table.doc.value return ret
def UserLibrary(path): """Create a user library instance from given resource file. This is used by Libdoc. """ from robot.parsing import ResourceFile from robot import utils from .arguments.argumentspec import ArgumentSpec from .userkeyword import UserLibrary as RuntimeUserLibrary resource = ResourceFile(path).populate() ret = RuntimeUserLibrary(resource.keyword_table.keywords, path) for handler in ret.handlers.values(): if handler.type != 'error': handler.doc = utils.unescape(handler._doc) else: handler.arguments = ArgumentSpec(handler.longname) handler.doc = '*Creating keyword failed: %s*' % handler.error ret.doc = resource.setting_table.doc.value return ret
def _get_variable_value(self, match, ignore_errors): match.resolve_base(self, ignore_errors) # TODO: Do we anymore need to reserve `*{var}` syntax for anything? if match.identifier == '*': logger.warn(r"Syntax '%s' is reserved for future use. Please " r"escape it like '\%s'." % (match, match)) return unic(match) try: value = self._finder.find(match) if match.items: value = self._get_variable_item(match, value) try: value = self._validate_value(match, value) except VariableError: raise except: raise VariableError("Resolving variable '%s' failed: %s" % (match, get_error_message())) except DataError: if not ignore_errors: raise value = unescape(match.match) return value
def _get_doc(self, res): doc = res.doc or "Documentation for resource file `%s`." % res.name return utils.unescape(doc)
def _get_doc(self, res): if res.doc: return unescape(res.doc) return "Documentation for resource file ``%s``." % res.name
def _get_doc(self, kw): if self._resource and not isinstance(kw, UserErrorHandler): return unescape(kw.doc) return kw.doc
def _start_suite_or_test_metadata(self, suite_or_test): suite_or_test.doc = utils.unescape(suite_or_test.doc) LogSerializer._start_suite_or_test_metadata(self, suite_or_test)
def _html(self, item): return utils.html_format(utils.unescape(item))
def _get_doc(self, resource): doc = getattr(resource, 'doc', '') # doc available only in 2.1+ if not doc: doc = "Documentation for resource file `%s`." % self.name return utils.unescape(doc)
def _yield_replaced(self, match, ignore_errors=False): while match: yield unescape(match.before) yield unic(self._get_variable_value(match, ignore_errors)) match = search_variable(match.after, ignore_errors=ignore_errors) yield unescape(match.string)
def _html(self, item): return html_format(unescape(item))
class _VariableScopes: def __init__(self, suite, parent_vars): # suite and parent are None only when used by copy_all if suite is not None: suite.variables.update(GLOBAL_VARIABLES) self._suite = self.current = suite.variables self._parents = [] if parent_vars is not None: self._parents.append(parent_vars.current) self._parents.extend(parent_vars._parents) self._test = None self._uk_handlers = [] def copy_all(self): vs = _VariableScopes(None, None) vs._suite = self._suite vs._test = self._test vs._uk_handlers = self._uk_handlers[:] vs._parents = self._parents[:] vs.current = self.current return vs def replace_list(self, items): return self.current.replace_list(items) def replace_scalar(self, items): return self.current.replace_scalar(items) def replace_string(self, string): return self.current.replace_string(string) def replace_from_beginning(self, how_many, args): # There might be @{list} variables and those might have more or less # arguments that is needed. Therefore we need to go through arguments # one by one. processed = [] while len(processed) < how_many and args: processed += self.current.replace_list([args.pop(0)]) # In case @{list} variable is unpacked, the arguments going further # needs to be escaped, otherwise those are unescaped twice. processed[how_many:] = [utils.escape(arg) for arg in processed[how_many:]] return processed + args def set_from_file(self, path, args, overwrite=False): variables = self._suite.set_from_file(path, args, overwrite) if self._test is not None: self._test._set_from_file(variables, overwrite=True, path=path) for varz in self._uk_handlers: varz._set_from_file(variables, overwrite=True, path=path) if self._uk_handlers: self.current._set_from_file(variables, overwrite=True, path=path) def set_from_variable_table(self, rawvariables): self._suite.set_from_variable_table(rawvariables) # TODO: This should be removed so that these objects themselves had # the capability of resolving variables. def replace_meta(self, name, item, errors): error = None for varz in [self.current] + self._parents: try: if name == 'Documentation': return varz.replace_string(item, ignore_errors=True) elif isinstance(item, basestring): return varz.replace_string(item) return varz.replace_list(item) except DataError, error: pass errors.append("Replacing variables from setting '%s' failed: %s" % (name, error)) return utils.unescape(item)