From 928b19226fa2fa90e17f797621e6641ab8934e14 Mon Sep 17 00:00:00 2001 From: Garrett Holmstrom Date: Fri, 14 Oct 2016 18:48:09 -0700 Subject: [PATCH] Eliminate _post_init --- requestbuilder/command.py | 8 +++----- requestbuilder/request.py | 33 ++++++++++++++++++++------------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/requestbuilder/command.py b/requestbuilder/command.py index 55bcbe2..15b63b4 100644 --- a/requestbuilder/command.py +++ b/requestbuilder/command.py @@ -99,13 +99,11 @@ class BaseCommand(object): # # Derived classes MUST call this method to ensure things stay sane. self.__do_cli = _do_cli - self._post_init() - - def _post_init(self): self._build_parser() if self.__do_cli: # Distribute CLI args to the various places that need them self.process_cli_args() + # This will raise an exception if kwargs contains anything unexpected. self.distribute_args() try: self.configure() @@ -250,7 +248,7 @@ class BaseCommand(object): # This makes it slightly more obvious that this is redacted by # the framework and not just a string by removing quotes. cli_args[key] = redacted - self.log.debug('parsed arguments: ' + str(cli_args)) + self.log.debug('parsed arguments from CLI: %s', str(cli_args)) def distribute_args(self): for key, val in self.args.iteritems(): @@ -337,7 +335,7 @@ class BaseCommand(object): def handle_cli_exception(self, err): msg_prefix = '{0}: error:'.format(os.path.basename(sys.argv[0])) if isinstance(err, ArgumentError) and self.__do_cli and not self.debug: - # Note that, unlike _post_init, we get to use self.debug instead + # Note that, unlike __init__, we get to use self.debug instead # of self.__debug self._cli_parser.error(str(err)) if isinstance(err, EnvironmentError): diff --git a/requestbuilder/request.py b/requestbuilder/request.py index 2e14bdb..8e15e4f 100644 --- a/requestbuilder/request.py +++ b/requestbuilder/request.py @@ -32,7 +32,7 @@ from requestbuilder.xmlparse import parse_listdelimited_aws_xml class BaseRequest(BaseCommand): - ''' + """ The basis for a command line tool that represents a request. The data for this request are stored in a few instance members: - method: the HTTP method to use (e.g. 'GET'). Defaults to self.METHOD. @@ -60,10 +60,12 @@ class BaseRequest(BaseCommand): Important members of this class, in addition to those inherited from BaseCommand, include: - SERVICE_CLASS: a class corresponding to the web service in use - - NAME: a string representing the name of this request. This - defaults to the class's name. + - AUTH_CLASS: a class corresponding to the authentication method + to use, if any + - NAME: a string representing the name of this request. + This defaults to the class's name. - METHOD: the HTTP method to use by default - ''' + """ SERVICE_CLASS = BaseService AUTH_CLASS = None @@ -73,8 +75,6 @@ class BaseRequest(BaseCommand): DEFAULT_ROUTES = (PARAMS,) def __init__(self, service=None, auth=None, **kwargs): - self.auth = auth - self.service = service # Parts of the HTTP request to be sent to the server. self.method = self.METHOD self.path = None @@ -87,18 +87,25 @@ class BaseRequest(BaseCommand): self.response = None self.__configured = False + self.__auth = auth + self.__service = service BaseCommand.__init__(self, **kwargs) - def _post_init(self): - if self.service is None and self.SERVICE_CLASS is not None: - self.service = self.SERVICE_CLASS(self.config, - loglevel=self.log.level) - if self.auth is None and self.AUTH_CLASS is not None: + @property + def auth(self): + if not self.__auth and self.AUTH_CLASS is not None: # pylint: disable=not-callable - self.auth = self.AUTH_CLASS(self.config, loglevel=self.log.level) + self.__auth = self.AUTH_CLASS(self.config, loglevel=self.log.level) # pylint: enable=not-callable - BaseCommand._post_init(self) + return self.__auth + + @property + def service(self): + if not self.__service and self.SERVICE_CLASS is not None: + self.__service = self.SERVICE_CLASS(self.config, + loglevel=self.log.level) + return self.__service @classmethod def from_other(cls, other, **kwargs):