Add getfilesystemencoding() for python < 2.3.
[matthijs/upstream/mobilegtd.git] / src / inout / io.py
1 import os,sys
2
3
4 def create_dir_if_necessary(path):
5     if len(path) > 0 and not os.path.exists(path):
6         os.makedirs(path)
7
8 def safe_chdir(path):
9     #try:
10     #    path = os_encode(path_unicode)
11     #except UnicodeError:
12     #    #logger.log('Error decoding path %s'%repr(path_unicode))
13     #    print 'Error decoding path %s'%repr(path_unicode)
14     #    path = path_unicode
15     create_dir_if_necessary(path)
16     os.chdir(path)
17
18 def create_file(file_path):
19     dir = os.path.dirname(os_encode(file_path))
20     create_dir_if_necessary(dir)
21     encoded_file_path = os_encode(file_path)
22     file_name = os.path.join(dir,os.path.basename(encoded_file_path))
23
24     f = file(file_name,'w')
25     return f
26
27 def getfilesystemencoding():
28     try:
29         # sys.getfilesystemencoding() was introduced in Python v2.3
30         return sys.getfilesystemencoding()
31     except AttributeError:
32         return 'ascii'
33
34 def os_encode(s):
35     # Encoding can be None, meaning the default system encoding should
36     # be used.
37     enc = getfilesystemencoding()
38     if enc:
39         return s.encode(enc)
40     else:
41         return s
42
43 def os_decode(s):
44     # Encoding can be None, meaning the default system encoding should
45     # be used.
46     enc = getfilesystemencoding()
47     if type(s) == unicode or not enc:
48         return s
49     else:
50         return unicode(s,enc)
51
52 def write(file_path,content):
53     f = create_file(file_path)
54     f.write(os_encode(content))
55     f.close()
56     from log.logging import logger
57     logger.log(u'Wrote %s to %s'%(content,os.path.abspath(file_path)))    
58
59 def list_dir(root,recursive=False,filter=None):
60     encoded_root = os_encode(root)
61     if not os.path.exists(encoded_root):
62         return []
63     all_files_and_dirs = []
64     for name in os.listdir(encoded_root):
65         file_name = os_decode(os.path.join(encoded_root,name))
66         if recursive and os.path.isdir(os_encode(file_name)):
67             all_files_and_dirs.extend(list_dir(file_name, True,filter))
68         if (not filter) or filter(file_name):
69             all_files_and_dirs.append(file_name)
70     return all_files_and_dirs
71
72 def guess_encoding(data):
73     #from logging import logger
74     encodings = ['ascii','utf-8','utf-16']
75     successful_encoding = None
76     if data[:3]=='\xEF\xBB\xBF':
77         data = data[3:]
78
79     for enc in encodings:
80         if not enc:
81             continue
82         try:
83             decoded = unicode(data, enc)
84             successful_encoding = enc
85             break
86         except (UnicodeError, LookupError):
87             pass
88     if successful_encoding is None:
89         raise UnicodeError('Unable to decode input data %s. Tried the'
90             ' following encodings: %s.' %(repr(data), ', '.join([repr(enc)
91                 for enc in encodings if enc])))
92     else:
93         #logger.log('Decoded %s to %s'%(repr(data),repr(decoded)),6)
94         return (decoded, successful_encoding)
95
96
97 def read_text_from_file(unicode_file_name):
98     from log.logging import logger
99     file_name = os_encode(unicode_file_name)
100 #    logger.log(u'Reading from %s'%os.path.abspath(file_name).decode('utf-8'))
101     f=file(file_name,'r')
102     raw=f.read()
103     f.close()
104     (text,encoding)=guess_encoding(raw)
105
106     return text
107 def parse_file_to_line_list(unicode_complete_path):
108     text = read_text_from_file(unicode_complete_path)
109     lines = text.splitlines()
110     return lines
111 def is_dir(unicode_path):
112     return os.path.isdir(os_encode(unicode_path))