Add edit_metadata method in collection
This commit is contained in:
		
							parent
							
								
									e0fc31e543
								
							
						
					
					
						commit
						b0969f62af
					
				@ -17,7 +17,7 @@ import inquirer
 | 
				
			|||||||
from ordigi import LOG
 | 
					from ordigi import LOG
 | 
				
			||||||
from ordigi.config import Config
 | 
					from ordigi.config import Config
 | 
				
			||||||
from ordigi.database import Sqlite
 | 
					from ordigi.database import Sqlite
 | 
				
			||||||
from ordigi.media import Medias
 | 
					from ordigi.media import Medias, WriteExif
 | 
				
			||||||
from ordigi.images import Image, Images
 | 
					from ordigi.images import Image, Images
 | 
				
			||||||
from ordigi import request
 | 
					from ordigi import request
 | 
				
			||||||
from ordigi.summary import Summary
 | 
					from ordigi.summary import Summary
 | 
				
			||||||
@ -258,7 +258,7 @@ class CollectionDb:
 | 
				
			|||||||
    def __init__(self, root):
 | 
					    def __init__(self, root):
 | 
				
			||||||
        self.sqlite = Sqlite(root)
 | 
					        self.sqlite = Sqlite(root)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def _format_row_data(self, table, metadata):
 | 
					    def _set_row_data(self, table, metadata):
 | 
				
			||||||
        row_data = {}
 | 
					        row_data = {}
 | 
				
			||||||
        for title in self.sqlite.tables[table]['header']:
 | 
					        for title in self.sqlite.tables[table]['header']:
 | 
				
			||||||
            key = utils.camel2snake(title)
 | 
					            key = utils.camel2snake(title)
 | 
				
			||||||
