Exemple #1
0
 def __init__(self):
     self.web_api = WebAPI()
     self.components_helper = ComponentsHelper()
Exemple #2
0
class ShowHelper(object):

    def __init__(self):
        self.web_api = WebAPI()
        self.components_helper = ComponentsHelper()

    @staticmethod
    def action_show_platforms(api_data):
        # type: (dict) -> bool
        """
        Print existing platforms.
        :param api_data: api data set
        :return: result
        """
        platforms = []

        if 'organization' not in api_data:
            print_line('Organization info error.')
            return False

        if 'platforms' not in api_data['organization']:
            print_line('Platform info error.')
            return False

        if len(api_data['organization']['platforms']) == 0:
            print_line('You have not Platforms.')
            return False

        for platform in api_data['organization']['platforms']:
            platforms.append({'name': platform['name'], 'description': platform['description']})

        print_platforms(platforms=platforms, title="Platforms", filename=api_data['file'])

        return True

    def action_show_projects(self, api_data):
        # type: (dict) -> bool
        """
        Print existing project for defined Platform.
        :param api_data: api data set
        :return: result
        """
        projects = []

        if 'organization' not in api_data:
            print_line('Organization info error.')
            return False

        if 'platforms' not in api_data['organization']:
            print_line('Platform info error.')
            return False

        if len(api_data['organization']['platforms']) == 0:
            print_line('You have not Platforms.')
            return False

        platform_number = self.web_api.get_platform_number_by_name(api_data=api_data)

        if platform_number == -1:
            print_line("No such platform: {0}.".format(api_data['platform']))
            return False

        if len(api_data['organization']['platforms'][platform_number]['projects']) == 0:
            print_line('You have not Projects.')
            return False

        for project in api_data['organization']['platforms'][platform_number]['projects']:
            projects.append({'name': project['name'], 'description': 'default project'})

        print_projects(projects=projects, title=api_data['platform'], filename=api_data['file'])

        return True

    def action_show_set(self, api_data):
        # type: (dict) -> bool
        """
        Print current Component set for defined Platform/Project.
        :param api_data: api data set
        :return: result
        """
        my_set = self.components_helper.get_current_set_name(api_data=api_data)

        if my_set[0] is None:
            print_line('Get set name error.')
            return False

        print_line('Current component set: {0}.'.format(my_set[0]))

        components = self.components_helper.get_current_component_set(api_data=api_data)[0]['components']

        if components[0] is None:
            print_line('Get component set error.')
            return False

        print_components(components=components, title=api_data['platform'] + '/' + api_data['project'], filename=api_data['file'])

        return True

    def action_show_issues(self, api_data):
        # type: (dict) -> bool
        """
        Print current Issues tor defined Platform/Project.
        :param api_data: api data set
        :return: result
        """
        api_data['platform_number'] = self.web_api.get_platform_number_by_name(api_data=api_data)

        if api_data['platform_number'] == -1:
            print_line("No such platform: {0}.".format(api_data['platform']))
            return False

        api_data['project_number'] = self.web_api.get_project_number_by_name(api_data=api_data)

        if api_data['project_number'] == -1:
            print_line("No such project {0} in platform {1}.".format(api_data['project'], api_data['platform']))
            return False

        api_data['project_url'] = api_data['organization']['platforms'][api_data['platform_number']]['projects'][api_data['project_number']]['url']

        if not self.web_api.send_get_issues_request(api_data=api_data):
            print_line("Cant load issues for platform {0} and project {1}.".format(api_data['platform'], api_data['project']))
            return False

        issues = api_data['issues']

        printed_issues = []

        for issue in issues:
            printed_issues.append({'name': 'title', 'description': issue['title']})
            printed_issues.append({'name': 'component', 'description': issue['component']['name'] + ' - ' + issue['component']['version']})
            printed_issues.append({'name': 'description', 'description': issue['description']})
            printed_issues.append({'name': 'author', 'description': issue['author']})
            printed_issues.append({'name': 'status', 'description': issue['status']})
            printed_issues.append({'name': '\n', 'description': ''})

        print_issues(issues=printed_issues, title=api_data['platform'] + '/' + api_data['project'], filename=api_data['file'])

        return True
