Python yaml.YAMLError() Examples

The following are 30 code examples of yaml.YAMLError(). 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: download.py    From gog-galaxy-plugin-downloader with GNU General Public License v3.0 9 votes vote down vote up
def get_plugin_config(config_uri):
    """
    Downloads/opens configuration yaml file, returns
    dict of Galaxy plugins
    """
    # Try to open the URI as a URL or fall back to opening local file
    try:
        config_uri_parsed = urlparse(config_uri)
        if config_uri_parsed.scheme in ['https', 'http']:
            url = urlopen(config_uri)
            yaml_data = url.read()
        else:
            with open(config_uri, 'r') as file_data:
                yaml_data = file_data.read()
    except URLError as e:
        print(e)

    # Parse the YAML configuration
    try:
        plugin_data = yaml.safe_load(yaml_data)

        return plugin_data['plugins']
    except yaml.YAMLError as e:
        print(e) 
Example #2
Source File: config.py    From vergeml with MIT License 6 votes vote down vote up
def load_yaml_file(filename, label='config file', loader=yaml.Loader):
    """Load a yaml config file.
    """
    try:
        with open(filename, "r") as file:
            res = yaml.load(file.read(), Loader=loader) or {}
            if not isinstance(res, dict):
                msg = f"Please ensure that {label} consists of key value pairs."
                raise VergeMLError(f"Invalid {label}: {filename}", msg)
            return res
    except yaml.YAMLError as err:
        if hasattr(err, 'problem_mark'):
            mark = getattr(err, 'problem_mark')
            problem = getattr(err, 'problem')
            message = f"Could not read {label} {filename}:"
            message += "\n" + display_err_in_file(filename, mark.line, mark.column, problem)
        elif hasattr(err, 'problem'):
            problem = getattr(err, 'problem')
            message = f"Could not read {label} {filename}: {problem}"
        else:
            message = f"Could not read {label} {filename}: YAML Error"

        suggestion = f"There is a syntax error in your {label} - please fix it and try again."

        raise VergeMLError(message, suggestion)

    except OSError as err:
        msg = "Please ensure the file exists and you have the required access privileges."
        raise VergeMLError(f"Could not open {label} {filename}: {err.strerror}", msg) 
Example #3
Source File: homework.py    From vj4 with GNU Affero General Public License v3.0 6 votes vote down vote up
def _parse_penalty_rules_yaml(penalty_rules):
  try:
    penalty_rules = yaml.safe_load(penalty_rules)
  except yaml.YAMLError:
    raise error.ValidationError('penalty_rules', 'parse error')
  if not isinstance(penalty_rules, dict):
    raise error.ValidationError('penalty_rules', 'invalid format')
  new_rules = collections.OrderedDict()
  try:
    for time, coefficient in sorted(penalty_rules.items(), key=lambda x: float(x[0])):
      time_val = str(int(float(time) * 60 * 60))
      coefficient_val = float(coefficient)
      new_rules[time_val] = coefficient_val
  except ValueError:
    raise error.ValidationError('penalty_rules', 'value error')
  return new_rules 
Example #4
Source File: util.py    From ftw with Apache License 2.0 6 votes vote down vote up
def extract_yaml(yaml_files):
    """
    Take a list of yaml_files and load them to return back
    to the testing program
    """
    loaded_yaml = []
    for yaml_file in yaml_files:
        try:
            with open(yaml_file, 'r') as fd:
                loaded_yaml.append(yaml.safe_load(fd))
        except IOError as e:
            print('Error reading file', yaml_file)
            raise e
        except yaml.YAMLError as e:
            print('Error parsing file', yaml_file)
            raise e
        except Exception as e:
            print('General error')
            raise e
    return loaded_yaml 
Example #5
Source File: config_generator.py    From rift-python with Apache License 2.0 6 votes vote down vote up
def parse_meta_configuration(file_name):
    try:
        with open(file_name, 'r') as stream:
            try:
                config = yaml.safe_load(stream)
            except yaml.YAMLError as exception:
                raise exception
    except IOError:
        fatal_error('Could not open input meta-configuration file "{}"'.format(file_name))
    validator = cerberus.Validator(SCHEMA)
    if not validator.validate(config, SCHEMA):
        pretty_printer = pprint.PrettyPrinter()
        pretty_printer.pprint(validator.errors)
        exit(1)
    return validator.normalized(config) 
Example #6
Source File: util.py    From ftw with Apache License 2.0 6 votes vote down vote up
def extract_yaml(yaml_files):
    """
    Take a list of yaml_files and load them to return back
    to the testing program
    """
    loaded_yaml = []
    for yaml_file in yaml_files:
        try:
            with io.open(yaml_file, encoding='utf-8') as fd:
                loaded_yaml.append(yaml.safe_load(fd))
        except IOError as e:
            print('Error reading file', yaml_file)
            raise e
        except yaml.YAMLError as e:
            print('Error parsing file', yaml_file)
            raise e
        except Exception as e:
            print('General error')
            raise e
    return loaded_yaml 
