<s> from __future__ import unicode_literals <EOL> import re <EOL> import random <EOL> _re_token = re . compile ( r'<STR_LIT>' ) <EOL> random = random . Random ( ) <EOL> class Generator ( object ) : <EOL> __config = { } <EOL> def __init__ ( self , ** config ) : <EOL> self . providers = [ ] <EOL> self . __config = dict ( <EOL> list ( self . __config . items ( ) ) + list ( config . items ( ) ) ) <EOL> def add_provider ( self , provider ) : <EOL> if type ( provider ) is type : <EOL> provider = provider ( self ) <EOL> self . providers . insert ( <NUM_LIT:0> , provider ) <EOL> for method_name in dir ( provider ) : <EOL> if method_name . startswith ( '<STR_LIT:_>' ) : <EOL> continue <EOL> faker_function = getattr ( provider , method_name ) <EOL> if hasattr ( faker_function , '<STR_LIT>' ) or isinstance ( faker_function , ( classmethod , staticmethod ) ) : <EOL> self . set_formatter ( method_name , faker_function ) <EOL> def provider ( self , name ) : <EOL> try : <EOL> lst = [ p for p in self . get_providers ( ) <EOL> if p . __provider__ == name . lower ( ) ] <EOL> return lst [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> return None <EOL> def get_providers ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . providers <EOL> @ property <EOL> def random ( self ) : <EOL> return random <EOL> def seed ( self , seed = None ) : <EOL> """<STR_LIT>""" <EOL> random . seed ( seed ) <EOL> def format ( self , formatter , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return self . get_formatter ( formatter ) ( * args , ** kwargs ) <EOL> def get_formatter ( self , formatter ) : <EOL> try : <EOL> return getattr ( self , formatter ) <EOL> except AttributeError : <EOL> raise AttributeError ( '<STR_LIT>' . format ( formatter ) ) <EOL> def set_formatter ( self , name , method ) : <EOL> """<STR_LIT>""" <EOL> setattr ( self , name , method ) <EOL> def parse ( self , text ) : <EOL> """<STR_LIT>""" <EOL> return _re_token . sub ( self . __format_token , text ) <EOL> def __format_token ( self , matches ) : <EOL> formatter = list ( matches . groups ( ) ) <EOL> formatter [ <NUM_LIT:1> ] = self . format ( formatter [ <NUM_LIT:1> ] ) <EOL> return '<STR_LIT>' . join ( formatter ) </s>
