def replace_macros(f, file_name, p_args): """ Replace macro references with its value. Values are extracted from the macro defined in the command line This is the callback function for process_file_list. It will get called once for each file in the input file list. :param f: database file :param file_name: file name (not used) :param p_args: command line arguments :return: None """ if debug_flag: print('\n-- replace_macros', f, file_name, p_args) m = EpicsMacro(p_args.macros, add_undefined=p_args.undefined) for line in f: line = line.rstrip() try: line = m.replace_macros(line, report_undefined=True) except KeyError as ex: print('# -- ' + str(ex)) print(line) return
def test_replace_macros(): m = EpicsMacro([('top', 'cs'), ('dev', 'motor'), ('sys', 'focus')]) assert (m.replace_macros('top=$(top),dev=$(dev) ${sys}') == 'top=cs,dev=motor focus') assert (m.replace_macros('${top}$(top)$(dev)') == 'cscsmotor') try: assert (m.replace_macros('${top}:$(dev):$(other)', report_undefined=True) == 'cs:motor') except KeyError: pass assert (m.replace_macros('${top}:$(dev):$(other)', report_undefined=False) == 'cs:motor:$(other)') m = EpicsMacro([('top', 'cs'), ('dev', 'motor'), ('sys', 'focus')], add_undefined=True) assert (m.replace_macros('$(top,undefined):${dev}:$(sys):${top}') == 'cs:motor:focus:cs') assert (m.replace_macros( '$(top,undefined):${dev,undefined}:${sys,undefined}:${top}') == 'cs:motor:focus:cs')