Example #7
Source File: Resources.py    From pcocc with GNU General Public License v3.0 6 votes vote down vote up
def load(self, filename):
        try:
            stream = file(filename, 'r')
            res_config = yaml.safe_load(stream)
        except yaml.YAMLError as err:
            raise InvalidConfigurationError(str(err))
        except IOError as err:
            raise InvalidConfigurationError(str(err))

        self._rsets = {}
        self.default_rset = None
        for name, res_attr in res_config.iteritems():
            self[name] = ResSet(name, res_attr)
            if res_attr.get('default', False):
                if self.default_rset:
                    raise InvalidConfigurationError(
                        'multiple default resource sets configured')
                else:
                    self.default_rset = name 
Example #8
Source File: ObjectStore.py    From pcocc with GNU General Public License v3.0 6 votes vote down vote up
def _read_meta(self, meta_path, name, revision):
        if not os.path.isfile(meta_path):
            raise ObjectNotFound(name, self._name, revision)

        try:
            with open(meta_path, 'r') as f:
                meta = yaml.safe_load(f)
        except (OSError, IOError) as e:
            raise PcoccError('Unable to get metadata for {0}: {1}'.format(
                    name, e))
        except yaml.YAMLError as e:
            raise PcoccError('Bad metadata for {0}: {1}'.format(
                    name, e))

        try:
            jsonschema.validate(meta,
                                yaml.safe_load(metadata_schema))
        except jsonschema.exceptions.ValidationError as e:
            raise PcoccError('Bad metadata for {0}: {1}'.format(
                    name, e))

        return meta 
Example #9
Source File: bootaction.py    From drydock with Apache License 2.0 6 votes vote down vote up
def _parse_package_list(self, data):
        """Parse data expecting a list of packages to install.

        Expect data to be a bytearray reprsenting a JSON or YAML
        document.

        :param data: A bytearray of data to parse
        """
        try:
            data_string = data.decode('utf-8')
            parsed_data = yaml.safe_load(data_string)

            if isinstance(parsed_data, dict):
                self.package_list = self._extract_package_list(parsed_data)
            else:
                raise errors.InvalidPackageListFormat(
                    "Package data should have a top-level mapping/object.")
        except yaml.YAMLError as ex:
            raise errors.InvalidPackageListFormat(
                "Invalid YAML in package list: %s" % str(ex)) 
Example #10
Source File: model.py    From benchexec with Apache License 2.0 6 votes vote down vote up
def load_task_definition_file(task_def_file):
    """Open and parse a task-definition file in YAML format."""
    try:
        with open(task_def_file) as f:
            task_def = yaml.safe_load(f)
    except OSError as e:
        raise BenchExecException("Cannot open task-definition file: " + str(e))
    except yaml.YAMLError as e:
        raise BenchExecException("Invalid task definition: " + str(e))

    if str(task_def.get("format_version")) not in ["0.1", "1.0"]:
        raise BenchExecException(
            "Task-definition file {} specifies invalid format_version '{}'.".format(
                task_def_file, task_def.get("format_version")
            )
        )

    return task_def 
Example #11
Source File: ObjectStore.py    From pcocc with GNU General Public License v3.0 6 votes vote down vote up
def _validate_repo_config(self):
        try:
            with open(self._config_path) as f:
                repo_config = yaml.safe_load(f)
                jsonschema.validate(repo_config,
                                        yaml.safe_load(repo_config_schema))

        except (yaml.YAMLError,
                IOError,
                jsonschema.exceptions.ValidationError) as err:
            raise PcoccError(
                'Bad repository config file {0} : {1}'.format(self._config_path,
                                                                      err))

        if repo_config['version'] != 1:
            raise InvalidConfigurationError(
                'unsupported repository {0} version'.format(self.name)) 
Example #12
Source File: confloader.py    From okcat with Apache License 2.0 5 votes vote down vote up
def load(self, yml_file_path):
        with open(handle_home_case(yml_file_path), 'r') as stream:
            try:
                self.yml_conf = yaml.load(stream, yaml.SafeLoader)
                from_conf_path = self.get_from()
                if from_conf_path is not None:
                    self.from_yml_conf = ConfLoader()
                    self.from_yml_conf.load(get_conf_path(from_conf_path))

                    # print(u'find yml configuration on %s:' % yml_file_path)
                    # self.dump()

            except yaml.YAMLError as exc:
                print(exc) 
Example #13
Source File: utils.py    From py2swagger with MIT License 5 votes vote down vote up
def yaml_load(data):
        try:
            return yaml.load(data)
        except (yaml.YAMLError, AttributeError):
            return None 
