<s> from collections import namedtuple , OrderedDict <EOL> from ast import literal_eval <EOL> import re <EOL> from datetime import datetime <EOL> import sys <EOL> ScriptFunction = namedtuple ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:body>' , '<STR_LIT>' ] ) <EOL> class Scripts ( object ) : <EOL> def __init__ ( self ) : <EOL> self . namespaces = { '<STR_LIT:default>' : { } } <EOL> def add ( self , functions , namespace = None ) : <EOL> if namespace : <EOL> dest = self . namespaces . get ( namespace ) <EOL> if dest : <EOL> dest . update ( functions ) <EOL> else : <EOL> self . namespaces [ namespace ] = functions <EOL> else : <EOL> self . namespaces [ '<STR_LIT:default>' ] . update ( functions ) <EOL> def get_scripts ( self , namespace = None ) : <EOL> if not namespace : <EOL> namespace = '<STR_LIT:default>' <EOL> return self . namespaces . get ( namespace ) <EOL> def get_script ( self , name , namespace = None ) : <EOL> if not namespace : <EOL> namespace = '<STR_LIT:default>' <EOL> functions = self . namespaces . get ( namespace ) <EOL> if functions : <EOL> return functions . get ( name ) <EOL> else : <EOL> return None <EOL> def script_body ( self , name , args = None , namespace = None ) : <EOL> function = self . get_script ( name , namespace ) <EOL> if not function : <EOL> return None <EOL> param_string = re . search ( r'<STR_LIT>' , function . signature ) <EOL> params = [ ParamDefault ( param . split ( '<STR_LIT:=>' ) ) for param in <EOL> param_string . group ( <NUM_LIT:1> ) . split ( '<STR_LIT:U+002C>' ) ] if param_string else None <EOL> if isinstance ( args , dict ) : <EOL> param_defaults = OrderedDict ( [ val + ( None , ) <EOL> if len ( val ) == <NUM_LIT:1> else val for val in params ] ) <EOL> for param in params : <EOL> if len ( param ) == <NUM_LIT:2> and param [ <NUM_LIT:0> ] not in args : <EOL> args [ param [ <NUM_LIT:0> ] ] = literal_eval ( param [ <NUM_LIT:1> ] ) <EOL> args = { k : v for k , v in args . items ( ) if k in param_defaults . keys ( ) } <EOL> elif isinstance ( args , tuple ) or isinstance ( args , list ) : <EOL> args = { params [ i ] [ <NUM_LIT:0> ] : args [ i ] if i < len ( args ) <EOL> else literal_eval ( params [ i ] [ <NUM_LIT:1> ] ) <EOL> for i in range ( <NUM_LIT:0> , max ( len ( params ) , len ( args ) ) ) <EOL> if i < len ( args ) or len ( params [ i ] ) == <NUM_LIT:2> } <EOL> else : <EOL> if params : <EOL> if args : <EOL> args = { params [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] : args } <EOL> else : <EOL> if len ( params [ <NUM_LIT:0> ] ) == <NUM_LIT:2> : <EOL> args = { params [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] : literal_eval ( params [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) } <EOL> else : <EOL> args = { } <EOL> else : <EOL> args = { } <EOL> split_body = re . split ( r'<STR_LIT>' , function . body ) <EOL> replacements = { } <EOL> for k , v in args . items ( ) : <EOL> if isinstance ( v , str ) or isinstance ( v , datetime ) : <EOL> replacements [ k ] = "<STR_LIT>" . format ( v ) <EOL> elif sys . version_info [ <NUM_LIT:0> ] < <NUM_LIT:3> and isinstance ( v , unicode ) : <EOL> replacements [ k ] = repr ( v . encode ( '<STR_LIT:utf-8>' ) ) <EOL> else : <EOL> replacements [ k ] = '<STR_LIT:{}>' . format ( v ) <EOL> for i , s in enumerate ( split_body ) : <EOL> if i % <NUM_LIT:4> == <NUM_LIT:0> : <EOL> for k , v in replacements . items ( ) : <EOL> split_body [ i ] = re . sub ( r'<STR_LIT>' . format ( k ) , <EOL> v , split_body [ i ] ) <EOL> return '<STR_LIT>' . join ( split_body ) <EOL> class ParamDefault ( tuple ) : <EOL> def __new__ ( self , pair ) : <EOL> l = len ( pair ) <EOL> if l > <NUM_LIT:2> : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return tuple . __new__ ( self , ( pair [ <NUM_LIT:0> ] . strip ( ) , ) + <EOL> ( ( pair [ <NUM_LIT:1> ] , ) if l > <NUM_LIT:1> else tuple ( ) ) ) </s>
