<s> from django . conf import settings <EOL> from django . db . models import get_app , get_models , get_model <EOL> from django . db import transaction <EOL> import random <EOL> import logging <EOL> import traceback <EOL> log = logging . getLogger ( __name__ ) <EOL> __author__ = "<STR_LIT>" <EOL> __contributors = """<STR_LIT>""" <EOL> __version__ = "<STR_LIT>" <EOL> class AbstractRecord ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , app , model = None , field = None ) : <EOL> self . app = app <EOL> self . model = model <EOL> self . field = field <EOL> self . many_to_many = False <EOL> self . obj = None <EOL> def is_app ( self ) : <EOL> return not self . model and not self . field <EOL> def is_model ( self ) : <EOL> return self . model and not self . field <EOL> def is_field ( self ) : <EOL> return self . model and self . field <EOL> def create_object ( self ) : <EOL> assert self . model is not None <EOL> Klass = self . model <EOL> self . obj = Klass ( ) <EOL> return self . obj <EOL> def set_object_property ( self , value ) : <EOL> assert self . obj and self . is_field ( ) <EOL> if self . many_to_many and self . obj and self . field . name and value : <EOL> manager = getattr ( self . obj , self . field . name ) <EOL> manager . add ( * value ) <EOL> if self . obj and self . field . name and value : <EOL> setattr ( self . obj , self . field . name , value ) <EOL> return self . obj <EOL> def save ( self ) : <EOL> self . obj . save ( ) <EOL> def __repr__ ( self ) : <EOL> if self . is_app ( ) : <EOL> return self . app <EOL> if self . is_model ( ) : <EOL> return "<STR_LIT>" % ( self . app , self . model . __name__ ) <EOL> if self . is_field ( ) : <EOL> return "<STR_LIT>" % ( self . app , self . model . __name__ , self . field . name ) <EOL> def __str__ ( self ) : <EOL> return self . __repr__ ( ) <EOL> def __unicode__ ( self ) : <EOL> return self . __repr__ ( ) <EOL> class SpamRegistry ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . global_handlers = { } <EOL> self . strict_handlers = { } <EOL> def _decorate ( self , key , attr ) : <EOL> def fn ( f ) : <EOL> attr . update ( { key : f } ) <EOL> return f <EOL> return fn <EOL> def strict_handler ( self , field_qname ) : <EOL> return self . _decorate ( field_qname , self . strict_handlers ) <EOL> def global_handler ( self , field_type ) : <EOL> return self . _decorate ( field_type , self . global_handlers ) <EOL> def get_handler ( self , record , strict = False ) : <EOL> if strict : <EOL> return self . strict_handlers . get ( str ( record ) , None ) <EOL> return self . strict_handlers . get ( str ( record ) , None ) or self . global_handlers . get ( record . field . get_internal_type ( ) , None ) <EOL> def __repr__ ( self ) : <EOL> return """<STR_LIT>""" % ( self . strict_handlers , self . global_handlers ) <EOL> def __str__ ( self ) : <EOL> return self . __repr__ ( ) <EOL> spam = SpamRegistry ( ) <EOL> class Dilla ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , apps = None , cycles = <NUM_LIT:1> , use_coin = True , spamlibs = None , single_transaction = True ) : <EOL> if spamlibs is None : <EOL> spamlibs = getattr ( settings , '<STR_LIT>' , [ ] ) <EOL> spamlibs . insert ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> for lib in spamlibs : <EOL> log . debug ( '<STR_LIT>' % lib ) <EOL> self . _dyn_import ( lib ) <EOL> app_list = apps or getattr ( settings , '<STR_LIT>' , None ) <EOL> assert app_list is not None , "<STR_LIT>" <EOL> self . spam_registry = spam <EOL> self . apps = app_list <EOL> self . cycles = cycles <EOL> self . use_coin = use_coin <EOL> self . appmodels = { } <EOL> self . current = None <EOL> self . rows_affected = <NUM_LIT:0> <EOL> self . fields_spammed = <NUM_LIT:0> <EOL> self . fields_omitted = <NUM_LIT:0> <EOL> def discover_models ( self ) : <EOL> apps = [ ( app , get_app ( app ) ) for app in self . apps ] <EOL> for app_name , app_module in apps : <EOL> self . appmodels [ app_name ] = [ ] <EOL> models = get_models ( app_module ) <EOL> models_to_exclude = getattr ( settings , '<STR_LIT>' , None ) <EOL> if models_to_exclude : <EOL> for excluded_model in models_to_exclude : <EOL> try : <EOL> models . remove ( get_model ( * excluded_model . split ( '<STR_LIT:.>' ) ) ) <EOL> except ValueError : <EOL> pass <EOL> self . appmodels [ app_name ] . extend ( models ) <EOL> log . debug ( '<STR_LIT>' % self . appmodels ) <EOL> def find_spam_handler ( self , record ) : <EOL> strict_handler = self . spam_registry . get_handler ( record , strict = True ) <EOL> if strict_handler : <EOL> return strict_handler <EOL> if record . field . choices : <EOL> choices = [ c for c in record . field . choices ] <EOL> return lambda x , y : random . choice ( choices ) [ <NUM_LIT:0> ] <EOL> return self . spam_registry . get_handler ( record , strict = False ) <EOL> def spam ( self , record ) : <EOL> """<STR_LIT>""" <EOL> log . debug ( '<STR_LIT>' % record ) <EOL> self . current = record <EOL> if record . is_app ( ) : <EOL> for model in self . appmodels . get ( str ( record ) ) : <EOL> record . model = model <EOL> record . create_object ( ) <EOL> record . field = None <EOL> self . spam ( record ) <EOL> elif record . is_model ( ) : <EOL> for field in [ field for field in record . model . _meta . fields if not field . auto_created ] : <EOL> record . many_to_many = False <EOL> record . field = field <EOL> self . spam ( record ) <EOL> record . save ( ) <EOL> self . rows_affected += <NUM_LIT:1> <EOL> for field in record . model . _meta . many_to_many : <EOL> record . field = field <EOL> record . many_to_many = True <EOL> self . spam ( record ) <EOL> elif record . is_field ( ) : <EOL> if record . field . blank and ( self . use_coin and self . _coin_toss ( ) ) : <EOL> self . fields_omitted += <NUM_LIT:1> <EOL> log . debug ( "<STR_LIT>" ) <EOL> return <EOL> handler = self . find_spam_handler ( record ) <EOL> if handler is not None : <EOL> value = handler ( record , record . field ) <EOL> if record . field . unique : <EOL> manager = record . model . _default_manager <EOL> for i in range ( <NUM_LIT:5> ) : <EOL> try : <EOL> _match = manager . get ( ** { record . field . name : value } ) <EOL> value = handler ( record , record . field ) <EOL> except record . model . DoesNotExist , e : <EOL> break <EOL> record . set_object_property ( value ) <EOL> self . fields_spammed += <NUM_LIT:1> <EOL> else : <EOL> log . warn ( "<STR_LIT>" % record ) <EOL> @ transaction . commit_manually <EOL> def run ( self ) : <EOL> try : <EOL> self . discover_models ( ) <EOL> for cycle in range ( self . cycles ) : <EOL> log . info ( "<STR_LIT>" % ( cycle + <NUM_LIT:1> , self . cycles ) ) <EOL> for app in self . appmodels . keys ( ) : <EOL> self . spam ( AbstractRecord ( app ) ) <EOL> transaction . commit ( ) <EOL> return ( len ( self . apps ) , <EOL> self . rows_affected , <EOL> self . fields_spammed , <EOL> self . fields_omitted ) <EOL> except Exception , e : <EOL> log . critical ( "<STR_LIT:%s>" % e ) <EOL> transaction . rollback ( ) <EOL> raise <EOL> def _dyn_import ( self , name ) : <EOL> mod , spamlib = name . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> m = __import__ ( mod , fromlist = [ spamlib ] ) <EOL> return getattr ( m , spamlib ) <EOL> def _coin_toss ( self ) : <EOL> return bool ( random . randrange ( <NUM_LIT:2> ) ) </s>
