~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/textfile.py

  • Committer: Aaron Bentley
  • Date: 2006-04-15 00:17:38 UTC
  • mto: This revision was merged to the branch mainline in revision 1673.
  • Revision ID: aaron.bentley@utoronto.ca-20060415001738-09feeab6b5ee61d1
Add text_file function

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from itertools import chain 
 
2
 
 
3
from bzrlib.errors import BinaryFile
 
4
from bzrlib.iterablefile import IterableFile
 
5
from bzrlib.osutils import file_iterator
 
6
def text_file(input):
 
7
    """Produce a file iterator that is guaranteed to be text, without seeking.
 
8
    BinaryFile is raised if the file contains a NUL in the first 1024 bytes.
 
9
    """
 
10
    first_chunk = input.read(1024)
 
11
    if '\x00' in first_chunk:
 
12
        raise BinaryFile()
 
13
    return IterableFile(chain((first_chunk,), file_iterator(input)))