Repository: incubator-ariatosca Updated Branches: refs/heads/pylint-aria-storage 03b2a22b5 -> 7064c90ce (forced update)
pylint cli package Project: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/commit/66ed02aa Tree: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/tree/66ed02aa Diff: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/diff/66ed02aa Branch: refs/heads/pylint-aria-storage Commit: 66ed02aaac2f0459038f98b090089aa7f6eee93c Parents: 177a3f6 Author: Dan Kilman <[email protected]> Authored: Wed Oct 19 17:21:29 2016 +0300 Committer: Dan Kilman <[email protected]> Committed: Thu Oct 20 12:42:46 2016 +0300 ---------------------------------------------------------------------- aria/cli/args_parser.py | 23 +++++++++++++++++-- aria/cli/cli.py | 13 +++++++++++ aria/cli/commands.py | 53 +++++++++++++++++++++++++++++--------------- aria/cli/config.py | 11 ++++++++- aria/cli/exceptions.py | 24 ++++++++++++++++++++ aria/cli/storage.py | 25 +++++++++++++++++++++ 6 files changed, 128 insertions(+), 21 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/66ed02aa/aria/cli/args_parser.py ---------------------------------------------------------------------- diff --git a/aria/cli/args_parser.py b/aria/cli/args_parser.py index 30d213f..f40919c 100644 --- a/aria/cli/args_parser.py +++ b/aria/cli/args_parser.py @@ -13,6 +13,10 @@ # See the License for the specific language governing permissions and # limitations under the License. +""" +Argument parsing configuration and functions +""" + import argparse from functools import partial @@ -20,6 +24,9 @@ NO_VERBOSE = 0 class SmartFormatter(argparse.HelpFormatter): + """ + TODO: what is this? + """ def _split_lines(self, text, width): if text.startswith('R|'): return text[2:].splitlines() @@ -27,10 +34,13 @@ class SmartFormatter(argparse.HelpFormatter): def sub_parser_decorator(func=None, **parser_settings): + """ + Decorated for sub_parser argument definitions + """ if not func: return partial(sub_parser_decorator, **parser_settings) - def wrapper(parser): + def _wrapper(parser): sub_parser = parser.add_parser(**parser_settings) sub_parser.add_argument( '-v', '--verbose', @@ -44,10 +54,13 @@ def sub_parser_decorator(func=None, **parser_settings): help='A unique ID for the deployment') func(sub_parser) return sub_parser - return wrapper + return _wrapper def config_parser(parser=None): + """ + Top level argparse configuration + """ parser = parser or argparse.ArgumentParser( prog='Aria', description="Aria's Command Line Interface", @@ -64,6 +77,9 @@ def config_parser(parser=None): help='Initialize environment', formatter_class=SmartFormatter) def add_init_parser(init): + """ + ``init`` command parser configuration + """ init.add_argument( '-p', '--blueprint-path', dest='blueprint_path', @@ -90,6 +106,9 @@ def add_init_parser(init): help='Execute a workflow', formatter_class=SmartFormatter) def add_execute_parser(execute): + """ + ``execute`` command parser configuration + """ execute.add_argument( '-w', '--workflow', dest='workflow_id', http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/66ed02aa/aria/cli/cli.py ---------------------------------------------------------------------- diff --git a/aria/cli/cli.py b/aria/cli/cli.py index 24573c9..a4046ac 100644 --- a/aria/cli/cli.py +++ b/aria/cli/cli.py @@ -13,6 +13,10 @@ # See the License for the specific language governing permissions and # limitations under the License. +""" +CLI Entry point +""" + import logging from aria.logger import ( @@ -31,6 +35,9 @@ __version__ = '0.1.0' class AriaCli(LoggerMixin): + """ + Context manager based class that enables proper top level error handling + """ def __init__(self, *args, **kwargs): super(AriaCli, self).__init__(*args, **kwargs) @@ -56,6 +63,9 @@ class AriaCli(LoggerMixin): pass def run(self): + """ + Parses user arguments and run the appropriate command + """ parser = config_parser() args = parser.parse_args() @@ -66,6 +76,9 @@ class AriaCli(LoggerMixin): def main(): + """ + CLI entry point + """ create_logger( handlers=[ create_console_log_handler(), http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/66ed02aa/aria/cli/commands.py ---------------------------------------------------------------------- diff --git a/aria/cli/commands.py b/aria/cli/commands.py index ab72435..a38229d 100644 --- a/aria/cli/commands.py +++ b/aria/cli/commands.py @@ -13,6 +13,10 @@ # See the License for the specific language governing permissions and # limitations under the License. +""" +CLI various commands implementation +""" + import json import os import sys @@ -51,6 +55,10 @@ from dsl_parser.tasks import prepare_deployment_plan class BaseCommand(LoggerMixin): + """ + Base class for CLI commands + """ + def __repr__(self): return 'AriaCli({cls.__name__})'.format(cls=self.__class__) @@ -73,20 +81,20 @@ class BaseCommand(LoggerMixin): parsed_dict = {} - def format_to_dict(input_string): + def _format_to_dict(input_string): self.logger.info('Processing inputs source: {0}'.format(input_string)) try: input_string = input_string.strip() try: parsed_dict.update(json.loads(input_string)) - except: + except BaseException: parsed_dict.update((input.split('=') - for input in input_string.split(';') - if input)) + for input in input_string.split(';') + if input)) except Exception as exc: raise AriaCliFormatInputsError(str(exc), inputs=input_string) - def handle_inputs_source(input_path): + def _handle_inputs_source(input_path): self.logger.info('Processing inputs source: {0}'.format(input_path)) try: with open(input_path) as input_file: @@ -104,26 +112,30 @@ class BaseCommand(LoggerMixin): for input_string in inputs if isinstance(inputs, list) else [inputs]: if os.path.isdir(input_string): for input_file in os.listdir(input_string): - handle_inputs_source(os.path.join(input_string, input_file)) + _handle_inputs_source(os.path.join(input_string, input_file)) continue input_files = glob(input_string) if input_files: for input_file in input_files: - handle_inputs_source(input_file) + _handle_inputs_source(input_file) continue - format_to_dict(input_string) + _format_to_dict(input_string) return parsed_dict class InitCommand(BaseCommand): + """ + ``init`` command implementation + """ + _IN_VIRTUAL_ENV = hasattr(sys, 'real_prefix') def __call__(self, args_namespace): super(InitCommand, self).__call__(args_namespace) - self.workspace_setup() + self._workspace_setup() inputs = self.parse_inputs(args_namespace.input) if args_namespace.input else None - plan, deployment_plan = self.parse_blueprint(args_namespace.blueprint_path, inputs) - self.create_storage( + plan, deployment_plan = self._parse_blueprint(args_namespace.blueprint_path, inputs) + self._create_storage( blueprint_plan=plan, blueprint_path=args_namespace.blueprint_path, deployment_plan=deployment_plan, @@ -133,10 +145,10 @@ class InitCommand(BaseCommand): self.logger.info('Initiated {0}'.format(args_namespace.blueprint_path)) self.logger.info( 'If you make changes to the blueprint, ' - 'run `aria local init` command again to apply them'.format( + 'run `aria local init -p {0}` command again to apply them'.format( args_namespace.blueprint_path)) - def workspace_setup(self): + def _workspace_setup(self): try: create_user_space() self.logger.debug( @@ -153,14 +165,14 @@ class InitCommand(BaseCommand): 'local storage path already exist - {0}'.format(local_storage())) return local_storage() - def parse_blueprint(self, blueprint_path, inputs=None): + def _parse_blueprint(self, blueprint_path, inputs=None): plan = parse_from_path(blueprint_path) self.logger.info('blueprint parsed successfully') deployment_plan = prepare_deployment_plan(plan=plan.copy(), inputs=inputs) return plan, deployment_plan - def create_storage( - self, + @staticmethod + def _create_storage( blueprint_path, blueprint_plan, deployment_plan, @@ -192,6 +204,10 @@ class InitCommand(BaseCommand): class ExecuteCommand(BaseCommand): + """ + ``execute`` command implementation + """ + def __call__(self, args_namespace): super(ExecuteCommand, self).__call__(args_namespace) parameters = (self.parse_inputs(args_namespace.parameters) @@ -232,8 +248,8 @@ class ExecuteCommand(BaseCommand): workflow_engine.execute() executor.close() + @staticmethod def _merge_and_validate_execution_parameters( - self, workflow, workflow_name, execution_parameters): @@ -269,7 +285,8 @@ class ExecuteCommand(BaseCommand): return merged_parameters - def _load_workflow_handler(self, handler_path): + @staticmethod + def _load_workflow_handler(handler_path): module_name, spec_handler_name = handler_path.rsplit('.', 1) try: module = import_module(module_name) http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/66ed02aa/aria/cli/config.py ---------------------------------------------------------------------- diff --git a/aria/cli/config.py b/aria/cli/config.py index a230bc0..d82886d 100644 --- a/aria/cli/config.py +++ b/aria/cli/config.py @@ -13,11 +13,16 @@ # See the License for the specific language governing permissions and # limitations under the License. +""" +CLI configuration +""" + import os +import logging from getpass import getuser from tempfile import gettempdir + from yaml import safe_load -import logging from .storage import config_file_path @@ -32,6 +37,10 @@ import_resolver = None def load_configurations(): + """ + Dynamically load attributes into the config module from the ``config.yaml`` defined in the user + configuration directory + """ config_path = config_file_path() with open(config_path) as config_file: globals().update(safe_load(config_file) or {}) http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/66ed02aa/aria/cli/exceptions.py ---------------------------------------------------------------------- diff --git a/aria/cli/exceptions.py b/aria/cli/exceptions.py index fb120be..6897731 100644 --- a/aria/cli/exceptions.py +++ b/aria/cli/exceptions.py @@ -13,17 +13,31 @@ # See the License for the specific language governing permissions and # limitations under the License. +""" +CLI various exception classes +""" + class AriaCliError(Exception): + """ + General CLI Exception class + """ pass class AriaCliFormatInputsError(AriaCliError): + """ + Raised when provided inputs are malformed. + """ + def __init__(self, message, inputs): self.inputs = inputs super(AriaCliFormatInputsError, self).__init__(message) def user_message(self): + """ + Describes the format error in detail. + """ return ( 'Invalid input format: {0}, ' 'the expected format is: ' @@ -31,11 +45,21 @@ class AriaCliFormatInputsError(AriaCliError): class AriaCliYAMLInputsError(AriaCliError): + """ + Raised when an invalid yaml file is provided + """ pass class AriaCliInvalidInputsError(AriaCliFormatInputsError): + """ + Raised when provided inputs are invalid. + """ + def user_message(self): + """ + Describes the error in detail. + """ return ( 'Invalid input: {0}. input must represent a dictionary.\n' 'Valid values can be one of:\n' http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/66ed02aa/aria/cli/storage.py ---------------------------------------------------------------------- diff --git a/aria/cli/storage.py b/aria/cli/storage.py index 05ed395..fa1518b 100644 --- a/aria/cli/storage.py +++ b/aria/cli/storage.py @@ -13,6 +13,10 @@ # See the License for the specific language governing permissions and # limitations under the License. +""" +Filesystem related CLI storage location and configuration +""" + import os import getpass from shutil import rmtree @@ -22,6 +26,9 @@ storage_directory_name = 'local-storage' def user_space(user_name=getpass.getuser()): + """ + Base work directory + """ user_path = '~{0}'.format(user_name) real_path = os.path.expanduser(user_path) if os.path.exists(real_path): @@ -30,18 +37,30 @@ def user_space(user_name=getpass.getuser()): def local_storage(user_name=getpass.getuser()): + """ + Base storage directory + """ return os.path.join(user_space(user_name), storage_directory_name) def local_model_storage(): + """ + Model storage directory + """ return os.path.join(local_storage(), 'models') def local_resource_storage(): + """ + Resource storage directory + """ return os.path.join(local_storage(), 'resources') def config_file_path(): + """ + Configuration file path + """ path = os.path.join(user_space(), 'config.yaml') if not os.path.exists(path): open(path, 'w').close() @@ -49,6 +68,9 @@ def config_file_path(): def create_user_space(user_name=getpass.getuser(), override=False): + """ + Creates the base work directory + """ path = user_space(user_name) if os.path.exists(path): if override: @@ -60,6 +82,9 @@ def create_user_space(user_name=getpass.getuser(), override=False): def create_local_storage(user_name=getpass.getuser(), override=False): + """ + Creates the base storage directory + """ path = local_storage(user_name) if os.path.exists(path): if override:
