예제 #1
0
 def edit_property_for_videos(
     self,
     name: str,
     video_indices: List[int],
     values_to_add: list,
     values_to_remove: list,
 ) -> None:
     print("TO CHANGE", len(video_indices), video_indices)
     prop_type = self.get_prop_type(name)
     if prop_type.multiple:
         values_to_add = prop_type(values_to_add)
         values_to_remove = prop_type(values_to_remove)
     else:
         assert len(values_to_add) < 2
         values_to_add = [prop_type(value) for value in values_to_add]
         values_to_remove = {prop_type(value) for value in values_to_remove}
     video_indices = set(video_indices)
     for video_id in video_indices:
         video = self.__get_video_from_id(video_id)
         if prop_type.multiple:
             values = set(video.properties.get(prop_type.name, ()))
             values.difference_update(values_to_remove)
             values.update(values_to_add)
             if values:
                 video.properties[prop_type.name] = sorted(values)
             elif prop_type.name in video.properties:
                 del video.properties[prop_type.name]
         else:
             if (values_to_remove and prop_type.name in video.properties and
                     video.properties[prop_type.name] in values_to_remove):
                 del video.properties[prop_type.name]
             if values_to_add:
                 video.properties[prop_type.name] = values_to_add[0]
     self.save()
     self.notifier.notify(notifications.PropertiesModified([name]))
예제 #2
0
 def edit_property_value(self, videos: Iterable[Video], name: str,
                         old_values: list, new_value: object) -> bool:
     old_values = set(old_values)
     modified = False
     prop_type = self.get_prop_type(name)
     if prop_type.multiple:
         prop_type.validate(old_values)
         prop_type.validate([new_value])
         for video in videos:
             if name in video.properties and video.properties[name]:
                 new_values = set(video.properties[name])
                 len_before = len(new_values)
                 new_values = new_values - old_values
                 len_after = len(new_values)
                 if len_before > len_after:
                     new_values.add(new_value)
                     video.properties[name] = sorted(new_values)
                     modified = True
     else:
         for old_value in old_values:
             prop_type.validate(old_value)
         prop_type.validate(new_value)
         for video in videos:
             if name in video.properties and video.properties[
                     name] in old_values:
                 video.properties[name] = new_value
                 modified = True
     if modified:
         self.save()
         self.notifier.notify(notifications.PropertiesModified([name]))
     return modified
예제 #3
0
 def __del_prop_val(self, videos: Iterable[Video], name: str,
                    values: list) -> List[Video]:
     values = set(values)
     modified = []
     prop_type = self.get_prop_type(name)
     if prop_type.multiple:
         prop_type.validate(values)
         for video in videos:
             if name in video.properties and video.properties[name]:
                 new_values = set(video.properties[name])
                 len_before = len(new_values)
                 new_values = new_values - values
                 len_after = len(new_values)
                 if len_before > len_after:
                     video.properties[name] = sorted(new_values)
                     modified.append(video)
     else:
         for value in values:
             prop_type.validate(value)
         for video in videos:
             if name in video.properties and video.properties[
                     name] in values:
                 del video.properties[name]
                 modified.append(video)
     if modified:
         self.save()
         self.notifier.notify(notifications.PropertiesModified([name]))
     return modified
예제 #4
0
 def set_video_properties(self, video_id: int, properties):
     video = self.select_one("video", ["properties"], video_id=video_id)
     new_properties = {**video.properties, **properties}
     self.modify("video", [video_id], properties=new_properties)
     self.save()
     self.notifier.notify(notifications.PropertiesModified(
         list(properties)))
예제 #5
0
 def classifier_select_group(self, group_id):
     prop_name = self.provider.grouping_layer.get_grouping().field
     path = self.provider.classifier_layer.get_path()
     value = self.provider.classifier_layer.get_group_value(group_id)
     new_path = path + [value]
     self.provider.classifier_layer.set_path(new_path)
     self.provider.group_layer.set_group_id(0)
     self.database.notifier.notify(notifications.PropertiesModified([prop_name]))
예제 #6
0
 def prop_to_uppercase(self, prop_name):
     prop_type = self.get_prop_type(prop_name)
     assert prop_type.type is str
     for video in self.get_videos("readable"):
         if prop_name in video.properties:
             if prop_type.multiple:
                 video.properties[prop_name] = sorted(
                     set(value.strip().upper()
                         for value in video.properties[prop_name]))
             else:
                 video.properties[prop_name] = (
                     video.properties[prop_name].strip().upper())
     self.save()
     self.notifier.notify(notifications.PropertiesModified([prop_name]))
예제 #7
0
 def fill_property_with_terms(self,
                              videos: Iterable[Video],
                              prop_name: str,
                              only_empty=False) -> None:
     prop_type = self.get_prop_type(prop_name)
     assert prop_type.multiple
     assert prop_type.type is str
     for video in videos:
         if only_empty and video.properties.get(prop_name, None):
             continue
         values = video.terms(as_set=True)
         values.update(video.properties.get(prop_name, ()))
         video.properties[prop_name] = prop_type(values)
     self.save()
     self.notifier.notify(notifications.PropertiesModified([prop_name]))
예제 #8
0
 def move_property_value(self, videos: Iterable[Video], old_name: str,
                         values: list, new_name: str) -> None:
     assert len(values) == 1, values
     value = values[0]
     prop_type = self.get_prop_type(new_name)
     prop_type.validate([value] if prop_type.multiple else value)
     videos = self.__del_prop_val(videos, old_name, [value])
     if prop_type.multiple:
         for video in videos:
             new_values = set(video.properties.get(new_name, ()))
             new_values.add(value)
             video.properties[new_name] = sorted(new_values)
     else:
         for video in videos:
             video.properties[new_name] = value
     if videos:
         self.save()
         self.notifier.notify(
             notifications.PropertiesModified([old_name, new_name]))
예제 #9
0
    def move_concatenated_prop_val(self, videos: Iterable[Video], path: list,
                                   from_property: str,
                                   to_property: str) -> int:
        from_prop_type = self.get_prop_type(from_property)
        to_prop_type = self.get_prop_type(to_property)
        assert from_prop_type.multiple
        assert to_prop_type.type is str
        from_prop_type.validate(path)
        new_value = " ".join(str(value) for value in path)
        to_prop_type.validate(
            [new_value] if to_prop_type.multiple else new_value)

        modified = []
        path_set = set(path)
        for video in videos:
            if from_property in video.properties and video.properties[
                    from_property]:
                new_values = set(video.properties[from_property])
                len_before = len(new_values)
                new_values = new_values - path_set
                if len_before == len(new_values) + len(path):
                    video.properties[from_property] = sorted(new_values)
                    modified.append(video)

        if to_prop_type.multiple:
            for video in modified:
                new_values = set(video.properties.get(to_property, ()))
                new_values.add(new_value)
                video.properties[to_property] = sorted(new_values)
        else:
            for video in modified:
                video.properties[to_property] = new_value

        if modified:
            self.save()
            self.notifier.notify(
                notifications.PropertiesModified([from_property, to_property]))

        return len(modified)
예제 #10
0
 def classifier_back(self):
     prop_name = self.provider.grouping_layer.get_grouping().field
     path = self.provider.classifier_layer.get_path()
     self.provider.classifier_layer.set_path(path[:-1])
     self.provider.group_layer.set_group_id(0)
     self.database.notifier.notify(notifications.PropertiesModified([prop_name]))