Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
 def test_addition_string_unique(self):
     """ Test content integrity using `unique` """
     test_file = FileAsObj()
     test_file.unique = True
     subject = 'uniq'
     self.assertTrue(test_file + subject)
     self.assertFalse(test_file + subject)
     self.assertTrue(test_file.contents == [subject])
Ejemplo n.º 3
0
 def test_add_string_unique(self):
     """ Test content integrity with unique. """
     test_file = FileAsObj()
     test_file.unique = True
     subject = 'uniq'
     self.assertTrue(test_file.add(subject))
     self.assertFalse(test_file.add(subject))
     self.assertTrue(test_file.contents == [subject])
Ejemplo n.º 4
0
 def test_unique_failure_during_read(self):
     """ Test wrong attribute type of self.unique during read() """
     test_file = FileAsObj()
     test_file.filename = TESTFILE
     test_file.contents = BLANKFILE.split('\n')
     self.assertTrue(test_file.save())
     test_file = FileAsObj()
     test_file.unique = 'this is invalid attr type'
     with self.assertRaises(AttributeError):
         test_file.read(TESTFILE)
Ejemplo n.º 5
0
 def test_blank_file_without_unique(self):
     """ Testing not-unique on empty lines during .read() """
     test_file = FileAsObj()
     test_file.filename = TESTFILE
     test_file.contents = BLANKFILE.split('\n')
     self.assertTrue(test_file.save())
     test_file = FileAsObj()
     test_file.unique = False
     test_file.read(TESTFILE)
     self.assertTrue(test_file.contents == ['', '', '', ''])
Ejemplo n.º 6
0
def example_add_line_to_file():
    """ Different methods to append a given line to the file, all work the same. """
    my_file = FileAsObj('/tmp/example_file.txt')
    my_file.add('foo')
    my_file.append('bar')
    # Add a new line to my_file that contains the word 'lol' and print True|False if my_file was changed.
    print(my_file + 'lol')
    # Add line even if it already exists in the file.
    my_file.unique = False
    my_file.add('foo')
Ejemplo n.º 7
0
 def test_addition_list_unique(self):
     """ Test adding a 3 element list with unique. """
     test_file = FileAsObj()
     test_file.unique = True
     subject = ['simultaneous', 'money shot', 'remedy']
     self.assertFalse(test_file.changed)
     self.assertTrue(test_file + subject)
     self.assertTrue(test_file.changed)
     self.assertFalse(test_file + subject)
     self.assertTrue(test_file.contents == subject)
     self.assertTrue(str(test_file) == '\n'.join(subject))
Ejemplo n.º 8
0
 def test_addition_unique_failure(self):
     """ Test wrong attribute type of self.unique. """
     test_file = FileAsObj()
     test_file.unique = 'this is invalid attr type'
     with self.assertRaises(AttributeError):
         test_file + '.'
Ejemplo n.º 9
0
 def test_unique_failure(self):
     """ Test wrong attribute type of self.unique during append() """
     test_file = FileAsObj()
     test_file.unique = 'this is invalid attr type'
     with self.assertRaises(AttributeError):
         test_file.append('.')
Ejemplo n.º 10
0
 def test_unique_failure(self):
     """ Test unique wrong attribute type. """
     test_file = FileAsObj()
     test_file.unique = 'this is invalid attr type'
     with self.assertRaises(AttributeError):
         test_file.add('.')