@ -268,10 +268,10 @@ class CollectionDb:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    def add_file_data(self, metadata):
 | 
					    def add_file_data(self, metadata):
 | 
				
			||||||
        """Save metadata informations to db"""
 | 
					        """Save metadata informations to db"""
 | 
				
			||||||
        loc_values = self._format_row_data('location', metadata)
 | 
					        loc_values = self._set_row_data('location', metadata)
 | 
				
			||||||
        metadata['location_id'] = self.sqlite.add_row('location', loc_values)
 | 
					        metadata['location_id'] = self.sqlite.add_row('location', loc_values)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        row_data = self._format_row_data('metadata', metadata)
 | 
					        row_data = self._set_row_data('metadata', metadata)
 | 
				
			||||||
        self.sqlite.add_row('metadata', row_data)
 | 
					        self.sqlite.add_row('metadata', row_data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -797,7 +797,7 @@ class Collection(SortMedias):
 | 
				
			|||||||
            metadata['file_path'] = os.path.relpath(file_path, self.root)
 | 
					            metadata['file_path'] = os.path.relpath(file_path, self.root)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            self.db.add_file_data(metadata)
 | 
					            self.db.add_file_data(metadata)
 | 
				
			||||||
            self.summary.append('update', file_path)
 | 
					            self.summary.append('update', True, file_path)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return self.summary
 | 
					        return self.summary
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -1114,7 +1114,7 @@ class Collection(SortMedias):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        return self.summary
 | 
					        return self.summary
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def fill_metadata(self, path, key, loc=None, overwrite=False):
 | 
					    def edit_metadata(self, path, key, loc=None, overwrite=False):
 | 
				
			||||||
        """Fill metadata and exif data for given key"""
 | 
					        """Fill metadata and exif data for given key"""
 | 
				
			||||||
        self._init_check_db()
 | 
					        self._init_check_db()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -1135,7 +1135,11 @@ class Collection(SortMedias):
 | 
				
			|||||||
        paths = self.paths.get_paths_list(path)
 | 
					        paths = self.paths.get_paths_list(path)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        for file_path in paths:
 | 
					        for file_path in paths:
 | 
				
			||||||
            media = self.medias.get_media(file_path, self.root, loc)
 | 
					            media = self.medias.get_media(file_path, self.root)
 | 
				
			||||||
 | 
					            media.get_metadata(
 | 
				
			||||||
 | 
					                self.root, loc, self.db.sqlite, False
 | 
				
			||||||
 | 
					            )
 | 
				
			||||||
 | 
					            media.metadata['file_path'] = os.path.relpath(file_path, self.root)
 | 
				
			||||||
            print()
 | 
					            print()
 | 
				
			||||||
            value = media.metadata[key]
 | 
					            value = media.metadata[key]
 | 
				
			||||||
            if overwrite or not value:
 | 
					            if overwrite or not value:
 | 
				
			||||||
@ -1152,20 +1156,27 @@ class Collection(SortMedias):
 | 
				
			|||||||
                # Validate value
 | 
					                # Validate value
 | 
				
			||||||
                if key in ('date_original', 'date_created', 'date_modified'):
 | 
					                if key in ('date_original', 'date_created', 'date_modified'):
 | 
				
			||||||
                    # Check date format
 | 
					                    # Check date format
 | 
				
			||||||
                    value = str(media.get_date_format(answer['value']))
 | 
					                    value = media.get_date_format(answer['value'])
 | 
				
			||||||
                else:
 | 
					                else:
 | 
				
			||||||
                    if not answer[key].isalnum():
 | 
					                    if not answer[key].isalnum():
 | 
				
			||||||
                        print("Invalid entry, use alphanumeric chars")
 | 
					                        print("Invalid entry, use alphanumeric chars")
 | 
				
			||||||
                        value = inquirer.prompt(prompt, theme=self.theme)
 | 
					                        value = inquirer.prompt(prompt, theme=self.theme)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                # print(f"{key}='{value}'")
 | 
					                result = False
 | 
				
			||||||
 | 
					                if value:
 | 
				
			||||||
                media.metadata[key] = value
 | 
					                    media.metadata[key] = value
 | 
				
			||||||
                # Update database
 | 
					                    # Update database
 | 
				
			||||||
                self.db.add_file_data(media.metadata)
 | 
					                    self.db.add_file_data(media.metadata)
 | 
				
			||||||
                # Update exif data
 | 
					                    # Update exif data
 | 
				
			||||||
                media.set_key_values(key, value)
 | 
					                    exif = WriteExif(
 | 
				
			||||||
 | 
					                        file_path,
 | 
				
			||||||
                self.summary.append('update', False, file_path)
 | 
					                        media.metadata,
 | 
				
			||||||
 | 
					                        ignore_tags=self.opt['Exif']['ignore_tags'],
 | 
				
			||||||
 | 
					                    )
 | 
				
			||||||
 | 
					                    result = exif.set_key_values(key, value)
 | 
				
			||||||
 | 
					                if result:
 | 
				
			||||||
 | 
					                    self.summary.append('update', True, file_path)
 | 
				
			||||||
 | 
					                else:
 | 
				
			||||||
 | 
					                    self.summary.append('update', False, file_path)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return self.summary
 | 
					        return self.summary
 | 
				
			||||||
 | 
				
			|||||||
@ -184,7 +184,7 @@ class WriteExif(ExifMetadata):
 | 
				
			|||||||
        :returns: value (str)
 | 
					        :returns: value (str)
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        # TODO overwrite mode check if fail
 | 
					        # TODO overwrite mode check if fail
 | 
				
			||||||
        return ExifTool(self.file_path).setvalue(tag, value)
 | 
					        return ExifTool(self.file_path, overwrite=True).setvalue(tag, value)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def set_key_values(self, key, value):
 | 
					    def set_key_values(self, key, value):
 | 
				
			||||||
        """Set tags values for given key"""
 | 
					        """Set tags values for given key"""
 | 
				
			||||||
 | 
				
			|||||||
@ -70,7 +70,10 @@ class TestOrdigi:
 | 
				
			|||||||
        ]
 | 
					        ]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        for command in commands:
 | 
					        for command in commands:
 | 
				
			||||||
            self.assert_cli(command, ['not_exist'], state=1)
 | 
					            if command.name == 'edit':
 | 
				
			||||||
 | 
					                self.assert_cli(command, ['-k', 'date_original', 'not_exist'], state=1)
 | 
				
			||||||
 | 
					            else:
 | 
				
			||||||
 | 
					                self.assert_cli(command, ['not_exist'], state=1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.assert_cli(cli._clone, ['not_exist'], state=2)
 | 
					        self.assert_cli(cli._clone, ['not_exist'], state=2)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -202,6 +205,9 @@ class TestOrdigi:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        paths = (str(self.src_path),)
 | 
					        paths = (str(self.src_path),)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # Workaround
 | 
				
			||||||
 | 
					        self.assert_cli(cli._update, paths)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.assert_cli(cli._compare, paths)
 | 
					        self.assert_cli(cli._compare, paths)
 | 
				
			||||||
        self.assert_options(cli._compare, bool_options, arg_options, paths)
 | 
					        self.assert_options(cli._compare, bool_options, arg_options, paths)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -251,24 +251,20 @@ class TestCollection:
 | 
				
			|||||||
        # Summary is created and there is no errors
 | 
					        # Summary is created and there is no errors
 | 
				
			||||||
        assert not summary.errors
 | 
					        assert not summary.errors
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @pytest.mark.skip()
 | 
					    # @pytest.mark.skip()
 | 
				
			||||||
    def test_fill_data(self, tmp_path, monkeypatch):
 | 
					    def test_edit_metadata(self, tmp_path, monkeypatch):
 | 
				
			||||||
        path = tmp_path / 'collection'
 | 
					        path = tmp_path / 'collection'
 | 
				
			||||||
        shutil.copytree(self.src_path, path)
 | 
					        shutil.copytree(self.src_path, path)
 | 
				
			||||||
        collection = Collection(path)
 | 
					        collection = Collection(path, {'cache': False})
 | 
				
			||||||
        # loc = GeoLocation()
 | 
					        # loc = GeoLocation()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#         def mockreturn(prompt, theme):
 | 
					        def mockreturn(prompt, theme):
 | 
				
			||||||
#             return {'value': '03-12-2021 08:12:35'}
 | 
					            return {'value': '03-12-2021 08:12:35'}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#         monkeypatch.setattr(inquirer, 'prompt', mockreturn)
 | 
					        monkeypatch.setattr(inquirer, 'prompt', mockreturn)
 | 
				
			||||||
#         collection.fill_data(path, 'date_original')
 | 
					 | 
				
			||||||
#         # check if db value is set
 | 
					 | 
				
			||||||
#         import ipdb; ipdb.set_trace()
 | 
					 | 
				
			||||||
#         date = collection.db.get_metadata_data('test_exif/invalid.invalid',
 | 
					 | 
				
			||||||
#                 'DateOriginal')
 | 
					 | 
				
			||||||
#         assert date == '2021-03-12 08:12:35'
 | 
					 | 
				
			||||||
        # Check if exif value is set
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        collection.edit_metadata(path, 'date_original', overwrite=True)
 | 
				
			||||||
        collection.fill_data(path, 'date_original', edit=True)
 | 
					        # check if db value is set
 | 
				
			||||||
 | 
					        date = collection.db.sqlite.get_metadata_data('test_exif/photo.rw2',
 | 
				
			||||||
 | 
					            'DateOriginal')
 | 
				
			||||||
 | 
					        assert date == '2021-03-12 08:12:35'
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user