<s> from __future__ import print_function <EOL> from __future__ import unicode_literals <EOL> import time <EOL> import warnings <EOL> from tdclient . model import Model <EOL> class Schema ( object ) : <EOL> """<STR_LIT>""" <EOL> class Field ( object ) : <EOL> def __init__ ( self , name , type ) : <EOL> self . _name = name <EOL> self . _type = type <EOL> @ property <EOL> def name ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _name <EOL> @ property <EOL> def type ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _type <EOL> def __init__ ( self , fields = None ) : <EOL> fields = [ ] if fields is None else fields <EOL> self . _fields = fields <EOL> @ property <EOL> def fields ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _fields <EOL> def add_field ( self , name , type ) : <EOL> """<STR_LIT>""" <EOL> self . _fields . append ( Schema . Field ( name , type ) ) <EOL> class Job ( Model ) : <EOL> """<STR_LIT>""" <EOL> STATUS_QUEUED = "<STR_LIT>" <EOL> STATUS_BOOTING = "<STR_LIT>" <EOL> STATUS_RUNNING = "<STR_LIT>" <EOL> STATUS_SUCCESS = "<STR_LIT:success>" <EOL> STATUS_ERROR = "<STR_LIT:error>" <EOL> STATUS_KILLED = "<STR_LIT>" <EOL> FINISHED_STATUS = [ STATUS_SUCCESS , STATUS_ERROR , STATUS_KILLED ] <EOL> JOB_PRIORITY = { <EOL> - <NUM_LIT:2> : "<STR_LIT>" , <EOL> - <NUM_LIT:1> : "<STR_LIT>" , <EOL> <NUM_LIT:0> : "<STR_LIT>" , <EOL> <NUM_LIT:1> : "<STR_LIT>" , <EOL> <NUM_LIT:2> : "<STR_LIT>" , <EOL> } <EOL> def __init__ ( self , client , job_id , type , query , ** kwargs ) : <EOL> super ( Job , self ) . __init__ ( client ) <EOL> self . _job_id = job_id <EOL> self . _type = type <EOL> self . _query = query <EOL> self . _feed ( kwargs ) <EOL> def _feed ( self , data = None ) : <EOL> data = { } if data is None else data <EOL> self . _url = data . get ( "<STR_LIT:url>" ) <EOL> self . _status = data . get ( "<STR_LIT:status>" ) <EOL> self . _debug = data . get ( "<STR_LIT>" ) <EOL> self . _start_at = data . get ( "<STR_LIT>" ) <EOL> self . _end_at = data . get ( "<STR_LIT>" ) <EOL> self . _cpu_time = data . get ( "<STR_LIT>" ) <EOL> self . _result = data . get ( "<STR_LIT:result>" ) <EOL> self . _result_size = data . get ( "<STR_LIT>" ) <EOL> self . _result_url = data . get ( "<STR_LIT>" ) <EOL> self . _hive_result_schema = data . get ( "<STR_LIT>" ) <EOL> self . _priority = data . get ( "<STR_LIT>" ) <EOL> self . _retry_limit = data . get ( "<STR_LIT>" ) <EOL> self . _org_name = data . get ( "<STR_LIT>" ) <EOL> self . _database = data . get ( "<STR_LIT>" ) <EOL> def update ( self ) : <EOL> """<STR_LIT>""" <EOL> data = self . _client . api . show_job ( self . _job_id ) <EOL> self . _feed ( data ) <EOL> def _update_status ( self ) : <EOL> warnings . warn ( "<STR_LIT>" , category = DeprecationWarning ) <EOL> self . update ( ) <EOL> def _update_progress ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _status not in self . FINISHED_STATUS : <EOL> self . _status = self . _client . job_status ( self . _job_id ) <EOL> @ property <EOL> def id ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _job_id <EOL> @ property <EOL> def job_id ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _job_id <EOL> @ property <EOL> def type ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _type <EOL> @ property <EOL> def result_size ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _result_size <EOL> @ property <EOL> def result_url ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _result_url <EOL> @ property <EOL> def result_schema ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _hive_result_schema <EOL> @ property <EOL> def priority ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _priority in self . JOB_PRIORITY : <EOL> return self . JOB_PRIORITY [ self . _priority ] <EOL> else : <EOL> return str ( self . _priority ) <EOL> @ property <EOL> def retry_limit ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _retry_limit <EOL> @ property <EOL> def org_name ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _org_name <EOL> @ property <EOL> def database ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _database <EOL> @ property <EOL> def debug ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _debug <EOL> def wait ( self , timeout = None , wait_interval = <NUM_LIT:5> , wait_callback = None , callback = None ) : <EOL> """<STR_LIT>""" <EOL> started_at = time . time ( ) <EOL> while not self . finished ( ) : <EOL> if timeout is None or abs ( time . time ( ) - started_at ) < timeout : <EOL> time . sleep ( wait_interval ) <EOL> cb = wait_callback if callable ( wait_callback ) else callback <EOL> if callable ( cb ) : <EOL> cb ( self ) <EOL> else : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> self . update ( ) <EOL> def kill ( self ) : <EOL> """<STR_LIT>""" <EOL> response = self . _client . kill ( self . job_id ) <EOL> self . update ( ) <EOL> return response <EOL> @ property <EOL> def query ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _query <EOL> def status ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _query is not None and not self . finished ( ) : <EOL> self . update ( ) <EOL> return self . _status <EOL> @ property <EOL> def url ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _url <EOL> def result ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . success ( ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> else : <EOL> self . update ( ) <EOL> if self . _result is None : <EOL> for row in self . _client . job_result_each ( self . _job_id ) : <EOL> yield row <EOL> else : <EOL> for row in self . _result : <EOL> yield row <EOL> def result_format ( self , fmt ) : <EOL> """<STR_LIT>""" <EOL> if not self . success ( ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> else : <EOL> self . update ( ) <EOL> if self . _result is None : <EOL> for row in self . _client . job_result_format_each ( self . _job_id , fmt ) : <EOL> yield row <EOL> else : <EOL> for row in self . _result : <EOL> yield row <EOL> def finished ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _update_progress ( ) <EOL> return self . _status in self . FINISHED_STATUS <EOL> def success ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _update_progress ( ) <EOL> return self . _status == self . STATUS_SUCCESS <EOL> def error ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _update_progress ( ) <EOL> return self . _status == self . STATUS_ERROR <EOL> def killed ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _update_progress ( ) <EOL> return self . _status == self . STATUS_KILLED <EOL> def queued ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _update_progress ( ) <EOL> return self . _status == self . STATUS_QUEUED <EOL> def running ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _update_progress ( ) <EOL> return self . _status == self . STATUS_RUNNING </s>