Example #14
Source File: texteditor.py    From Tkinter-GUI-Programming-by-Example with MIT License 5 votes vote down vote up
def load_font_file(self, file_path):
        with open(file_path, 'r') as stream:
            try:
                config = yaml.load(stream)
            except yaml.YAMLError as error:
                print(error)
                return

        self.font_family = config['family']
        self.font_size = config['size'] 
Example #15
Source File: highlighter.py    From Tkinter-GUI-Programming-by-Example with MIT License 5 votes vote down vote up
def parse_syntax_file(self):
        with open(self.syntax_file, 'r') as stream:
            try:
                config = yaml.load(stream)
            except yaml.YAMLError as error:
                print(error)
                return

        self.categories = config['categories']
        self.numbers_color = config['numbers']['color']
        self.strings_color = config['strings']['color']

        self.configure_tags() 
Example #16
Source File: texteditor.py    From Tkinter-GUI-Programming-by-Example with MIT License 5 votes vote down vote up
def load_scheme_file(self, scheme):
        with open(scheme, 'r') as stream:
            try:
                config = yaml.load(stream)
            except yaml.YAMLError as error:
                print(error)
                return

        self.foreground = config['foreground']
        self.background = config['background']
        self.text_foreground = config['text_foreground']
        self.text_background = config['text_background'] 
Example #17
Source File: highlighter.py    From Tkinter-GUI-Programming-by-Example with MIT License 5 votes vote down vote up
def parse_syntax_file(self):
        with open(self.syntax_file, 'r') as stream:
            try:
                config = yaml.load(stream)
            except yaml.YAMLError as error:
                print(error)
                return

        self.categories = config['categories']
        self.numbers_colour = config['numbers']['colour']
        self.strings_colour = config['strings']['colour']

        self.configure_tags() 
Example #18
Source File: texteditor.py    From Tkinter-GUI-Programming-by-Example with MIT License 5 votes vote down vote up
def load_scheme_file(self, scheme):
        with open(scheme, 'r') as stream:
            try:
                config = yaml.load(stream)
            except yaml.YAMLError as error:
                print(error)
                return

        self.foreground = config['foreground']
        self.background = config['background']
        self.text_foreground = config['text_foreground']
        self.text_background = config['text_background'] 
Example #19
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 #20
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 #21
Source File: Tbon.py    From pcocc with GNU General Public License v3.0 5 votes vote down vote up
def load_yaml(cls, yaml_data):
        cert = yaml.safe_load(yaml_data)
        try:
            key_data = cert["key_data"]
            cert_data = cert["cert_data"]
            ca_cert_data = cert["ca_cert_data"]

        except (KeyError, yaml.YAMLError) as err:
            raise PcoccError("Unable to load certificate: " + str(err))

        return cls(key_data, cert_data, ca_cert_data) 
Example #22
Source File: redgem.py    From pytfa with Apache License 2.0 5 votes vote down vote up
def read_parameters(self, parameters_path):
        with open(parameters_path, 'r') as stream:
            try:
                self.params = yaml.safe_load(stream)
                print("Opened parameters file")
            except yaml.YAMLError as exc:
                print(exc) 
Example #23
Source File: config.py    From VLC-Scheduler with MIT License 5 votes vote down vote up
def load_yaml_config():
    path = os.getenv(CONFIG_ENV_VAR) or locate_yaml_config()
    
    with open(path, 'r') as stream:
        try:
            config = yaml.safe_load(stream)
        except yaml.YAMLError as e:
            raise ConfigLoadError('%s does not contain valid YAML.' % CONFIG_FILENAME) from e
    
    return config 
Example #24
Source File: Batch.py    From pcocc with GNU General Public License v3.0 5 votes vote down vote up
def load(batch_config_file, batchid, batchname, default_batchname,
             proc_type, batchuser):
        """Factory function to initialize a batch manager"""
        try:
            stream = file(batch_config_file, 'r')
            batch_config = yaml.safe_load(stream)
        except yaml.YAMLError as err:
            raise InvalidConfigurationError(str(err))
        except IOError as err:
            raise InvalidConfigurationError(str(err))

        try:
            jsonschema.validate(batch_config,
                                yaml.safe_load(batch_config_schema))
        except jsonschema.exceptions.ValidationError as err:
            raise InvalidConfigurationError(str(err))

        settings = batch_config['settings']

        if batch_config['type'] == 'slurm':
            return SlurmManager(
                batchid, batchname, default_batchname,
                settings, proc_type, batchuser)
        elif batch_config['type'] == 'local':
            return LocalManager(
                batchid, batchname, default_batchname,
                settings, proc_type, batchuser)
        else:
            raise InvalidConfigurationError("Invalid batch manager type") 
Example #25
Source File: k8s_common.py    From ansible-kubernetes-modules with Apache License 2.0 5 votes vote down vote up
def load_resource_definition(self, src):
        """ Load the requested src path """
        result = None
        path = os.path.normpath(src)
        self.helper.log("Reading definition from {}".format(path))
        if not os.path.exists(path):
            self.fail_json(msg="Error accessing {}. Does the file exist?".format(path))
        try:
            result = yaml.safe_load(open(path, 'r'))
        except (IOError, yaml.YAMLError) as exc:
            self.fail_json(msg="Error loading resource_definition: {}".format(exc))
        return result 
Example #26
Source File: __init__.py    From dionaea with GNU General Public License v2.0 5 votes vote down vote up
def load_config_from_files(filename_patterns):
    configs = []
    for filename_pattern in filename_patterns:
        for filename in glob.glob(filename_pattern):
            fp = open(filename)
            try:
                file_configs = yaml.safe_load(fp)
            except yaml.YAMLError as e:
                if hasattr(e, 'problem_mark'):
                    mark = e.problem_mark
                    logger.error(
                        "Error while parsing config file '%s' at line: %d column: %d message: '%s'",
                        filename,
                        mark.line + 1,
                        mark.column + 1,
                        e.problem
                    )
                    if e.context is not None:
                        logger.debug("Parser(context): %s" % e.context)
                else:
                    logger.error("Unknown error while parsing config file '%s'", filename)

                # Skip processing
                continue

            if isinstance(file_configs, (tuple, list)):
                configs += file_configs
    return configs 
Example #27
Source File: app_config.py    From cellphonedb with MIT License 5 votes vote down vote up
def _load_yaml(yaml_name):
        with open(yaml_name, 'r') as stream:
            try:
                return yaml.load(stream, Loader=yaml.SafeLoader)
            except yaml.YAMLError as exec:
                print(exec) 
Example #28
Source File: initialise_config.py    From bakery with Apache License 2.0 5 votes vote down vote up
def load_config(file_name):
    """Loads the yml file.

    Args:
        file_name: Filename
    """
    with open(file_name) as stream:
        try:
            return yaml.load(stream)

        except yaml.YAMLError as exc:
            print exc 
Example #29
Source File: cli.py    From ChromaTerm with MIT License 5 votes vote down vote up
def load(self, data, clear=True, rgb=False):
        """Reads rules from a YAML-based string, formatted like so:
            rules:
            - description: My first rule
              regex: Hello
              color: b#123123
            - description: My second rule is specfic to groups
              regex: W(or)ld
              color:
                0: f#321321
                1: bold

          Any errors are printed to stderr.

          Args:
            data (str): A string containg YAML data.
            clear (bool): Whether to clear the existing rules or not. If data
                could not be parsed as a YAML string, the existing rules are not
                cleared.
            rgb (bool): Whether the terminal is RGB-capable or not.
        """
        # Load the YAML configuration file
        try:
            data = yaml.safe_load(data) or {}
        except yaml.YAMLError as exception:
            eprint('Parse error:', exception)
            return

        if clear:
            self._rules = []

        rules = data.get('rules') if isinstance(data, dict) else None

        for rule in rules if isinstance(rules, list) else []:
            parsed_rule = self.parse_rule(rule, rgb=rgb)

            if isinstance(parsed_rule, Rule):
                self.add_rule(parsed_rule)
            else:
                eprint(parsed_rule) 
Example #30
Source File: elfcore.py    From rekall with GNU General Public License v2.0 5 votes vote down vote up
def LoadMetadata(self, offset):
        """Load the WinPmem metadata from the elf file."""
        self.session.logging.error(
            "DEPRECATED Elf metadata found! "
            "This will not be supported in the next release.")
        try:
            data = utils.SmartUnicode(self.base.read(offset, 1024*1024))
            yaml_file = data.split('...\n')[0]

            metadata = yaml.safe_load(yaml_file)
        except (yaml.YAMLError, TypeError) as e:
            self.session.logging.error(
                "Invalid file metadata, skipping: %s" % e)
            return

        for session_param, metadata_key in (("dtb", "CR3"),
                                            ("kernel_base", "KernBase")):
            if metadata_key in metadata:
                self.session.SetParameter(
                    session_param, metadata[metadata_key])

        previous_section = metadata.pop("PreviousHeader", None)
        if previous_section is not None:
            self.LoadMetadata(previous_section)

        pagefile_offset = metadata.get("PagefileOffset", None)
        pagefile_size = metadata.get("PagefileSize", None)

        if pagefile_offset is not None and pagefile_size is not None:
            self.LoadPageFile(pagefile_offset, pagefile_size)

        self._metadata.update(metadata)