<s> import inspect <EOL> from cassandra import cqlengine <EOL> import django <EOL> from django . conf import settings <EOL> class CursorWrapper ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cursor , db ) : <EOL> self . cursor = cursor <EOL> self . db = db <EOL> WRAP_ERROR_ATTRS = frozenset ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> def __getattr__ ( self , attr ) : <EOL> cursor_attr = getattr ( self . cursor , attr ) <EOL> if attr in CursorWrapper . WRAP_ERROR_ATTRS : <EOL> return self . db . wrap_database_errors ( cursor_attr ) <EOL> else : <EOL> return cursor_attr <EOL> def __iter__ ( self ) : <EOL> return iter ( self . cursor ) <EOL> def __enter__ ( self ) : <EOL> return self <EOL> def __exit__ ( self , type , value , traceback ) : <EOL> pass <EOL> def callproc ( self , procname , params = None ) : <EOL> with self . db . wrap_database_errors : <EOL> if params is None : <EOL> return self . cursor . callproc ( procname ) <EOL> else : <EOL> return self . cursor . callproc ( procname , params ) <EOL> def execute ( self , sql , params = None ) : <EOL> with self . db . wrap_database_errors : <EOL> if params is None : <EOL> return self . cursor . execute ( sql ) <EOL> else : <EOL> return self . cursor . execute ( sql , params ) <EOL> def executemany ( self , sql , param_list ) : <EOL> with self . db . wrap_database_errors : <EOL> return self . cursor . executemany ( sql , param_list ) <EOL> def get_installed_apps ( ) : <EOL> """<STR_LIT>""" <EOL> if django . VERSION >= ( <NUM_LIT:1> , <NUM_LIT:7> ) : <EOL> from django . apps import apps <EOL> return [ a . models_module for a in apps . get_app_configs ( ) <EOL> if a . models_module is not None ] <EOL> else : <EOL> from django . db import models <EOL> return models . get_apps ( ) <EOL> def get_cql_models ( app , keyspace = None ) : <EOL> """<STR_LIT>""" <EOL> from cassandra . cqlengine . models import DEFAULT_KEYSPACE <EOL> keyspace = keyspace or DEFAULT_KEYSPACE <EOL> models = [ ] <EOL> for name , obj in inspect . getmembers ( app ) : <EOL> if inspect . isclass ( obj ) and issubclass ( obj , cqlengine . models . Model ) and not obj . __abstract__ : <EOL> if ( obj . __keyspace__ is None and keyspace == DEFAULT_KEYSPACE ) or obj . __keyspace__ == keyspace : <EOL> models . append ( obj ) <EOL> return models <EOL> def get_cassandra_connections ( ) : <EOL> """<STR_LIT>""" <EOL> from django . db import connections <EOL> for alias in connections : <EOL> engine = connections [ alias ] . settings_dict . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if engine == '<STR_LIT>' : <EOL> yield alias , connections [ alias ] <EOL> def get_cassandra_connection ( alias = None , name = None ) : <EOL> """<STR_LIT>""" <EOL> for _alias , connection in get_cassandra_connections ( ) : <EOL> if alias is not None : <EOL> if alias == _alias : <EOL> return connection <EOL> elif name is not None : <EOL> if name == connection . settings_dict [ '<STR_LIT>' ] : <EOL> return connection <EOL> else : <EOL> return connection <EOL> def get_cassandra_db_alias ( ) : <EOL> from django . db import connections <EOL> for alias in connections : <EOL> engine = connections [ alias ] . settings_dict . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if engine == '<STR_LIT>' : <EOL> return alias <EOL> def get_engine_from_db_alias ( db_alias ) : <EOL> """<STR_LIT>""" <EOL> return settings . DATABASES . get ( db_alias , { } ) . get ( '<STR_LIT>' , None ) </s>
