def setUp(self):
     self.read_file = FileReader("config.ini")
     self.read_file1 = FileReader("config_empty.ini")
     self.read_file2 = FileReader("config_no_rigth_format.ini")
     self.read_file01 = FileReader("anything.ini")
     self.read_file02 = FileReader("anything.xls")
     self.out_put_file_not_exist = ''
     self.file_empty = " "
     self.right_format = ['Output_file:Txt\n','Default_alghoritm:Backtracking\n','Dificult_level:Easier\n']
Beispiel #2
0
class SettingsConfiguratorDisplay:
    legend = "\nThis is the current setting values of configuration file:\n"
    setting_menu = "\nPlease select the option to modify any value or leave by default\n"
    path = ("../Configuration/config.ini")

    read_file_setting = ""
    def __init__(self, config_file):
        self.read_file_setting = FileReader(self.path)

    def configuresettings(self):
        os.system('cls')
        print self.legend
        value_setting = self.read_file_setting.read_txt_file()

        for i in range(len(value_setting)):
                       print value_setting[i]
        self.sub_menu_setting()

    def sub_menu_setting(self):

        print self.setting_menu
        print '\
                1: Leave By default\n\
                2: Modify Setting Values\n\
                3: Back To Main Menu\n'

        value = int(raw_input())
        if value == 1:
            return Menu().menu()

        if value == 2:
            return SaveSetting().save_all_setting_in_file(self.path)

        if value == 3:
            return Menu().menu()
Beispiel #3
0
class SudokuDisplayer:

    def display(self,file):
        os.system('cls')
        solv = Solver(file)
        if solv.solvesudoku()== False:
            return FileResolver().resolvefromfile()
        else:
            self.matrixtosave = solv.solvesudoku()
            return self.savesudoku(self.matrixtosave)

    def validateoption(self):
        print "\n\nOptions:\n1:Back to Menu\n2:Exit"
        self.option = raw_input('\n')
        if self.option == '1':
            return Menu().menu()
        if self.option == '2':
            return Exit().exit()
        else:
            return self.validateoption()

    def savesudoku(self, matrix):
        absolutepath = os.path.abspath("../Configuration/config.ini")
        self.readconf = FileReader(absolutepath)
        extension = self.readconf.read_output_file()
        extension = extension.lower()
        name = raw_input("Please insert a name to store the resolved sudoku file\n")
        store = Storer(matrix, name, extension)
        store.save_matrix_to_file()
        return self.validateoption()
Beispiel #4
0
 def savesudoku(self, matrix):
     absolutepath = os.path.abspath("../Configuration/config.ini")
     self.readconf = FileReader(absolutepath)
     extension = self.readconf.read_output_file()
     extension = extension.lower()
     name = raw_input("Please insert a name to store the resolved sudoku file\n")
     store = Storer(matrix, name, extension)
     store.save_matrix_to_file()
     return self.validateoption()
Beispiel #5
0
 def __init__(self,config_file):
     '''
     Constructor: define which is the difficult level set by the user at config.ini file
     '''
     self.readfile = FileReader(config_file)
     value_level = self.readfile.read_dificult_level()
     if value_level == 'NoneType':
         self.level = ""
     else:
         self.level = self.readfile.read_dificult_level().lower()
class Configuration:
   
    config =['Output_file','Default_algorithm','Difficult_level']
    #settings = {config[0]:'Txt', config[1]:'Norvig', config[2]:'Easier'}
    settings = []

    def __init__(self, config_file):
        self.config_setting = FileReader(config_file)
        self.settings = self.config_setting.right_contain_of_the_ini_file()


    def leave_default_value(self):
        self.config =['Output_file','Default_algorithm','Difficult_level']
        self.settings = {self.config[0]:'Txt', self.config[1]:'Norvig', self.config[2]:'Easier'}
        return self.settings

    def modify_existing_settings(self, custom_name, custom_value):
        if not custom_name:
            return self.settings
        
        elif self.exist_custom_name(custom_name) != False:
            pos = self.exist_custom_name(custom_name)
            self.settings[pos] = custom_name+":"+custom_value
            return self.settings
        else: 
            return False
            

    def add_new_custom_setting(self, new_label, new_value):
        self.settings.append(new_label+":"+new_value)
        self.config.append(new_value)
        return self.settings

    def exist_custom_name(self,custom_name):
        num_elements = len(self.config)
        i = 0
        while i < range(num_elements):
            if custom_name == self.config[i]:
                return i
            else:
                i += 1 
        return False
Beispiel #7
0
 def __init__(self, config_file):
     self.read_file_setting = FileReader(self.path)
