<s> import json <EOL> import logging <EOL> import os <EOL> import sys <EOL> import time <EOL> logging . basicConfig ( level = logging . ERROR ) <EOL> top_dir = os . path . abspath ( os . path . join ( os . path . dirname ( __file__ ) , <EOL> os . pardir , <EOL> os . pardir ) ) <EOL> sys . path . insert ( <NUM_LIT:0> , top_dir ) <EOL> from oslo_utils import uuidutils <EOL> from taskflow import engines <EOL> from taskflow . listeners import printing <EOL> from taskflow . patterns import graph_flow as gf <EOL> from taskflow . patterns import linear_flow as lf <EOL> from taskflow import task <EOL> from taskflow . utils import misc <EOL> class DB ( object ) : <EOL> def query ( self , sql ) : <EOL> print ( "<STR_LIT>" % ( sql ) ) <EOL> class UrlCaller ( object ) : <EOL> def __init__ ( self ) : <EOL> self . _send_time = <NUM_LIT:0.5> <EOL> self . _chunks = <NUM_LIT> <EOL> def send ( self , url , data , status_cb = None ) : <EOL> sleep_time = float ( self . _send_time ) / self . _chunks <EOL> for i in range ( <NUM_LIT:0> , len ( data ) ) : <EOL> time . sleep ( sleep_time ) <EOL> if status_cb : <EOL> status_cb ( float ( i ) / len ( data ) ) <EOL> class ResourceFetcher ( object ) : <EOL> def __init__ ( self ) : <EOL> self . _db_handle = None <EOL> self . _url_handle = None <EOL> @ property <EOL> def db_handle ( self ) : <EOL> if self . _db_handle is None : <EOL> self . _db_handle = DB ( ) <EOL> return self . _db_handle <EOL> @ property <EOL> def url_handle ( self ) : <EOL> if self . _url_handle is None : <EOL> self . _url_handle = UrlCaller ( ) <EOL> return self . _url_handle <EOL> class ExtractInputRequest ( task . Task ) : <EOL> def __init__ ( self , resources ) : <EOL> super ( ExtractInputRequest , self ) . __init__ ( provides = "<STR_LIT>" ) <EOL> self . _resources = resources <EOL> def execute ( self , request ) : <EOL> return { <EOL> '<STR_LIT:user>' : request . user , <EOL> '<STR_LIT>' : misc . as_int ( request . id ) , <EOL> '<STR_LIT>' : uuidutils . generate_uuid ( ) , <EOL> } <EOL> class MakeDBEntry ( task . Task ) : <EOL> def __init__ ( self , resources ) : <EOL> super ( MakeDBEntry , self ) . __init__ ( ) <EOL> self . _resources = resources <EOL> def execute ( self , parsed_request ) : <EOL> db_handle = self . _resources . db_handle <EOL> db_handle . query ( "<STR_LIT>" % ( parsed_request ) ) <EOL> def revert ( self , result , parsed_request ) : <EOL> db_handle = self . _resources . db_handle <EOL> db_handle . query ( "<STR_LIT>" % ( parsed_request ) ) <EOL> class ActivateDriver ( task . Task ) : <EOL> def __init__ ( self , resources ) : <EOL> super ( ActivateDriver , self ) . __init__ ( provides = '<STR_LIT>' ) <EOL> self . _resources = resources <EOL> self . _url = "<STR_LIT>" <EOL> def execute ( self , parsed_request ) : <EOL> print ( "<STR_LIT>" % ( self . _url ) ) <EOL> url_sender = self . _resources . url_handle <EOL> url_sender . send ( self . _url , json . dumps ( parsed_request ) , <EOL> status_cb = self . update_progress ) <EOL> return self . _url <EOL> def update_progress ( self , progress , ** kwargs ) : <EOL> super ( ActivateDriver , self ) . update_progress ( progress , ** kwargs ) <EOL> print ( "<STR_LIT>" % ( self . name , progress * <NUM_LIT:100> ) ) <EOL> class DeclareSuccess ( task . Task ) : <EOL> def execute ( self , sent_to ) : <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" % ( sent_to ) ) <EOL> class DummyUser ( object ) : <EOL> def __init__ ( self , user , id_ ) : <EOL> self . user = user <EOL> self . id = id_ <EOL> resources = ResourceFetcher ( ) <EOL> flow = lf . Flow ( "<STR_LIT>" ) <EOL> flow . add ( ExtractInputRequest ( resources ) , MakeDBEntry ( resources ) ) <EOL> sub_flow = gf . Flow ( "<STR_LIT>" ) <EOL> sub_flow . add ( ActivateDriver ( resources ) , DeclareSuccess ( ) ) <EOL> flow . add ( sub_flow ) <EOL> store = { <EOL> '<STR_LIT>' : DummyUser ( user = "<STR_LIT>" , id_ = "<STR_LIT>" ) , <EOL> } <EOL> eng = engines . load ( flow , engine = '<STR_LIT>' , store = store ) <EOL> with printing . PrintingListener ( eng ) : <EOL> eng . run ( ) </s>
