<s> from abc import ABCMeta , abstractmethod <EOL> import os <EOL> from dimagi . utils . couch . database import get_db <EOL> from dimagi . utils . couch . sync_docs import DesignInfo <EOL> PREINDEX_PLUGINS = { } <EOL> def register_preindex_plugin ( plugin ) : <EOL> PREINDEX_PLUGINS [ plugin . app_label ] = plugin <EOL> class PreindexPlugin ( object ) : <EOL> __metaclass__ = ABCMeta <EOL> synced = False <EOL> @ abstractmethod <EOL> def _get_designs ( self ) : <EOL> raise NotImplementedError ( ) <EOL> def get_designs ( self ) : <EOL> used = set ( ) <EOL> designs = [ ] <EOL> for design in self . _get_designs ( ) : <EOL> key = ( design . db . uri , design . app_label ) <EOL> if key not in used : <EOL> designs . append ( design ) <EOL> used . add ( key ) <EOL> return designs <EOL> @ classmethod <EOL> def register ( cls , * args , ** kwargs ) : <EOL> register_preindex_plugin ( cls ( * args , ** kwargs ) ) <EOL> def sync_design_docs ( self , temp = None ) : <EOL> from corehq . preindex . accessors import sync_design_doc <EOL> if self . synced : <EOL> return <EOL> self . synced = True <EOL> for design in self . get_designs ( ) : <EOL> sync_design_doc ( design , temp = temp ) <EOL> def copy_designs ( self , temp = None , delete = True ) : <EOL> from corehq . preindex . accessors import copy_design_doc <EOL> for design in self . get_designs ( ) : <EOL> copy_design_doc ( design , temp = temp , delete = delete ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' . format ( <EOL> cls = self . __class__ , <EOL> self = self , <EOL> ) <EOL> def __init__ ( self , app_label , file ) : <EOL> assert '<STR_LIT>' == self . __class__ . __name__ <EOL> self . app_label = app_label <EOL> self . dir = os . path . abspath ( os . path . dirname ( file ) ) <EOL> def get_dbs_from_app_label_and_map ( app_label , app_db_map ) : <EOL> if app_db_map and app_label in app_db_map : <EOL> db_names = app_db_map [ app_label ] <EOL> if not isinstance ( db_names , ( list , tuple , set ) ) : <EOL> db_names = [ db_names ] <EOL> return [ get_db ( db_name ) for db_name in set ( db_names ) ] <EOL> return [ get_db ( ) ] <EOL> class CouchAppsPreindexPlugin ( PreindexPlugin ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , app_label , file , app_db_map = None ) : <EOL> self . app_label = app_label <EOL> self . dir = os . path . abspath ( os . path . dirname ( file ) ) <EOL> self . app_db_map = app_db_map <EOL> def get_couchapps ( self ) : <EOL> return [ d for d in os . listdir ( self . dir ) <EOL> if os . path . isdir ( os . path . join ( self . dir , d ) ) ] <EOL> def _get_designs ( self ) : <EOL> return [ <EOL> DesignInfo ( app_label = app_label , db = db , <EOL> design_path = os . path . join ( self . dir , app_label ) ) <EOL> for app_label in self . get_couchapps ( ) <EOL> for db in self . get_dbs ( app_label ) <EOL> ] <EOL> def get_dbs ( self , app_label ) : <EOL> return get_dbs_from_app_label_and_map ( app_label , self . app_db_map ) <EOL> class ExtraPreindexPlugin ( PreindexPlugin ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , app_label , file , db_names = None ) : <EOL> self . app_label = app_label <EOL> self . dir = os . path . abspath ( os . path . dirname ( file ) ) <EOL> self . db_names = db_names <EOL> def _get_designs ( self ) : <EOL> return [ <EOL> DesignInfo ( <EOL> app_label = self . app_label , <EOL> db = db , <EOL> design_path = os . path . join ( self . dir , "<STR_LIT>" ) <EOL> ) <EOL> for db in self . get_dbs ( self . app_label ) <EOL> ] <EOL> def get_dbs ( self , app_label ) : <EOL> return get_dbs_from_app_label_and_map ( app_label , { app_label : self . db_names } ) </s>
