Esempio n. 1
0
    def test_returns_false_if_relocation_of_file_without_extension_fails(self):
        temp_path = self.tempdir.path
        dir_1 = os.path.join(temp_path, 'abc', 'fig', 'one', 'two', 'three')
        dir_2 = os.path.join(temp_path, 'abc', 'fig', 'one', 'two')
        dir_3 = os.path.join(temp_path, 'abc', 'fig')
        self.tempdir.makedir(dir_1)
        file_1 = self.tempdir.write(
            os.path.join(dir_1, '146 awesome street $# yea'), '')

        f1 = File(file_1)
        f1.move_to(dst_root_path=dir_2,
                   group=True,
                   by_extension=True,
                   group_folder_name='sample dir')
        with self.subTest(1):
            compare(True, f1.exists)
        with self.subTest(2):
            final_path = os.path.join(dir_2, 'sample dir', 'UNDEFINED',
                                      '146 awesome street $# yea')
            compare(final_path, f1.path)
        f1.move_to(dst_root_path=dir_3,
                   group=False,
                   by_extension=True,
                   group_folder_name='sample dir')
        with self.subTest(3):
            final_path = os.path.join(dir_3, 'UNDEFINED',
                                      '146 awesome street $# yea')
            compare(final_path, f1.path)
Esempio n. 2
0
    def test_returns_false_if_relocation_to_parent_folder_fails(self):
        temp_path = self.tempdir.path
        dir_1 = os.path.join(temp_path, 'abc', 'fig', 'one', 'two', 'three')
        dir_2 = os.path.join(temp_path, 'abc', 'fig', 'one', 'two')
        dir_3 = os.path.join(temp_path, 'abc', 'fig')
        self.tempdir.makedir(dir_1)
        file_1 = self.tempdir.write(os.path.join(dir_1, 'my awesome cat.txt'),
                                    '')

        f1 = File(file_1)
        f1.move_to(dst_root_path=dir_2,
                   group=True,
                   by_extension=True,
                   group_folder_name='sample dir')
        with self.subTest(1):
            final_path = os.path.join(dir_2, 'sample dir', 'TXT',
                                      'my awesome cat.txt')
            compare(final_path, f1.path)
        f1.move_to(dst_root_path=dir_3,
                   group=True,
                   by_extension=True,
                   group_folder_name='sample dir')
        with self.subTest(2):
            final_path = os.path.join(dir_3, 'sample dir', 'TXT',
                                      'my awesome cat.txt')
            compare(final_path, f1.path)
Esempio n. 3
0
    def sort_files(self, src=None, send_message=None):
        """Move files in relation to their extensions and categories.

        This function runs according to the patterns of the sdir module.
        """
        source_path = src or self.src
        destination_path = self.dst or source_path
        string_pattern = self.search_string_pattern
        file_types = self.file_types
        glob_pattern = self.glob_pattern
        group_folder_name = self.group_folder_name

        string_glob_pattern = string_pattern + glob_pattern

        glob_files = (i for item in file_types for i in iglob(
            os.path.join(source_path, string_glob_pattern + item))
                      if os.path.isfile(i))
        send_message(through=['status', 'progress_text'],
                     msg='Searching for files...',
                     weight=1)

        for file_ in glob_files:
            file_instance = File(os.path.join(source_path, file_))

            initial_path = file_instance.path
            initial_name = file_instance.name
            last_modified = os.path.getmtime(initial_path)

            file_instance.move_to(destination_path,
                                  group=self.group,
                                  group_folder_name=group_folder_name,
                                  by_extension=self.by_extension)
            new_path = file_instance.path
            if initial_path != new_path:
                msg = 'Moving {} to {}'.format(file_instance.name, new_path)
                send_message(through=['status', 'progress_text'],
                             msg=msg,
                             weight=1)
                # Write to DB
                hash_path = hashlib.md5(
                    initial_path.encode('utf-8')).hexdigest()

                file_dict = {
                    'filename': initial_name,
                    'filepath_hash': hash_path,
                    'last_modified': datetime.fromtimestamp(last_modified)
                }
                path_dict = {'source': initial_path, 'destination': new_path}

                this_file_dict = {
                    initial_name: {
                        'file': file_dict,
                        'path': path_dict
                    }
                }

                self.database_dict.update(this_file_dict)
Esempio n. 4
0
    def test_returns_false_if_grouping_fails_group_true(self):
        dir_1 = self.tempdir.makedir('abc/fig/')
        dir_2 = self.tempdir.makedir('one/twp/')
        self.tempdir.makedir('some/dir/')
        file_1 = self.tempdir.write(
            os.path.join(dir_1, 'my awesome cat.txt'), '')
        self.tempdir.write(os.path.join(dir_1, 'my awesome cat.txt'), '')
        self.tempdir.write(os.path.join(dir_1, '1.jpeg'), '')

        f1 = File(file_1)
        f1.move_to(dst_root_path=dir_2, group=True, by_extension=False,
                   group_folder_name=None)
        with self.subTest(1):
            final_path = os.path.join(dir_2, 'document', 'my awesome cat.txt')
            compare(final_path, f1.path)
Esempio n. 5
0
    def test_returns_false_if_relocation_to_existing_child_folder_fails(self):
        dir_1 = self.tempdir.makedir('abc/fig/')
        dir_2 = self.tempdir.makedir('abc/fig/one/two')
        dir_3 = self.tempdir.makedir('abc/fig/one/two/three/')
        file_1 = self.tempdir.write(
            os.path.join(dir_1, 'my awesome cat.txt'), '')

        f1 = File(file_1)
        f1.move_to(dst_root_path=dir_2, group=True, by_extension=True,
                   group_folder_name='sample dir')
        with self.subTest(1):
            final_path = os.path.join(
                dir_2, 'sample dir', 'TXT', 'my awesome cat.txt')
            compare(final_path, f1.path)
        f1.move_to(dst_root_path=dir_3, group=True, by_extension=True,
                   group_folder_name='sample dir')
        with self.subTest(2):
            final_path = os.path.join(
                dir_3, 'sample dir', 'TXT', 'my awesome cat.txt')
            compare(final_path, f1.path)
Esempio n. 6
0
 def test_returns_false_if_file_moving_fails(self):
     dir_1 = self.tempdir.makedir('abc/fig/')
     dir_2 = self.tempdir.makedir('one/twp/')
     dir_3 = self.tempdir.makedir('some/dir/')
     file_ = self.tempdir.write(
         os.path.join(dir_1, 'my awesome cat.txt'), '')
     f = File(file_)
     f.move_to(dir_2, group=False)
     with self.subTest(1):
         compare(os.path.join(dir_2, 'TXT', 'my awesome cat.txt'), f.path)
     with self.subTest(2):
         compare(True, f.exists)
     f.move_to(dir_3, group=True, by_extension=True)
     with self.subTest(3):
         compare(os.path.join(dir_3, 'document',
                              'TXT', 'my awesome cat.txt'), f.path)
     with self.subTest(4):
         compare(True, f.exists)
     with self.subTest(5):
         compare(False, os.path.isfile(os.path.join(
             dir_2, 'TXT', 'my awesome cat.txt')))