<s> import contextlib <EOL> import hashlib <EOL> import logging <EOL> import os <EOL> import random <EOL> import sys <EOL> import time <EOL> logging . basicConfig ( level = logging . ERROR ) <EOL> self_dir = os . path . abspath ( os . path . dirname ( __file__ ) ) <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> sys . path . insert ( <NUM_LIT:0> , self_dir ) <EOL> from oslo_utils import uuidutils <EOL> from taskflow import engines <EOL> from taskflow . patterns import graph_flow as gf <EOL> from taskflow . patterns import linear_flow as lf <EOL> from taskflow . persistence import models <EOL> from taskflow import task <EOL> import example_utils <EOL> @ contextlib . contextmanager <EOL> def slow_down ( how_long = <NUM_LIT:0.5> ) : <EOL> try : <EOL> yield how_long <EOL> finally : <EOL> print ( "<STR_LIT>" ) <EOL> time . sleep ( how_long ) <EOL> def find_flow_detail ( backend , book_id , flow_id ) : <EOL> with contextlib . closing ( backend . get_connection ( ) ) as conn : <EOL> lb = conn . get_logbook ( book_id ) <EOL> return lb . find ( flow_id ) <EOL> class PrintText ( task . Task ) : <EOL> def __init__ ( self , print_what , no_slow = False ) : <EOL> content_hash = hashlib . md5 ( print_what . encode ( '<STR_LIT:utf-8>' ) ) . hexdigest ( ) [ <NUM_LIT:0> : <NUM_LIT:8> ] <EOL> super ( PrintText , self ) . __init__ ( name = "<STR_LIT>" % ( content_hash ) ) <EOL> self . _text = print_what <EOL> self . _no_slow = no_slow <EOL> def execute ( self ) : <EOL> if self . _no_slow : <EOL> print ( "<STR_LIT:->" * ( len ( self . _text ) ) ) <EOL> print ( self . _text ) <EOL> print ( "<STR_LIT:->" * ( len ( self . _text ) ) ) <EOL> else : <EOL> with slow_down ( ) : <EOL> print ( "<STR_LIT:->" * ( len ( self . _text ) ) ) <EOL> print ( self . _text ) <EOL> print ( "<STR_LIT:->" * ( len ( self . _text ) ) ) <EOL> class CreateSpecForVolumes ( task . Task ) : <EOL> def execute ( self ) : <EOL> volumes = [ ] <EOL> for i in range ( <NUM_LIT:0> , random . randint ( <NUM_LIT:1> , <NUM_LIT:10> ) ) : <EOL> volumes . append ( { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT:location>' : "<STR_LIT>" % ( i + <NUM_LIT:1> ) , <EOL> } ) <EOL> return volumes <EOL> class PrepareVolumes ( task . Task ) : <EOL> def execute ( self , volume_specs ) : <EOL> for v in volume_specs : <EOL> with slow_down ( ) : <EOL> print ( "<STR_LIT>" % ( v ) ) <EOL> with slow_down ( ) : <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" % ( v ) ) <EOL> flow = lf . Flow ( "<STR_LIT:root>" ) . add ( <EOL> PrintText ( "<STR_LIT>" , no_slow = True ) , <EOL> gf . Flow ( '<STR_LIT>' ) . add ( <EOL> CreateSpecForVolumes ( "<STR_LIT>" , provides = '<STR_LIT>' ) , <EOL> PrintText ( "<STR_LIT>" ) , <EOL> PrepareVolumes ( ) , <EOL> ) , <EOL> PrintText ( "<STR_LIT>" , no_slow = True ) ) <EOL> with example_utils . get_backend ( ) as backend : <EOL> try : <EOL> book_id , flow_id = sys . argv [ <NUM_LIT:2> ] . split ( "<STR_LIT:+>" , <NUM_LIT:1> ) <EOL> except ( IndexError , ValueError ) : <EOL> book_id = None <EOL> flow_id = None <EOL> if not all ( [ book_id , flow_id ] ) : <EOL> book = models . LogBook ( '<STR_LIT>' ) <EOL> flow_detail = models . FlowDetail ( "<STR_LIT:root>" , uuid = uuidutils . generate_uuid ( ) ) <EOL> book . add ( flow_detail ) <EOL> with contextlib . closing ( backend . get_connection ( ) ) as conn : <EOL> conn . save_logbook ( book ) <EOL> print ( "<STR_LIT>" % ( book . uuid , <EOL> flow_detail . uuid ) ) <EOL> print ( "<STR_LIT>" ) <EOL> else : <EOL> flow_detail = find_flow_detail ( backend , book_id , flow_id ) <EOL> engine = engines . load ( flow , <EOL> flow_detail = flow_detail , <EOL> backend = backend , engine = '<STR_LIT>' ) <EOL> engine . run ( ) </s>
