def function_add_arg(clazz, files, function_name, arg_name, options=None): 'Add a argument to all python functions found.' check.check_string_seq(files) check.check_string(function_name) check.check_string(arg_name) check.check_refactor_options(options, allow_none=True) options = options or refactor_options() items = clazz.grep(files, function_name, refactor_ast_node_type.FUNCTION, options=options) file_map = items.make_file_map() for filename, file_items in file_map.items(): replacements = {} #print(f'filename={filename}') for next_item in file_items: new_definition = next_item.definition_add_arg(arg_name) replacements[next_item.definition] = new_definition #print(f'old definition:\n{next_item.definition}\n---------------------') #print(f'new definition:\n{new_definition}\n---------------------') file_replace.replace( filename, replacements, backup=options.backup, word_boundary=options.word_boundary, word_boundary_chars=options.word_boundary_chars)
def test_file_replace_ascii(self): tmp = self.make_temp_file(content='This is foo fooey foo_kiwi.\n') replacements = { 'This': 'That', 'foo': 'bar', } file_replace.replace(tmp, replacements, backup=False, word_boundary=False) self.assert_text_file_equal('That is bar barey bar_kiwi.\n', tmp)
def test_file_replace_utf8(self): tmp = self.make_temp_file(content='This is bér béry.\n') replacements = { 'This': 'That', 'bér': 'föö', } file_replace.replace(tmp, replacements, backup=False, word_boundary=True) self.assert_text_file_equal('That is föö béry.\n', tmp)
def test_file_replace_with_word_boundary_and_underscore(self): tmp = self.make_temp_file(content='This is foo foo_bar.\n') replacements = { 'This': 'That', 'foo': 'kiwi', } file_replace.replace( tmp, replacements, backup=False, word_boundary=True, word_boundary_chars=word_boundary.CHARS_UNDERSCORE) self.assert_text_file_equal('That is kiwi kiwi_bar.\n', tmp)
def replace_text(clazz, files, src_pattern, dst_pattern, options = None): check.check_string(src_pattern) check.check_string(dst_pattern) check.check_refactor_options(options, allow_none = True) clazz._log.log_method_d() options = options or refactor_options() text_files = refactor_files.resolve_text_files(files) matching_files = refactor_files.match_files(text_files, src_pattern, options = options) replacements = { src_pattern: dst_pattern } for filename in matching_files: file_replace.replace(filename, replacements, backup = False, word_boundary = options.word_boundary, word_boundary_chars = options.word_boundary_chars)