Commit f2057616 authored by carlosperate's avatar carlosperate

Server compiler settings file directory now set on constructor.

Updated the start script to set the file directory to the project root directory.
Updated unit test.
parent fad1c53f
...@@ -71,17 +71,22 @@ class ServerCompilerSettings(object): ...@@ -71,17 +71,22 @@ class ServerCompilerSettings(object):
# #
# Singleton creator and destructor # Singleton creator and destructor
# #
def __new__(cls, *args, **kwargs): def __new__(cls, settings_dir=None, *args, **kwargs):
""" Creating or returning the singleton instance. """ """
Creating or returning the singleton instance.
The argument settings_file_dir is only processed on first
initialisation, and any future calls to the constructor will returned
the already initialised instance with a set settings_file_dir.
"""
if not cls.__singleton_instance: if not cls.__singleton_instance:
# Create the singleton instance # Create the singleton instance
cls.__singleton_instance =\ cls.__singleton_instance =\
super(ServerCompilerSettings, cls).__new__(cls, *args, **kwargs) super(ServerCompilerSettings, cls).__new__(cls, *args, **kwargs)
# Initialise the instance, defaults if file not found # Initialise the instance, defaults if file not found
cls.__singleton_instance.__initialise() cls.__singleton_instance.__initialise(settings_dir)
return cls.__singleton_instance return cls.__singleton_instance
def __initialise(self): def __initialise(self, settings_dir=None):
# Create variables to be used with accessors # Create variables to be used with accessors
self.__launch_IDE_option__ = None self.__launch_IDE_option__ = None
self.__compiler_dir__ = None self.__compiler_dir__ = None
...@@ -91,6 +96,15 @@ class ServerCompilerSettings(object): ...@@ -91,6 +96,15 @@ class ServerCompilerSettings(object):
self.__arduino_board_value__ = None self.__arduino_board_value__ = None
self.__serial_port_key__ = None self.__serial_port_key__ = None
self.__serial_port_value__ = None self.__serial_port_value__ = None
if settings_dir:
self.__settings_path = os.path.join(
settings_dir, self.__settings_filename)
else:
# If not set, the file path will be same location as the executed
# python code that calls this class
called_script_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
self.__settings_path = os.path.normpath(
os.path.join(called_script_dir, self.__settings_filename))
# Since this value is not saved in the settings file initialise here # Since this value is not saved in the settings file initialise here
self.set_launch_ide_default() self.set_launch_ide_default()
# Load settings from file # Load settings from file
...@@ -524,17 +538,17 @@ class ServerCompilerSettings(object): ...@@ -524,17 +538,17 @@ class ServerCompilerSettings(object):
# Set the path and create/overwrite the file # Set the path and create/overwrite the file
try: try:
settings_file = codecs.open( settings_file = codecs.open(self.__settings_path, 'wb+', 'utf8')
self.get_settings_file_path(), 'wb+', 'utf8') try:
settings_parser.write(settings_file) settings_parser.write(settings_file)
print('Settings file saved to:') print('Settings file saved to:\n\t%s' % self.__settings_path)
sys.stdout.flush() sys.stdout.flush()
except Exception as e:
print(e)
print('\nUnable to write the settings file to:')
finally: finally:
settings_file.close() settings_file.close()
print('\t' + self.get_settings_file_path()) except Exception as e:
print(e)
print('Unable to write the settings file to:\n\t%s' %
self.__settings_path)
def read_settings(self): def read_settings(self):
""" """
...@@ -542,7 +556,7 @@ class ServerCompilerSettings(object): ...@@ -542,7 +556,7 @@ class ServerCompilerSettings(object):
member variables. If it cannot read the file it sets the variables member variables. If it cannot read the file it sets the variables
to the default value. to the default value.
""" """
settings_dict = self.read_settings_file() settings_dict = self.get_settings_file_data()
if settings_dict: if settings_dict:
self.set_compiler_dir_from_file(settings_dict['arduino_exec_path']) self.set_compiler_dir_from_file(settings_dict['arduino_exec_path'])
self.set_arduino_board_from_file(settings_dict['arduino_board']) self.set_arduino_board_from_file(settings_dict['arduino_board'])
...@@ -567,7 +581,7 @@ class ServerCompilerSettings(object): ...@@ -567,7 +581,7 @@ class ServerCompilerSettings(object):
# does the set_default_settings() function, so save them either way. # does the set_default_settings() function, so save them either way.
self.save_settings() self.save_settings()
def read_settings_file(self): def get_settings_file_data(self):
""" """
Creates a dictionary from the settings stored in a file. Creates a dictionary from the settings stored in a file.
:return: A dictionary with all the options and values from the settings :return: A dictionary with all the options and values from the settings
...@@ -577,7 +591,7 @@ class ServerCompilerSettings(object): ...@@ -577,7 +591,7 @@ class ServerCompilerSettings(object):
settings_parser = ConfigParser.ConfigParser() settings_parser = ConfigParser.ConfigParser()
try: try:
settings_parser.readfp( settings_parser.readfp(
codecs.open(self.get_settings_file_path(), 'r', 'utf8')) codecs.open(self.__settings_path, 'r', 'utf8'))
settings_dict['arduino_exec_path'] =\ settings_dict['arduino_exec_path'] =\
settings_parser.get('Arduino_IDE', 'arduino_exec_path') settings_parser.get('Arduino_IDE', 'arduino_exec_path')
settings_dict['arduino_board'] =\ settings_dict['arduino_board'] =\
...@@ -592,24 +606,12 @@ class ServerCompilerSettings(object): ...@@ -592,24 +606,12 @@ class ServerCompilerSettings(object):
except Exception as e: except Exception as e:
print('\nSettings file corrupted or not found in:') print('\nSettings file corrupted or not found in:')
settings_dict = None settings_dict = None
print('\t' + self.get_settings_file_path()) print('\t' + self.__settings_path)
return settings_dict return settings_dict
def delete_settings_file(self): def delete_settings_file(self):
if os.path.exists(self.get_settings_file_path()): if os.path.exists(self.__settings_path):
os.remove(self.get_settings_file_path()) os.remove(self.__settings_path)
def get_settings_file_path(self):
"""
Returns the settings file path or creates the path if not invoked before.
The file is saved in the same directory as this python source code file.
:return: path to the settings file
"""
if not self.__settings_path:
this_package_dir = os.path.dirname(os.path.realpath(__file__))
self.__settings_path = os.path.normpath(
os.path.join(this_package_dir, self.__settings_filename))
return self.__settings_path
def get_board_value_from_key(self, string_key): def get_board_value_from_key(self, string_key):
""" """
......
...@@ -90,7 +90,7 @@ class ServerCompilerSettingsTestCase(unittest.TestCase): ...@@ -90,7 +90,7 @@ class ServerCompilerSettingsTestCase(unittest.TestCase):
def test_settings_file_read(self): def test_settings_file_read(self):
ServerCompilerSettings() ServerCompilerSettings()
ServerCompilerSettings().set_default_settings() ServerCompilerSettings().set_default_settings()
ServerCompilerSettings().read_settings_file() ServerCompilerSettings().get_settings_file_data()
ServerCompilerSettings().save_settings() ServerCompilerSettings().save_settings()
......
...@@ -134,9 +134,6 @@ def main(): ...@@ -134,9 +134,6 @@ def main():
print('\n\n======= Parsing Command line arguments =======\n') print('\n\n======= Parsing Command line arguments =======\n')
find_project_root, launch_browser, server_root = parsing_cl_args() find_project_root, launch_browser, server_root = parsing_cl_args()
print('\n\n======= Loading Settings =======')
ardublocklyserver.compilersettings.ServerCompilerSettings()
print('\n\n======= Resolving server and project paths =======\n') print('\n\n======= Resolving server and project paths =======\n')
# Based on command line options, set the server root to the ardublockly # Based on command line options, set the server root to the ardublockly
# project root directory, a directory specified in the arguments, or by # project root directory, a directory specified in the arguments, or by
...@@ -158,6 +155,12 @@ def main(): ...@@ -158,6 +155,12 @@ def main():
'the server root directory !') 'the server root directory !')
print("Selected server root: %s" % server_root) print("Selected server root: %s" % server_root)
print('\n\n======= Loading Settings =======')
# ServerCompilerSettings is a singleton, no need to save instance
ardublocklyserver.compilersettings.ServerCompilerSettings(
ardublockly_root_dir)
print('\n\n======= Starting Server =======\n')
if launch_browser: if launch_browser:
# Find the relative path from server root to ardublockly html # Find the relative path from server root to ardublockly html
ardublockly_html_dir = os.path.join(ardublockly_root_dir, 'ardublockly') ardublockly_html_dir = os.path.join(ardublockly_root_dir, 'ardublockly')
...@@ -166,7 +169,6 @@ def main(): ...@@ -166,7 +169,6 @@ def main():
relative_path) relative_path)
open_browser(relative_path) open_browser(relative_path)
print('\n\n======= Starting Server =======\n')
ardublocklyserver.server.start_server(server_root) ardublocklyserver.server.start_server(server_root)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment