[docs]classObject:""" A class to represent an object in a workflow. A object is a file, stored either locally or remotely. :param str handle: The handle of the object. """def__init__(self,handle:str):""" Create a new object. :param str handle: The handle of the object. """self.handle=handledef__str__(self):returnf"<Object {self.handle}>"def__repr__(self):returnf"<Object {self.handle}>"
[docs]defreplace_templates(self,replacements:dict[str,str]):""" Replace strings in the object with the given replacements. :param dict[str, str] replacements: The replacements to make. """forkey,valueinreplacements.items():ifkeyinself.handle:self.handle=self.handle.replace(key,value)
[docs]classObjectsManager:""" A class to manage objects in a workflow. Allows creation, retrieval, and management of objects. """def__init__(self):self.objects={}
[docs]defcreate_object(self,handle:str)->Object:""" Create and return a new object. :param str handle: The handle of the object. :return: The created object. """obj=Object(handle)self.objects[handle]=objreturnobj
[docs]defadd_object(self,obj:Object):""" Add an object to the manager. :param Object obj: The object to add. """self.objects[obj.handle]=obj
[docs]defget_object(self,handle:str)->Object:""" Get an object by its handle. :param str handle: The handle of the object. """returnself.objects[handle]
[docs]defget_or_create_object(self,handle:str)->Object:""" Get an object by its handle, creating it if it does not exist. :param str handle: The handle of the object. """ifhandlenotinself.objects:returnself.create_object(handle)returnself.get_object(handle)
[docs]deffind_by_regex_in_objects(self,regex:str,return_group:int)->list[str]:""" Find all occurrences of a regex in the task. :param regex: The regex to search for. :param return_group: The group to return. :return: A list of matches. """res=[]forobjinself.objects.values():match=re.search(regex,obj.handle)ifmatch:res.append(match.group(return_group))returnres
[docs]classObjectList:""" A class to represent a list of objects in a workflow. """def__init__(self):self.objects=[]
[docs]defappend(self,obj:Object):""" Append an object to the list. :param Object obj: The object to append. """self.objects.append(obj)
[docs]defextend(self,objects:list[Object]):""" Extend the list with the given objects. :param list[Object] objects: The objects to extend the list with. """self.objects.extend(objects)
def__getitem__(self,index:int)->Object:""" Get an object by its index. :param int index: The index of the object. :return: The object at the given index. """returnself.objects[index]def__len__(self)->int:""" Get the length of the list. :return: The length of the list. """returnlen(self.objects)def__str__(self):""" Get the string representation of the list. :return: The string representation of the list. """returnf"<ObjectList {self.objects}>"
[docs]defunion(self,other:"ObjectList"):""" Union the list with another list of objects. :param ObjectList other: The other list to union with. """objects={}forobjinself.objects:objects[obj.handle]=objforobjinother.objects:ifobj.handlenotinobjects:objects[obj.handle]=objself.objects=list(objects.values())