Preserve order of tags when cleaning

This commit is contained in:
Miroslav Kovar 2019-10-03 15:44:10 +02:00 committed by Lex Berezhny
parent 7cb03dde44
commit 3c96fda59b
3 changed files with 10 additions and 15 deletions

View file

@ -540,17 +540,11 @@ class TagList(BaseMessageList[str]):
__slots__ = () __slots__ = ()
item_class = str item_class = str
@classmethod
def create_normalized(cls, message):
message[:] = clean_tags(message)
return cls(message)
def append(self, tag: str): def append(self, tag: str):
tag = normalize_tag(tag) tag = normalize_tag(tag)
if tag not in self._message: if tag and tag not in self.message:
self._message.append(tag) self.message.append(tag)
def extend(self, tags: List[str]): def extend(self, tags: List[str]):
tags = clean_tags(tags) for tag in tags:
tags = list(set(tags).difference(set(self._message))) self.append(tag)
self._message.extend(tags)

View file

@ -167,10 +167,6 @@ class BaseClaim:
def tags(self) -> List[str]: def tags(self) -> List[str]:
return TagList(self.claim.message.tags) return TagList(self.claim.message.tags)
@tags.setter
def tags(self, tags: List[str]):
self.claim.message.tags = TagList.create_normalized(tags)
@property @property
def languages(self) -> LanguageList: def languages(self) -> LanguageList:
return LanguageList(self.claim.message.languages) return LanguageList(self.claim.message.languages)

View file

@ -10,4 +10,9 @@ def normalize_tag(tag: str):
def clean_tags(tags: List[str]): def clean_tags(tags: List[str]):
return [tag for tag in set(normalize_tag(tag) for tag in tags) if tag] clean = []
for ind, tag in enumerate(tags):
norm_tag = normalize_tag(tag)
if norm_tag and norm_tag not in clean[:ind]:
clean.append(norm_tag)
return clean