decorators: Move interruptable() to cola.decorators
[git-cola.git] / cola / core.py
blob6baa1edd63c327dcdf1c68c33ec95f71d0f0f3b7
1 """This module provides core functions for handling unicode and UNIX quirks
3 The @interruptable functions retry when system calls are interrupted,
4 e.g. when python raises an IOError or OSError with errno == EINTR.
6 """
7 from cola.decorators import interruptable
9 # Some files are not in UTF-8; some other aren't in any codification.
10 # Remember that GIT doesn't care about encodings (saves binary data)
11 _encoding_tests = [
12 'utf-8',
13 'iso-8859-15',
14 'windows1252',
15 'ascii',
16 # <-- add encodings here
19 def decode(enc):
20 """decode(encoded_string) returns an unencoded unicode string
21 """
22 for encoding in _encoding_tests:
23 try:
24 return unicode(enc.decode(encoding))
25 except:
26 pass
27 # this shouldn't ever happen... FIXME
28 return unicode(enc)
31 def encode(unenc):
32 """encode(unencoded_string) returns a string encoded in utf-8
33 """
34 return unenc.encode('utf-8', 'replace')
37 @interruptable
38 def read(fh):
39 """Read from a filehandle and retry when interrupted"""
40 return fh.read()
43 @interruptable
44 def write(fh, content):
45 """Write to a filehandle and retry when interrupted"""
46 return fh.write(content)
49 @interruptable
50 def wait(proc):
51 """Wait on a subprocess and retry when interrupted"""
52 return proc.wait()
55 @interruptable
56 def readline(fh):
57 return fh.readline()