def set_default_chart_save_location(user_set):

    # initialise default location
    new_default_save_location = APP_DEFAULT_CHART_SAVE_FOLDER
    original_location = definitions.DEFAULT_CHART_SAVE_FOLDER
    new_setting = {}

    if user_set:
        new_default_save_location = user_set_chart_save_folder()

    # Ensure saved value has correct separators.
    chart_save_parent_folder_path = Path(new_default_save_location)
    new_chart_save_folder_str = str(
        Path.joinpath(chart_save_parent_folder_path, CHART_SAVE_FOLDER_NAME))

    # Initialise and save chart save location.
    definitions.DEFAULT_CHART_SAVE_FOLDER = new_chart_save_folder_str

    new_setting['user_default_chart_save_folder'] = new_chart_save_folder_str
    create_app_settings_file()
    edit_app_settings_file(new_setting)

    if original_location:
        move_file(original_location, new_chart_save_folder_str)

    print(
        f'Default chart save folder set to {definitions.DEFAULT_CHART_SAVE_FOLDER}'
    )

    # Create chart save location
    Path(new_setting['user_default_chart_save_folder']).mkdir(parents=True,
                                                              exist_ok=True)
    def test_move_file_non_existent_original(self, monkeypatch):
        def mocked_move(origin, destination):
            # Should not be called.
            raise NotImplementedError

        monkeypatch.setattr(file_functions, 'move', mocked_move)

        move_file('Non-existent origin', 'No destination')
Beispiel #3
0
def move_chart_save_folder(original_location: Union[Path, str],
                           new_location: Union[Path, str]):
    """
    Tests if the supplied path to the original chart save folder exists, moving
    to the supplied new location if it does. Otherwise does nothing.

    :param original_location: Path or str
    :param new_location: Path or str
    :return: None
    """
    original_location_path = Path(original_location)
    if original_location_path.exists():
        move_file(original_location, new_location)
    def test_move_file(self, tmpdir, test_file, monkeypatch):
        def mocked_move(origin, destination):
            # Test copyfile called with expected arguments.
            assert (origin, destination) == (str(original_filepath),
                                             str(destination_filepath))

        original_filepath = test_file(tmpdir)
        destination_filepath = Path('some destination')

        monkeypatch.setattr(file_functions, 'move', mocked_move)

        assert not Path.exists(destination_filepath)
        move_file(original_filepath, destination_filepath)
    def test_move_file_moves_file(self, tmpdir, test_file):
        original_filepath = test_file(tmpdir)
        original_filename = original_filepath.name

        destination_dir = Path(tmpdir, 'new_directory')
        Path.mkdir(destination_dir)
        destination_filepath = Path(destination_dir, original_filename)

        assert not Path.exists(destination_filepath)
        move_file(original_filepath, destination_filepath)

        # Assert file in destination and not in origin.
        assert Path.exists(
            destination_filepath) and not Path.exists(original_filepath)
Beispiel #6
0
def move_chart_save_folder(original_location: Path,
                           new_location: Path) -> None:
    """
    Move existent chart save folder to new location.

    Tests if the supplied path to the original chart save folder exists,
    moving to the supplied new location if it does. Otherwise does
    nothing.

    :param original_location: Path
    :param new_location: Path
    :return: None
    """
    original_location_path = Path(original_location)
    if original_location_path.exists():
        move_file(original_location, new_location)
Beispiel #7
0
    def _move_avatar_to_class_data(self, new_class: NewClass,
                                   avatar_filename: str) -> None:
        """
        Moves avatar from NewClass.temp_dir to new class' avatars dir.

        Will not repeat moves of same filename if image already exists in
        avatars directory, to avoid repeated overwrite with same file.

        :param new_class: NewClass
        :param avatar_filename: str
        :return: None
        """
        origin_path = new_class.temp_avatars_dir.joinpath(avatar_filename)
        destination_path = self.class_data_path.joinpath(new_class.id,
                                                         'avatars',
                                                         avatar_filename)
        if not destination_path.exists():  # Avatar not already in database/class data.
            move_file(origin_path, destination_path)
    def test_move_file_directory_containing_file(self, tmpdir, test_file):
        # Create directory with file in it.
        original_dirname = 'original_dir'
        original_dir = Path(tmpdir, original_dirname)
        Path.mkdir(original_dir)
        original_filepath = test_file(original_dir)
        original_filename = original_filepath.name

        destination_dir = Path(tmpdir, 'new_directory')
        Path.mkdir(destination_dir)
        destination_filepath = Path(destination_dir, original_dirname,
                                    original_filename)

        assert not Path.exists(destination_filepath)
        # Move original folder with file in it.
        move_file(original_dir, destination_dir)

        # Assert original directory no longer exists, file in destination and not in origin.
        assert not Path.exists(original_dir)
        assert Path.exists(
            destination_filepath) and not Path.exists(original_filepath)
Beispiel #9
0
 def test_move_file(self):
     assert os.path.exists(self.original_filename)
     assert not os.path.exists(self.destination_path)
     move_file(self.original_path, self.destination_path)
     assert os.path.exists(self.destination_path)
     assert not os.path.exists(self.original_path)
Beispiel #10
0
 def test_move_file(self):
     move_file(self.original_folder_name, self.destination_folder_name)
     assert os.path.exists(self.destination_path)
     assert not os.path.exists(self.original_file_path)
Beispiel #11
0
def move_chart_save_folder(original_location, new_location):
    original_location_path = Path(original_location)
    if original_location_path.exists():
        move_file(original_location, new_location)
Beispiel #12
0
    def test_move_file_mocking_move_non_existent_origin(
            self, mock_path_exists, mock_move):
        mock_path_exists.return_value = False  # Assume the path exists.

        move_file(self.original_path, self.destination_path)
        mock_move.assert_not_called()
Beispiel #13
0
    def test_move_file_mocking_move(self, mock_path_exists, mock_move):
        mock_path_exists.return_value = True  # Assume the path exists.

        move_file(self.original_path, self.destination_path)
        mock_move.assert_called_once_with(self.original_path,
                                          self.destination_path)