Beispiel #8
0
class SudokuGenerator:
    '''
    Generator class will create new sudoku games, based on already stored files
    '''
    level = ''
    files = []
    script_dir = os.path.dirname(__file__) 
    path_easy = os.path.join(script_dir, "Sudoku_Levels/Easy/")
    path_medium = os.path.join(script_dir,'Sudoku_Levels/Medium/')
    path_hard = os.path.join(script_dir,'Sudoku_Levels/Hard/')
    
    def __init__(self,config_file):
        '''
        Constructor: define which is the difficult level set by the user at config.ini file
        '''
        self.readfile = FileReader(config_file)
        value_level = self.readfile.read_dificult_level()
        if value_level == 'NoneType':
            self.level = ""
        else:
            self.level = self.readfile.read_dificult_level().lower()
                           
    def retrieve_file_names(self):
        """
        retrieve_file_names method retrieves all file names stored based on difficult level
        """
        if not self.level:
            return "No Setting"
        
        else:
            if self.level.strip() == "easier":
                self.files = [f for f in os.listdir(self.path_easy) if f.endswith('.txt')]
                return self.files
                      
            elif self.level.strip() == "medium":
                
                self.files = [f for f in os.listdir(self.path_medium) if f.endswith('.txt')]
                return self.files
            
            elif self.level.strip() == "difficult":
                self.files = [f for f in os.listdir(self.path_hard) if f.endswith('.txt')]
                return self.files
            
            else:
                return "There is not Sudoku game for this difficult level"
    
    def select_file(self):
        """
        Select any file from the relevant difficult set in the configuration file and return a list
        """
        file_list = self.retrieve_file_names()
              
        if file_list != "No Setting":
            num_files = len(file_list)
            num_f = randint(0, num_files-1)
            return file_list[num_f]
        else: 
            return "No Setting"
       
    def read_file(self):
        """
        Read the selected file from the list to display
        """
        value = []
        files = self.select_file()
        converter = GeneralConverter()
        
        if files == 'No Setting':
            return files
        
        elif self.level.strip() == "easier":
            value = converter.convert_txt_file_to_string(self.path_easy+'/'+files)
  
        elif self.level.strip() == "medium":
            value = converter.convert_txt_file_to_string(self.path_medium+'/'+files)
            
        elif self.level.strip() == "difficult":
            value = converter.convert_txt_file_to_string(self.path_hard+'/'+files)
        if len(value) == 81:
            return converter.convert_string_to_matrix(value)
class TestFileReaderClassAndMethods(unittest.TestCase):

    def setUp(self):
        self.read_file = FileReader("config.ini")
        self.read_file1 = FileReader("config_empty.ini")
        self.read_file2 = FileReader("config_no_rigth_format.ini")
        self.read_file01 = FileReader("anything.ini")
        self.read_file02 = FileReader("anything.xls")
        self.out_put_file_not_exist = ''
        self.file_empty = " "
        self.right_format = ['Output_file:Txt\n','Default_alghoritm:Backtracking\n','Dificult_level:Easier\n']
                            
        
     
    def test_verify_config_file_exist(self):
        exist_file = self.read_file.file_exist()
        self.assertEqual(True, exist_file)
  
    def test_verify_config_file_is_empty(self):
        empty_file = self.read_file.file_is_empty()
        self.assertEqual(False, empty_file)

    def test_when_config_file_is_empty_return_True(self):
        empty_file = self.read_file1.file_is_empty()
        self.assertEqual(True, empty_file)

    def test_verify_config_file_has_right_extension_ini(self):
        format_file_name = self.read_file.right_configuration_extension()
        self.assertTrue(format_file_name)
    
    def test_verify_config_file_has_the_new_custom_values(self):
        custom_setting_file = self.read_file.file_exist()
        self.assertEqual(True, custom_setting_file)

    def test_just_right_extension_file_is_read(self):
        self.assertTrue(self.read_file.right_configuration_extension())

    def test_that_a_no_defined_extension_file_is_not_taken_into_account(self):
        self.assertFalse(self.read_file02.right_configuration_extension())

    def test_that_config_file_has_the_right_info_according_to_defined_format(self):
        read0=self.read_file.right_contain_of_the_ini_file()
        self.assertEqual(read0, self.right_format)

    def test_an_error_is_raised_when_there_is_not_file(self):
        read01=self.read_file01.file_exist()
        self.failureException(read01)
    
    def test_the_right_output_file_value_is_retrived(self):
        value = self.read_file.read_output_file()
        self.assertEqual("txt", value.lower())
    
    def test_that_right_difficult_level_is_retrived(self):
        value = self.read_file.read_dificult_level()
        self.assertEqual("easier", value.lower())
    
    def test_that_right_default_algrithm_is_retrived(self):
        value = self.read_file.read_default_algorithm()
        self.assertEqual("backtracking", value.lower())
 def __init__(self, config_file):
     self.config_setting = FileReader(config_file)
     self.settings = self.config_setting.right_contain_of_the_ini_file()