22#
33# Save and retrieve the compiler settings into a text file.
44#
5- # Copyright (c) 2015 carlosperate https://github.com/carlosperate/
5+ # Copyright (c) 2017 carlosperate https://github.com/carlosperate/
66# Licensed under the Apache License, Version 2.0 (the "License"):
77# http://www.apache.org/licenses/LICENSE-2.0
88#
99# The ServerCompilerSettings is a singleton class maintains in memory, and
1010# writes the Arduino IDE settings into a file.
1111# On first invocation of the singleton it reads the settings from the file.
1212#
13- from __future__ import unicode_literals , absolute_import
13+ from __future__ import unicode_literals , absolute_import , print_function
1414import os
1515import re
1616import sys
1717import codecs
18-
19- #try:
20- # # 2.x name
21- # import ConfigParser
22- #except ImportError:
23- # # 3.x name
24- # import configparser as ConfigParser
25-
18+ # configparser port of the 3.5 version to support unicode across all versions
2619from ardublocklyserver import configparser
2720import ardublocklyserver .serialport
2821
@@ -143,15 +136,15 @@ def set_compiler_dir(self, new_compiler_dir):
143136 'JavaApplicationStub' )):
144137 new_compiler_dir = os .path .join (new_compiler_dir , 'Contents' ,
145138 'MacOS' , 'JavaApplicationStub' )
146- print ('\n Compiler file in OS X located within the app bundle.' )
139+ print ('Compiler file in OS X located within the app bundle.' )
147140 elif os .path .isfile (os .path .join (
148141 new_compiler_dir , 'Contents' , 'MacOS' , 'Arduino' )):
149142 new_compiler_dir = os .path .join (
150143 new_compiler_dir , 'Contents' , 'MacOS' , 'Arduino' )
151- print ('\n Compiler file in OS X located within the app bundle.' )
144+ print ('Compiler file in OS X located within the app bundle.' )
152145 else :
153- print ('Could not locate the Arduino executable within the OS X '
154- 'app bundle. These are the available files:' )
146+ print ('Could not locate the Arduino executable within the OS '
147+ 'X app bundle. These are the available files:' )
155148 try :
156149 print ('%s' % os .listdir (
157150 '%s/Contents/MacOS/' % new_compiler_dir ))
@@ -161,11 +154,11 @@ def set_compiler_dir(self, new_compiler_dir):
161154 # Check directory
162155 if os .path .isfile (new_compiler_dir ):
163156 self .__compiler_dir = new_compiler_dir
164- print ('\n Compiler directory set to:\n \t %s' % self .__compiler_dir )
157+ print ('Compiler directory set to:\n \t %s' % self .__compiler_dir )
165158 self .save_settings ()
166159 else :
167- print ('\n The provided compiler path is not valid !!!' )
168- print ( ' \t ' + new_compiler_dir )
160+ print ('The provided compiler path is not valid !!!'
161+ ' \n \t %s' % new_compiler_dir )
169162 if self .__compiler_dir :
170163 print ('Previous compiler path maintained:\n \t %s' %
171164 self .__compiler_dir )
@@ -185,7 +178,7 @@ def set_compiler_dir_from_file(self, new_compiler_dir):
185178 if os .path .exists (new_compiler_dir ):
186179 self .__compiler_dir = new_compiler_dir
187180 else :
188- print ('\n The provided compiler path in the settings file is not ' +
181+ print ('The provided compiler path in the settings file is not '
189182 'valid:' )
190183 print ('\t %s' % new_compiler_dir )
191184 self .set_compiler_dir_default ()
@@ -201,11 +194,11 @@ def set_sketch_name(self, new_sketch_name):
201194 """ Only accept letters, numbers, underscores and dashes. """
202195 if re .match ("^[\w\d_-]*$" , new_sketch_name ):
203196 self .__sketch_name = new_sketch_name
204- print ('\n Sketch name set to:\n \t %s' % self .__sketch_name )
197+ print ('Sketch name set to:\n \t %s' % self .__sketch_name )
205198 self .save_settings ()
206199 else :
207- print ('\n Provided Sketch name contains invalid characters: !!!' )
208- print ( ' \t %s' % new_sketch_name )
200+ print ('Provided Sketch name contains invalid characters: !!!'
201+ ' \n \t %s' % new_sketch_name )
209202 if self .__sketch_name :
210203 print ('Previous Sketch name maintained:\n \t %s' %
211204 self .__sketch_name )
@@ -225,8 +218,8 @@ def set_sketch_name_from_file(self, new_sketch_name):
225218 if re .match ("^[\w\d_-]*$" , new_sketch_name ):
226219 self .__sketch_name = new_sketch_name
227220 else :
228- print ('\n Settings file Sketch name contains invalid characters:' )
229- print ( ' \t %s' % new_sketch_name .decode ("utf8" ))
221+ print ('Settings file Sketch name contains invalid characters:'
222+ ' \n \t %s' % new_sketch_name .decode ("utf8" ))
230223 self .set_sketch_name_default ()
231224 print ('Default Sketch name set:\n \t %s' % self .__sketch_name )
232225
@@ -240,11 +233,11 @@ def set_sketch_dir(self, new_sketch_dir):
240233 """ The sketch directory must be a folder """
241234 if os .path .isdir (new_sketch_dir ):
242235 self .__sketch_dir = new_sketch_dir
243- print ('\n Sketch directory set to:\n \t %s' % self .__sketch_dir )
236+ print ('Sketch directory set to:\n \t %s' % self .__sketch_dir )
244237 self .save_settings ()
245238 else :
246- print ('\n The provided sketch directory is not valid !!!' )
247- print ( ' \t %s' % new_sketch_dir )
239+ print ('The provided sketch directory is not valid !!!'
240+ ' \n \t %s' % new_sketch_dir )
248241 if self .__sketch_dir :
249242 print ('Previous Sketch directory maintained:\n \t %s' %
250243 self .__sketch_dir )
@@ -265,8 +258,8 @@ def set_sketch_dir_from_file(self, new_sketch_dir):
265258 if os .path .isdir (new_sketch_dir ):
266259 self .__sketch_dir = new_sketch_dir
267260 else :
268- print ('\n Settings file sketch directory is not valid:' )
269- print ( ' \t %s' % new_sketch_dir )
261+ print ('Settings file sketch directory is not valid:'
262+ ' \n \t %s' % new_sketch_dir )
270263 self .set_sketch_dir_default ()
271264 print ('Default Sketch directory set:\n \t %s' % self .__sketch_dir )
272265
@@ -280,11 +273,11 @@ def set_arduino_board(self, new_board):
280273 if new_board in self .__arduino_types :
281274 self .__arduino_board_value = self .__arduino_types [new_board ]
282275 self .__arduino_board_key = new_board
283- print ('\n Arduino Board set to:\n \t %s' % self .__arduino_board_key )
276+ print ('Arduino Board set to:\n \t %s' % self .__arduino_board_key )
284277 self .save_settings ()
285278 else :
286- print ('\n Provided Arduino Board does not exist: !!!' )
287- print ( ' \t %s' % new_board )
279+ print ('Provided Arduino Board does not exist: !!!'
280+ ' \n \t %s' % new_board )
288281 if self .__arduino_board_key and self .__arduino_board_value :
289282 print ('Previous Arduino board type maintained:\n \t %s' %
290283 self .__arduino_board_key )
@@ -306,8 +299,8 @@ def set_arduino_board_from_file(self, new_board):
306299 self .__arduino_board_value = self .__arduino_types [new_board ]
307300 self .__arduino_board_key = new_board
308301 else :
309- print ('\n Settings file Arduino Board does not exist:' )
310- print ( ' \t %s' % new_board )
302+ print ('Settings file Arduino Board does not exist:\n \t %s'
303+ % new_board )
311304 self .set_arduino_board_default ()
312305 print ('Default Arduino board type set:\n \t %s' %
313306 self .__arduino_board_key )
@@ -334,12 +327,12 @@ def get_serial_port(self):
334327 """
335328 self .populate_serial_port_list ()
336329 if not self .__serial_ports :
337- print ('\n There are no available Serial Ports !!!' )
330+ print ('There are no available Serial Ports !!!' )
338331 self .__serial_port_key = None
339332 self .__serial_port_value = None
340333 self .save_settings ()
341334 elif self .__serial_port_value not in self .__serial_ports .values ():
342- print ('\n The selected Serial Port is no longer available !!!' )
335+ print ('The selected Serial Port is no longer available !!!' )
343336 self .__serial_port_key = None
344337 self .__serial_port_value = None
345338 self .save_settings ()
@@ -366,18 +359,18 @@ def set_serial_port(self, new_port):
366359 # Now we check if the Port is still available
367360 self .populate_serial_port_list ()
368361 if not self .__serial_ports :
369- print ('\n There are no available Serial Ports !!!' )
362+ print ('There are no available Serial Ports !!!' )
370363 self .__serial_port_key = None
371364 self .__serial_port_value = None
372365 elif self .__serial_port_value not in self .__serial_ports .values ():
373- print ('\n The selected Serial Port is no longer available !!!' )
366+ print ('The selected Serial Port is no longer available !!!' )
374367 self .__serial_port_key = None
375368 self .__serial_port_value = None
376- print ('\n Serial Port set to:\n \t %s' % self .__serial_port_value )
369+ print ('Serial Port set to:\n \t %s' % self .__serial_port_value )
377370 self .save_settings ()
378371 else :
379- print ('\n Provided Serial Port is not valid: !!!' )
380- print ( ' \t %s' % new_port )
372+ print ('Provided Serial Port is not valid: !!!'
373+ ' \n \t %s' % new_port )
381374 if self .__serial_port_key and self .__serial_port_value :
382375 print ('Previous Serial Port maintained:\n \t %s' %
383376 self .__serial_port_value )
@@ -420,8 +413,8 @@ def set_serial_port_from_file(self, new_port_value):
420413 self .__serial_port_value = value
421414 set_default = False
422415 if set_default :
423- print ('\n Settings file Serial Port is not currently available:' )
424- print ( ' \t %s' % new_port_value )
416+ print ('Settings file Serial Port is not currently available:'
417+ ' \n \t %s' % new_port_value )
425418 self .set_serial_port_default ()
426419 print ('Default Serial Port set:\n \t %s' % self .__serial_port_value )
427420
@@ -434,12 +427,12 @@ def get_serial_port_flag(self):
434427 """
435428 self .populate_serial_port_list ()
436429 if not self .__serial_ports :
437- print ('\n There are no available Serial Ports !!!' )
430+ print ('There are no available Serial Ports !!!' )
438431 self .__serial_port_key = None
439432 self .__serial_port_value = None
440433 self .save_settings ()
441434 elif self .__serial_port_value not in self .__serial_ports .values ():
442- print ('\n The selected Serial Port is no longer available !!!' )
435+ print ('The selected Serial Port is no longer available !!!' )
443436 self .__serial_port_key = None
444437 self .__serial_port_value = None
445438 self .save_settings ()
@@ -481,12 +474,12 @@ def get_load_ide(self):
481474 def set_load_ide (self , new_load_option ):
482475 if new_load_option in self .__ide_load_options :
483476 self .__load_ide_option = new_load_option
484- print ('\n IDE options set to:\n \t %s' %
477+ print ('IDE options set to:\n \t %s' %
485478 self .__ide_load_options [self .__load_ide_option ])
486479 self .save_settings ()
487480 else :
488- print ('\n The provided "Load IDE option" is not valid !!!' )
489- print ( ' \t %s' % new_load_option )
481+ print ('The provided "Load IDE option" is not valid !!!'
482+ ' \n \t %s' % new_load_option )
490483 if self .__load_ide_option :
491484 print ('Previous "Load IDE option" maintained:\n \t %s' %
492485 self .__ide_load_options [self .__load_ide_option ])
@@ -506,8 +499,8 @@ def set_load_ide_from_file(self, new_load_option):
506499 if new_load_option in self .__ide_load_options :
507500 self .__load_ide_option = new_load_option
508501 else :
509- print ('\n Settings file "Load IDE option" is not valid:' )
510- print ( ' \t %s' % new_load_option )
502+ print ('Settings file "Load IDE option" is not valid:'
503+ ' \n \t %s' % new_load_option )
511504 self .set_load_ide_default ()
512505 print ('Default "Load IDE option" set:\n \t %s' %
513506 self .__load_ide_option )
@@ -583,18 +576,18 @@ def read_settings(self):
583576 self .set_sketch_dir_from_file (settings_dict ['sketch_directory' ])
584577 self .set_load_ide_from_file (settings_dict ['ide_load' ])
585578 else :
586- print ('\n Settings will be set to the default values.' )
579+ print ('Settings will be set to the default values.' )
587580 self .set_default_settings ()
588581
589582 # Printing the settings to be able to easily spot issues at load
590- print ('\n Final settings loaded:' )
583+ print ('Final settings loaded:' )
591584 print ('\t Compiler directory: %s' % self .__compiler_dir )
592585 print ('\t Arduino Board Key: %s' % self .__arduino_board_key )
593586 print ('\t Arduino Board Value: %s' % self .__arduino_board_value )
594587 print ('\t Serial Port Value: %s' % self .__serial_port_value )
595588 print ('\t Sketch Name: %s' % self .__sketch_name )
596589 print ('\t Sketch Directory: %s' % self .__sketch_dir )
597- print ('\t Load IDE option: %s\n ' % self .__load_ide_option )
590+ print ('\t Load IDE option: %s' % self .__load_ide_option )
598591
599592 # The read X_from_file() functions do not save new settings and neither
600593 # does the set_default_settings() function, so save them either way.
@@ -623,11 +616,11 @@ def get_settings_file_data(self):
623616 settings_parser .get ('Arduino_Sketch' , 'sketch_directory' )
624617 settings_dict ['ide_load' ] = \
625618 settings_parser .get ('Ardublockly' , 'ide_load' )
626- print ('\n Settings loaded from:' )
619+ print ('Settings loaded from:\n \t %s' % self . __settings_path )
627620 except Exception as e :
628- print ('\n Settings file corrupted or not found in:' )
621+ print ('Settings file corrupted or not found in:\n \t %s'
622+ % self .__settings_path )
629623 settings_dict = None
630- print ('\t %s' % self .__settings_path )
631624 return settings_dict
632625
633626 def delete_settings_file (self ):
0 commit comments