def test_check_failure(self):
     """ Test check() method with wrong parameter type. """
     test_file = FileAsObj()
     with self.assertRaises(TypeError):
         test_file.check(1)
     with self.assertRaises(TypeError):
         test_file.check(False)
 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)
示例#3
0
 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))
示例#4
0
# Get all lines that contain a #
result = my_file.grep('#')


#
# Remove all lines that contain the word "foo"
# NOTE: this is dangerous and rarely needed.
my_file.rm(my_file.grep('foo'))


#
# Check for line in file, if exists remove it
if 'foo bar' in my_file:
    my_file.rm('foo bar')
# or
my_file.rm(my_file.check('foo bar'))
# or
my_file.rm('foo bar')  # OK if line not found.


#
# Now that you've changed data let's save it back to disk.
my_file.save()
# or
my_file.write()
# of
if my_file.virgin is False:
    my_file.save()


#
 def test_check_line_not_present(self):
     """ Test check() method with line that is not present in file. """
     test_file = FileAsObj()
     test_file.contents = TESTCONTENTS.split('\n')
     self.assertFalse(test_file.check('This line does not exist in the file.'))
 def test_check_blank_line(self):
     """ Test check() method with empty line that is present in file. """
     test_file = FileAsObj()
     test_file.contents = BLANKFILE.split('\n')
     self.assertEqual(test_file.check(''), '')
 def test_check_present(self):
     """ Test check() method with line that is present in file. """
     test_file = FileAsObj()
     test_file.contents = TESTCONTENTS.split('\n')
     self.assertTrue(test_file.check('# This is a test hosts file'))
示例#8
0
#
# Get all lines that contain a #
result = my_file.grep('#')

#
# Remove all lines that contain the word "foo"
# NOTE: this is dangerous and rarely needed.
my_file.rm(my_file.grep('foo'))

#
# Check for line in file, if exists remove it
if 'foo bar' in my_file:
    my_file.rm('foo bar')
# or
my_file.rm(my_file.check('foo bar'))
# or
my_file.rm('foo bar')  # OK if line not found.

#
# Now that you've changed data let's save it back to disk.
my_file.save()
# or
my_file.write()
# of
if my_file.virgin is False:
    my_file.save()

#
# NOTE: you'll probably never need to worry about this part. If it doesn't make sense, just ignore it.
# As things are checked or changed with FileAsObj an internal log is updated.
示例#9
0
def example_search_for_whole_line_using_check():
    """ Shorter version, find a complete line in file. """
    my_file = FileAsObj('/etc/hosts')
    return my_file.check('127.0.0.1 localhost')