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')
27 def getfilesystemencoding():
29 # sys.getfilesystemencoding() was introduced in Python v2.3
30 return sys.getfilesystemencoding()
31 except AttributeError:
35 # Encoding can be None, meaning the default system encoding should
37 enc = getfilesystemencoding()
44 # Encoding can be None, meaning the default system encoding should
46 enc = getfilesystemencoding()
47 if type(s) == unicode or not enc:
52 def write(file_path,content):
53 f = create_file(file_path)
54 f.write(os_encode(content))
56 from log.logging import logger
57 logger.log(u'Wrote %s to %s'%(content,os.path.abspath(file_path)))
59 def list_dir(root,recursive=False,filter=None):
60 encoded_root = os_encode(root)
61 if not os.path.exists(encoded_root):
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
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':
83 decoded = unicode(data, enc)
84 successful_encoding = enc
86 except (UnicodeError, LookupError):
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])))
93 #logger.log('Decoded %s to %s'%(repr(data),repr(decoded)),6)
94 return (decoded, successful_encoding)
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')
104 (text,encoding)=guess_encoding(raw)
107 def parse_file_to_line_list(unicode_complete_path):
108 text = read_text_from_file(unicode_complete_path)
109 lines = text.splitlines()
111 def is_dir(unicode_path):
112 return os.path.isdir(os_encode(unicode_path))