33from functools import lru_cache
44
55from octue .exceptions import InvalidTagException
6- from octue .mixins import Filterable
7- from octue .resources .filter_containers import FilterSet
6+ from octue .mixins import Filterable , Serialisable
7+ from octue .resources .filter_containers import FilterList , FilterSet
8+ from octue .utils .encoders import OctueJSONEncoder
89
910
1011TAG_PATTERN = re .compile (r"^$|^[a-z0-9][a-z0-9:\-]*(?<![:-])$" )
@@ -28,8 +29,11 @@ def name(self):
2829 @property
2930 @lru_cache (maxsize = 1 )
3031 def subtags (self ):
31- """ Return the subtags of the tag as a new TagSet (e.g. TagSet({'a', 'b', 'c'}) for the Tag('a:b:c'). """
32- return TagSet ({Tag (subtag_name ) for subtag_name in (self .name .split (":" ))})
32+ """Return the subtags of the tag in order as a FilterList (e.g. FilterList(['a', 'b', 'c']) for Tag('a:b:c').
33+
34+ :return FilterList(Tag):
35+ """
36+ return FilterList (Tag (subtag_name ) for subtag_name in self .name .split (":" ))
3337
3438 def __eq__ (self , other ):
3539 if isinstance (other , str ):
@@ -85,7 +89,7 @@ def _clean(name):
8589 return cleaned_name
8690
8791
88- class TagSet :
92+ class TagSet ( Serialisable ) :
8993 """ Class to handle a set of tags as a string. """
9094
9195 _FILTERSET_ATTRIBUTE = "tags"
@@ -114,10 +118,6 @@ def __init__(self, tags=None, *args, **kwargs):
114118 "Tags must be expressed as a whitespace-delimited string or an iterable of strings or Tag instances."
115119 )
116120
117- def __str__ (self ):
118- """ Serialise tags to a sorted list string. """
119- return self .serialise ()
120-
121121 def __eq__ (self , other ):
122122 """ Does this TagSet have the same tags as another TagSet? """
123123 if not isinstance (other , TagSet ):
@@ -161,10 +161,35 @@ def any_tag_contains(self, value):
161161 """ Return True if any of the tags contains value. """
162162 return any (value in tag for tag in self )
163163
164- def serialise (self , to_string = True ):
165- """ Serialise tags to a sorted list string. """
166- serialised_tags = sorted (tag .name for tag in self )
164+ def filter (self , filter_name = None , filter_value = None ):
165+ """Filter the tags with the given filter for the given value.
166+
167+ :param str filter_name:
168+ :param any filter_value:
169+ :return octue.resources.filter_containers.FilterSet:
170+ """
171+ return self .tags .filter (filter_name = filter_name , filter_value = filter_value )
172+
173+ def serialise (self , to_string = False , ** kwargs ):
174+ """Serialise to a sorted list of tag names.
175+
176+ :param bool to_string:
177+ :return list|str:
178+ """
179+ string = json .dumps (
180+ sorted (tag .name for tag in self .tags ), cls = OctueJSONEncoder , sort_keys = True , indent = 4 , ** kwargs
181+ )
167182
168183 if to_string :
169- return str (serialised_tags )
170- return serialised_tags
184+ return string
185+
186+ return json .loads (string )
187+
188+ @classmethod
189+ def deserialise (cls , serialised_tagset ):
190+ """Deserialise from a sorted list of tag names.
191+
192+ :param list serialised_tagset:
193+ :return TagSet:
194+ """
195+ return cls (tags = serialised_tagset )
0 commit comments