<s> """<STR_LIT>""" <EOL> import logging <EOL> from constants import DEFAULT_CREATOR <EOL> from bson . objectid import ObjectId <EOL> import base , crane , entity , relation , tigress , turtle , mantis , panda , user , engine <EOL> import yaml <EOL> logger = logging . getLogger ( "<STR_LIT>" ) <EOL> _config = None <EOL> _initialized = False <EOL> def UUID ( objId = None ) : <EOL> if objId is None : <EOL> return ObjectId ( ) <EOL> elif isinstance ( objId , basestring ) : <EOL> return ObjectId ( objId ) <EOL> else : <EOL> return objId <EOL> def yaml2json ( yamlFileName ) : <EOL> with open ( yamlFileName , '<STR_LIT:r>' ) as yf : <EOL> return yaml . load ( yf ) <EOL> return None <EOL> def initialize ( config = None ) : <EOL> global _config , _initialized <EOL> if _initialized : <EOL> return <EOL> if config is not None : <EOL> _config = config <EOL> logger . info ( '<STR_LIT>' ) <EOL> _initialized = crane . initialize_storage ( _config ) <EOL> return _initialized <EOL> def exits ( ) : <EOL> global _initialized <EOL> if _config is None : <EOL> return False <EOL> logger . info ( '<STR_LIT>' ) <EOL> crane . exit_storage ( ) <EOL> _initialized = False <EOL> return True <EOL> def reloads ( config = None ) : <EOL> if config : <EOL> exits ( ) <EOL> reload ( crane ) <EOL> initialize ( config ) <EOL> reload ( user ) <EOL> reload ( base ) <EOL> reload ( panda ) <EOL> reload ( tigress ) <EOL> reload ( turtle ) <EOL> reload ( mantis ) <EOL> reload ( entity ) <EOL> reload ( relation ) <EOL> reload ( engine ) <EOL> crane . reload_storage ( ) <EOL> def dummy ( duration ) : <EOL> crane . entityStore . load_one_in_id ( { '<STR_LIT:name>' : '<STR_LIT>' } ) <EOL> import time <EOL> time . sleep ( duration ) <EOL> def convert_entities ( collectionName = None ) : <EOL> crane . entityStore . set_collection_name ( collectionName ) <EOL> return crane . entityStore . convert_to_MONKObject ( '<STR_LIT>' ) <EOL> def load_entities ( entities = [ ] , query = { } , skip = <NUM_LIT:0> , num = <NUM_LIT:100> , collectionName = None ) : <EOL> crane . entityStore . set_collection_name ( collectionName ) <EOL> if not entities : <EOL> entities = [ ent [ '<STR_LIT>' ] for ent in crane . entityStore . load_all_in_ids ( query , skip , num ) ] <EOL> return crane . entityStore . load_or_create_all ( entities ) <EOL> def save_entities ( entities , collectionName = None ) : <EOL> crane . entityStore . set_collection_name ( collectionName ) <EOL> [ crane . entityStore . update_one_in_fields ( ent , ent . generic ( ) ) for ent in entities ] <EOL> def load_entity ( entity , collectionName = None ) : <EOL> crane . entityStore . set_collection_name ( collectionName ) <EOL> return crane . entityStore . load_or_create ( entity ) <EOL> def load_entities_in_ids ( query = { } , skip = <NUM_LIT:0> , num = <NUM_LIT:100> ) : <EOL> ids = [ ent [ '<STR_LIT>' ] for ent in crane . entityStore . load_all_in_ids ( query , skip , num ) ] <EOL> return ids <EOL> def has_engine_in_store ( engineName ) : <EOL> logger . debug ( '<STR_LIT>' . format ( crane . engineStore . _defaultCollectionName , crane . engineStore . _database ) ) <EOL> return crane . engineStore . has_name_user ( engineName , DEFAULT_CREATOR ) <EOL> def find_engine ( query ) : <EOL> logger . debug ( '<STR_LIT>' . format ( crane . engineStore . _defaultCollectionName , crane . engineStore . _database ) ) <EOL> ids = [ _engine [ '<STR_LIT>' ] for _engine in crane . engineStore . load_all_in_ids ( query , <NUM_LIT:0> , <NUM_LIT:0> ) ] <EOL> return crane . engineStore . load_all_by_ids ( ids ) <EOL> def create_engine ( engineScript ) : <EOL> logger . debug ( '<STR_LIT>' . format ( crane . engineStore . _defaultCollectionName , crane . engineStore . _database ) ) <EOL> if '<STR_LIT>' not in engineScript : <EOL> engineScript [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> _engine = crane . engineStore . load_or_create ( engineScript , True ) <EOL> if _engine is None : <EOL> logger . error ( '<STR_LIT>' . format ( engineScript ) ) <EOL> return None <EOL> return _engine <EOL> def load_engine ( engineName ) : <EOL> logger . debug ( '<STR_LIT>' . format ( crane . engineStore . _defaultCollectionName , crane . engineStore . _database ) ) <EOL> _engine = crane . engineStore . load_or_create ( { '<STR_LIT:name>' : engineName , '<STR_LIT>' : DEFAULT_CREATOR } ) <EOL> if _engine is None : <EOL> logger . error ( '<STR_LIT>' . format ( engineName ) ) <EOL> return None <EOL> return _engine <EOL> def save_engine ( engineName ) : <EOL> logger . debug ( '<STR_LIT>' . format ( crane . engineStore . _defaultCollectionName , crane . engineStore . _database ) ) <EOL> _engine = load_engine ( engineName ) <EOL> if _engine : <EOL> _engine . save ( ) <EOL> return True <EOL> return False <EOL> def delete_engine ( engineName ) : <EOL> logger . debug ( '<STR_LIT>' . format ( crane . engineStore . _defaultCollectionName , crane . engineStore . _database ) ) <EOL> _engine = load_engine ( engineName ) <EOL> if _engine : <EOL> crane . engineStore . delete_by_id ( _engine . _id ) <EOL> return True <EOL> return False <EOL> def has_user_in_store ( userName ) : <EOL> return crane . userStore . has_name_user ( userName , DEFAULT_CREATOR ) <EOL> def find_users ( query ) : <EOL> ids = [ _user [ '<STR_LIT>' ] for _user in crane . userStore . load_all_in_ids ( query , <NUM_LIT:0> , <NUM_LIT:0> ) ] <EOL> return crane . userStore . load_all_by_ids ( ids ) <EOL> def create_user ( userScript ) : <EOL> logger . debug ( '<STR_LIT>' . format ( userScript ) ) <EOL> if '<STR_LIT>' not in userScript : <EOL> userScript [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> _user = crane . userStore . load_or_create ( userScript , True ) <EOL> if _user is None : <EOL> logger . error ( '<STR_LIT>' . format ( userScript ) ) <EOL> return None <EOL> if _user . password != userScript . get ( '<STR_LIT:password>' , '<STR_LIT>' ) : <EOL> logger . error ( '<STR_LIT>' . format ( _user . name ) ) <EOL> return None <EOL> return _user <EOL> def load_user ( userName , password = '<STR_LIT>' ) : <EOL> _user = crane . userStore . load_or_create ( { '<STR_LIT:name>' : userName , '<STR_LIT>' : DEFAULT_CREATOR } ) <EOL> if _user is None : <EOL> logger . error ( '<STR_LIT>' . format ( userName ) ) <EOL> return None <EOL> if _user . password != password : <EOL> logger . error ( '<STR_LIT>' . format ( userName , password ) ) <EOL> return None <EOL> return _user <EOL> def save_user ( userName , password = '<STR_LIT>' ) : <EOL> _user = load_user ( userName , password ) <EOL> if _user : <EOL> _user . save ( ) <EOL> return True <EOL> return False <EOL> def delete_user ( userName , password = '<STR_LIT>' ) : <EOL> _user = load_user ( userName , password ) <EOL> if _user : <EOL> crane . userStore . delete_by_id ( _user . _id ) <EOL> return True <EOL> return False <EOL> def has_turtle_in_store ( turtleName , userName ) : <EOL> return crane . turtleStore . has_name_user ( turtleName , userName ) <EOL> def clone_turtle ( turtleName , userName , follower ) : <EOL> if not has_turtle_in_store ( turtleName , follower ) : <EOL> _turtle = load_turtle ( turtleName , userName ) <EOL> if not _turtle : <EOL> logger . error ( '<STR_LIT>' . format ( turtleName , userName ) ) <EOL> return None <EOL> newTurtle = _turtle . clone ( follower ) <EOL> newTurtle . leader = userName <EOL> newTurtle . followers = [ ] <EOL> newTurtle . save ( ) <EOL> return newTurtle <EOL> else : <EOL> follow_turtle ( turtleName , follower , leader = userName ) <EOL> logger . error ( '<STR_LIT>' . format ( userName ) ) <EOL> return None <EOL> def remove_turtle ( turtleName , userName , deep = False ) : <EOL> _turtle = load_turtle ( turtleName , userName ) <EOL> if not _turtle : <EOL> logger . error ( '<STR_LIT>' . format ( turtleName , userName ) ) <EOL> return False <EOL> return _turtle . delete ( deep ) <EOL> def follow_turtle ( turtleName , userName , leader = None , follower = None ) : <EOL> try : <EOL> _turtle = load_turtle ( turtleName , userName ) <EOL> rt = True <EOL> if follower : <EOL> rt = rt & _turtle . add_follower ( follower ) <EOL> if leader : <EOL> rt = rt & _turtle . add_leader ( leader ) <EOL> return rt <EOL> except Exception as e : <EOL> logger . error ( e ) <EOL> logger . error ( '<STR_LIT>' . format ( turtleName , userName ) ) <EOL> return False <EOL> def unfollow_turtle ( turtleName , userName , leader = None , follower = None ) : <EOL> try : <EOL> _turtle = load_turtle ( turtleName , userName ) <EOL> rt = True <EOL> if follower : <EOL> rt = rt & _turtle . remove_follower ( follower ) <EOL> if leader : <EOL> rt = rt & _turtle . remove_leader ( leader ) <EOL> return rt <EOL> except Exception as e : <EOL> logger . error ( e ) <EOL> logger . error ( '<STR_LIT>' . format ( turtleName , userName ) ) <EOL> return False <EOL> def find_turtles ( query ) : <EOL> ids = [ t [ '<STR_LIT>' ] for t in crane . turtleStore . load_all_in_ids ( query , <NUM_LIT:0> , <NUM_LIT:0> ) ] <EOL> return crane . turtleStore . load_all_by_ids ( ids ) <EOL> def create_turtle ( turtleScript ) : <EOL> _turtle = crane . turtleStore . load_or_create ( turtleScript , True ) <EOL> if _turtle is None : <EOL> logger . error ( '<STR_LIT>' . format ( turtleScript ) ) <EOL> return None <EOL> return _turtle <EOL> def load_turtle ( turtleName , userName ) : <EOL> return crane . turtleStore . load_or_create ( { '<STR_LIT:name>' : turtleName , '<STR_LIT>' : userName } ) <EOL> def save_turtle ( turtleName , userName ) : <EOL> _turtle = load_turtle ( turtleName , userName ) <EOL> if not _turtle : <EOL> logger . error ( '<STR_LIT>' . format ( turtleName , userName ) ) <EOL> return False <EOL> _turtle . save ( ) <EOL> return True <EOL> def entity_collection ( turtleName , userName ) : <EOL> _turtle = load_turtle ( turtleName , userName ) <EOL> if _turtle : <EOL> return _turtle . entityCollectionName <EOL> else : <EOL> logger . warning ( '<STR_LIT>' . format ( userName , turtleName ) ) <EOL> return None <EOL> def create_panda ( pandaScript ) : <EOL> return crane . pandaStore . load_or_create ( pandaScript ) <EOL> def find_pandas ( query , fields = None ) : <EOL> return crane . pandaStore . load_all ( query , fields ) <EOL> def add_panda ( turtleName , userName , panda ) : <EOL> _turtle = load_turtle ( turtleName , userName ) <EOL> if _turtle : <EOL> return _turtle . add_panda ( panda ) <EOL> else : <EOL> logger . warning ( '<STR_LIT>' . format ( userName , turtleName , panda . name ) ) <EOL> return None <EOL> def delete_panda ( turtleName , userName , panda ) : <EOL> _turtle = load_turtle ( turtleName , userName ) <EOL> if _turtle : <EOL> return _turtle . delete_panda ( panda ) <EOL> else : <EOL> logger . warning ( '<STR_LIT>' . format ( userName , turtleName , panda . name ) ) <EOL> return None <EOL> def add_data ( turtleName , userName , ent ) : <EOL> _turtle = load_turtle ( turtleName , userName ) <EOL> if _turtle : <EOL> crane . entityStore . set_collection_name ( _turtle . entityCollectionName ) <EOL> ent = crane . entityStore . load_or_create ( UUID ( ent ) , True ) <EOL> return _turtle . add_data ( ent ) <EOL> else : <EOL> logger . warning ( '<STR_LIT>' . format ( userName , turtleName ) ) <EOL> return False <EOL> def checkout ( turtleName , userName ) : <EOL> _turtle = load_turtle ( turtleName , userName ) <EOL> if _turtle : <EOL> _turtle . checkout ( ) <EOL> return True <EOL> else : <EOL> logger . warning ( '<STR_LIT>' . format ( userName , turtleName ) ) <EOL> return False <EOL> def train ( turtleName , userName ) : <EOL> _turtle = load_turtle ( turtleName , userName ) <EOL> if _turtle : <EOL> _turtle . train ( ) <EOL> return True <EOL> else : <EOL> logger . warning ( '<STR_LIT>' . format ( userName , turtleName ) ) <EOL> return False <EOL> def get_leader ( turtleName , userName ) : <EOL> _turtle = load_turtle ( turtleName , userName ) <EOL> if _turtle : <EOL> if _turtle . leader is None : <EOL> return userName <EOL> else : <EOL> return _turtle . leader <EOL> else : <EOL> logger . warning ( '<STR_LIT>' . format ( userName , turtleName ) ) <EOL> return None <EOL> def get_followers ( turtleName , userName ) : <EOL> _turtle = load_turtle ( turtleName , userName ) <EOL> if _turtle : <EOL> return _turtle . followers <EOL> else : <EOL> return [ ] <EOL> def commit ( turtleName , userName ) : <EOL> _turtle = load_turtle ( turtleName , userName ) <EOL> if _turtle : <EOL> _turtle . commit ( ) <EOL> return True <EOL> else : <EOL> logger . warning ( '<STR_LIT>' . format ( userName , turtleName ) ) <EOL> return False <EOL> def merge ( turtleName , userName , follower ) : <EOL> _turtle = load_turtle ( turtleName , userName ) <EOL> if _turtle : <EOL> return _turtle . merge ( follower ) <EOL> else : <EOL> logger . warning ( '<STR_LIT>' . format ( userName , turtleName , follower ) ) <EOL> return False <EOL> def set_mantis_parameter ( turtleName , userName , para , value ) : <EOL> _turtle = load_turtle ( turtleName , userName ) <EOL> if _turtle : <EOL> _turtle . set_mantis_parameter ( para , value ) <EOL> return True <EOL> else : <EOL> logger . warning ( '<STR_LIT>' . format ( userName , turtleName ) ) <EOL> return False <EOL> def predict ( turtleName , userName , entity , fields = None ) : <EOL> _turtle = load_turtle ( turtleName , userName ) <EOL> if _turtle : <EOL> entity = crane . entityStore . load_or_create ( UUID ( entity ) ) <EOL> return _turtle . predict ( entity , fields ) <EOL> else : <EOL> logger . warning ( '<STR_LIT>' . format ( userName , turtleName ) ) <EOL> return <NUM_LIT:0> <EOL> def reset ( turtleName , userName ) : <EOL> _turtle = load_turtle ( turtleName , userName ) <EOL> if _turtle : <EOL> _turtle . reset ( ) <EOL> return True <EOL> else : <EOL> logger . warning ( '<STR_LIT>' . format ( userName , turtleName ) ) <EOL> return False <EOL> def reset_all_data ( turtleName , userName ) : <EOL> _turtle = load_turtle ( turtleName , userName ) <EOL> if _turtle : <EOL> _turtle . reset ( ) <EOL> _turtle . reset_data ( ) <EOL> return True <EOL> else : <EOL> logger . warning ( '<STR_LIT>' . format ( userName , turtleName ) ) <EOL> return False <EOL> def find_type ( typeName ) : <EOL> return base . monkFactory . find ( typeName ) <EOL> def show_help ( ) : <EOL> return { '<STR_LIT>' : '<STR_LIT:hello>' } </s>
