Python yaml.MarkedYAMLError() Examples

The following are 8 code examples of yaml.MarkedYAMLError(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module yaml , or try the search function .
Example #1
Source File: flatten.py    From operator-courier with Apache License 2.0 6 votes vote down vote up
def get_folder_semver(folder_path: str):
    for item in os.listdir(folder_path):
        item_path = os.path.join(folder_path, item)
        if not os.path.isfile(item_path) or not is_yaml_file(item_path):
            continue

        with open(item_path, 'r') as f:
            file_content = f.read()

        if identify.get_operator_artifact_type(file_content) == 'ClusterServiceVersion':
            try:
                csv_version = safe_load(file_content)['spec']['version']
            except MarkedYAMLError:
                msg = f'{item} is not a valid YAML file.'
                logger.error(msg)
                raise OpCourierBadYaml(msg)
            except KeyError:
                msg = f'{item} is not a valid CSV file as "spec.version" ' \
                      f'field is required'
                logger.error(msg)
                raise OpCourierBadBundle(msg, {})
            return csv_version

    return None 
Example #2
Source File: identify.py    From operator-courier with Apache License 2.0 6 votes vote down vote up
def get_operator_artifact_type(operatorArtifactString):
    """get_operator_artifact_type takes a yaml string and determines if it is
    one of the expected bundle types.

    :param operatorArtifactString: Yaml string to type check
    """

    # Default to unknown file unless identified
    artifact_type = UNKNOWN_FILE

    try:
        operatorArtifact = safe_load(operatorArtifactString)
    except MarkedYAMLError:
        msg = "Courier requires valid input YAML files"
        logger.error(msg)
        raise OpCourierBadYaml(msg)
    else:
        if isinstance(operatorArtifact, dict):
            if "packageName" in operatorArtifact:
                artifact_type = PKG_STR
            elif operatorArtifact.get("kind") in {CRD_STR, CSV_STR}:
                artifact_type = operatorArtifact["kind"]
        return artifact_type 
Example #3
Source File: local_filesystem.py    From airflow with Apache License 2.0 6 votes vote down vote up
def _parse_yaml_file(file_path: str) -> Tuple[Dict[str, List[str]], List[FileSyntaxError]]:
    """
    Parse a file in the YAML format.

    :param file_path: The location of the file that will be processed.
    :type file_path: str
    :return: Tuple with mapping of key and list of values and list of syntax errors
    """
    with open(file_path) as f:
        content = f.read()

    if not content:
        return {}, [FileSyntaxError(line_no=1, message="The file is empty.")]
    try:
        secrets = yaml.safe_load(content)
    except yaml.MarkedYAMLError as e:
        return {}, [FileSyntaxError(line_no=e.problem_mark.line, message=str(e))]
    if not isinstance(secrets, dict):
        return {}, [FileSyntaxError(line_no=1, message="The file should contain the object.")]

    return secrets, [] 
Example #4
Source File: config_builder.py    From haros with MIT License 6 votes vote down vote up
def make_rosparam(self, name, ns, value, condition):
    # ---- lazy rosparam import as per the oringinal roslaunch code
        global rosparam
        if rosparam is None:
            import rosparam
        try:
            value = yaml.safe_load(value)
        except yaml.MarkedYAMLError as e:
            raise ConfigurationError(str(e))
    # ----- try to use given name, namespace or both
        ns = self._ns_join(ns or self.private_ns, self.private_ns)
        if name:
            name = self._ns_join(name, ns)
        else:
            if not isinstance(value, dict):
                raise ConfigurationError("'param' attribute must be set"
                                         " for non-dictionary values")
    # ----- this will unfold, so we can use namespace in place of a name
            name = ns
        conditions = list(self.conditions)
        if not condition is True:
            conditions.append(condition)
        self._yaml_param(name, value, conditions, private = False) 
Example #5
Source File: yaml.py    From PyPlanet with GNU General Public License v3.0 6 votes vote down vote up
def load(self):
		# Prepare + load directory.
		super().load()

		# Load the files and parse Yaml.
		parsed_settings = dict()

		try:
			for file_name in self.files:
				file_path = os.path.join(self.directory, file_name)
				with open(file_path, 'r') as file_handle:
					parsed_settings.update(yaml.safe_load(file_handle))
		except (yaml.YAMLError, yaml.MarkedYAMLError) as e:
			raise ImproperlyConfigured(
				'Your settings file(s) contain invalid YAML syntax! Please fix and restart!, {}'.format(str(e))
			)

		# Loop and set in local settings (+ uppercase keys).
		for key, value in parsed_settings.items():
			self.settings[key.upper()] = value 
Example #6
Source File: utils.py    From backend.ai-manager with GNU Lesser General Public License v3.0 5 votes vote down vote up
def check_api_params(checker: t.Trafaret, loads: Callable[[str], Any] = None,
                     query_param_checker: t.Trafaret = None) -> Any:
    # FIXME: replace ... with [web.Request, Any...] in the future mypy
    def wrap(handler: Callable[..., Awaitable[web.Response]]):

        @functools.wraps(handler)
        async def wrapped(request: web.Request, *args, **kwargs) -> web.Response:
            orig_params: Any
            body: str = ''
            try:
                body_exists = request.can_read_body
                if body_exists:
                    body = await request.text()
                    if request.content_type == 'text/yaml':
                        orig_params = yaml.load(body, Loader=yaml.BaseLoader)
                    else:
                        orig_params = (loads or json.loads)(body)
                else:
                    orig_params = dict(request.query)
                stripped_params = orig_params.copy()
                stripped_params.pop('owner_access_key', None)
                log.debug('stripped raw params: {}', mask_sensitive_keys(stripped_params))
                checked_params = checker.check(stripped_params)
                if body_exists and query_param_checker:
                    query_params = query_param_checker.check(request.query)
                    kwargs['query'] = query_params
            except (json.decoder.JSONDecodeError, yaml.YAMLError, yaml.MarkedYAMLError):
                raise InvalidAPIParameters('Malformed body')
            except t.DataError as e:
                raise InvalidAPIParameters('Input validation error',
                                           extra_data=e.as_dict())
            return await handler(request, checked_params, *args, **kwargs)

        return wrapped

    return wrap 
Example #7
Source File: session_template.py    From backend.ai-manager with GNU Lesser General Public License v3.0 5 votes vote down vote up
def put(request: web.Request, params: Any) -> web.Response:
    dbpool = request.app['dbpool']
    template_id = request.match_info['template_id']

    requester_access_key, owner_access_key = await get_access_key_scopes(request, params)
    log.info('PUT (ak:{0}/{1})',
             requester_access_key, owner_access_key if owner_access_key != requester_access_key else '*')

    async with dbpool.acquire() as conn, conn.begin():
        query = (sa.select([session_templates.c.id])
                   .select_from(session_templates)
                   .where((session_templates.c.id == template_id) &
                          (session_templates.c.is_active)
                          ))
        result = await conn.scalar(query)
        if not result:
            raise TaskTemplateNotFound
        try:
            body = json.loads(params['payload'])
        except json.JSONDecodeError:
            body = yaml.load(params['payload'], Loader=yaml.BaseLoader)
        except (yaml.YAMLError, yaml.MarkedYAMLError):
            raise InvalidAPIParameters('Malformed payload')
        body = task_template_v1.check(body)
        query = (sa.update(session_templates)
                   .values(template=body, name=body['metadata']['name'])
                   .where((session_templates.c.id == template_id)))
        result = await conn.execute(query)
        assert result.rowcount == 1

        return web.json_response({'success': True}) 
Example #8
Source File: _config_parser.py    From webviz-config with MIT License 5 votes vote down vote up
def __init__(self, yaml_file: str):

        ConfigParser.check_for_tabs_in_file(yaml_file)

        try:
            self._configuration = yaml.safe_load(open(yaml_file, "r"))
        except yaml.MarkedYAMLError as excep:
            extra_info = (
                f"There is something wrong in the configuration file {yaml_file}. "
            )

            if hasattr(excep, "problem_mark"):
                extra_info += (
                    "The typo is probably somewhere around "
                    f"line {excep.problem_mark.line + 1}."
                )

            raise type(excep)(
                f"{excep}. {terminal_colors.RED}{terminal_colors.BOLD}"
                f"{extra_info}{terminal_colors.END}"
            ).with_traceback(sys.exc_info()[2])

        self._config_folder = pathlib.Path(yaml_file).parent
        self._page_ids: typing.List[str] = []
        self._assets: set = set()
        self.clean_configuration()