Robot Framework Integrated Development Environment (RIDE)
FileWriter.py
Go to the documentation of this file.
1 # Copyright 2010 Orbitz WorldWide
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 
15 # Modified by NSN
16 # Copyright 2010-2012 Nokia Solutions and Networks
17 # Copyright 2013-2015 Nokia Networks
18 # Copyright 2016- Robot Framework Foundation
19 #
20 # Licensed under the Apache License, Version 2.0 (the "License");
21 # you may not use this file except in compliance with the License.
22 # You may obtain a copy of the License at
23 #
24 # http://www.apache.org/licenses/LICENSE-2.0
25 #
26 # Unless required by applicable law or agreed to in writing, software
27 # distributed under the License is distributed on an "AS IS" BASIS,
28 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29 # See the License for the specific language governing permissions and
30 # limitations under the License.
31 
32 import codecs
33 import os
34 
35 from robotide.context import IS_WINDOWS
36 from sys import getfilesystemencoding
37 
38 OUTPUT_ENCODING = getfilesystemencoding()
39 
40 
41 class FileWriter:
42 
43  @staticmethod
44  def write(file_path, lines, windows_mode, mode='w'):
45  if not os.path.exists(os.path.dirname(file_path)):
46  os.makedirs(os.path.dirname(file_path))
47  if IS_WINDOWS:
48  f = codecs.open(file_path, mode=windows_mode)
49  for item in lines:
50  if isinstance(item, str):
51  enc_arg = item.encode('UTF-8') # OUTPUT_ENCODING
52  else:
53  enc_arg = item
54  try:
55  f.write(enc_arg)
56  f.write("\n".encode(OUTPUT_ENCODING))
57  except UnicodeError:
58  f.write(bytes(item, 'UTF-8'))
59  f.write(b"\n")
60  else:
61  f = codecs.open(file_path, mode, "UTF-8")
62  f.write("\n".join(lines))
63  f.close()
def write(file_path, lines, windows_mode, mode='w')
Definition: FileWriter.py:44