Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import namedtuple
- from collections.abc import Iterable
- import asyncio
- from uuid import uuid4
- class Autoprotocol(object):
- def __init__(self):
- super().__init__()
- self.toPerform = []
- self.eventLoop = asyncio.new_event_loop()
- self.firstAwaitSentinal = self.eventLoop.create_task(
- self._firstAwaitSentinal())
- self.assignments = {}
- def DEFINE(self, *taskGroups):
- tasks = Autoprotocol.tasksFromGroups(taskGroups)
- for task in tasks:
- self.toPerform.append(
- task.perform(self.eventLoop)
- )
- return tasks
- def exec(self):
- asyncio.set_event_loop(self.eventLoop)
- self.toPerform.insert(0, self.firstAwaitSentinal)
- while len(self.toPerform) > 0:
- tasks, self.toPerform = self.toPerform, []
- self.eventLoop.run_until_complete(asyncio.gather(*tasks))
- async def _firstAwaitSentinal(self):
- print("INTITAL")
- await asyncio.sleep(0)
- print("END INITIAL")
- def DECLARE(self, **kwargs):
- self.DEFINE(*kwargs.values())
- for key in kwargs:
- self.assignments[key] = kwargs[key]
- def USE(self, key):
- if key not in self.assignments:
- raise ValueError("'"+str(key)+"' is not assigned a value")
- return self.assignments[key]
- def IF(self, condition):
- return Autoprotocol.IfThenList(condition)
- @classmethod
- def tasksFromGroups(cls, taskGroups):
- for el in taskGroups:
- if isinstance(el, Iterable) and not isinstance(el, (str, bytes)):
- yield from Autoprotocol.tasksFromGroups(el)
- else:
- if el != None and isinstance(el, Autoprotocol.TaskFactory):
- yield el
- @classmethod
- def Condition(cls, func):
- def taskFactoryFactory(*args, **kwargs):
- return Autoprotocol.ConditionFactory(func, *args, **kwargs)
- return taskFactoryFactory
- @classmethod
- def Instruction(cls, typeName):
- def decorator(func):
- def taskFactoryFactory(*args, **kwargs):
- return Autoprotocol.TaskFactory(typeName, func, *args, **kwargs)
- return taskFactoryFactory
- return decorator
- class IfThenList(list):
- def __init__(self,condition):
- super().__init__()
- self.condition = condition
- self.append(condition)
- def THEN(self,*taskGroups):
- tasks = Autoprotocol.tasksFromGroups(taskGroups)
- for task in tasks:
- task.addCondition(self.condition, True)
- self.append(task)
- return self
- def ELSE(self,*taskGroups):
- tasks = Autoprotocol.tasksFromGroups(taskGroups)
- for task in tasks:
- task.addCondition(self.condition, False)
- self.append(task)
- return self
- class TaskFactory(object):
- def __init__(self, name, func, *args, **kwargs):
- super().__init__()
- self.typeName = name
- self.id = str(uuid4())
- self.func = func
- self.args = args
- self.kwargs = kwargs
- self.lastPerformance = None
- self.conditions = []
- print("DECLARE! "+str(self))
- for l in (args, kwargs.values()):
- for arg in l:
- if isinstance(arg, Autoprotocol.TaskFactory):
- print("DEPENDS! "+str(self)+" ==> "+str(arg))
- def __str__(self):
- return self.typeName+"::"+self.func.__name__+"::"+self.id
- def perform(self, eventLoop):
- self.lastPerformance = eventLoop.create_task(self._run())
- return self.lastPerformance
- def addCondition(self, condition, ifTrue):
- self.conditions.append(condition)
- symbol = " --> " if ifTrue else " -->! "
- print("CONDITION! "+str(condition)+symbol+str(self))
- def __await__(self):
- return self.lastPerformance.__await__()
- async def _run(self):
- I = Autoprotocol.TaskFactory.Informer("INFORM! "+str(self))
- r = await self.func(I, *self.args, **self.kwargs)
- await asyncio.sleep(3)
- return r
- class Informer():
- def __init__(self, name):
- super().__init__()
- self.name = name
- def __getattr__(self, attr):
- def p(x=""):
- print(self.name, "--", attr, ":", x)
- return p
- class ConditionFactory(TaskFactory):
- def __init__(self, func, *args, **kwargs):
- async def condition(*args, **kwargs):
- r = await func(*args, **kwargs)
- print("DECIDED! "+str(self)+" "+str(r))
- return r
- condition.__name__ = func.__name__
- super().__init__("CONDITION", condition, *args, **kwargs)
- Autoprotocol.Container = namedtuple(
- "Container", ["name", "id", "cont_type", "discard"])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement