@ -1,3 +1,4 @@
import importlib
import times
import times
from uuid import uuid4
from uuid import uuid4
from cPickle import loads , dumps , UnpicklingError
from cPickle import loads , dumps , UnpicklingError
@ -15,9 +16,6 @@ def unpickle(pickled_string):
"""
"""
try :
try :
obj = loads ( pickled_string )
obj = loads ( pickled_string )
except AttributeError as e :
raise UnpickleError ( ' Could not unpickle: %s ' % e . message ,
pickled_string )
except ( StandardError , UnpicklingError ) :
except ( StandardError , UnpicklingError ) :
raise UnpickleError ( ' Could not unpickle. ' , pickled_string )
raise UnpickleError ( ' Could not unpickle. ' , pickled_string )
return obj
return obj
@ -34,15 +32,28 @@ class Job(object):
keyword arguments .
keyword arguments .
"""
"""
job = Job ( )
job = Job ( )
job . _func = func
job . _func _name = ' %s . %s ' % ( func . __module__ , func . __name__ )
job . _args = args
job . _args = args
job . _kwargs = kwargs
job . _kwargs = kwargs
job . description = job . get_call_string ( )
job . description = job . get_call_string ( )
return job
return job
@property
def func_name ( self ) :
return self . _func_name
@property
@property
def func ( self ) :
def func ( self ) :
return self . _func
import warnings
warnings . warn ( ' Don \' t use this! ' , DeprecationWarning )
func_name = self . func_name
if func_name is None :
return None
module_name , func_name = func_name . rsplit ( ' . ' , 1 )
module = importlib . import_module ( module_name )
return getattr ( module , func_name )
@property
@property
def args ( self ) :
def args ( self ) :
@ -69,7 +80,7 @@ class Job(object):
def __init__ ( self , id = None ) :
def __init__ ( self , id = None ) :
self . _id = id
self . _id = id
self . created_at = times . now ( )
self . created_at = times . now ( )
self . _func = None
self . _func _name = None
self . _args = None
self . _args = None
self . _kwargs = None
self . _kwargs = None
self . description = None
self . description = None
@ -111,7 +122,7 @@ class Job(object):
def job_tuple ( self ) :
def job_tuple ( self ) :
""" Returns the job tuple that encodes the actual function call that
""" Returns the job tuple that encodes the actual function call that
this job represents . """
this job represents . """
return ( self . func , self . args , self . kwargs )
return ( self . func _name , self . args , self . kwargs )
@property
@property
def return_value ( self ) :
def return_value ( self ) :
@ -160,7 +171,7 @@ class Job(object):
else :
else :
return times . to_universal ( date_str )
return times . to_universal ( date_str )
self . _func , self . _args , self . _kwargs = unpickle ( data )
self . _func _name , self . _args , self . _kwargs = unpickle ( data )
self . created_at = to_date ( created_at )
self . created_at = to_date ( created_at )
self . origin = origin
self . origin = origin
self . description = description
self . description = description
@ -180,7 +191,7 @@ class Job(object):
obj = { }
obj = { }
obj [ ' created_at ' ] = times . format ( self . created_at , ' UTC ' )
obj [ ' created_at ' ] = times . format ( self . created_at , ' UTC ' )
if self . func is not None :
if self . func _name is not None :
obj [ ' data ' ] = dumps ( self . job_tuple )
obj [ ' data ' ] = dumps ( self . job_tuple )
if self . origin is not None :
if self . origin is not None :
obj [ ' origin ' ] = self . origin
obj [ ' origin ' ] = self . origin
@ -227,13 +238,13 @@ class Job(object):
""" Returns a string representation of the call, formatted as a regular
""" Returns a string representation of the call, formatted as a regular
Python function invocation statement .
Python function invocation statement .
"""
"""
if self . func is None :
if self . func _name is None :
return None
return None
arg_list = [ repr ( arg ) for arg in self . args ]
arg_list = [ repr ( arg ) for arg in self . args ]
arg_list + = [ ' %s = %r ' % ( k , v ) for k , v in self . kwargs . items ( ) ]
arg_list + = [ ' %s = %r ' % ( k , v ) for k , v in self . kwargs . items ( ) ]
args = ' , ' . join ( arg_list )
args = ' , ' . join ( arg_list )
return ' %s ( %s ) ' % ( self . func . _ _name__ , args )
return ' %s ( %s ) ' % ( self . func _name, args )
def __str__ ( self ) :
def __str__ ( self ) :
return ' <Job %s : %s > ' % ( self . id , self . description )
return ' <Job %s : %s > ' % ( self . id , self . description )