1""" 2Utility functions for the DSL 3""" 4 5from ..thrift import JavaObjectArg 6 7 8def to_java_arg(arg): 9 """Converts Python objects to equivalent Thrift JavaObjectArgs""" 10 if isinstance(arg, bool): 11 java_arg = JavaObjectArg(bool_arg=arg) 12 elif isinstance(arg, int): 13 # Just use long all the time since Python 3 doesn't 14 # distinguish between long and int 15 java_arg = JavaObjectArg(long_arg=arg) 16 elif isinstance(arg, bytes): 17 java_arg = JavaObjectArg(binary_arg=arg) 18 elif isinstance(arg, str): 19 java_arg = JavaObjectArg(string_arg=arg) 20 elif isinstance(arg, float): 21 java_arg = JavaObjectArg(double_arg=arg) 22 else: 23 raise TypeError( 24 "Only basic data types can be specified" 25 " as arguments to JavaObject " 26 "constructors. Given: {!r}".format(arg) 27 ) 28 return java_arg 29 30 31def to_python_arg(java_arg): 32 """Convert a Thrift JavaObjectArg into a basic Python data type""" 33 arg = None 34 for val in java_arg.__dict__.values(): 35 if val is not None: 36 arg = val 37 break 38 return arg 39 40 41def to_python_arg_list(java_arg_list): 42 """Convert a list of Thrift JavaObjectArg objects into a list of basic types""" 43 return [to_python_arg(java_arg) for java_arg in java_arg_list] 44