def _resolve_yaml_reference(v, p): org_v = v # internal references are of the form ${{path}} where path is a yaml path like root.executors[0].name def repl_fn(matchobj): match_str = matchobj.group(0) match_str_origin = match_str match_str = re.sub( yaml_ref_regex, '{\\1}', match_str ) # from ${{var}} to {var} to leverage python formatter try: # "root" context is now the global namespace # "this" context is now the current node namespace match_str = match_str.format(root=expand_map, this=p, ENV=env_map) except AttributeError as ex: raise AttributeError( 'variable replacement is failed, please check your YAML file.' ) from ex except KeyError: return match_str_origin return match_str v = re.sub(yaml_ref_regex, repl_fn, v) return parse_arg(v)
def __call__(self, parser, args, values, option_string=None): """ call the KVAppendAction .. # noqa: DAR401 :param parser: the parser :param args: args to initialize the values :param values: the values to add to the parser :param option_string: inherited, not used """ import json import re from jina.helper import parse_arg d = getattr(args, self.dest) or {} for value in values: try: d.update(json.loads(value)) except json.JSONDecodeError: try: k, v = re.split(r'[:=]\s*', value, maxsplit=1) except ValueError: raise argparse.ArgumentTypeError( f'could not parse argument \"{values[0]}\" as k=v format' ) d[k] = parse_arg(v) setattr(args, self.dest, d)
def _sub(v): # substitute template with actual value either from context or env variable # v could contain template of the form # # 1) ${{ var }},${{ context.var }},${{ CONTEXT.var }} when need to be parsed with the context dict # or # 2 ) ${{ ENV.var }},${{ env.var }},$var ( deprecated) when need to be parsed with env # # # internally env var (1) and context var (2) are treated differently, both of them are cast to a unique and # normalize template format and then are parsed # 1) context variables placeholder are cast to $$var then we use the ContextVarTemplate to parse the context # variables # 2) env variables placeholder are cast to $var then we leverage the os.path.expandvars to replace by # environment variables. if env_var_deprecated_regex.findall( v) and not env_var_regex.findall( v): # catch expressions of form '$var' warnings.warn( 'Specifying environment variables via the syntax `$var` is deprecated.' 'Use `${{ ENV.var }}` instead.', category=DeprecationWarning, ) if new_env_var_regex.findall( v): # handle expressions of form '${{ ENV.var}}', v = _to_env_var_synatx(v) if context_dot_regex.findall(v): v = _to_normal_context_var(v) if context_var_regex.findall( v): # handle expressions of form '${{ var }}' v = _var_to_substitutable(v) if context: v = ContextVarTemplate(v).safe_substitute( context) # use vars provided in context v = os.path.expandvars( v) # gets env var and parses to python objects if neededd return parse_arg(v)