SHARE
TWEET

Untitled

a guest Oct 23rd, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from collections import namedtuple
  2. from collections.abc import Iterable
  3. import asyncio
  4. from uuid import uuid4
  5.  
  6.  
  7. class Autoprotocol(object):
  8.     def __init__(self):
  9.         super().__init__()
  10.         self.toPerform = []
  11.         self.eventLoop = asyncio.new_event_loop()
  12.         self.firstAwaitSentinal = self.eventLoop.create_task(
  13.             self._firstAwaitSentinal())
  14.         self.assignments = {}
  15.  
  16.     def DEFINE(self, *taskGroups):
  17.         tasks = Autoprotocol.tasksFromGroups(taskGroups)
  18.         for task in tasks:
  19.             self.toPerform.append(
  20.                 task.perform(self.eventLoop)
  21.             )
  22.         return tasks
  23.  
  24.     def exec(self):
  25.         asyncio.set_event_loop(self.eventLoop)
  26.         self.toPerform.insert(0, self.firstAwaitSentinal)
  27.         while len(self.toPerform) > 0:
  28.             tasks, self.toPerform = self.toPerform, []
  29.             self.eventLoop.run_until_complete(asyncio.gather(*tasks))
  30.  
  31.     async def _firstAwaitSentinal(self):
  32.         print("INTITAL")
  33.         await asyncio.sleep(0)
  34.         print("END INITIAL")
  35.  
  36.     def DECLARE(self, **kwargs):
  37.         self.DEFINE(*kwargs.values())
  38.         for key in kwargs:
  39.             self.assignments[key] = kwargs[key]
  40.  
  41.     def USE(self, key):
  42.         if key not in self.assignments:
  43.             raise ValueError("'"+str(key)+"' is not assigned a value")
  44.         return self.assignments[key]
  45.  
  46.     def IF(self, condition):
  47.         return Autoprotocol.IfThenList(condition)
  48.  
  49.     @classmethod
  50.     def tasksFromGroups(cls, taskGroups):
  51.         for el in taskGroups:
  52.             if isinstance(el, Iterable) and not isinstance(el, (str, bytes)):
  53.                 yield from Autoprotocol.tasksFromGroups(el)
  54.             else:
  55.                 if el != None and isinstance(el, Autoprotocol.TaskFactory):
  56.                     yield el
  57.  
  58.     @classmethod
  59.     def Condition(cls, func):
  60.         def taskFactoryFactory(*args, **kwargs):
  61.             return Autoprotocol.ConditionFactory(func, *args, **kwargs)
  62.         return taskFactoryFactory
  63.  
  64.     @classmethod
  65.     def Instruction(cls, typeName):
  66.         def decorator(func):
  67.             def taskFactoryFactory(*args, **kwargs):
  68.                 return Autoprotocol.TaskFactory(typeName, func, *args, **kwargs)
  69.             return taskFactoryFactory
  70.         return decorator
  71.  
  72.     class IfThenList(list):
  73.         def __init__(self,condition):
  74.             super().__init__()
  75.             self.condition = condition
  76.             self.append(condition)
  77.  
  78.         def THEN(self,*taskGroups):
  79.             tasks = Autoprotocol.tasksFromGroups(taskGroups)
  80.             for task in tasks:
  81.                 task.addCondition(self.condition, True)
  82.                 self.append(task)
  83.             return self
  84.        
  85.         def ELSE(self,*taskGroups):
  86.             tasks = Autoprotocol.tasksFromGroups(taskGroups)
  87.             for task in tasks:
  88.                 task.addCondition(self.condition, False)
  89.                 self.append(task)
  90.             return self
  91.  
  92.     class TaskFactory(object):
  93.         def __init__(self, name, func, *args, **kwargs):
  94.             super().__init__()
  95.             self.typeName = name
  96.             self.id = str(uuid4())
  97.             self.func = func
  98.             self.args = args
  99.             self.kwargs = kwargs
  100.             self.lastPerformance = None
  101.             self.conditions = []
  102.             print("DECLARE! "+str(self))
  103.             for l in (args, kwargs.values()):
  104.                 for arg in l:
  105.                     if isinstance(arg, Autoprotocol.TaskFactory):
  106.                         print("DEPENDS! "+str(self)+" ==> "+str(arg))
  107.  
  108.         def __str__(self):
  109.             return self.typeName+"::"+self.func.__name__+"::"+self.id
  110.  
  111.         def perform(self, eventLoop):
  112.             self.lastPerformance = eventLoop.create_task(self._run())
  113.             return self.lastPerformance
  114.  
  115.         def addCondition(self, condition, ifTrue):
  116.             self.conditions.append(condition)
  117.             symbol = " --> " if ifTrue else " -->! "
  118.             print("CONDITION! "+str(condition)+symbol+str(self))
  119.  
  120.         def __await__(self):
  121.             return self.lastPerformance.__await__()
  122.  
  123.         async def _run(self):
  124.             I = Autoprotocol.TaskFactory.Informer("INFORM! "+str(self))
  125.             r = await self.func(I, *self.args, **self.kwargs)
  126.             await asyncio.sleep(3)
  127.             return r
  128.  
  129.         class Informer():
  130.             def __init__(self, name):
  131.                 super().__init__()
  132.                 self.name = name
  133.  
  134.             def __getattr__(self, attr):
  135.                 def p(x=""):
  136.                     print(self.name, "--", attr, ":", x)
  137.                 return p
  138.  
  139.     class ConditionFactory(TaskFactory):
  140.         def __init__(self, func, *args, **kwargs):
  141.             async def condition(*args, **kwargs):
  142.                 r = await func(*args, **kwargs)
  143.                 print("DECIDED! "+str(self)+" "+str(r))
  144.                 return r
  145.             condition.__name__ = func.__name__
  146.             super().__init__("CONDITION", condition, *args, **kwargs)
  147.  
  148.  
  149. Autoprotocol.Container = namedtuple(
  150.     "Container", ["name", "id", "cont_type", "discard"])
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top