def example_all(): """ Use a bunch of methods on a file. """ my_file = FileAsObj() my_file.filename = '/tmp/example_file.txt' my_file.add('# First change!') my_file.save() my_file = FileAsObj('/tmp/example_file.txt') my_file.unique = True my_file.sorted = True my_file.add('1') my_file.add('1') my_file.add('2') my_file.add('20 foo') my_file.add('200 bar') my_file.add('# Comment') my_file.unique = False my_file.add('# Comment') my_file.add('# Comment') my_file.unique = True my_file.rm(my_file.egrep('^#.*')) my_file.rm(my_file.grep('foo')) my_file.replace(my_file.egrep('^2'), 'This line was replaced.') print(my_file) print(my_file.log)
def test_replace_list(self): test_file = FileAsObj(TestFile, verbose=True) old = ['#', '# ', '#1'] new = '###' self.assertTrue(test_file.replace(old, new)) for this in old: self.assertFalse(test_file.check(this))
def test_replace_whole_line(self): """ Test substitute a line. """ test_file = FileAsObj() test_file.add(TESTCONTENTS) old = '172.19.18.17 freebird.example.com' new = '172.19.18.17 freebird.example.com # Added 1976.10.29 -jh' self.assertTrue(test_file.replace(old, new))
def test_replace_regex(self): """ Test substitute lines using a valid regex. """ test_file = FileAsObj() test_file.add(TESTCONTENTS) old = test_file.egrep('^[ ]+#.*') new = '###' self.assertTrue(test_file.replace(old, new)) self.assertFalse(test_file.egrep('^[ ]+#.*'))
def test_replace_list(self): """ Test substitute lines using a list of strings. """ test_file = FileAsObj() test_file.contents = TESTCONTENTS.split('\n') self.assertFalse(test_file.changed) old = ['#', '# ', '#1'] new = '###' self.assertTrue(test_file.replace(old, new)) self.assertTrue(test_file.changed) for this in old: self.assertFalse(test_file.check(this)) self.assertEqual(test_file.check(new), new)
def test_replace_failure(self): """ Test wrong param type in replace() """ test_file = FileAsObj() with self.assertRaises(TypeError): test_file.replace(1, '') with self.assertRaises(TypeError): test_file.replace(True, '') with self.assertRaises(TypeError): test_file.replace('', True)
def test_replace_regex(self): test_file = FileAsObj(TestFile, verbose=True) old = test_file.egrep('^[ ]+#.*') new = '###' self.assertTrue(test_file.replace(old, new)) self.assertFalse(test_file.egrep('^[ ]+#.*'))
def test_replace_whole_line(self): test_file = FileAsObj(TestFile, verbose=True) old = '172.19.18.17 freebird.example.com' new = '172.19.18.17 freebird.example.com # Added 1976.10.29 -jh' self.assertTrue(test_file.replace(old, new))
) print('---') x = '#' print('Remove whole line "{0}" from {1}, RESULT={2}'.format( x, test_file.grep(x), test_file.rm(x)) ) print(test_file.grep(x)) print('---') old = '172.19.18.17 freebird.example.com' new = '172.19.18.17 freebird.example.com # Added 1976.10.29 -jh' print('Replace {0} with {1}'.format(old, new)) print(test_file.replace(old, new)) print('---') x = '^[ ]+#.*' print('Remove {0} from {1}, RESULT={2}'.format( x, test_file.egrep(x), test_file.rm(test_file.egrep(x))) ) print('---') x = '#FOO' print('Add line {0}'.format(x)) print(test_file.add(x))
x = ' h0st.*' print('Remove {0} from {1}, RESULT={2}'.format( x, test_file.egrep(x), test_file.rm(test_file.egrep(x)))) print('---') x = '#' print('Remove whole line "{0}" from {1}, RESULT={2}'.format( x, test_file.grep(x), test_file.rm(x))) print(test_file.grep(x)) print('---') old = '172.19.18.17 freebird.example.com' new = '172.19.18.17 freebird.example.com # Added 1976.10.29 -jh' print('Replace {0} with {1}'.format(old, new)) print(test_file.replace(old, new)) print('---') x = '^[ ]+#.*' print('Remove {0} from {1}, RESULT={2}'.format( x, test_file.egrep(x), test_file.rm(test_file.egrep(x)))) print('---') x = '#FOO' print('Add line {0}'.format(x)) print(test_file.add(x)) x = '#FOO' print('Add line {0}'.format(x)) print(test_file.add(x))