<s> import collections <EOL> import sys <EOL> from wordpress_xmlrpc . compat import xmlrpc_client , dict_type <EOL> from wordpress_xmlrpc . exceptions import ServerConnectionError , UnsupportedXmlrpcMethodError , InvalidCredentialsError , XmlrpcDisabledError <EOL> class Client ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , url , username , password , blog_id = <NUM_LIT:0> , transport = None , verbose = False ) : <EOL> self . url = url <EOL> self . username = username <EOL> self . password = password <EOL> self . blog_id = blog_id <EOL> try : <EOL> self . server = xmlrpc_client . ServerProxy ( url , allow_none = True , transport = transport , <EOL> verbose = verbose ) <EOL> self . supported_methods = self . server . mt . supportedMethods ( ) <EOL> except xmlrpc_client . ProtocolError : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> raise ServerConnectionError ( repr ( e ) ) <EOL> def call ( self , method ) : <EOL> if method . method_name not in self . supported_methods : <EOL> raise UnsupportedXmlrpcMethodError ( method . method_name ) <EOL> server_method = getattr ( self . server , method . method_name ) <EOL> args = method . get_args ( self ) <EOL> try : <EOL> raw_result = server_method ( * args ) <EOL> except xmlrpc_client . Fault : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> if e . faultCode == <NUM_LIT> : <EOL> raise InvalidCredentialsError ( e . faultString ) <EOL> elif e . faultCode == <NUM_LIT> : <EOL> raise XmlrpcDisabledError ( e . faultString ) <EOL> else : <EOL> raise <EOL> return method . process_result ( raw_result ) <EOL> class XmlrpcMethod ( object ) : <EOL> """<STR_LIT>""" <EOL> method_name = None <EOL> method_args = tuple ( ) <EOL> optional_args = tuple ( ) <EOL> results_class = None <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> if self . method_args or self . optional_args : <EOL> if self . optional_args : <EOL> max_num_args = len ( self . method_args ) + len ( self . optional_args ) <EOL> if not ( len ( self . method_args ) <= len ( args ) <= max_num_args ) : <EOL> raise ValueError ( "<STR_LIT>" % self . method_name ) <EOL> else : <EOL> if len ( args ) != len ( self . method_args ) : <EOL> raise ValueError ( "<STR_LIT>" % self . method_name ) <EOL> for i , arg_name in enumerate ( self . method_args ) : <EOL> setattr ( self , arg_name , args [ i ] ) <EOL> if self . optional_args : <EOL> for i , arg_name in enumerate ( self . optional_args , start = len ( self . method_args ) ) : <EOL> if i >= len ( args ) : <EOL> break <EOL> setattr ( self , arg_name , args [ i ] ) <EOL> if '<STR_LIT>' in kwargs : <EOL> self . results_class = kwargs [ '<STR_LIT>' ] <EOL> def default_args ( self , client ) : <EOL> """<STR_LIT>""" <EOL> return tuple ( ) <EOL> def get_args ( self , client ) : <EOL> """<STR_LIT>""" <EOL> default_args = self . default_args ( client ) <EOL> if self . method_args or self . optional_args : <EOL> optional_args = getattr ( self , '<STR_LIT>' , tuple ( ) ) <EOL> args = [ ] <EOL> for arg in ( self . method_args + optional_args ) : <EOL> if hasattr ( self , arg ) : <EOL> obj = getattr ( self , arg ) <EOL> if hasattr ( obj , '<STR_LIT>' ) : <EOL> args . append ( obj . struct ) <EOL> else : <EOL> args . append ( obj ) <EOL> args = list ( default_args ) + args <EOL> else : <EOL> args = default_args <EOL> return args <EOL> def process_result ( self , raw_result ) : <EOL> """<STR_LIT>""" <EOL> if self . results_class and raw_result : <EOL> if isinstance ( raw_result , dict_type ) : <EOL> return self . results_class ( raw_result ) <EOL> elif isinstance ( raw_result , collections . Iterable ) : <EOL> return [ self . results_class ( result ) for result in raw_result ] <EOL> return raw_result <EOL> class AnonymousMethod ( XmlrpcMethod ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class AuthenticatedMethod ( XmlrpcMethod ) : <EOL> """<STR_LIT>""" <EOL> def default_args ( self , client ) : <EOL> return ( client . blog_id , client . username , client . password ) </s>