Exemple #3
0
class SetHelper(object):
    def __init__(self):
        self.web_api = WebAPI()
        self.components_helper = ComponentsHelper()

    def create_set_validate(self, api_data):
        # type: (dict) -> bool
        if api_data['platform'] is None:
            print_line(
                'Empty Platform name. Please use --platform=platform_name parameter.'
            )
            return False

        platforms = self.get_my_platforms(api_data=api_data)

        if api_data['platform'] not in platforms:
            print_line("Platform {0} does not exists.".format(
                api_data['platform']))
            return False

        if api_data['project'] is None:
            print_line(
                'Empty Project name. Please use --project=project_name parameter.'
            )
            return False

        projects = self.get_my_projects(api_data=api_data)

        if api_data['project'] not in projects:
            print_line("Project {0} does not exists.".format(
                api_data['project']))
            return False

        set_name = api_data['set']
        current_set_name = self.components_helper.get_current_set_name(
            api_data=api_data)[0]

        if current_set_name == set_name:
            print_line(
                'Current set with name {0} already exists.'.format(set_name))
            print_line(
                'Please, use another name, or use no --set parameter to autoincrement set name.'
            )
            return False

        if set_name is None:
            if current_set_name[-1].isdigit():
                d = int(current_set_name[-1])
                d = d + 1
                current_set_name = current_set_name[:-1]
                set_name = current_set_name + str(d)

            else:
                set_name = current_set_name + '.1'

        api_data['set'] = set_name

        return True

    # Target = OS packages

    def collect_data_for_set_os_auto_system_none(self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with OS packages, collected by shell command.
        :param api_data: api data set
        :return: result
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_os_auto_system_none(
                api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".format(api_data['project']))
                return False

            api_data['project_url'] = api_data['organization']['platforms'][
                api_data['platform_number']]['projects'][
                    api_data['project_number']]['url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True

        return False

    def collect_data_for_set_os_auto_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with OS packages, collected from shell command
        and stored in file, defined in path.
        :param api_data: api data set
        :return: result
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_os_auto_system_path(
                api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".format(api_data['project']))
                return False

            api_data['project_url'] = api_data['organization']['platforms'][
                api_data['platform_number']]['projects'][
                    api_data['project_number']]['url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True

        return False

    # Target = Python packages

    def collect_data_for_set_pip_auto_system_none(self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with Python PIP packages, collected from shell command.
        :param api_data:
        :return:
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_pip_auto_system_none(
                api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".format(api_data['project']))
                return False

            api_data['project_url'] = api_data['organization']['platforms'][
                api_data['platform_number']]['projects'][
                    api_data['project_number']]['url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True

        return False

    def collect_data_for_set_pip_auto_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with Python PIP packages, collected from shell command
        and stored in file, defined in path.
        :param api_data: api data set
        :return: result
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_pip_auto_system_path(
                api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".format(api_data['project']))
                return False

            api_data['project_url'] = api_data['organization']['platforms'][
                api_data['platform_number']]['projects'][
                    api_data['project_number']]['url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True

        return False

    def collect_data_for_set_requirements_auto_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with Python requirements.txt file, defined in path.
        :param api_data: spi data set
        :return: result
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_requirements_auto_system_path(
                api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".format(api_data['project']))
                return False

            api_data['project_url'] = api_data['organization']['platforms'][
                api_data['platform_number']]['projects'][
                    api_data['project_number']]['url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True

        return False

    def collect_data_for_set_npm_auto_system_none(self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with NPM packages, collected from shell command (nmp list --json).
        Shell command runs global from root path.
        :param api_data:
        :return:
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_npm_auto_system_none(
                api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".format(api_data['project']))
                return False

            api_data['project_url'] = api_data['organization']['platforms'][
                api_data['platform_number']]['projects'][
                    api_data['project_number']]['url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True

        return False

    def collect_data_for_set_npm_local_auto_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with NPM packages, collected from shell command (npm list --json).
        Shell command runs local from path, defined by --file parameter.
        :param api_data:
        :return:
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_npm_local_auto_system_none(
                api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".format(api_data['project']))
                return False

            api_data['project_url'] = api_data['organization']['platforms'][
                api_data['platform_number']]['projects'][
                    api_data['project_number']]['url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True

        return False

    def collect_data_for_set_npm_auto_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with NPM packages, collected from shell command (npm list --json)
        and stored in file, defined in path.
        :param api_data: api data set
        :return: result
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_npm_auto_system_path(
                api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".format(api_data['project']))
                return False

            api_data['project_url'] = api_data['organization']['platforms'][
                api_data['platform_number']]['projects'][
                    api_data['project_number']]['url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True

        return False

    def collect_data_for_set_package_json_auto_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with NPM packages from package.json, defined by --file parameter.
        :param api_data: api data set
        :return: result
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_package_json_auto_system_path(
                api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".format(api_data['project']))
                return False

            api_data['project_url'] = api_data['organization']['platforms'][
                api_data['platform_number']]['projects'][
                    api_data['project_number']]['url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True

        return False

    def collect_data_for_set_package_lock_json_auto_system_path(
            self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with NPM packages from package-lock.json, defined by --file parameter.
        :param api_data:
        :return:
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_npm_lock_auto_system_path(
                api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".format(api_data['project']))
                return False

            api_data['project_url'] = api_data['organization']['platforms'][
                api_data['platform_number']]['projects'][
                    api_data['project_number']]['url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True

        return False

    def collect_data_for_set_gem_auto_system_none(self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with Ruby packages, collected from shell command.
        :param api_data:
        :return:
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_gem_auto_system_none(
                api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".format(api_data['project']))
                return False

            api_data['project_url'] = api_data['organization']['platforms'][
                api_data['platform_number']]['projects'][
                    api_data['project_number']]['url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True

        return False

    def collect_data_for_set_gem_auto_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with Ruby packages, collected from shell command and
        stored in gem list file, defined in --file parameter.
        :param api_data:
        :return:
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_gem_auto_system_path(
                api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".format(api_data['project']))
                return False

            api_data['project_url'] = api_data['organization']['platforms'][
                api_data['platform_number']]['projects'][
                    api_data['project_number']]['url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True

        return False

    def collect_data_for_set_gemfile_auto_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with Ruby packages, collected from Gemfile, defined
        by --file parameter.
        :param api_data:
        :return:
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_gemfile_auto_system_path(
                api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".format(api_data['project']))
                return False

            api_data['project_url'] = api_data['organization']['platforms'][
                api_data['platform_number']]['projects'][
                    api_data['project_number']]['url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True

        return False

    def collect_data_for_set_gemfile_lock_auto_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with Ruby packages, collected from Gemfile.lock file,
        defined by --file parameter.
        :param api_data:
        :return:
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_gemfile_lock_auto_system_path(
                api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".format(api_data['project']))
                return False

            api_data['project_url'] = api_data['organization']['platforms'][
                api_data['platform_number']]['projects'][
                    api_data['project_number']]['url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True

        return False

    def collect_data_for_set_any_auto_user_path(self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with different packages, collected in file,
        defined by path with simple multiline format: name=version…
        :param api_data:
        :return:
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_any_auto_user_path(
                api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".format(api_data['project']))
                return False

            api_data['project_url'] = api_data['organization']['platforms'][
                api_data['platform_number']]['projects'][
                    api_data['project_number']]['url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True

        return False

    def collect_data_for_set_any_manual_user_none(self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with different packages, asked in interactive mode.
        :param api_data: api data set
        :return: result
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_any_manual_user_none(
                api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".format(api_data['project']))
                return False

            api_data['project_url'] = api_data['organization']['platforms'][
                api_data['platform_number']]['projects'][
                    api_data['project_number']]['url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True

        return False

    def collect_data_for_set_php_composer_json_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with packages from PHP Composer.json file.
        :param api_data: api data set
        :return: result
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_php_composer_json_system_path(
                api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".api_data['project'])
                return False

            api_data['project_url'] = api_data['organization']['platforms'][
                api_data['platform_number']]['projects'][
                    api_data['project_number']]['url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True

        return False

    def collect_data_for_set_php_composer_lock_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with packages from PHP Composer.lock file.
        :param api_data: api data set
        :return: result
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_php_composer_lock_system_path(
                api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".format(api_data['project']))
                return False

            api_data['project_url'] = api_data['organization']['platforms'][
                api_data['platform_number']]['projects'][
                    api_data['project_number']]['url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True

        return False

    def collect_data_for_set_maven_pom_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with packages from Maven pom.xml file.
        :param api_data: api data set
        :return: result
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_maven_pom(api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".format(api_data['project']))
                return False

            api_data['project_url'] = api_data['organization']['platforms'][
                api_data['platform_number']]['projects'][
                    api_data['project_number']]['url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True
        return False

    def collect_data_for_set_yarn_lock_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Create Component Set with packages from yarn.lock file.
        :param api_data: api data set
        :return: result
        """
        components = api_data['components']
        api_data['components'] = []
        if self.components_helper.get_components_yarn_lock(api_data=api_data):
            api_data[
                'platform_number'] = self.web_api.get_platform_number_by_name(
                    api_data=api_data)
            if api_data['platform_number'] == -1:
                print_line("No such platform: {0}".format(
                    api_data['platform']))
                return False

            api_data[
                'project_number'] = self.web_api.get_project_number_by_name(
                    api_data=api_data)
            if api_data['project_number'] == -1:
                print_line("No such project: {0}".format(api_data['project']))
                return False

            api_data['project_url'] = \
            api_data['organization']['platforms'][api_data['platform_number']]['projects'][api_data['project_number']][
                'url']

            for component in api_data['components']:
                components.append(component)
            api_data['components'] = components
            return True
        return False

    @staticmethod
    def get_my_platforms(api_data):
        # type: (dict) -> list
        """
        Get platforms names as list.
        :param api_data: api data set
        :return: result
        """

        if api_data['organization'] is None:
            return []

        if api_data['organization']['platforms'] is None:
            return []

        platforms = []
        for platform in api_data['organization']['platforms']:
            platforms.append(platform['name'])

        return platforms

    def get_my_projects(self, api_data):
        # type: (dict) -> list
        """
        Get projects names as list.
        :param api_data: api data set
        :return: result
        """

        if api_data['organization'] is None:
            return []

        if api_data['organization']['platforms'] is None:
            return []

        platform_number = self.web_api.get_platform_number_by_name(
            api_data=api_data)

        if platform_number == -1:
            return []

        projects = []
        for project in api_data['organization']['platforms'][platform_number][
                'projects']:
            projects.append(project['name'])

        return projects
Exemple #4
0
 def __init__(self):
     self.web_api = WebAPI()
Exemple #5
0
class API(object):
    """
    Main CLI App API class.
    """
    def __init__(self):
        self.web_api = WebAPI()

    # -------------------------------------------------------------------------
    # Run actions
    # -------------------------------------------------------------------------

    def run_action(self, api_data):
        """
        Main routing method for CLI app actions.
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Validate api data set structure

        if not self.validate_api_data_structure_is_ok(api_data=api_data):
            return False

        # Validate actions

        if not self.validate_action_type_is_ok(api_data=api_data):
            return False

        # Action: save config into file and exit

        if api_data['action'] == Actions.SAVE_CONFIG:
            return self.save_config_to_file(api_data=api_data)

        # Select login method for current session

        if not self.select_login_method(api_data=api_data):
            return False

        # Action: Login into server

        if not self.action_login_server_success(api_data=api_data):
            return False

        # If login was successfull - get organization parameters from server

        if not self.get_organization_parameters_from_server(api_data=api_data):
            return False

        # Action: Create new Platform

        if api_data['action'] == Actions.CREATE_PLATFORM:
            return self.action_create_new_platform(api_data=api_data)

        # Action: Create new Project

        elif api_data['action'] == Actions.CREATE_PROJECT:
            return self.action_create_new_project(api_data=api_data)

        # Action: Create new Component Set

        elif api_data['action'] == Actions.CREATE_SET:
            return self.action_create_new_set(api_data=api_data)

        # Action: Show information about Platforms, Projects, Component Set or Issues

        elif api_data['action'] == Actions.SHOW_PLATFORMS or \
                api_data['action'] == Actions.SHOW_PROJECTS or \
                api_data['action'] == Actions.SHOW_SET or \
                api_data['action'] == Actions.SHOW_ISSUES:
            return self.action_show_platforms_projects_or_sets(
                api_data=api_data)

        # Action: Delete Platform

        elif api_data['action'] == Actions.DELETE_PLATFORM:
            return self.action_delete_platform(api_data=api_data)

        # Action: Delete Project

        elif api_data['action'] == Actions.DELETE_PROJECT:
            return self.action_delete_project(api_data=api_data)

        # Action: Archive Platform

        elif api_data['action'] == Actions.ARCHIVE_PLATFORM:
            return self.action_archive_platform(api_data=api_data)

        # Action: Archive Project

        elif api_data['action'] == Actions.ARCHIVE_PROJECT:
            return self.action_archive_project(api_data=api_data)

        # Action: Restore Platform

        elif api_data['action'] == Actions.RESTORE_PLATFORM:
            return self.action_restore_platform(api_data=api_data)

        # Action: Restore Project

        elif api_data['action'] == Actions.RESTORE_PROJECT:
            return self.action_restore_project(api_data=api_data)

        # Otherwise

        print_line("Unknown action code: {0}.".format(api_data['action']))
        return False

    # -------------------------------------------------------------------------
    # LOGIN
    # -------------------------------------------------------------------------

    def select_login_method(self, api_data):
        # type: (dict) -> bool
        """
        Select login method for current session.
        :param api_data: api data set
        :return: result, modify api_data
        """

        # If auth_token or user/password presents in command line

        if api_data['login_method'] == 'token' or api_data[
                'login_method'] == 'username_and_password':
            if api_data['team'] is None or api_data['team'] == '':
                print_line('Token authorization requires --team parameter.')
                return False

            return True

        # If login will be with data from config file

        elif api_data['login_method'] == 'config_file':
            if self.load_config_from_file(api_data=api_data):
                if api_data['auth_token'] is not None and api_data[
                        'auth_token'] != '':
                    api_data['login_method'] = 'token'
                else:
                    api_data['login_method'] = 'username_and_password'
                return True

        # Otherwise

        return False

    def action_login_server_success(self, api_data):
        """
        Log in into server.
        :param api_data: api data set
        :return: result, modify api_data
        """

        # If login method is auth_token

        if api_data['login_method'] == 'token':
            return self.web_api.send_login_token_request(api_data=api_data)

        # If login method is user/password

        elif api_data['login_method'] == 'username_and_password':
            return self.web_api.send_login_user_password_request(
                api_data=api_data)

        # Otherwise

        return False

    # -------------------------------------------------------------------------
    # GET ORGANIZATION PARAMETERS
    # -------------------------------------------------------------------------

    def get_organization_parameters_from_server(self, api_data):
        """
        Get organization parameters from Surepatch server and fill the
        appropriate structure.
        :param api_data: api data set
        :return: result, modify api_data
        """

        return self.web_api.send_get_organization_parameters_request(
            api_data=api_data)

    # -------------------------------------------------------------------------
    # PLATFORM
    # -------------------------------------------------------------------------

    @staticmethod
    def action_create_new_platform(api_data):
        """
        Run action: CREATE New Platform.
        :param api_data: api data set
        :return: result, modify api_data
        """

        platform_helper = PlatformHelper()

        # Validate Platform parameters

        if not platform_helper.create_platform_validate(api_data=api_data):
            return False

        # Create new Platform

        return platform_helper.create_platform(api_data=api_data)

    # -------------------------------------------------------------------------
    # PROJECT
    # -------------------------------------------------------------------------

    def action_create_new_project(self, api_data):
        """
        Run action: CREATE New Project in different cases.
        :param api_data: api data set
        :return: result, modify api_data
        """

        project_helper = ProjectHelper()

        # Validate Project parameters

        if not project_helper.create_project_validate(api_data=api_data):
            return False

        # For loop -> for targets. and than, send web request

        targets = api_data['target']
        files = api_data['file']

        for i in range(0, len(targets)):
            # Select variant of CREATE Project action

            api_data['target'] = targets[i]
            api_data['file'] = files[i]

            # Create new project with OS packages {from shell request}

            if api_data['target'] == Targets.OS and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is None:
                if not project_helper.collect_data_for_project_os_auto_system_none(
                        api_data=api_data):
                    return False

            # Create new project with OS packages from shell request unloading file {from path}

            if api_data['target'] == Targets.OS and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not project_helper.collect_data_for_project_os_auto_system_path(
                        api_data=api_data):
                    return False

            # Create new project with Python 2 PIP packages {from shell request}

            if api_data['target'] == Targets.PIP and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is None:
                if not project_helper.collect_data_for_project_pip_auto_system_none(
                        api_data=api_data):
                    return False

            # Create new project with Python 3 PIP packages {from shell request}

            if api_data['target'] == Targets.PIP3 and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is None:
                if not project_helper.collect_data_for_project_pip_auto_system_none(
                        api_data=api_data):
                    return False

            # Create new project with Python 2 PIP from file {from path}

            if api_data['target'] == Targets.PIP and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not project_helper.collect_data_for_data_for_project_pip_auto_system_path(
                        api_data=api_data):
                    return False

            # Create new project with Python 3 PIP from file {from path}

            if api_data['target'] == Targets.PIP3 and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not project_helper.collect_data_for_data_for_project_pip_auto_system_path(
                        api_data=api_data):
                    return False

            # Create new project with Python 2 PIP from requirements.txt {from path}
            if api_data['target'] == Targets.REQ and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not project_helper.collect_data_for_project_requirements_auto_system_path(
                        api_data=api_data):
                    return False

            # Create new project with Python 3 PIP from requirements.txt {from path}

            if api_data['target'] == Targets.REQ3 and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not project_helper.collect_data_for_project_requirements_auto_system_path(
                        api_data=api_data):
                    return False

            # Create new project with Python 2 PIP from requirements.txt {from path}

            if api_data['target'] == Targets.REQUIREMENTS and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not project_helper.collect_data_for_project_requirements_auto_system_path(
                        api_data=api_data):
                    return False

            # Create new project with Python 3 PIP from requirements.txt {from path}

            if api_data['target'] == Targets.REQUIREMENTS3 and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not project_helper.collect_data_for_project_requirements_auto_system_path(
                        api_data=api_data):
                    return False

            # Create new project with NPM packages {from shell request} - from root path

            if api_data['target'] == Targets.NPM and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is None:
                if not project_helper.collect_data_for_project_npm_auto_system_none(
                        api_data=api_data):
                    return False

            # Create new project with NPM packages {from shell request} - from local path

            if api_data['target'] == Targets.NPM_LOCAL and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not project_helper.collect_data_for_project_npm_local_auto_system_none(
                        api_data=api_data):
                    return False

            # Create new project with NPM packages {from path}

            if api_data['target'] == Targets.NPM and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not project_helper.collect_data_for_project_npm_auto_system_path(
                        api_data=api_data):
                    return False

            # Create new project with NPM package.json file {from path}

            if api_data['target'] == Targets.PACKAGE_JSON and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not project_helper.collect_data_for_project_package_json_auto_system_path(
                        api_data=api_data):
                    return False

            # Create new project with NPM package_lock.json file {from path}

            if api_data['target'] == Targets.PACKAGE_LOCK_JSON and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not project_helper.collect_data_for_project_package_lock_json_auto_system_path(
                        api_data=api_data):
                    return False

            # Create new project with GEM packages {from shell request}

            if api_data['target'] == Targets.GEM and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is None:
                if not project_helper.collect_data_for_project_gem_auto_system_none(
                        api_data=api_data):
                    return False

            # Create new project with GEM packages from shell request unloading file {from path}

            if api_data['target'] == Targets.GEM and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not project_helper.collect_data_for_project_gem_auto_system_path(
                        api_data=api_data):
                    return False

            # Create new project with GEMFILE file {from path}

            if api_data['target'] == Targets.GEMFILE and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not project_helper.collect_data_for_project_gemfile_auto_system_path(
                        api_data=api_data):
                    return False

            # Create new project with GEMFILE.lock file {from path}

            if api_data['target'] == Targets.GEMFILE_LOCK and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not project_helper.collect_data_for_project_gemfile_lock_auto_system_path(
                        api_data=api_data):
                    return False

            # Create new project from user format file {from path}

            if api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.USER and \
                    api_data['file'] is not None:
                if not project_helper.collect_data_for_project_any_auto_user_path(
                        api_data=api_data):
                    return False

            # Create new project from interactive mode

            if api_data['method'] == Methods.MANUAL and \
                    api_data['format'] == Formats.USER and \
                    api_data['file'] is None:
                if not project_helper.collect_data_for_project_any_manual_user_none(
                        api_data=api_data):
                    return False

            # Create project with PHP Composer.json file {from path}

            if api_data['target'] == Targets.PHP_COMPOSER_JSON and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not project_helper.collect_data_for_project_php_composer_json_system_path(
                        api_data=api_data):
                    return False

            # Create project with PHP Composer.lock file {from path}

            if api_data['target'] == Targets.PHP_COMPOSER_LOCK and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not project_helper.collect_data_for_project_php_composer_lock_system_path(
                        api_data=api_data):
                    return False

            # Create project with Maven pom.xml file {from path}

            if api_data['target'] == Targets.POM and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not project_helper.collect_data_for_project_maven_pom_system_path(
                        api_data=api_data):
                    return False

            # Create project with yarn.lock file {from path}

            if api_data['target'] == Targets.YARN and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not project_helper.collect_data_for_project_yarn_lock_system_path(
                        api_data=api_data):
                    return False

            # Create project with node_modules folder {from path}

            if api_data['target'] == Targets.NODE_MODULES and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not project_helper.cllect_data_for_project_node_modules_system_path(
                        api_data=api_data):
                    return False

        # If there are some collected components

        if len(api_data['components']) > 0:
            return self.web_api.send_create_new_project_request(
                api_data=api_data)

        # Otherwise

        print_line(
            'Something wrong with app parameters or components list is empty. Please, look through README.md'
        )
        return False

    # -------------------------------------------------------------------------
    # SET
    # -------------------------------------------------------------------------

    def action_create_new_set(self, api_data):
        """
        Run action: CREATE New Set in different cases.
        :param api_data: api data set
        :return: result, modify api_data
        """

        set_helper = SetHelper()

        # Validate new Component Set parameters

        if not set_helper.create_set_validate(api_data=api_data):
            return False

        targets = api_data['target']
        files = api_data['file']

        # For list of targets

        for i in range(0, len(targets)):

            api_data['target'] = targets[i]
            api_data['file'] = files[i]

            # Create new set with OS packages {from shell request}

            if api_data['target'] == Targets.OS and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is None:
                if not set_helper.collect_data_for_set_os_auto_system_none(
                        api_data=api_data):
                    return False

            # Create set with OS packages from shell request unloading file {from path}

            if api_data['target'] == Targets.OS and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not set_helper.collect_data_for_set_os_auto_system_path(
                        api_data=api_data):
                    return False

            # Create set with Python 2 PIP packages {from shell request}

            if api_data['target'] == Targets.PIP and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is None:
                if not set_helper.collect_data_for_set_pip_auto_system_none(
                        api_data=api_data):
                    return False

            # Create set with Python 3 PIP packages {from shell request}

            if api_data['target'] == Targets.PIP3 and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is None:
                if not set_helper.collect_data_for_set_pip_auto_system_none(
                        api_data=api_data):
                    return False

            # Create set with Python 2 PIP from file {from path}

            if api_data['target'] == Targets.PIP and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not set_helper.collect_data_for_set_pip_auto_system_path(
                        api_data=api_data):
                    return False

            # Create set with Python 3 PIP from file {from path}

            if api_data['target'] == Targets.PIP3 and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not set_helper.collect_data_for_set_pip_auto_system_path(
                        api_data=api_data):
                    return False

            # Create set with PIP requirements.txt {from path}

            if api_data['target'] == Targets.REQ and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not set_helper.collect_data_for_set_requirements_auto_system_path(
                        api_data=api_data):
                    return False

            # Create set with requirements pip3

            if api_data['target'] == Targets.REQ3 and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not set_helper.collect_data_for_set_requirements_auto_system_path(
                        api_data=api_data):
                    return False

            # Create set from requirements file

            if api_data['target'] == Targets.REQUIREMENTS and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not set_helper.collect_data_for_set_requirements_auto_system_path(
                        api_data=api_data):
                    return False

            # Create set from requirements pip3 file

            if api_data['target'] == Targets.REQUIREMENTS3 and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not set_helper.collect_data_for_set_requirements_auto_system_path(
                        api_data=api_data):
                    return False

            # Create set with NPM packages {from shell request} - global

            if api_data['target'] == Targets.NPM and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is None:
                if not set_helper.collect_data_for_set_npm_auto_system_none(
                        api_data=api_data):
                    return False

            # Create set with NPM packages {from shell request} - local

            if api_data['target'] == Targets.NPM_LOCAL and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not set_helper.collect_data_for_set_npm_local_auto_system_path(
                        api_data=api_data):
                    return False

            # Create set with NPM packages {from file}

            if api_data['target'] == Targets.NPM and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not set_helper.collect_data_for_set_npm_auto_system_path(
                        api_data=api_data):
                    return False

            # Create set with NPM package.json file {from path}

            if api_data['target'] == Targets.PACKAGE_JSON and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not set_helper.collect_data_for_set_package_json_auto_system_path(
                        api_data=api_data):
                    return False

            # Create set with NPM package_lock.json file {from path}

            if api_data['target'] == Targets.PACKAGE_LOCK_JSON and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not set_helper.collect_data_for_set_package_lock_json_auto_system_path(
                        api_data=api_data):
                    return False

            # Create set with GEM packages {from shell request}

            if api_data['target'] == Targets.GEM and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is None:
                if not set_helper.collect_data_for_set_gem_auto_system_none(
                        api_data=api_data):
                    return False

            # Create set with GEM packages from shell request unloading file {from path}

            if api_data['target'] == Targets.GEM and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not set_helper.collect_data_for_set_gem_auto_system_path(
                        api_data=api_data):
                    return False

            # Create set with GEMLIST file {from path}

            if api_data['target'] == Targets.GEMFILE and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not set_helper.collect_data_for_set_gemfile_auto_system_path(
                        api_data=api_data):
                    return False

            # Create set with GEMLIST file {from path}

            if api_data['target'] == Targets.GEMFILE_LOCK and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not set_helper.collect_data_for_set_gemfile_lock_auto_system_path(
                        api_data=api_data):
                    return False

            # Create set with User defined packages in file (from path{

            if api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.USER and \
                    api_data['file'] is not None:
                if not set_helper.collect_data_for_set_any_auto_user_path(
                        api_data=api_data):
                    return False

            # Create set with User defined packages in interactive mode

            if api_data['method'] == Methods.MANUAL and \
                    api_data['format'] == Formats.USER and \
                    api_data['file'] is None:
                if not set_helper.collect_data_for_set_any_manual_user_none(
                        api_data=api_data):
                    return False

            # Create set with PHP Composer.json file {from path}

            if api_data['target'] == Targets.PHP_COMPOSER_JSON and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                return set_helper.collect_data_for_set_php_composer_json_system_path(
                    api_data=api_data)

            # Create set with PHP Composer.lock file {from path}

            if api_data['target'] == Targets.PHP_COMPOSER_LOCK and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not set_helper.collect_data_for_set_php_composer_lock_system_path(
                        api_data=api_data):
                    return False

            # Create set with pom.xml

            if api_data['target'] == Targets.POM and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not set_helper.collect_data_for_set_maven_pom_system_path(
                        api_data=api_data):
                    return False

            # Create set with yarn.lock file {from path}

            if api_data['target'] == Targets.YARN and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not set_helper.collect_data_for_set_yarn_lock_system_path(
                        api_data=api_data):
                    return False

            # Create set with node_modules folder {from path}

            if api_data['target'] == Targets.NODE_MODULES and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file']is not None:
                if not set_helper.collect_data_for_set_node_modules_system_path(
                        api_data=api_data):
                    return False

        # If there are some components

        if len(api_data['components']) > 0:
            return self.web_api.send_create_new_component_set_request(
                api_data=api_data)

        # Otherwise

        print_line(
            'Something wrong with app parameters. Please, look through README.md'
        )
        return False

    # -------------------------------------------------------------------------
    # Show
    # -------------------------------------------------------------------------

    def action_show_platforms_projects_or_sets(self, api_data):
        """
        Run action: Show platforms, projects or component sets.
        :param api_data: api data set
        :return: result
        """

        show_helper = ShowHelper()

        # If action is Show Platforms

        if api_data['action'] == Actions.SHOW_PLATFORMS:
            return show_helper.action_show_platforms(api_data=api_data)

        # If action is Show Projects

        elif api_data['action'] == Actions.SHOW_PROJECTS:
            if api_data['platform'] is None or \
                    api_data['platform'] == '':
                print_line('Empty platform name.')
                return False

            # Get number of Platform in list of Platforms

            platform_number = self.web_api.get_platform_number_by_name(
                api_data=api_data)

            # It there are not such Platform in list of Platforms

            if platform_number == -1:
                print_line("No such platform: {0}.".format(
                    api_data['platform']))
                return False

            # It is OK, so run action

            return show_helper.action_show_projects(api_data=api_data)

        # If action if Show current Component Set

        elif api_data['action'] == Actions.SHOW_SET:
            if api_data['platform'] is None or \
                    api_data['platform'] == '':
                print_line('Empty platform name.')
                return False

            # Get number of Platform in list of Platforms

            platform_number = self.web_api.get_platform_number_by_name(
                api_data=api_data)

            # It there are not such Platform in list of Platforms

            if platform_number == -1:
                print_line("No such platform: {0}.".format(
                    api_data['platform']))
                return False

            # If Project name is empty

            if api_data['project'] is None or \
                    api_data['project'] == '':
                print_line('Empty project name.')
                return False

            # Get number of Project in list of Projects

            project_number = self.web_api.get_project_number_by_name(
                api_data=api_data)

            # If there are not such Project in list of Projects

            if project_number == -1:
                print_line("No such project {0} in platform {1}.".format(
                    api_data['project'], api_data['platform']))
                return False

            # It is OK, so run action

            return show_helper.action_show_set(api_data=api_data)

        # If action is Show Issues

        elif api_data['action'] == Actions.SHOW_ISSUES:
            if api_data['platform'] is None or \
                    api_data['platform'] == '':
                print_line('Empty platform name.')
                return False

            # Get number of Platform in list of Platforms

            platform_number = self.web_api.get_platform_number_by_name(
                api_data=api_data)

            # It there are not such Platform in list of Platforms

            if platform_number == -1:
                print_line("No such platform: {0}.".format(
                    api_data['platform']))
                return False

            # If Project name is empty

            if api_data['project'] is None or \
                    api_data['project'] == '':
                print_line('Empty platform name.')
                return False

            # Get number of Project in list of Projects

            project_number = self.web_api.get_project_number_by_name(
                api_data=api_data)

            # If there are not such Project in list of Projects

            if project_number == -1:
                print_line("No such project {0} in platform {1}.".format(
                    api_data['project'], api_data['platform']))
                return False

            # It is OK, so run action

            return show_helper.action_show_issues(api_data=api_data)

        return False

    # -------------------------------------------------------------------------
    # Delete Platform ot Project
    # -------------------------------------------------------------------------

    @staticmethod
    def action_delete_platform(api_data):
        """
        Run action: Delete defined Platform.
        :param api_data: api data set
        :return: result
        """

        platform_helper = PlatformHelper()

        return platform_helper.delete_platform(api_data=api_data)

    @staticmethod
    def action_delete_project(api_data):
        """
        Run action: Delete defined Project.
        :param api_data: api data set
        :return: result
        """
        project_helper = ProjectHelper()

        return project_helper.delete_project(api_data=api_data)

    # -------------------------------------------------------------------------
    # Archive Platform or Project
    # -------------------------------------------------------------------------

    @staticmethod
    def action_archive_platform(api_data):
        """
        Run action: Archive defined Platform.
        :param api_data: api data set
        :return: result
        """
        platform_helper = PlatformHelper()

        return platform_helper.archive_platform(api_data=api_data)

    @staticmethod
    def action_archive_project(api_data):
        """
        Run action: Archive defined Project.
        :param api_data: api data set
        :return: result
        """
        project_helper = ProjectHelper()

        return project_helper.archive_project(api_data=api_data)

    # -------------------------------------------------------------------------
    # Restore Platform or Project
    # -------------------------------------------------------------------------

    @staticmethod
    def action_restore_platform(api_data):
        """
        Run action: restore Platform from Archive.
        :param api_data: api data set
        :return: result
        """
        platform_helper = PlatformHelper()

        return platform_helper.restore_platform(api_data=api_data)

    @staticmethod
    def action_restore_project(api_data):
        """
        Run action: Restore Project from Archive.
        :param api_data: api data set
        :return: result
        """
        project_helper = ProjectHelper()

        return project_helper.restore_project(api_data=api_data)

    # -------------------------------------------------------------------------
    # Validators
    # -------------------------------------------------------------------------

    @staticmethod
    def validate_api_data_structure_is_ok(api_data):
        # type: (dict) -> bool
        """
        Validate api_data contents.
        :param api_data: api data set
        :return: result
        """

        # If app execute as python module - redefine OS parameteres

        if 'os_type' not in api_data:
            api_data['os_type'] = get_os_platform()

        if 'os_version' not in api_data:
            api_data['os_version'] = get_os_version(get_os_platform())

        if 'os_sp' not in api_data:
            api_data['os_sp'] = get_os_sp(get_os_platform())

        if 'os_release' not in api_data:
            api_data['os_release'] = get_os_release()

        if 'os_machine' not in api_data:
            api_data['os_machine'] = get_os_machine()

        # For login mode

        if 'auth_token' not in api_data:
            api_data['auth_token'] = None

        if api_data['auth_token'] is not None and api_data['auth_token'] != '':
            api_data['login_method'] = 'token'

        else:
            if (api_data['user'] is not None and api_data['user'] != '') and \
                    api_data['password'] is not None and api_data['password'] != '':
                api_data['login_method'] = 'username_and_password'

            else:
                api_data['login_method'] = 'config_file'

        # For targets

        if 'target' not in api_data:
            api_data['target'] = None

        if api_data['target'] is None:
            api_data['target'] = ''

        # For files

        if 'file' not in api_data:
            api_data['file'] = None

        if api_data['file'] is None:
            api_data['file'] = 'no'

        # For methods

        if 'method' not in api_data:
            api_data['method'] = None

        if api_data['method'] is None:
            api_data['method'] = Methods.AUTO

        # For formats

        if 'format' not in api_data:
            api_data['format'] = None

        if api_data['format'] is None:
            api_data['format'] = Formats.SYSTEM

        # For logging

        if 'logging' not in api_data:
            api_data['logging'] = None

        if api_data['logging'] is None:
            api_data['logging'] = 'off'

        # For server target

        if 'server' not in api_data:
            api_data['server'] = None

        if api_data['server'] is None:
            api_data['server'] = 'prod'

        # Define if targets more than one

        targets = api_data['target'].replace('[', '').replace(']', '').replace(
            ' ', '').split(',')

        if len(targets) == 0:
            print_line('Wrong number of targets.')
            return False

        api_data['target'] = targets

        # Define if files more than one, if it is true -
        # redefine files as list with length of targets

        files = api_data['file'].replace('[',
                                         '').replace(']',
                                                     '').replace(' ',
                                                                 '').split(',')

        if len(targets) != len(files):
            print_line(
                'Number of targets not equals number of files. For targets, '
                'that do not require files - use "no".')
            print_line('For example: ... --target[os,req] '
                       '--file=no,/home/user/project/requirements.txt.')
            return False

        api_data['file'] = []

        for file in files:
            if file == 'no':
                api_data['file'].append(None)
            else:
                api_data['file'].append(file)

        # Clear components and packages lists

        api_data['components'] = []
        api_data['packages'] = []

        return True

    @staticmethod
    def validate_action_type_is_ok(api_data):
        """
        Check if action type, pointed in arguments match with template.
        :param api_data: api data set
        :return: result
        """

        if 'action' not in api_data:
            return False

        if api_data['action'] != Actions.SAVE_CONFIG and \
                api_data['action'] != Actions.CREATE_PLATFORM and \
                api_data['action'] != Actions.CREATE_PROJECT and \
                api_data['action'] != Actions.CREATE_SET and \
                api_data['action'] != Actions.SHOW_PLATFORMS and \
                api_data['action'] != Actions.SHOW_PROJECTS and \
                api_data['action'] != Actions.SHOW_SET and \
                api_data['action'] != Actions.SHOW_ISSUES and \
                api_data['action'] != Actions.DELETE_PLATFORM and \
                api_data['action'] != Actions.DELETE_PROJECT and \
                api_data['action'] != Actions.ARCHIVE_PLATFORM and \
                api_data['action'] != Actions.ARCHIVE_PROJECT and \
                api_data['action'] != Actions.RESTORE_PLATFORM and \
                api_data['action'] != Actions.RESTORE_PROJECT:
            return False

        return True

    # -------------------------------------------------------------------------
    # Config actions
    # -------------------------------------------------------------------------

    @staticmethod
    def save_config_to_file(api_data):
        """
        Save data into config fle in yaml format.
        :param api_data: api data set
        :return: result
        """

        # Default parameters for config file

        file_name = '.surepatch.yaml'
        file_path = os.path.expanduser('~')
        full_path = os.path.join(file_path, file_name)

        # Config template

        config = dict(team=api_data['team'],
                      user=api_data['user'],
                      password=api_data['password'],
                      auth_token=api_data['auth_token'],
                      logo=api_data['logo'],
                      logging=api_data['logging'],
                      server=api_data['server'])

        # open config file

        with open(full_path, 'w') as yaml_config_file:
            try:
                yaml.dump(config, yaml_config_file)
                return True

            except yaml.YAMLError as yaml_exception:
                print_line(
                    'Config file save in yaml format exception: {0}.'.format(
                        yaml_exception))
                return False

            finally:
                yaml_config_file.close()

    def load_config_from_file(self, api_data):
        """
        Load data from config file in yaml format.
        :param api_data: api data set
        :return: result
        """

        # Default parameters for config file

        file_name = '.surepatch.yaml'
        file_path = os.path.expanduser('~')
        full_path = os.path.join(file_path, file_name)

        # Save changable flags

        server = api_data['server']

        # If file does not exist

        if not os.path.isfile(full_path):
            print_line('Config file does not exist: ~/{0}.'.format(file_name))
            print_line(
                'Create config file first with parameter --action=save_config.'
            )
            return False

        components_helper = ComponentsHelper()

        # Define config file encoding

        if components_helper.define_file_encoding(full_path) == 'undefined':
            print_line('Undefined file encoding. Please, use utf-8 or utf-16.')
            return False

        # Open config file

        with open(full_path, 'r') as yaml_config_file:
            try:

                # Load config file contents in YAML format

                config = yaml.load(yaml_config_file)

                # Analyse config elements

                if 'team' not in config:
                    config['team'] = None

                api_data['team'] = config['team']

                if 'user' not in config:
                    config['user'] = None

                api_data['user'] = config['user']

                if 'password' not in config:
                    config['password'] = None

                api_data['password'] = config['password']

                if 'auth_token' not in config:
                    config['auth-token'] = None

                api_data['auth_token'] = config['auth_token']

                if 'logo' not in config:
                    config['logo'] = 'off'

                api_data['logo'] = config['logo']

                if 'logging' not in config:
                    config['logging'] = 'off'

                api_data['logging'] = config['logging']

                if 'server' not in config:
                    config['server'] = 'prod'

                if config['server'] != server:
                    api_data['server'] = server
                else:
                    api_data['server'] = config['server']

                return True

            except yaml.YAMLError as yaml_exception:
                print_line(
                    'Get an exception while read config file: {0}.'.format(
                        yaml_exception))
                return False

            finally:
                yaml_config_file.close()
class ProjectHelper(object):
    """
    Project Helper class.
    """
    def __init__(self):
        self.web_api = WebAPI()
        self.components_helper = ComponentsHelper()

    def create_project_validate(self, api_data):
        # type: (dict) -> bool
        """
        Run action: CREATE New Project in different cases.
        :return: result, modify api_data
        """

        # Validate Platform and Project parameters

        if 'platform' not in api_data:
            print_line('There are no --platform parameter given.')
            return False

        if 'project' not in api_data:
            print_line('There are no --platform parameter given.')
            return False

        if api_data['platform'] is None or api_data['platform'] == '':
            print_line('Empty platform name.')
            return False

        if api_data['project'] is None or api_data['project'] == '':
            print_line('Empty project name.')
            return False

        # Get Platforms

        platforms = self.get_my_platforms(api_data=api_data)

        if api_data['platform'] not in platforms:
            print_line("Platform {0} does not exists.".format(
                api_data['platform']))
            return False

        # Get Projects

        projects = self.get_my_projects(api_data=api_data)

        if api_data['project'] in projects:
            print_line("Project {0} already exists.".format(
                api_data['project']))
            return False

        # Normal output

        return True

    # Target = OS packages

    def collect_data_for_project_os_auto_system_none(self, api_data):
        # type: (dict) -> bool
        """
        Collect data from OS packages by shell command.
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Get components

        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_os_auto_system_none(
                api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    def collect_data_for_project_os_auto_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Collect data from OS packages by shell command,
        stored in file, defined in path.
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Get components

        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_os_auto_system_path(
                api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    # Target = Python packages

    def collect_data_for_project_pip_auto_system_none(self, api_data):
        # type: (dict) -> bool
        """
        Collect Python PIP packages from shell command.
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Get components

        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_pip_auto_system_none(
                api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    def collect_data_for_data_for_project_pip_auto_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Collect Python PIP packages from shell command,
        stored in file, defined in path.
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Get components

        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_pip_auto_system_path(
                api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    def collect_data_for_project_requirements_auto_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Collect Python requirements.txt file, defined in path.
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Get components

        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_requirements_auto_system_path(
                api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    # Target = NodeJS NPM packages

    def collect_data_for_project_npm_auto_system_none(self, api_data):
        # type: (dict) -> bool
        """
        Collect NPM packages, collected from shell command (nmp list --json).
        Shell command runs global from root path.
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Get components

        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_npm_auto_system_none(
                api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    def collect_data_for_project_npm_auto_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Collect NPM packages from shell command (npm list --json),
        stored in file, defined in path.
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Got components

        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_npm_auto_system_path(
                api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    def collect_data_for_project_npm_local_auto_system_none(self, api_data):
        # type: (dict) -> bool
        """
        Collect NPM packages from shell command (npm list --json).
        Shell command runs local from path, defined by --file parameter.
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Get components

        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_npm_local_auto_system_none(
                api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    def collect_data_for_project_package_lock_json_auto_system_path(
            self, api_data):
        # type: (dict) -> bool
        """
        Collect NPM packages from package-lock.json, defined by --file parameter.
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Get components

        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_npm_lock_auto_system_path(
                api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    def collect_data_for_project_package_json_auto_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Collect NPM packages from package.json, defined by --file parameter.
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Get components

        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_package_json_auto_system_path(
                api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    # Target = Ruby packages

    def collect_data_for_project_gem_auto_system_none(self, api_data):
        # type: (dict) -> bool
        """
        Collect Ruby packages, collected from shell command.
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Get components

        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_gem_auto_system_none(
                api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    def collect_data_for_project_gem_auto_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Collect Ruby packages from shell command and
        stored in gem list file, defined in --file parameter.
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Get components

        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_gem_auto_system_path(
                api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    def collect_data_for_project_gemfile_auto_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Collect Ruby packages, collected from Gemfile, defined
        by --file parameter.
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Get components

        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_gemfile_auto_system_path(
                api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    def collect_data_for_project_gemfile_lock_auto_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Collect Ruby packages, collected from Gemfile.lock file,
        defined by --file parameter.
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Get components

        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_gemfile_lock_auto_system_path(
                api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    def collect_data_for_project_any_auto_user_path(self, api_data):
        # type: (dict) -> bool
        """
        Collect different packages, collected in file,
        defined by path with simple multiline format: name=version…
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Get components

        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_any_auto_user_path(
                api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    def collect_data_for_project_any_manual_user_none(self, api_data):
        # type: (dict) -> bool
        """
        Collect different packages, asked in interactive mode.
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Get components

        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_any_manual_user_none(
                api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    def collect_data_for_project_php_composer_json_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Collect PHP packages from Composer.json file.
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Get components

        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_php_composer_json_system_path(
                api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    def collect_data_for_project_php_composer_lock_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Collect PHP packages from Composer.json file.
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Get components

        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_php_composer_lock_system_path(
                api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    def collect_data_for_project_maven_pom_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Collect packages with Maven pom.xml file
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Get components
        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_maven_pom(api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    def collect_data_for_project_yarn_lock_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Collect packages with yarn.lock file
        :param api_data: api data set
        :return: result, modify api_data
        """

        # Get components

        components = api_data['components']
        api_data['components'] = []

        if self.components_helper.get_components_yarn_lock(api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal output

            api_data['components'] = components
            return True

        # Otherwise

        return False

    def cllect_data_for_project_node_modules_system_path(self, api_data):
        # type: (dict) -> bool
        """
        Collect data for node_modules from file
        :param api_data: api data set
        :return: result
        """

        # Get components

        components = api_data['components']
        api_data['components']

        if self.components_helper.get_components_node_modules(
                api_data=api_data):
            for component in api_data['components']:
                components.append(component)

            # Normal utput

            api_data['components'] = components
            return True

        # Otherwise

        return False

    def delete_project(self, api_data):
        # type: (dict) -> bool
        """
        Run action: Delete defined Project.
        :param api_data: api data set
        :return: result
        """

        # Get Platform number

        api_data['platform_number'] = self.web_api.get_platform_number_by_name(
            api_data=api_data)

        if api_data['platform_number'] == -1:
            print_line("Platform {0} does not exist.".format(
                api_data['platform']))
            return False

        # Get Project number

        api_data['project_number'] = self.web_api.get_project_number_by_name(
            api_data=api_data)

        if api_data['project_number'] == -1:
            print_line("Project {0} does not exist.".format(
                api_data['project']))
            return False

        # Get Project ID

        api_data['project_id'] = api_data['organization']['platforms'][
            api_data['platform_number']]['projects'][
                api_data['project_number']]['id']

        # Send Delete request

        return self.web_api.send_delete_project_request(api_data=api_data)

    def archive_project(self, api_data):
        # type: (dict) -> bool
        """
        Run action: Archive defined Project.
        :param api_data: api data set
        :return: result
        """

        # get Platform number

        api_data['platform_number'] = self.web_api.get_platform_number_by_name(
            api_data=api_data)

        if api_data['platform_number'] == -1:
            print_line("Platform {0} does not exist.".format(
                api_data['platform']))
            return False

        # get Project number

        api_data['project_number'] = self.web_api.get_project_number_by_name(
            api_data=api_data)

        if api_data['project_number'] == -1:
            print_line("Project {0} does not exist.".format(
                api_data['project']))
            return False

        # Get parameters

        organization = api_data['organization']
        platforms = organization['platforms']
        platform = platforms[api_data['platform_number']]
        projects = platform['projects']
        project = projects[api_data['project_number']]
        project_id = project['id']
        project_url = project['url']
        api_data['project_id'] = project_id
        api_data['project_url'] = project_url

        # Send Archive request

        return self.web_api.send_archive_project_request(api_data=api_data)

    def restore_project(self, api_data):
        # type: (dict) -> bool
        """
        Run action: Restore Project from Archive.
        :param api_data:
        :return:
        """

        # Get Platform number

        if api_data['platform'] is None or api_data['platform'] == '':
            print_line('Empty platform name.')
            return False

        if api_data['project'] is None or api_data['project'] == '':
            print_line('Empty project name.')
            return False

        # Get archived projects

        if not self.web_api.send_get_archived_projects_request(
                api_data=api_data):
            print_line('There were errors in obtaining archived projects.')
            return False

        api_data['project_id'] = None
        api_data['project_url'] = None
        my_archived_project = dict()

        # Verify archived projects

        for archive_project in api_data['archive_projects']:
            if api_data['project'] == archive_project['name']:
                api_data['project_id'] = archive_project['_id']
                api_data['project_url'] = archive_project['url']
                my_archived_project = archive_project
                break

        if api_data['project_id'] is None:
            print_line("Not such project {0} in archive.".format(
                api_data['project']))
            return False

        if my_archived_project['platform_id']['name'] != api_data['platform']:
            print_line("Defined project {0} not in defined platform "
                       "{1}.".format(api_data['project'],
                                     api_data['platform']))
            return False

        # Sent Restore request

        return self.web_api.send_restore_project_request(api_data=api_data)

    @staticmethod
    def get_my_platforms(api_data):
        # type: (dict) -> list
        """
        Get platforms names as list.
        :param api_data: api data set
        :return: result
        """

        # Check organization and platforms

        if api_data['organization'] is None:
            return []

        if api_data['organization']['platforms'] is None:
            return []

        # Get Platform name

        platforms = []
        for platform in api_data['organization']['platforms']:
            platforms.append(platform['name'])

        return platforms

    def get_my_projects(self, api_data):
        # type: (dict) -> list
        """
        Get projects names as list.
        :param api_data: api data set
        :return: result
        """

        # Check organization and platforms

        if api_data['organization'] is None:
            return []

        if api_data['organization']['platforms'] is None:
            return []

        # Get Platform number

        platform_number = self.web_api.get_platform_number_by_name(
            api_data=api_data)

        if platform_number == -1:
            return []

        # Get Projects

        projects = []

        for project in api_data['organization']['platforms'][platform_number][
                'projects']:
            projects.append(project['name'])

        return projects
Exemple #7
0
class PlatformHelper(object):
    """
    Platform Helper class.
    """

    def __init__(self):
        self.web_api = WebAPI()

    @staticmethod
    def create_platform_validate(api_data):
        # type: (dict) -> bool
        """
        Validate parameters.
        :param api_data: api data set
        :return: result
        """

        # Validate Platform

        if 'platform' not in api_data:
            print_line('There are no --platform parameter given.')
            return False

        if api_data['platform'] is None or api_data['platform'] == '':
            print_line('Empty platform name, please use --platform flag.')
            return False

        # Validate description

        if 'description' not in api_data:
            api_data['description'] = None

        if api_data['description'] is None or api_data['description'] == '':
            print_line('Empty platform description. Change description to "default platform".')
            api_data['description'] = "default platform"

        # Normal output

        return True

    def create_platform(self, api_data):
        # type: (dict) -> bool
        """
        Run action: Create Platform
        :param api_data: api data set
        :return: result
        """

        return self.web_api.send_create_new_platform_request(api_data=api_data)

    def delete_platform(self, api_data):
        # type: (dict) -> bool
        """
        Run action: Delete defined Platform.
        :param api_data: api data set
        :return: result
        """

        # Get platform by name

        api_data['platform_id'] = self.web_api.get_platform_id_by_name(api_data=api_data)

        if api_data['platform_id'] == -1:
            print_line("Platform {0} does not exist.".format(api_data['platform']))
            return False

        # Normal output

        return self.web_api.send_delete_platform_request(api_data=api_data)

    def archive_platform(self, api_data):
        # type: (dict) -> bool
        """
        Run action: Archive defined Platform.
        :param api_data: api data set
        :return: result
        """

        # Get Platform Id by name

        api_data['platform_id'] = self.web_api.get_platform_id_by_name(api_data=api_data)

        if api_data['platform_id'] == -1:
            print_line("Platform {0} does not exist.".format(api_data['platform']))
            return False

        # Get separate parameters for request

        organization = api_data['organization']
        platforms = organization['platforms']
        platform_number = self.web_api.get_platform_number_by_name(api_data=api_data)
        platform = platforms[platform_number]
        platform_url = platform['url']
        api_data['platform_url'] = platform_url

        # Normal response

        return self.web_api.send_archive_platform_request(api_data=api_data)

    def restore_platform(self, api_data):
        # type: (dict) -> bool
        """
        Run action: restore Platform from Archive.
        :param api_data: api data set
        :return: result
        """

        # Verify platform

        if api_data['platform'] is None or api_data['platform'] == '':
            print_line('Empty platform name.')
            return False

        # Get archived platforms

        if not self.web_api.send_get_archived_platforms_request(api_data=api_data):
            print_line('There were errors in obtaining archived platforms.')
            return False

        api_data['platform_id'] = None
        api_data['platform_url'] = None

        # Get Platform parameters

        for archive_platform in api_data['archive_platforms']:
            if api_data['platform'] == archive_platform['name']:
                api_data['platform_id'] = archive_platform['_id']
                api_data['platform_url'] = archive_platform['url']
                break

        if api_data['platform_id'] is None:
            print_line("Not such platform {0} in archive.".format(api_data['platform']))
            return False

        # Normal output

        return self.web_api.send_restore_platform_request(api_data=api_data)
Exemple #8
0
class API(object):
    """Main CLI App API.
    """

    def __init__(self):
        self.web_api = WebAPI()

    # -------------------------------------------------------------------------
    # Run actions
    # -------------------------------------------------------------------------

    def run_action(self, api_data):
        """
        Main routing method for app actions.
        :param api_data: api data set
        :return: result, modify api_data
        """

        if not self.check_action_type_match(api_data=api_data):
            return False

        if api_data['action'] == Actions.SAVE_CONFIG:
            return self.save_config_to_file(api_data=api_data)

        if not self.select_login_method(api_data=api_data):
            return False

        if not self.action_login_server_success(api_data=api_data):
            return False

        if not self.get_organization_parameters_from_server(api_data=api_data):
            return False

        if api_data['action'] == Actions.CREATE_PLATFORM:
            return self.action_create_new_platform(api_data=api_data)

        elif api_data['action'] == Actions.CREATE_PROJECT:
            return self.action_create_new_project(api_data=api_data)

        elif api_data['action'] == Actions.CREATE_SET:
            return self.action_create_new_set(api_data=api_data)

        elif api_data['action'] == Actions.SHOW_PLATFORMS or \
                api_data['action'] == Actions.SHOW_PROJECTS or \
                api_data['action'] == Actions.SHOW_SET or \
                api_data['action'] == Actions.SHOW_ISSUES:
            return self.action_show_platforms_projects_or_sets(api_data=api_data)

        elif api_data['action'] == Actions.DELETE_PLATFORM:
            return self.action_delete_platform(api_data=api_data)

        elif api_data['action'] == Actions.DELETE_PROJECT:
            return self.action_delete_project(api_data=api_data)

        elif api_data['action'] == Actions.ARCHIVE_PLATFORM:
            return self.action_archive_platform(api_data=api_data)

        elif api_data['action'] == Actions.ARCHIVE_PROJECT:
            return self.action_archive_project(api_data=api_data)

        elif api_data['action'] == Actions.RESTORE_PLATFORM:
            return self.action_restore_platform(api_data=api_data)

        elif api_data['action'] == Actions.RESTORE_PROJECT:
            return self.action_restore_project(api_data=api_data)

        print_line("Unknown action code: {0}.".format(api_data['action']))
        return False

    # -------------------------------------------------------------------------
    # LOGIN
    # -------------------------------------------------------------------------

    def select_login_method(self, api_data):
        # type: (dict) -> bool
        if api_data['login_method'] == 'token' or api_data['login_method'] == 'username_and_password':
            if api_data['team'] is None or api_data['team'] == '':
                print_line('Token authorization requires --team parameter.')
                return False
            return True
        elif api_data['login_method'] == 'config_file':
            if self.load_config_from_file(api_data=api_data):
                if api_data['auth_token'] is not None and api_data['auth_token'] != '':
                    api_data['login_method'] = 'token'
                else:
                    api_data['login_method'] = 'username_and_password'
                return True
        return False

    def action_login_server_success(self, api_data):
        """
        Log in into server.
        :param api_data: api data set
        :return: result, modify api_data
        """
        if api_data['login_method'] == 'token':
            return self.web_api.send_login_token_request(api_data=api_data)
        elif api_data['login_method'] == 'username_and_password':
            return self.web_api.send_login_user_password_request(api_data=api_data)

    # -------------------------------------------------------------------------
    # GET ORGANIZATION PARAMETERS
    # -------------------------------------------------------------------------

    def get_organization_parameters_from_server(self, api_data):
        """
        Get organization parameters from Surepatch server and fill the
        appropriate structure.
        :param api_data: api data set
        :return: result, modify api_data
        """

        return self.web_api.send_get_organization_parameters_request(api_data=api_data)

    # -------------------------------------------------------------------------
    # PLATFORM
    # -------------------------------------------------------------------------

    @staticmethod
    def action_create_new_platform(api_data):
        """
        Run action: CREATE New Platform.
        :param api_data: api data set
        :return: result, modify api_data
        """
        platform_helper = PlatformHelper()

        if not platform_helper.create_platform_validate(api_data=api_data):
            return False

        return platform_helper.create_platform(api_data=api_data)

    # -------------------------------------------------------------------------
    # PROJECT
    # -------------------------------------------------------------------------

    def action_create_new_project(self, api_data):
        """
        Run action: CREATE New Project in different cases.
        :param api_data: api data set
        :return: result, modify api_data
        """
        project_helper = ProjectHelper()

        if not project_helper.create_project_validate(api_data=api_data):
            return False

        # For loop -> for targets. and than, send web request

        targets = api_data['target']
        files = api_data['file']

        for i in range(0, len(targets)):
            # Select variant of CREATE Project action

            api_data['target'] = targets[i]
            api_data['file'] = files[i]

            # Create new project with OS packages {from shell request}
            if api_data['target'] == Targets.OS and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is None:
                if not project_helper.collect_data_for_project_os_auto_system_none(api_data=api_data):
                    return False

            # Create new project with OS packages from shell request unloading file {from path}
            if api_data['target'] == Targets.OS and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not project_helper.collect_data_for_project_os_auto_system_path(api_data=api_data):
                    return False

            # Create new project with PIP packages {from shell request}
            if api_data['target'] == Targets.PIP and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is None:
                if not project_helper.collect_data_for_project_pip_auto_system_none(api_data=api_data):
                    return False

            if api_data['target'] == Targets.PIP3 and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is None:
                if not project_helper.collect_data_for_project_pip_auto_system_none(api_data=api_data):
                    return False

            # Create new project with PIP from file {from path}
            if api_data['target'] == Targets.PIP and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not project_helper.collect_data_for_data_for_project_pip_auto_system_path(api_data=api_data):
                    return False

            if api_data['target'] == Targets.PIP3 and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not project_helper.collect_data_for_data_for_project_pip_auto_system_path(api_data=api_data):
                    return False

            # Create new project with PIP requirements.txt {from path}
            if api_data['target'] == Targets.REQ and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not project_helper.collect_data_for_project_requirements_auto_system_path(api_data=api_data):
                    return False

            if api_data['target'] == Targets.REQ3 and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not project_helper.collect_data_for_project_requirements_auto_system_path(api_data=api_data):
                    return False

            if api_data['target'] == Targets.REQUIREMENTS and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not project_helper.collect_data_for_project_requirements_auto_system_path(api_data=api_data):
                    return False

            if api_data['target'] == Targets.REQUIREMENTS3 and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not project_helper.collect_data_for_project_requirements_auto_system_path(api_data=api_data):
                    return False

            # Create new project with NPM packages {from shell request} - global
            if api_data['target'] == Targets.NPM and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is None:
                if not project_helper.collect_data_for_project_npm_auto_system_none(api_data=api_data):
                    return False

            # Create new project with NPM packages {from shell request} - local
            if api_data['target'] == Targets.NPM_LOCAL and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not project_helper.collect_data_for_project_npm_local_auto_system_none(api_data=api_data):
                    return False

            # Create new project with NPM packages {from file}
            if api_data['target'] == Targets.NPM and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not project_helper.collect_data_for_project_npm_auto_system_path(api_data=api_data):
                    return False

            # Create new project with NPM package.json file {from path}
            if api_data['target'] == Targets.PACKAGE_JSON and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not project_helper.collect_data_for_project_package_json_auto_system_path(api_data=api_data):
                    return False

            # Create new project with NPM package_lock.json file {from path}
            if api_data['target'] == Targets.PACKAGE_LOCK_JSON and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not project_helper.collect_data_for_project_package_lock_json_auto_system_path(api_data=api_data):
                    return False

            # Create new project with GEM packages {from shell request}
            if api_data['target'] == Targets.GEM and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is None:
                if not project_helper.collect_data_for_project_gem_auto_system_none(api_data=api_data):
                    return False

            # Create new project with GEM packages from shell request unloading file {from path}
            if api_data['target'] == Targets.GEM and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not project_helper.collect_data_for_project_gem_auto_system_path(api_data=api_data):
                    return False

            # Create new project with GEMFILE file {from path}
            if api_data['target'] == Targets.GEMFILE and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not project_helper.collect_data_for_project_gemfile_auto_system_path(api_data=api_data):
                    return False

            # Create new project with GEMFILE.lock file {from path}
            if api_data['target'] == Targets.GEMFILE_LOCK and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not project_helper.collect_data_for_project_gemfile_lock_auto_system_path(api_data=api_data):
                    return False

            # Create new project from user format file {from path}
            if api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.USER and \
                    api_data['file'] is not None:
                if not project_helper.collect_data_for_project_any_auto_user_path(api_data=api_data):
                    return False

            # Create new project from interactive mode
            if api_data['method'] == Methods.MANUAL and \
                    api_data['format'] == Formats.USER and \
                    api_data['file'] is None:
                if not project_helper.collect_data_for_project_any_manual_user_none(api_data=api_data):
                    return False

            # Create project with PHP Composer.json file {from path}
            if api_data['target'] == Targets.PHP_COMPOSER_JSON and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not project_helper.collect_data_for_project_php_composer_json_system_path(api_data=api_data):
                    return False

            # Create project with PHP Composer.lock file {from path}
            if api_data['target'] == Targets.PHP_COMPOSER_LOCK and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not project_helper.collect_data_for_project_php_composer_lock_system_path(api_data=api_data):
                    return False

            # Create project with Maven pom.xml file {from path}
            if api_data['target'] == Targets.POM and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not project_helper.collect_data_for_project_maven_pom_system_path(api_data=api_data):
                    return False

            # Create project with yarn.lock file {from path}
            if api_data['target'] == Targets.YARN and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not project_helper.collect_data_for_project_yarn_lock_system_path(api_data=api_data):
                    return False

        if len(api_data['components']) > 0:
            return self.web_api.send_create_new_project_request(api_data=api_data)

        print_line('Something wrong with app parameters or components list is empty. Please, look through README.md')
        return True

    # -------------------------------------------------------------------------
    # SET
    # -------------------------------------------------------------------------

    def action_create_new_set(self, api_data):
        """
        Run action: CREATE New Set in different cases.
        :param api_data: api data set
        :return: result, modify api_data
        """

        set_helper = SetHelper()

        if not set_helper.create_set_validate(api_data=api_data):
            return False

        targets = api_data['target']
        files = api_data['file']

        for i in range(0, len(targets)):

            api_data['target'] = targets[i]
            api_data['file'] = files[i]

            # Create new set with OS packages {from shell request}
            if api_data['target'] == Targets.OS and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is None:
                if not set_helper.collect_data_for_set_os_auto_system_none(api_data=api_data):
                    return False

            # Create set with OS packages from shell request unloading file {from path}
            if api_data['target'] == Targets.OS and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not set_helper.collect_data_for_set_os_auto_system_path(api_data=api_data):
                    return False

            # Create set with PIP packages {from shell request}
            if api_data['target'] == Targets.PIP and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is None:
                if not set_helper.collect_data_for_set_pip_auto_system_none(api_data=api_data):
                    return False

            if api_data['target'] == Targets.PIP3 and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is None:
                if not set_helper.collect_data_for_set_pip_auto_system_none(api_data=api_data):
                    return False

            # Create set with PIP from file {from path}
            if api_data['target'] == Targets.PIP and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not set_helper.collect_data_for_set_pip_auto_system_path(api_data=api_data):
                    return False

            if api_data['target'] == Targets.PIP3 and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not set_helper.collect_data_for_set_pip_auto_system_path(api_data=api_data):
                    return False

            # Create set with PIP requirements.txt {from path}
            if api_data['target'] == Targets.REQ and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not set_helper.collect_data_for_set_requirements_auto_system_path(api_data=api_data):
                    return False

            # Create set with requirements pip3
            if api_data['target'] == Targets.REQ3 and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not set_helper.collect_data_for_set_requirements_auto_system_path(api_data=api_data):
                    return False

            # Create set from requirements file
            if api_data['target'] == Targets.REQUIREMENTS and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not set_helper.collect_data_for_set_requirements_auto_system_path(api_data=api_data):
                    return False

            # Create set from requirements pip3 file
            if api_data['target'] == Targets.REQUIREMENTS3 and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not set_helper.collect_data_for_set_requirements_auto_system_path(api_data=api_data):
                    return False

            # Create set with NPM packages {from shell request} - global
            if api_data['target'] == Targets.NPM and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is None:
                if not set_helper.collect_data_for_set_npm_auto_system_none(api_data=api_data):
                    return False

            # Create set with NPM packages {from shell request} - local
            if api_data['target'] == Targets.NPM_LOCAL and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not set_helper.collect_data_for_set_npm_local_auto_system_path(api_data=api_data):
                    return False

            # Create set with NPM packages {from file}
            if api_data['target'] == Targets.NPM and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not set_helper.collect_data_for_set_npm_auto_system_path(api_data=api_data):
                    return False

            # Create set with NPM package.json file {from path}
            if api_data['target'] == Targets.PACKAGE_JSON and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not set_helper.collect_data_for_set_package_json_auto_system_path(api_data=api_data):
                    return False

            # Create set with NPM package_lock.json file {from path}
            if api_data['target'] == Targets.PACKAGE_LOCK_JSON and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not set_helper.collect_data_for_set_package_lock_json_auto_system_path(api_data=api_data):
                    return False

            # Create set with GEM packages {from shell request}
            if api_data['target'] == Targets.GEM and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is None:
                if not set_helper.collect_data_for_set_gem_auto_system_none(api_data=api_data):
                    return False

            # Create set with GEM packages from shell request unloading file {from path}
            if api_data['target'] == Targets.GEM and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not set_helper.collect_data_for_set_gem_auto_system_path(api_data=api_data):
                    return False

            # Create set with GEMLIST file {from path}
            if api_data['target'] == Targets.GEMFILE and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not set_helper.collect_data_for_set_gemfile_auto_system_path(api_data=api_data):
                    return False

            # Create set with GEMLIST file {from path}
            if api_data['target'] == Targets.GEMFILE_LOCK and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not set_helper.collect_data_for_set_gemfile_lock_auto_system_path(api_data=api_data):
                    return False

            # Create set with User defined packages in file (from path{
            if api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.USER and \
                    api_data['file'] is not None:
                if not set_helper.collect_data_for_set_any_auto_user_path(api_data=api_data):
                    return False

            # Create set with User defined packages in interactive mode
            if api_data['method'] == Methods.MANUAL and \
                    api_data['format'] == Formats.USER and \
                    api_data['file'] is None:
                if not set_helper.collect_data_for_set_any_manual_user_none(api_data=api_data):
                    return False

            # Create set with PHP Composer.json file {from path}
            if api_data['target'] == Targets.PHP_COMPOSER_JSON and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                return set_helper.collect_data_for_set_php_composer_json_system_path(api_data=api_data)

            # Create set with PHP Composer.lock file {from path}
            if api_data['target'] == Targets.PHP_COMPOSER_LOCK and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not set_helper.collect_data_for_set_php_composer_lock_system_path(api_data=api_data):
                    return False

            # Create set with pom.xml
            if api_data['target'] == Targets.POM and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not set_helper.collect_data_for_set_maven_pom_system_path(api_data=api_data):
                    return False

            # Create set with yarn.lock file {from path}
            if api_data['target'] == Targets.YARN and \
                    api_data['method'] == Methods.AUTO and \
                    api_data['format'] == Formats.SYSTEM and \
                    api_data['file'] is not None:
                if not set_helper.collect_data_for_set_yarn_lock_system_path(api_data=api_data):
                    return False
        
        if len(api_data['components']) > 0:
            return self.web_api.send_create_new_component_set_request(api_data=api_data)

        print_line('Something wrong with app parameters. Please, look through README.md')
        return False

    # -------------------------------------------------------------------------
    # Show
    # -------------------------------------------------------------------------

    def action_show_platforms_projects_or_sets(self, api_data):
        """
        Run action: Show platforms, projects or component sets.
        :param api_data: api data set
        :return: result
        """
        show_helper = ShowHelper()

        if api_data['action'] == Actions.SHOW_PLATFORMS:
            return show_helper.action_show_platforms(api_data=api_data)

        elif api_data['action'] == Actions.SHOW_PROJECTS:
            if api_data['platform'] is None or \
                    api_data['platform'] == '':
                print_line('Empty platform name.')
                return False

            platform_number = self.web_api.get_platform_number_by_name(api_data=api_data)

            if platform_number == -1:
                print_line("No such platform: {0}.".format(api_data['platform']))
                return False

            return show_helper.action_show_projects(api_data=api_data)

        elif api_data['action'] == Actions.SHOW_SET:
            if api_data['platform'] is None or \
                    api_data['platform'] == '':
                print_line('Empty platform name.')
                return False

            platform_number = self.web_api.get_platform_number_by_name(api_data=api_data)

            if platform_number == -1:
                print_line("No such platform: {0}.".format(api_data['platform']))
                return False

            if api_data['project'] is None or \
                    api_data['project'] == '':
                print_line('Empty project name.')
                return False

            project_number = self.web_api.get_project_number_by_name(api_data=api_data)

            if project_number == -1:
                print_line("No such project {0} in platform {1}.".format(api_data['project'], api_data['platform']))
                return False

            return show_helper.action_show_set(api_data=api_data)

        elif api_data['action'] == Actions.SHOW_ISSUES:
            if api_data['platform'] is None or \
                    api_data['platform'] == '':
                print_line('Empty platform name.')
                return False

            platform_number = self.web_api.get_platform_number_by_name(api_data=api_data)

            if platform_number == -1:
                print_line("No such platform: {0}.".format(api_data['platform']))
                return False

            if api_data['project'] is None or \
                    api_data['project'] == '':
                print_line('Empty platform name.')
                return False

            project_number = self.web_api.get_project_number_by_name(api_data=api_data)

            if project_number == -1:
                print_line("No such project {0} in platform {1}.".format(api_data['project'], api_data['platform']))
                return False

            return show_helper.action_show_issues(api_data=api_data)

        return False

    # -------------------------------------------------------------------------
    # Delete
    # -------------------------------------------------------------------------

    @staticmethod
    def action_delete_platform(api_data):
        """
        Run action: Delete defined Platform.
        :param api_data: api data set
        :return: result
        """
        platform_helper = PlatformHelper()

        return platform_helper.delete_platform(api_data=api_data)

    @staticmethod
    def action_delete_project(api_data):
        """
        Run action: Delete defined Project.
        :param api_data: api data set
        :return: result
        """
        project_helper = ProjectHelper()

        return project_helper.delete_project(api_data=api_data)

    # -------------------------------------------------------------------------
    # Archive
    # -------------------------------------------------------------------------

    @staticmethod
    def action_archive_platform(api_data):
        """
        Run action: Archive defined Platform.
        :param api_data: api data set
        :return: result
        """
        platform_helper = PlatformHelper()

        return platform_helper.archive_platform(api_data=api_data)

    @staticmethod
    def action_archive_project(api_data):
        """
        Run action: Archive defined Project.
        :param api_data: api data set
        :return: result
        """
        project_helper = ProjectHelper()

        return project_helper.archive_project(api_data=api_data)

    # -------------------------------------------------------------------------
    # Restore
    # -------------------------------------------------------------------------

    @staticmethod
    def action_restore_platform(api_data):
        """
        Run action: restore Platform from Archive.
        :param api_data: api data set
        :return: result
        """
        platform_helper = PlatformHelper()

        return platform_helper.restore_platform(api_data=api_data)

    @staticmethod
    def action_restore_project(api_data):
        """
        Run action: Restore Project from Archive.
        :param api_data:
        :return:
        """
        project_helper = ProjectHelper()

        return project_helper.restore_project(api_data=api_data)

    # -------------------------------------------------------------------------
    # Checkers
    # -------------------------------------------------------------------------

    @staticmethod
    def check_action_type_match(api_data):
        """
        Check if action type, pointed in arguments match with template.
        :param api_data: api data set
        :return: result
        """

        if 'action' not in api_data:
            return False

        if api_data['action'] != Actions.SAVE_CONFIG and \
                api_data['action'] != Actions.CREATE_PLATFORM and \
                api_data['action'] != Actions.CREATE_PROJECT and \
                api_data['action'] != Actions.CREATE_SET and \
                api_data['action'] != Actions.SHOW_PLATFORMS and \
                api_data['action'] != Actions.SHOW_PROJECTS and \
                api_data['action'] != Actions.SHOW_SET and \
                api_data['action'] != Actions.SHOW_ISSUES and \
                api_data['action'] != Actions.DELETE_PLATFORM and \
                api_data['action'] != Actions.DELETE_PROJECT and \
                api_data['action'] != Actions.ARCHIVE_PLATFORM and \
                api_data['action'] != Actions.ARCHIVE_PROJECT and \
                api_data['action'] != Actions.RESTORE_PLATFORM and \
                api_data['action'] != Actions.RESTORE_PROJECT:
            return False

        return True

    # -------------------------------------------------------------------------
    # Config actions
    # -------------------------------------------------------------------------

    @staticmethod
    def save_config_to_file(api_data):
        """
        Save data into config fle in yaml format.
        :param api_data: api data set
        :return: result
        """

        file_name = '.surepatch.yaml'
        file_path = os.path.expanduser('~')
        full_path = os.path.join(file_path, file_name)

        config = dict(
            team=api_data['team'],
            user=api_data['user'],
            password=api_data['password'],
            auth_token=api_data['auth_token'],
            logo=api_data['logo']
        )

        with open(full_path, 'w') as yaml_config_file:
            try:
                yaml.dump(config, yaml_config_file)
                return True

            except yaml.YAMLError as yaml_exception:
                print_line('Config file save in yaml format exception: {0}.'.format(yaml_exception))
                return False

            finally:
                yaml_config_file.close()

    def load_config_from_file(self, api_data):
        """
        Load data from config file in yaml format.
        :param api_data: api data set
        :return: result
        """

        file_name = '.surepatch.yaml'
        file_path = os.path.expanduser('~')
        full_path = os.path.join(file_path, file_name)

        if not os.path.isfile(full_path):
            print_line('Config file does not exist: ~/{0}.'.format(file_name))
            print_line('Create config file first with parameter --action=save_config.')
            return False

        components_helper = ComponentsHelper()
        enc = components_helper.define_file_encoding(full_path)

        if enc == 'undefined':
            print_line('Undefined file encoding. Please, use utf-8 or utf-16.')
            return False

        with open(full_path, 'r') as yaml_config_file:
            try:
                config = yaml.load(yaml_config_file)

                if 'team' not in config:
                    config['team'] = None

                api_data['team'] = config['team']

                if 'user' not in config:
                    config['user'] = None

                api_data['user'] = config['user']

                if 'password' not in config:
                    config['password'] = None

                api_data['password'] = config['password']

                if 'auth_token' not in config:
                    config['auth-token'] = None

                api_data['auth_token'] = config['auth_token']

                if 'logi' not in config:
                    config['logo'] = 'off'

                api_data['logo'] = config['logo']

                return True

            except yaml.YAMLError as yaml_exception:
                print_line('Get an exception while read config file: {0}.'.format(yaml_exception))
                return False

            finally:
                yaml_config_file.close()