<s> import datetime <EOL> from distutils . version import LooseVersion <EOL> from . resultsets import ResourceSet <EOL> from . utilities import MemorizeFormatter , is_unicode , to_string <EOL> from . exceptions import ( <EOL> ResourceError , <EOL> ResourceBadMethodError , <EOL> ResourceFilterError , <EOL> ResourceNoFiltersProvidedError , <EOL> ResourceNoFieldsProvidedError , <EOL> ResourceVersionMismatchError , <EOL> ResourceNotFoundError , <EOL> ValidationError , <EOL> ResourceRequirementsError <EOL> ) <EOL> class ResourceManager ( object ) : <EOL> """<STR_LIT>""" <EOL> url = '<STR_LIT>' <EOL> params = { } <EOL> container = None <EOL> def __init__ ( self , redmine , resource_name ) : <EOL> """<STR_LIT>""" <EOL> resource_class = None <EOL> resource_name = '<STR_LIT>' . join ( word [ <NUM_LIT:0> ] . upper ( ) + word [ <NUM_LIT:1> : ] for word in resource_name . split ( '<STR_LIT:_>' ) ) <EOL> resource_paths = tuple ( ( redmine . custom_resource_paths or ( ) ) ) + ( '<STR_LIT>' , ) <EOL> for path in resource_paths : <EOL> try : <EOL> resource_class = getattr ( __import__ ( path , fromlist = [ resource_name ] ) , resource_name ) <EOL> break <EOL> except ( ImportError , AttributeError ) : <EOL> continue <EOL> if resource_class is None : <EOL> raise ResourceError <EOL> if redmine . ver is not None and LooseVersion ( str ( redmine . ver ) ) < LooseVersion ( resource_class . redmine_version ) : <EOL> raise ResourceVersionMismatchError <EOL> self . redmine = redmine <EOL> self . resource_class = resource_class <EOL> def retrieve ( self , ** params ) : <EOL> """<STR_LIT>""" <EOL> self . params . update ( ** params ) <EOL> results = [ ] <EOL> total_count = <NUM_LIT:0> <EOL> limit = self . params . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> offset = self . params . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> if limit == <NUM_LIT:0> : <EOL> limit = <NUM_LIT:100> <EOL> while True : <EOL> try : <EOL> response = self . redmine . request ( '<STR_LIT>' , self . url , params = dict ( self . params , limit = limit , offset = offset ) ) <EOL> except ResourceNotFoundError : <EOL> if self . resource_class . requirements : <EOL> raise ResourceRequirementsError ( self . resource_class . requirements ) <EOL> raise ResourceNotFoundError <EOL> if isinstance ( response [ self . container ] , dict ) : <EOL> results = response [ self . container ] <EOL> total_count = <NUM_LIT:1> <EOL> break <EOL> if all ( response . get ( param ) is not None for param in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> total_count = response [ '<STR_LIT>' ] <EOL> results . extend ( response [ self . container ] ) <EOL> if self . params . get ( '<STR_LIT>' , <NUM_LIT:0> ) == <NUM_LIT:0> : <EOL> offset += limit <EOL> if total_count <= offset : <EOL> break <EOL> else : <EOL> limit -= <NUM_LIT:100> <EOL> offset += <NUM_LIT:100> <EOL> if limit <= <NUM_LIT:0> : <EOL> break <EOL> else : <EOL> total_count = len ( response [ self . container ] ) <EOL> results = response [ self . container ] [ offset : None if self . params . get ( '<STR_LIT>' , <NUM_LIT:0> ) == <NUM_LIT:0> else limit + offset ] <EOL> break <EOL> return results , total_count <EOL> def to_resource ( self , resource ) : <EOL> """<STR_LIT>""" <EOL> return self . resource_class ( self , resource ) <EOL> def to_resource_set ( self , resources ) : <EOL> """<STR_LIT>""" <EOL> return ResourceSet ( self , resources ) <EOL> def new ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . to_resource ( { } ) <EOL> def get ( self , resource_id , ** params ) : <EOL> """<STR_LIT>""" <EOL> if self . resource_class . query_one is None or self . resource_class . container_one is None : <EOL> raise ResourceBadMethodError <EOL> if is_unicode ( resource_id ) : <EOL> resource_id = to_string ( resource_id ) <EOL> try : <EOL> self . url = '<STR_LIT>' . format ( self . redmine . url , self . resource_class . query_one . format ( resource_id , ** params ) ) <EOL> except KeyError as exception : <EOL> raise ValidationError ( '<STR_LIT>' . format ( exception ) ) <EOL> self . params = self . prepare_params ( params ) <EOL> self . container = self . resource_class . container_one <EOL> return self . resource_class ( self , self . retrieve ( ) [ <NUM_LIT:0> ] ) <EOL> def all ( self , ** params ) : <EOL> """<STR_LIT>""" <EOL> if self . resource_class . query_all is None or self . resource_class . container_all is None : <EOL> raise ResourceBadMethodError <EOL> self . url = '<STR_LIT>' . format ( self . redmine . url , self . resource_class . query_all ) <EOL> self . params = self . prepare_params ( params ) <EOL> self . container = self . resource_class . container_all <EOL> return ResourceSet ( self ) <EOL> def filter ( self , ** filters ) : <EOL> """<STR_LIT>""" <EOL> if self . resource_class . query_filter is None or self . resource_class . container_filter is None : <EOL> raise ResourceBadMethodError <EOL> if not filters : <EOL> raise ResourceNoFiltersProvidedError <EOL> try : <EOL> self . url = '<STR_LIT>' . format ( self . redmine . url , self . resource_class . query_filter . format ( ** filters ) ) <EOL> self . container = self . resource_class . container_filter . format ( ** filters ) <EOL> except KeyError : <EOL> raise ResourceFilterError <EOL> self . params = self . prepare_params ( filters ) <EOL> return ResourceSet ( self ) <EOL> def create ( self , ** fields ) : <EOL> """<STR_LIT>""" <EOL> if self . resource_class . query_create is None or self . resource_class . container_create is None : <EOL> raise ResourceBadMethodError <EOL> if not fields : <EOL> raise ResourceNoFieldsProvidedError <EOL> formatter = MemorizeFormatter ( ) <EOL> title = fields . get ( '<STR_LIT:title>' ) <EOL> if title is not None and is_unicode ( title ) : <EOL> fields [ '<STR_LIT:title>' ] = to_string ( title ) <EOL> try : <EOL> url = '<STR_LIT>' . format ( self . redmine . url , formatter . format ( self . resource_class . query_create , ** fields ) ) <EOL> except KeyError as exception : <EOL> raise ValidationError ( '<STR_LIT>' . format ( exception ) ) <EOL> self . container = self . resource_class . container_one <EOL> data = { self . resource_class . container_create : self . prepare_params ( formatter . unused_kwargs ) } <EOL> if '<STR_LIT>' in data [ self . resource_class . container_create ] : <EOL> data [ '<STR_LIT>' ] = data [ self . resource_class . container_create ] . pop ( '<STR_LIT>' ) <EOL> for index , attachment in enumerate ( data [ '<STR_LIT>' ] ) : <EOL> data [ '<STR_LIT>' ] [ index ] [ '<STR_LIT>' ] = self . redmine . upload ( attachment . get ( '<STR_LIT:path>' , '<STR_LIT>' ) ) <EOL> try : <EOL> response = self . redmine . request ( '<STR_LIT>' , url , data = data ) <EOL> except ResourceNotFoundError : <EOL> response = self . redmine . request ( '<STR_LIT>' , url , data = data ) <EOL> try : <EOL> resource = self . to_resource ( response [ self . container ] ) <EOL> except TypeError : <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> self . params = formatter . used_kwargs <EOL> self . url = '<STR_LIT>' . format ( <EOL> self . redmine . url , <EOL> self . resource_class . query_one . format ( resource . internal_id , ** fields ) <EOL> ) <EOL> return resource <EOL> def update ( self , resource_id , ** fields ) : <EOL> """<STR_LIT>""" <EOL> if self . resource_class . query_update is None or self . resource_class . container_update is None : <EOL> raise ResourceBadMethodError <EOL> if not fields : <EOL> raise ResourceNoFieldsProvidedError <EOL> formatter = MemorizeFormatter ( ) <EOL> if is_unicode ( resource_id ) : <EOL> resource_id = to_string ( resource_id ) <EOL> try : <EOL> query_update = formatter . format ( self . resource_class . query_update , resource_id , ** fields ) <EOL> except KeyError as exception : <EOL> param = str ( exception ) . replace ( "<STR_LIT:'>" , "<STR_LIT>" ) <EOL> if param in self . params : <EOL> fields [ param ] = self . params [ param ] <EOL> query_update = formatter . format ( self . resource_class . query_update , resource_id , ** fields ) <EOL> else : <EOL> raise ValidationError ( '<STR_LIT>' . format ( exception ) ) <EOL> url = '<STR_LIT>' . format ( self . redmine . url , query_update ) <EOL> data = { self . resource_class . container_update : self . prepare_params ( formatter . unused_kwargs ) } <EOL> if '<STR_LIT>' in data [ self . resource_class . container_update ] : <EOL> data [ '<STR_LIT>' ] = data [ self . resource_class . container_update ] . pop ( '<STR_LIT>' ) <EOL> for index , attachment in enumerate ( data [ '<STR_LIT>' ] ) : <EOL> data [ '<STR_LIT>' ] [ index ] [ '<STR_LIT>' ] = self . redmine . upload ( attachment . get ( '<STR_LIT:path>' , '<STR_LIT>' ) ) <EOL> return self . redmine . request ( '<STR_LIT>' , url , data = data ) <EOL> def delete ( self , resource_id , ** params ) : <EOL> """<STR_LIT>""" <EOL> if self . resource_class . query_delete is None : <EOL> raise ResourceBadMethodError <EOL> if is_unicode ( resource_id ) : <EOL> resource_id = to_string ( resource_id ) <EOL> try : <EOL> url = '<STR_LIT>' . format ( self . redmine . url , self . resource_class . query_delete . format ( resource_id , ** params ) ) <EOL> except KeyError as exception : <EOL> raise ValidationError ( '<STR_LIT>' . format ( exception ) ) <EOL> return self . redmine . request ( '<STR_LIT>' , url , params = self . prepare_params ( params ) ) <EOL> def prepare_params ( self , params ) : <EOL> """<STR_LIT>""" <EOL> for name , value in params . items ( ) : <EOL> type_ = type ( value ) <EOL> if type_ is datetime . date : <EOL> params [ name ] = value . strftime ( self . redmine . date_format ) <EOL> elif type_ is datetime . datetime : <EOL> params [ name ] = value . strftime ( self . redmine . datetime_format ) <EOL> return self . resource_class . translate_params ( params ) </s>
