diff --git a/TODO b/TODO index d1f241a9..57c25505 100644 --- a/TODO +++ b/TODO @@ -1,3 +1,10 @@ +- add more checks to Python vips_call() + + need an exception class + + how do we structure the module? + + - grep for other vips_class_find() problems: do we use it for simple class diff --git a/python/try.py b/python/try.py index 3e62beef..aaecc9d6 100755 --- a/python/try.py +++ b/python/try.py @@ -1,6 +1,5 @@ #!/usr/bin/python -import gc import sys # you might need this in your .bashrc @@ -21,10 +20,10 @@ print 'a.props.width =', a.props.width print 'direct call:' -a = Vips.Image.new_from_file(sys.argv[1]) +b = Vips.Image.new_from_file(sys.argv[1]) -print 'a.get_width() =', a.get_width() -print 'a.props.width =', a.props.width +print 'b.get_width() =', b.get_width() +print 'b.props.width =', b.props.width print 'call operation:' @@ -41,7 +40,7 @@ for prop in op.props: print '\tassigned', op.get_assigned(prop.name) op.props.left = a -op.props.right = a +op.props.right = b op2 = Vips.cache_operation_build(op) if op2 == None: print Vips.error_buffer() @@ -54,9 +53,72 @@ print 'out.props.format =', out.props.format out.write_to_file("x.v") -print 'starting shutdown ...' -# sometimes have to do several GCs to get them all, not sure why -#for i in range(10): -# gc.collect () -#print 'shutdown!' +print 'generic call:' + +def required_input(flags): + enm = Vips.ArgumentFlags + return flags & enm.INPUT and flags & enm.REQUIRED + +def optional_input(flags): + enm = Vips.ArgumentFlags + return flags & enm.INPUT and not flags & enm.REQUIRED + +def required_output(flags): + enm = Vips.ArgumentFlags + return flags & enm.OUTPUT and flags & enm.REQUIRED + +def optional_output(flags): + enm = Vips.ArgumentFlags + return flags & enm.OUTPUT and not flags & enm.REQUIRED + +def vips_call(name, *required, **optional): + op = Vips.Operation.new(name) + + # set required input args + i = 0 + for prop in op.props: + flags = op.get_flags(prop.name) + if required_input(flags): + if i >= len(required): + print 'too few required args!' + + op.props.__setattr__(prop.name, required[i]) + i += 1 + + if i < len(required): + print 'too many required args!' + + # set optional input args + for i in optional.keys(): + flags = op.get_flags(i) + if optional_input(flags): + op.props.__setattr__(i, optional[i]) + + # call + op2 = Vips.cache_operation_build(op) + if op2 == None: + print Vips.error_buffer() + + # gather output args + out = [] + for prop in op2.props: + flags = op2.get_flags(prop.name) + if required_output(flags): + out.append(op2.props.__getattribute__(prop.name)) + for i in optional.keys(): + flags = op2.get_flags(i) + if optional_output(flags): + out.append(op2.props.__getattribute__(i)) + + if len(out) == 1: + out = out[0] + + # unref everything now we have refs to all outputs we want + op2.unref_outputs() + + return out + +im = vips_call("add", a, b) + +im.write_to_file("x2.v")