68b69130999fdfa2771326449965d07ae8d4734d
[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
28 def os_encode(s):
29     return s.encode(sys.getfilesystemencoding())
30
31 def os_decode(s):
32     if type(s) == unicode:
33         return s
34     else:
35         return unicode(s,sys.getfilesystemencoding())
36
37 def write(file_path,content):
38     f = create_file(file_path)
39     f.write(os_encode(content))
40     f.close()
41     from log.logging import logger
42     logger.log(u'Wrote %s to %s'%(content,os.path.abspath(file_path)))    
43
44 def list_dir(root,recursive=False,filter=None):
45     encoded_root = os_encode(root)
46     if not os.path.exists(encoded_root):
47         return []
48     all_files_and_dirs = []
49     for name in os.listdir(encoded_root):
50         file_name = os_decode(os.path.join(encoded_root,name))
51         if recursive and os.path.isdir(os_encode(file_name)):
52             all_files_and_dirs.extend(list_dir(file_name, True,filter))
53         if (not filter) or filter(file_name):
54             all_files_and_dirs.append(file_name)
55     return all_files_and_dirs
56
57 def guess_encoding(data):
58     #from logging import logger
59     encodings = ['ascii','utf-8','utf-16']
60     successful_encoding = None
61     if data[:3]=='\xEF\xBB\xBF':
62         data = data[3:]
63
64     for enc in encodings:
65         if not enc:
66             continue
67         try:
68             decoded = unicode(data, enc)
69             successful_encoding = enc
70             break
71         except (UnicodeError, LookupError):
72             pass
73     if successful_encoding is None:
74         raise UnicodeError('Unable to decode input data %s. Tried the'
75             ' following encodings: %s.' %(repr(data), ', '.join([repr(enc)
76                 for enc in encodings if enc])))
77     else:
78         #logger.log('Decoded %s to %s'%(repr(data),repr(decoded)),6)
79         return (decoded, successful_encoding)
80
81
82 def read_text_from_file(unicode_file_name):
83     from log.logging import logger
84     file_name = os_encode(unicode_file_name)
85 #    logger.log(u'Reading from %s'%os.path.abspath(file_name).decode('utf-8'))
86     f=file(file_name,'r')
87     raw=f.read()
88     f.close()
89     (text,encoding)=guess_encoding(raw)
90
91     return text
92 def parse_file_to_line_list(unicode_complete_path):
93     text = read_text_from_file(unicode_complete_path)
94     lines = text.splitlines()
95     return lines
96 def is_dir(unicode_path):
97     return os.path.isdir(os_encode(unicode_path))