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):
#
# Singleton creator and destructor
#
def __new__(cls, *args, **kwargs):
""" Creating or returning the singleton instance. """
def __new__(cls, settings_dir=None, *args, **kwargs):
"""
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:
# Create the singleton instance
cls.__singleton_instance =\
super(ServerCompilerSettings, cls).__new__(cls, *args, **kwargs)
# Initialise the instance, defaults if file not found
cls.__singleton_instance.__initialise()
cls.__singleton_instance.__initialise(settings_dir)
return cls.__singleton_instance
def __initialise(self):
def __initialise(self, settings_dir=None):
# Create variables to be used with accessors
self.__launch_IDE_option__ = None
self.__compiler_dir__ = None
......@@ -91,6 +96,15 @@ class ServerCompilerSettings(object):
self.__arduino_board_value__ = None
self.__serial_port_key__ = 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
self.set_launch_ide_default()
# Load settings from file
......@@ -524,17 +538,17 @@ class ServerCompilerSettings(object):
# Set the path and create/overwrite the file
try:
settings_file = codecs.open(
self.get_settings_file_path(), 'wb+', 'utf8')
settings_parser.write(settings_file)
print('Settings file saved to:')
sys.stdout.flush()
settings_file = codecs.open(self.__settings_path, 'wb+', 'utf8')
try:
settings_parser.write(settings_file)
print('Settings file saved to:\n\t%s' % self.__settings_path)
sys.stdout.flush()
finally:
settings_file.close()
except Exception as e:
print(e)
print('\nUnable to write the settings file to:')
finally:
settings_file.close()
print('\t' + self.get_settings_file_path())
print('Unable to write the settings file to:\n\t%s' %
self.__settings_path)
def read_settings(self):
"""
......@@ -542,7 +556,7 @@ class ServerCompilerSettings(object):
member variables. If it cannot read the file it sets the variables
to the default value.
"""
settings_dict = self.read_settings_file()
settings_dict = self.get_settings_file_data()
if settings_dict:
self.set_compiler_dir_from_file(settings_dict['arduino_exec_path'])
self.set_arduino_board_from_file(settings_dict['arduino_board'])
......@@ -567,7 +581,7 @@ class ServerCompilerSettings(object):
# does the set_default_settings() function, so save them either way.
self.save_settings()
def read_settings_file(self):
def get_settings_file_data(self):
"""
Creates a dictionary from the settings stored in a file.
:return: A dictionary with all the options and values from the settings
......@@ -577,7 +591,7 @@ class ServerCompilerSettings(object):
settings_parser = ConfigParser.ConfigParser()
try:
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_parser.get('Arduino_IDE', 'arduino_exec_path')
settings_dict['arduino_board'] =\
......@@ -592,24 +606,12 @@ class ServerCompilerSettings(object):
except Exception as e:
print('\nSettings file corrupted or not found in:')
settings_dict = None
print('\t' + self.get_settings_file_path())
print('\t' + self.__settings_path)
return settings_dict
def delete_settings_file(self):
if os.path.exists(self.get_settings_file_path()):
os.remove(self.get_settings_file_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
if os.path.exists(self.__settings_path):
os.remove(self.__settings_path)
def get_board_value_from_key(self, string_key):
"""
......
......@@ -90,7 +90,7 @@ class ServerCompilerSettingsTestCase(unittest.TestCase):
def test_settings_file_read(self):
ServerCompilerSettings()
ServerCompilerSettings().set_default_settings()
ServerCompilerSettings().read_settings_file()
ServerCompilerSettings().get_settings_file_data()
ServerCompilerSettings().save_settings()
......
......@@ -134,9 +134,6 @@ def main():
print('\n\n======= Parsing Command line arguments =======\n')
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')
# Based on command line options, set the server root to the ardublockly
# project root directory, a directory specified in the arguments, or by
......@@ -158,6 +155,12 @@ def main():
'the server root directory !')
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:
# Find the relative path from server root to ardublockly html
ardublockly_html_dir = os.path.join(ardublockly_root_dir, 'ardublockly')
......@@ -166,7 +169,6 @@ def main():
relative_path)
open_browser(relative_path)
print('\n\n======= Starting Server =======\n')
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