<s> import sys <EOL> import os <EOL> from pyactiveresource . activeresource import ActiveResource <EOL> from pyactiveresource . connection import ResourceNotFound , MethodNotAllowed <EOL> import urlparse <EOL> import pprint <EOL> class RedmineResource ( ActiveResource ) : <EOL> def __init__ ( self , attributes = None , prefix_options = None ) : <EOL> if isinstance ( attributes , basestring ) : <EOL> self . name = attributes <EOL> super ( RedmineResource , self ) . __init__ ( attributes , prefix_options ) <EOL> def __repr__ ( self ) : <EOL> return pprint . pformat ( self . attributes , indent = <NUM_LIT:2> , width = <NUM_LIT> ) <EOL> class Project ( RedmineResource ) : pass <EOL> class Issue ( RedmineResource ) : pass <EOL> class Group ( RedmineResource ) : pass <EOL> class User ( RedmineResource ) : pass <EOL> class Membership ( RedmineResource ) : pass <EOL> class Role ( RedmineResource ) : pass <EOL> class Version ( RedmineResource ) : pass <EOL> class IssueCategory ( RedmineResource ) : pass <EOL> class TimeEntry ( RedmineResource ) : pass <EOL> class Permission ( RedmineResource ) : pass <EOL> class CustomField ( RedmineResource ) : pass <EOL> class RedmineException ( Exception ) : pass <EOL> class RedmineClient ( object ) : <EOL> def __init__ ( self , api_key , url , login , password ) : <EOL> RedmineResource . site = url <EOL> if api_key is not None : <EOL> RedmineResource . headers = { '<STR_LIT>' : api_key } <EOL> else : <EOL> self . _user = login <EOL> self . _password = password <EOL> RedmineResource . user = login <EOL> RedmineResource . password = password <EOL> if not Membership . prefix_source . endswith ( '<STR_LIT:/>' ) : <EOL> Membership . prefix_source += '<STR_LIT:/>' <EOL> Membership . prefix_source += '<STR_LIT>' <EOL> @ property <EOL> def headers ( self ) : <EOL> headers = { } <EOL> if RedmineResource . headers : <EOL> for name , value in RedmineResource . headers . items ( ) : <EOL> headers [ name ] = value <EOL> if RedmineResource . connection . auth : <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT>' + RedmineResource . connection . auth <EOL> return headers <EOL> def get_project ( self , project_id ) : <EOL> try : <EOL> return Project . find ( project_id ) <EOL> except ResourceNotFound : <EOL> raise RedmineException ( <EOL> "<STR_LIT>" % project_id ) <EOL> def get_projects ( self , project_ids = None ) : <EOL> if project_ids : <EOL> return [ self . get_project ( id_ ) for id_ in project_ids ] <EOL> return Project . find ( ) <EOL> def get_issues ( self , project_id = None ) : <EOL> return Issue . find ( None , None , project_id = project_id ) <EOL> def get_issue_details ( self , issue_id ) : <EOL> return Issue . find ( issue_id , <EOL> include = '<STR_LIT>' ) <EOL> def get_project_issues ( self , _id , _limit = None , _offset = None ) : <EOL> return_data = [ ] <EOL> if _limit : <EOL> if _offset is None : <EOL> _offset = <NUM_LIT:0> <EOL> issues = Issue . find ( None , None , project_id = _id , <EOL> limit = _limit , offset = _offset , sort = '<STR_LIT:id>' , status_id = '<STR_LIT:*>' ) <EOL> elif _offset : <EOL> issues = Issue . find ( None , None , project_id = _id , <EOL> offset = _offset , sort = '<STR_LIT:id>' , status_id = '<STR_LIT:*>' ) <EOL> else : <EOL> issues = Issue . find ( None , None , project_id = _id , sort = '<STR_LIT:id>' , status_id = '<STR_LIT:*>' ) <EOL> for issue_id in [ issue . id for issue in issues ] : <EOL> return_data . append ( self . get_issue_details ( issue_id ) ) <EOL> return return_data <EOL> def get_user ( self , user_id ) : <EOL> return User . find ( user_id , None , include = '<STR_LIT>' ) <EOL> def get_users ( self , user_ids = None ) : <EOL> if user_ids : <EOL> return [ self . get_user ( uid ) for uid in user_ids ] <EOL> return User . find ( None , None , include = '<STR_LIT>' ) <EOL> def get_groups ( self ) : <EOL> return Group . find ( ) <EOL> def get_project_members ( self , id_ ) : <EOL> return Membership . find ( None , None , project_id = id_ ) <EOL> def get_roles ( self ) : <EOL> roles = Role . find ( ) <EOL> for role in roles : <EOL> try : <EOL> role . attributes [ '<STR_LIT>' ] = Role ( ) . find ( role . id ) . permissions <EOL> except ( ResourceNotFound , MethodNotAllowed ) : <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> break <EOL> return roles <EOL> def get_category ( self , id_ ) : <EOL> return IssueCategory . find ( id_ ) <EOL> def get_version ( self , id_ ) : <EOL> return Version . find ( id_ ) <EOL> def get_time_entries ( self , issue_id ) : <EOL> return TimeEntry . find ( None , None , issue_id = issue_id ) </s>
