Example #1
0
    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()
Example #2
0
    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()