<s> from trac . env import Environment <EOL> from trac . attachment import Attachment <EOL> from tracLib import * <EOL> from ConfigParser import ConfigParser <EOL> import tracLib <EOL> import tracLib . timetracking <EOL> class Client ( object ) : <EOL> def __init__ ( self , env_path ) : <EOL> self . env_path = env_path <EOL> self . env = Environment ( env_path ) <EOL> self . db_cnx = self . env . get_db_cnx ( ) <EOL> self . _registered_users_logins = [ ] <EOL> self . _timetracking_plugins = self . _get_timetracking_plugins ( ) <EOL> def _get_timetracking_plugins ( self ) : <EOL> plugins = { } <EOL> if tracLib . SUPPORT_TIME_TRACKING == '<STR_LIT>' : <EOL> for plugin in tracLib . timetracking . plugins : <EOL> plugin_name = plugin . get_name ( ) <EOL> for com_name , com_enabled in self . env . _component_rules . items ( ) : <EOL> if com_name . startswith ( plugin_name ) and com_enabled and plugin_name not in plugins : <EOL> plugins [ plugin_name ] = plugin ( self . env ) <EOL> else : <EOL> for plugin in tracLib . timetracking . plugins : <EOL> plugin_name = plugin . get_name ( ) <EOL> if plugin_name == tracLib . SUPPORT_TIME_TRACKING : <EOL> plugins [ plugin_name ] = plugin ( self . env ) <EOL> break ; <EOL> for plugin_name in plugins . keys ( ) : <EOL> print "<STR_LIT>" % plugin_name <EOL> return plugins . values ( ) <EOL> def get_project_description ( self ) : <EOL> return self . env . project_description <EOL> def get_users ( self ) : <EOL> result = self . env . get_known_users ( ) <EOL> trac_users = list ( [ ] ) <EOL> for user in result : <EOL> user_login = user [ <NUM_LIT:0> ] . lower ( ) <EOL> if user_login in self . _registered_users_logins : <EOL> continue <EOL> u = TracUser ( user_login ) <EOL> u . email = user [ <NUM_LIT:2> ] <EOL> trac_users . append ( u ) <EOL> self . _registered_users_logins . append ( user_login ) <EOL> if not tracLib . ACCEPT_NON_AUTHORISED_USERS : <EOL> return trac_users <EOL> user_fields = [ ( "<STR_LIT>" , "<STR_LIT>" ) , ( "<STR_LIT>" , "<STR_LIT>" ) , ( "<STR_LIT>" , "<STR_LIT>" ) , ( "<STR_LIT>" , "<STR_LIT>" ) ] <EOL> first = True <EOL> request = "<STR_LIT>" <EOL> for column_name , table_name in user_fields : <EOL> if first : <EOL> first = False <EOL> else : <EOL> request += "<STR_LIT>" <EOL> request += "<STR_LIT>" % ( column_name , table_name ) <EOL> cursor = self . db_cnx . cursor ( ) <EOL> cursor . execute ( request ) <EOL> for row in cursor : <EOL> if row [ <NUM_LIT:0> ] not in self . _registered_users_logins : <EOL> trac_user = self . _get_non_authorised_user ( row [ <NUM_LIT:0> ] ) <EOL> if trac_user is not None : <EOL> trac_users . append ( trac_user ) <EOL> self . _registered_users_logins . append ( trac_user . name ) <EOL> return trac_users <EOL> def _get_non_authorised_user ( self , user_name ) : <EOL> if user_name is None : <EOL> return None <EOL> start = user_name . find ( "<STR_LIT:<>" ) <EOL> end = user_name . rfind ( "<STR_LIT:>>" ) <EOL> if ( start > - <NUM_LIT:1> ) and ( end > start + <NUM_LIT:1> ) : <EOL> if user_name . find ( "<STR_LIT:@>" , start , end ) > <NUM_LIT:0> : <EOL> user = TracUser ( user_name [ start + <NUM_LIT:1> : end ] . replace ( "<STR_LIT:U+0020>" , "<STR_LIT:_>" ) ) <EOL> user . email = user_name [ start + <NUM_LIT:1> : end ] . replace ( "<STR_LIT:U+0020>" , "<STR_LIT:_>" ) <EOL> return user <EOL> return None <EOL> def _get_user_login ( self , user_name ) : <EOL> if user_name is None : <EOL> return None <EOL> if user_name in self . _registered_users_logins : <EOL> return user_name <EOL> if not tracLib . ACCEPT_NON_AUTHORISED_USERS : <EOL> return None <EOL> user = self . _get_non_authorised_user ( user_name ) <EOL> if ( user is None ) or ( user . name not in self . _registered_users_logins ) : <EOL> return None <EOL> return user . name <EOL> def get_severities ( self ) : <EOL> return self . _get_data_from_enum ( "<STR_LIT>" ) <EOL> def get_issue_types ( self ) : <EOL> return self . _get_data_from_enum ( "<STR_LIT>" ) <EOL> def get_issue_priorities ( self ) : <EOL> return self . _get_data_from_enum ( "<STR_LIT>" ) <EOL> def get_issue_resolutions ( self ) : <EOL> return [ TracResolution ( name ) for name in self . _get_data_from_enum ( "<STR_LIT>" ) ] <EOL> def get_components ( self ) : <EOL> cursor = self . db_cnx . cursor ( ) <EOL> cursor . execute ( "<STR_LIT>" ) <EOL> trac_components = list ( [ ] ) <EOL> for row in cursor : <EOL> component = TracComponent ( row [ <NUM_LIT:0> ] ) <EOL> component . owner = self . _get_user_login ( component . owner ) <EOL> if row [ <NUM_LIT:2> ] is not None : <EOL> component . description = row [ <NUM_LIT:2> ] <EOL> trac_components . append ( component ) <EOL> return trac_components <EOL> def get_versions ( self ) : <EOL> cursor = self . db_cnx . cursor ( ) <EOL> cursor . execute ( "<STR_LIT>" ) <EOL> trac_versions = list ( [ ] ) <EOL> for row in cursor : <EOL> version = TracVersion ( row [ <NUM_LIT:0> ] ) <EOL> if row [ <NUM_LIT:1> ] : <EOL> version . time = to_unix_time ( row [ <NUM_LIT:1> ] ) <EOL> if row [ <NUM_LIT:2> ] is not None : <EOL> version . description = row [ <NUM_LIT:2> ] <EOL> trac_versions . append ( version ) <EOL> return trac_versions <EOL> def get_issues ( self ) : <EOL> cursor = self . db_cnx . cursor ( ) <EOL> cursor . execute ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> trac_issues = list ( [ ] ) <EOL> for row in cursor : <EOL> issue = TracIssue ( row [ <NUM_LIT:0> ] ) <EOL> issue . time = to_unix_time ( row [ <NUM_LIT:2> ] ) <EOL> issue . changetime = to_unix_time ( row [ <NUM_LIT:3> ] ) <EOL> issue . reporter = self . _get_user_login ( row [ <NUM_LIT:8> ] ) <EOL> if row [ <NUM_LIT:9> ] is not None : <EOL> cc = row [ <NUM_LIT:9> ] . split ( "<STR_LIT:U+002C>" ) <EOL> for c in cc : <EOL> if len ( c ) > <NUM_LIT:0> : <EOL> cc_name = self . _get_user_login ( c . strip ( ) ) <EOL> if cc_name is not None : <EOL> issue . cc . add ( cc_name ) <EOL> issue . summary = row [ <NUM_LIT> ] <EOL> issue . description = row [ <NUM_LIT> ] <EOL> issue . custom_fields [ "<STR_LIT>" ] = row [ <NUM_LIT:1> ] <EOL> issue . custom_fields [ "<STR_LIT>" ] = row [ <NUM_LIT:4> ] <EOL> issue . custom_fields [ "<STR_LIT>" ] = row [ <NUM_LIT:5> ] <EOL> issue . custom_fields [ "<STR_LIT>" ] = row [ <NUM_LIT:6> ] <EOL> issue . custom_fields [ "<STR_LIT>" ] = self . _get_user_login ( row [ <NUM_LIT:7> ] ) <EOL> issue . custom_fields [ "<STR_LIT>" ] = row [ <NUM_LIT:10> ] <EOL> issue . custom_fields [ "<STR_LIT>" ] = row [ <NUM_LIT:11> ] <EOL> issue . custom_fields [ "<STR_LIT>" ] = row [ <NUM_LIT:12> ] <EOL> if row [ <NUM_LIT:15> ] is not None : <EOL> keywords = row [ <NUM_LIT:15> ] . rsplit ( "<STR_LIT:U+002C>" ) <EOL> for kw in keywords : <EOL> if len ( kw ) > <NUM_LIT:0> : <EOL> issue . keywords . add ( kw . strip ( ) ) <EOL> custom_field_cursor = self . db_cnx . cursor ( ) <EOL> custom_field_cursor . execute ( "<STR_LIT>" , ( str ( row [ <NUM_LIT:0> ] ) , ) ) <EOL> for cf in custom_field_cursor : <EOL> issue . custom_fields [ cf [ <NUM_LIT:0> ] . capitalize ( ) ] = cf [ <NUM_LIT:1> ] <EOL> attachment_cursor = self . db_cnx . cursor ( ) <EOL> attachment_cursor . execute ( "<STR_LIT>" <EOL> "<STR_LIT>" , ( "<STR_LIT>" , str ( issue . id ) ) ) <EOL> for elem in attachment_cursor : <EOL> at = TracAttachment ( Attachment . _get_path ( self . env . path , '<STR_LIT>' , str ( issue . id ) , elem [ <NUM_LIT:0> ] ) ) <EOL> at . name = elem [ <NUM_LIT:0> ] <EOL> at . size = elem [ <NUM_LIT:1> ] <EOL> at . time = to_unix_time ( elem [ <NUM_LIT:2> ] ) <EOL> at . description = elem [ <NUM_LIT:3> ] <EOL> at . author_name = elem [ <NUM_LIT:4> ] <EOL> issue . attachment . add ( at ) <EOL> trac_issues . append ( issue ) <EOL> change_cursor = self . db_cnx . cursor ( ) <EOL> change_cursor . execute ( "<STR_LIT>" , ( str ( row [ <NUM_LIT:0> ] ) , "<STR_LIT>" , ) ) <EOL> for elem in change_cursor : <EOL> if ( elem [ <NUM_LIT:2> ] is None ) or ( not len ( elem [ <NUM_LIT:2> ] . lstrip ( ) ) ) : <EOL> continue <EOL> comment = TracComment ( to_unix_time ( elem [ <NUM_LIT:0> ] ) ) <EOL> comment . author = str ( elem [ <NUM_LIT:1> ] ) <EOL> comment . content = unicode ( elem [ <NUM_LIT:2> ] ) <EOL> comment . id = elem [ <NUM_LIT:3> ] <EOL> issue . comments . add ( comment ) <EOL> for ttp in self . _timetracking_plugins : <EOL> issue . workitems . update ( set ( ttp [ row [ <NUM_LIT:0> ] ] ) ) <EOL> return trac_issues <EOL> def get_custom_fields_declared ( self ) : <EOL> ini_file_path = self . env_path + "<STR_LIT>" <EOL> parser = ConfigParser ( ) <EOL> parser . read ( ini_file_path ) <EOL> if not ( "<STR_LIT>" in parser . sections ( ) ) : <EOL> return set ( [ ] ) <EOL> result = parser . items ( "<STR_LIT>" ) <EOL> items = dict ( [ ] ) <EOL> for elem in result : <EOL> items [ elem [ <NUM_LIT:0> ] ] = elem [ <NUM_LIT:1> ] <EOL> keys = items . keys ( ) <EOL> custom_fields = list ( [ ] ) <EOL> for k in keys : <EOL> if not ( "<STR_LIT:.>" in k ) : <EOL> field = TracCustomFieldDeclaration ( k . capitalize ( ) ) <EOL> field . type = items [ k ] <EOL> options_key = k + "<STR_LIT>" <EOL> if options_key in items : <EOL> opts_str = items [ options_key ] <EOL> opts = opts_str . rsplit ( "<STR_LIT:|>" ) <EOL> for o in opts : <EOL> field . options . append ( o ) <EOL> value_key = k + "<STR_LIT>" <EOL> if value_key in items : <EOL> field . value = items [ value_key ] <EOL> label_key = k + "<STR_LIT>" <EOL> if label_key in items : <EOL> field . label = items [ label_key ] <EOL> custom_fields . append ( field ) <EOL> return custom_fields <EOL> def _get_data_from_enum ( self , type_name ) : <EOL> cursor = self . db_cnx . cursor ( ) <EOL> cursor . execute ( "<STR_LIT>" , ( type_name , ) ) <EOL> return [ row [ <NUM_LIT:0> ] for row in cursor ] </s>
