doc: Update copyright
[git-cola.git] / cola / core.py
blob241e60b996ca6a6387547bd6d1c5c75fee31de5b
1 """This module provides core functions for handling unicode and UNIX quirks
3 OSX and others are known to interrupt system calls
5 http://en.wikipedia.org/wiki/PCLSRing
6 http://en.wikipedia.org/wiki/Unix_philosophy#Worse_is_better
8 The {read,write,wait}_nointr functions handle this situation
9 """
10 import errno
12 # Some files are not in UTF-8; some other aren't in any codification.
13 # Remember that GIT doesn't care about encodings (saves binary data)
14 _encoding_tests = [
15 'utf-8',
16 'iso-8859-15',
17 'windows1252',
18 'ascii',
19 # <-- add encodings here
22 def decode(enc):
23 """decode(encoded_string) returns an unencoded unicode string
24 """
25 for encoding in _encoding_tests:
26 try:
27 return unicode(enc.decode(encoding))
28 except:
29 pass
30 # this shouldn't ever happen... FIXME
31 return unicode(enc)
33 def encode(unenc):
34 """encode(unencoded_string) returns a string encoded in utf-8
35 """
36 return unenc.encode('utf-8', 'replace')
38 def read_nointr(fh):
39 """Read from a filehandle and retry when interrupted"""
40 while True:
41 try:
42 content = fh.read()
43 break
44 except IOError, e:
45 if e.errno == errno.EINTR:
46 continue
47 raise e
48 except OSError, e:
49 if e.errno == errno.EINTR:
50 continue
51 raise e
52 return content
54 def write_nointr(fh, content):
55 """Write to a filehandle and retry when interrupted"""
56 while True:
57 try:
58 content = fh.write(content)
59 break
60 except IOError, e:
61 if e.errno == errno.EINTR:
62 continue
63 raise e
64 except OSError, e:
65 if e.errno == errno.EINTR:
66 continue
67 raise e
68 return content
70 def wait_nointr(proc):
71 """Wait on a subprocess and retry when interrupted"""
72 while True:
73 try:
74 status = proc.wait()
75 break
76 except IOError, e:
77 if e.errno == errno.EINTR:
78 continue
79 raise e
80 except OSError, e:
81 if e.errno == errno.EINTR:
82 continue
83 raise e
84 return status