/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/textfile.py

  • Committer: Aaron Bentley
  • Date: 2006-04-16 17:26:33 UTC
  • mto: This revision was merged to the branch mainline in revision 1673.
  • Revision ID: aaron.bentley@utoronto.ca-20060416172633-4841d41fc9724d63
Added more tests

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)))
 
14
 
 
15
 
 
16
def check_text_lines(lines):
 
17
    """Raise BinaryFile if the supplied lines contain NULs.
 
18
    Only the first 1024 characters are checked.
 
19
    """
 
20
    f = IterableFile(lines)
 
21
    if '\x00' in f.read(1024):
 
22
        raise BinaryFile()
 
23
 
 
24
def check_text_path(path):
 
25
    """Check whether the supplied path is a text, not binary file.
 
26
    Raise BinaryFile if a NUL occurs in the first 1024 bytes.
 
27
    """
 
28
    text_file(open(path, 'rb'))