def substitute_dependencies(a, db): from compmake import Promise # XXX: this is a workaround if type(a).__name__ in ['ObjectSpec']: return deepcopy(a) if isinstance(a, dict): ca = type(a) rest = [(k, substitute_dependencies(v, db=db)) for k, v in a.items()] try: res = ca(rest) #print('%s->%s' % (a, str(res))) return res except (BaseException, TypeError) as e: msg = 'Could not instance something looking like a dict.', raise_wrapped(Exception, e, msg, ca=ca) elif isinstance(a, list): # XXX: This fails for subclasses of list return type(a)([substitute_dependencies(x, db=db) for x in a]) elif isinstance(a, tuple): # XXX: This fails for subclasses of tuples return type(a)([substitute_dependencies(x, db=db) for x in a]) elif isinstance(a, Promise): s = get_job_userobject(a.job_id, db=db) return substitute_dependencies(s, db=db) else: return deepcopy(a)
def get_job_userobject_resolved(job_id, db): """ This gets the job's result, and recursively substitute all dependencies. """ ob = get_job_userobject(job_id, db) all_deps = collect_dependencies(ob) for dep in all_deps: if not job_userobject_exists(dep, db): msg = 'Cannot resolve %r: dependency %r was not done.' % ( job_id, dep) raise CompmakeBug(msg) return substitute_dependencies(ob, db)
def get_job_userobject_resolved(job_id, db): """ This gets the job's result, and recursively substitute all dependencies. """ ob = get_job_userobject(job_id, db) all_deps = collect_dependencies(ob) for dep in all_deps: if not job_userobject_exists(dep, db): msg = 'Cannot resolve %r: dependency %r was not done.' % (job_id, dep) raise CompmakeBug(msg) return substitute_dependencies(ob, db)
def substitute_dependencies(a, db): from compmake import Promise # XXX: this is a workaround if leave_it_alone(a): return deepcopy(a) if isinstance(a, dict): ca = type(a) rest = [(k, substitute_dependencies(v, db=db)) for k, v in a.items()] try: res = ca(rest) #print('%s->%s' % (a, str(res))) return res except (BaseException, TypeError) as e: msg = 'Could not instance something looking like a dict.', raise_wrapped(Exception, e, msg, ca=ca) elif isinstance(a, list): # XXX: This fails for subclasses of list return type(a)([substitute_dependencies(x, db=db) for x in a]) elif isinstance(a, tuple): # First, check that there are dependencies deps_in_tuple = collect_dependencies(a) if not deps_in_tuple: # if not, just return the tuple return a # XXX: This fails for subclasses of tuples assert not isnamedtupleinstance(a), a ta = type(a) contents = ([substitute_dependencies(x, db=db) for x in a]) try: return ta(contents) except TypeError as e: msg = 'Cannot reconstruct complex tuple.' raise_wrapped(ValueError, e, msg, ta=ta, contents=contents) elif isinstance(a, Promise): s = get_job_userobject(a.job_id, db=db) return substitute_dependencies(s, db=db) else: return a