<s> from youtrack import YouTrackException <EOL> LOGGED_COMMENT_LENGTH = <NUM_LIT:10> <EOL> class SafeCommandExecutor ( object ) : <EOL> def __init__ ( self , yt , logger ) : <EOL> self . yt = yt <EOL> self . logger = logger <EOL> self . debug_mode = False <EOL> def setDebugMode ( self , on ) : <EOL> self . debug_mode = on <EOL> def executeCommand ( self , issue_id , command , comment = None , run_as = None ) : <EOL> if command != '<STR_LIT>' : <EOL> try : <EOL> if not self . debug_mode : <EOL> self . yt . executeCommand ( issue_id , command , comment = comment , run_as = run_as ) <EOL> if comment : <EOL> self . logger . logAction ( issue_id , self . yt , '<STR_LIT>' + comment [ <NUM_LIT:0> : LOGGED_COMMENT_LENGTH ] + '<STR_LIT>' , run_as ) <EOL> else : <EOL> self . logger . logAction ( issue_id , self . yt , '<STR_LIT>' + command + '<STR_LIT>' , run_as ) <EOL> except Exception , e : <EOL> self . logger . logError ( e , issue_id , self . yt , '<STR_LIT>' + command + '<STR_LIT>' , run_as ) <EOL> def executeUserImport ( self , user ) : <EOL> if user : <EOL> try : <EOL> if not self . debug_mode : <EOL> self . yt . importUsers ( [ user ] ) <EOL> self . logger . logAction ( '<STR_LIT>' , self . yt , '<STR_LIT>' + str ( user . login ) + '<STR_LIT>' ) <EOL> except YouTrackException , e : <EOL> self . logger . logError ( e , '<STR_LIT>' , self . yt , '<STR_LIT>' + user . login + '<STR_LIT>' ) <EOL> def createIssue ( self , project_id , summary , description , issue_from_id ) : <EOL> try : <EOL> created_issue_id = '<STR_LIT>' <EOL> if not self . debug_mode : <EOL> created_message = self . yt . createIssue ( project_id , None , summary , description ) <EOL> created_issue_id = project_id + '<STR_LIT:->' + created_message . rpartition ( '<STR_LIT:->' ) [ <NUM_LIT:2> ] <EOL> self . logger . logAction ( str ( issue_from_id ) + '<STR_LIT>' + created_issue_id , self . yt , '<STR_LIT>' ) <EOL> return None if self . debug_mode else created_issue_id <EOL> except Exception , e : <EOL> self . logger . logError ( e , str ( issue_from_id ) + '<STR_LIT>' , self . yt , '<STR_LIT>' ) <EOL> return None <EOL> def importLinks ( self , links , permitted_issue_ids ) : <EOL> maxLinks = <NUM_LIT:100> <EOL> links_to_import = [ ] <EOL> for link in links : <EOL> if link . target not in permitted_issue_ids : <EOL> message = '<STR_LIT>' + self . _getPrettyLink ( link ) + '<STR_LIT>' + link . target + '<STR_LIT>' <EOL> self . logger . logError ( None , '<STR_LIT>' , self . yt , message ) <EOL> elif link . source not in permitted_issue_ids : <EOL> message = '<STR_LIT>' + self . _getPrettyLink ( link ) + '<STR_LIT>' + link . source + '<STR_LIT>' <EOL> self . logger . logError ( None , '<STR_LIT>' , self . yt , message ) <EOL> else : <EOL> links_to_import . append ( link ) <EOL> if len ( links_to_import ) == maxLinks : <EOL> self . _import_links_batch ( links_to_import ) <EOL> links_to_import = [ ] <EOL> if len ( links_to_import ) : <EOL> self . _import_links_batch ( links_to_import ) <EOL> def _import_links_batch ( self , links_to_import ) : <EOL> if not self . debug_mode : <EOL> self . yt . importLinks ( links_to_import ) <EOL> for link in links_to_import : <EOL> message = '<STR_LIT>' + self . _getPrettyLink ( link ) <EOL> self . logger . logAction ( '<STR_LIT>' , self . yt , message ) <EOL> def _getPrettyLink ( self , link ) : <EOL> return link . typeName + '<STR_LIT>' + link . source + '<STR_LIT>' + link . target <EOL> def getLogger ( self ) : <EOL> return self . logger </s>
