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_constructor(): try: EpicsMacro() except TypeError: pass try: EpicsMacro(None) except TypeError: pass try: EpicsMacro(['a', 'b', 'c']) except IndexError: pass EpicsMacro([('a', 'b'), ('c', 'd')])
def read_subs_file(database_name): """ Read the macro substitution file for a give database. It returns an EpicsMacro object with all the definitions in the file. :param database_name: database file name :type database_name: str :return: EpicsMacro object with macros definitions in the subs file :rtype: EpicsMacro """ subs_filename = splitext(database_name)[0] + '.subs' # print(splitext(database_name)[0]) # print subs_filename if exists(subs_filename): macro_list = [] f = open(subs_filename, 'r') for line in f: line = line.strip().split() # print line macro_list.append(line) # print macro_list m = EpicsMacro(macro_list) # print m return m else: return None
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')
def list_macros(f, file_name, p_args): """ List all macros found in a database, one per line. :param f: database file :param file_name: file name (not used) :param p_args: command line arguments (not used) :return: None """ if debug_flag: print('\n-- list_macros', f, file_name, p_args) m = EpicsMacro(p_args.macros) # Look for macros in all lines output_set = set() for line in f: output_set |= m.get_macros(line.rstrip()) # Print the output set for macro_name in output_set: print(macro_name) return
def test_get_macros(): m = EpicsMacro([('top', 'cs'), ('dev', 'motor'), ('sys', 'focus')]) assert (m.get_macros('$(top) ${top}')) == {'top'} assert (m.get_macros('$(top) ${dev}')) == {'top', 'dev'} assert (m.get_macros('$(top) ${dev} $(sys)')) == {'top', 'dev', 'sys'}
def test_cleaned_macro(): assert (EpicsMacro._cleaned_macro('${top}') == 'top') assert (EpicsMacro._cleaned_macro('$(top)') == 'top')
nargs=2, dest='macros', default=[], help='macros to substitute') parser.add_argument('--debug', action='store_true', dest='debug', default=False, help=SUPPRESS) return parser.parse_args(argv[1:]) if __name__ == '__main__': try: # args = get_args(['dbdiff', 'test_data/diff1.db', 'test_data/diff2.db', '--debug']) # args = get_args(['dbdiff', 'test_data/diff1.db', 'test_data/diff2.db']) # args = get_args(['dbdiff', '-c', '-m', 'top', 'cr:', 'test_data/crtop_legacy.db', 'test_data/crtop_rtems.db']) args = get_args(sys.argv) debug_flag = args.debug if args.macros: diff_macros = EpicsMacro(args.macros, add_undefined=True) if debug_flag: print(args) diff_files(args.input_file[0], args.input_file[1], args) except Exception as e: print(e) sys.exit(1)