4 def create_dir_if_necessary(path):
5 if len(path) > 0 and not os.path.exists(path):
10 # path = os_encode(path_unicode)
12 # #logger.log('Error decoding path %s'%repr(path_unicode))
13 # print 'Error decoding path %s'%repr(path_unicode)
15 create_dir_if_necessary(path)
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))
24 f = file(file_name,'w')
29 return s.encode(sys.getfilesystemencoding())
32 if type(s) == unicode:
35 return unicode(s,sys.getfilesystemencoding())
37 def write(file_path,content):
38 f = create_file(file_path)
39 f.write(os_encode(content))
41 from log.logging import logger
42 logger.log(u'Wrote %s to %s'%(content,os.path.abspath(file_path)))
44 def list_dir(root,recursive=False,filter=None):
45 encoded_root = os_encode(root)
46 if not os.path.exists(encoded_root):
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
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':
68 decoded = unicode(data, enc)
69 successful_encoding = enc
71 except (UnicodeError, LookupError):
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])))
78 #logger.log('Decoded %s to %s'%(repr(data),repr(decoded)),6)
79 return (decoded, successful_encoding)
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'))
89 (text,encoding)=guess_encoding(raw)
92 def parse_file_to_line_list(unicode_complete_path):
93 text = read_text_from_file(unicode_complete_path)
94 lines = text.splitlines()
96 def is_dir(unicode_path):
97 return os.path.isdir(os_encode(unicode_path))