Python yaml.BaseLoader() Examples

The following are 29 code examples of yaml.BaseLoader(). 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: index_reader.py    From container-pipeline-service with GNU General Public License v3.0 6 votes vote down vote up
def read_yaml(self, filepath):
        """
        Read the YAML file at specified location

        return the yaml data on success
        raise an exception upon failure reading/load the file
        """
        try:
            with open(filepath) as fin:
                data = yaml.load(fin, Loader=yaml.BaseLoader)
        except yaml.YAMLError as exc:
            _print("Failed to read {}".format(filepath))
            _print("Error: {}".format(exc))
            return None
        else:
            return data 
Example #2
Source File: 角色.py    From Librian with Mozilla Public License 2.0 6 votes vote down vote up
def 導入有立繪的角色():
    工程路徑, 臨時立繪文件夾, psd立繪路徑 = 虛擬機環境.工程路徑, 虛擬機環境.臨時立繪文件夾, 虛擬機環境.psd立繪路徑
    try:
        with 讀txt.讀(f'{虛擬機環境.psd立繪路徑}/映射.yaml') as f:
            映射表 = yaml.load(f, Loader=yaml.BaseLoader)
            if 映射表:
                for 角色名 in 映射表:
                    if not os.path.isdir(f'{工程路徑}/{臨時立繪文件夾}/{角色名}'):
                        psd拆包.拆包(f'{psd立繪路徑}/{角色名}.psd', f'{工程路徑}/{臨時立繪文件夾}')
                    logging.info(f'角色「{角色名}」的立繪: \n{映射表[角色名]}')
                    角色(角色名, 映射表[角色名])
            for i in os.listdir(psd立繪路徑):
                if i.endswith('.png'):
                    前名 = os.path.basename(os.path.splitext(i)[0])
                    if not os.path.isdir(f'{工程路徑}/{臨時立繪文件夾}/{前名}'):
                        全名 = os.path.join(psd立繪路徑, i)
                        psd拆包.png假裝拆包(全名, f'{工程路徑}/{臨時立繪文件夾}')
                    角色(前名, {
                        '衣': {'_默認': ['_']},
                        '顏': {'_默認': []},
                    })
    except Exception as e:
        logging.warning('角色立繪沒有導入。')
        logging.exception(e) 
Example #3
Source File: parser.py    From yamllint with GNU General Public License v3.0 6 votes vote down vote up
def token_or_comment_generator(buffer):
    yaml_loader = yaml.BaseLoader(buffer)

    try:
        prev = None
        curr = yaml_loader.get_token()
        while curr is not None:
            next = yaml_loader.get_token()
            nextnext = (yaml_loader.peek_token()
                        if yaml_loader.check_token() else None)

            yield Token(curr.start_mark.line + 1, curr, prev, next, nextnext)

            for comment in comments_between_tokens(curr, next):
                yield comment

            prev = curr
            curr = next

    except yaml.scanner.ScannerError:
        pass 
Example #4
Source File: cassandra_utils_test.py    From cassandra-medusa with Apache License 2.0 5 votes vote down vote up
def test_yaml_token_enforcement_with_tokens_and_autobootstrap(self):
        with open('tests/resources/yaml/original/cassandra_with_tokens.yaml', 'r') as f:
            shutil.copyfile('tests/resources/yaml/original/cassandra_with_tokens_and_autobootstrap.yaml',
                            'tests/resources/yaml/work/cassandra_with_tokens_and_autobootstrap.yaml')
        config = configparser.ConfigParser(interpolation=None)
        config['cassandra'] = {
            'config_file': os.path.join(os.path.dirname(__file__),
                                        'resources/yaml/work/cassandra_with_tokens_and_autobootstrap.yaml'),
            'start_cmd': '/etc/init.d/cassandra start',
            'stop_cmd': '/etc/init.d/cassandra stop',
            'is_ccm': '1'
        }

        medusa_config = MedusaConfig(
            storage=None,
            monitoring=None,
            cassandra=_namedtuple_from_dict(CassandraConfig, config['cassandra']),
            ssh=None,
            checks=None,
            logging=None
        )

        cassandra = Cassandra(medusa_config.cassandra)
        tokens = ['1', '2', '3']
        cassandra.replaceTokensInCassandraYamlAndDisableBootstrap(tokens)

        with open('tests/resources/yaml/work/cassandra_with_tokens_and_autobootstrap.yaml', 'r') as f:
            modified_yaml = yaml.load(f, Loader=yaml.BaseLoader)
            self.assertEqual(modified_yaml.get('num_tokens'), '3')
            self.assertEqual(modified_yaml.get('initial_token'), '1,2,3')
            self.assertEqual(modified_yaml.get('auto_bootstrap'), 'false') 
Example #5
Source File: job_calc.py    From acousticbrainz-server with GNU General Public License v2.0 5 votes vote down vote up
def create_profile(in_file, out_file, sha1):
    """Prepare a profile file for use with essentia. Sanity check to make sure
    important values are present.
    """

    try:
        with open(in_file, 'r') as f:
            doc = yaml.load(f, Loader=yaml.BaseLoader)
    except IOError as e:
        current_app.logger.error("Cannot read profile %s: %s" % (in_file, e))
        sys.exit(-1)

    try:
        models_ver = doc['mergeValues']['metadata']['version']['highlevel']['models_essentia_git_sha']
    except KeyError:
        models_ver = None

    if not models_ver:
        current_app.logger.warn("profile.conf.in needs to have 'metadata : version : highlevel :"
                                   " models_essentia_git_sha' defined.")
        sys.exit(-1)

    doc['mergeValues']['metadata']['version']['highlevel']['essentia_build_sha'] = sha1

    try:
        with open(out_file, 'w') as yaml_file:
            yaml_file.write( yaml.dump(doc, default_flow_style=False))
    except IOError as e:
        current_app.logger.error("Cannot write profile %s: %s" % (out_file, e))
        sys.exit(-1) 
Example #6
Source File: config.py    From satpy with GNU General Public License v3.0 5 votes vote down vote up
def check_yaml_configs(configs, key):
    """Get a diagnostic for the yaml *configs*.

    *key* is the section to look for to get a name for the config at hand.
    """
    diagnostic = {}
    for i in configs:
        for fname in i:
            with open(fname) as stream:
                try:
                    res = yaml.load(stream, Loader=UnsafeLoader)
                    msg = 'ok'
                except yaml.YAMLError as err:
                    stream.seek(0)
                    res = yaml.load(stream, Loader=BaseLoader)
                    if err.context == 'while constructing a Python object':
                        msg = err.problem
                    else:
                        msg = 'error'
                finally:
                    try:
                        diagnostic[res[key]['name']] = msg
                    except (KeyError, TypeError):
                        # this object doesn't have a 'name'
                        pass
    return diagnostic 
Example #7
Source File: virginmedia.py    From virgin-media-hub3 with GNU General Public License v3.0 5 votes vote down vote up
def _setup_properties(dirname):
    """Add class variables from the yaml file"""
    import yaml
    with open(os.path.join(dirname, "attributes.yml")) as attr_file:
        attrmap = yaml.load(attr_file, Loader=yaml.BaseLoader)

    oids = list(attrmap.keys())
    for oid1, oid2 in zip(oids, oids[1:]):
        if oidsplit(oid2) < oidsplit(oid1):
            warnings.warn("OID ordering is wrong: %s should be after %s" % (oid2, oid1))

    names = []
    for oid, settings in attrmap.items():
        try:
            if settings['name'] in names:
                raise ValueError("Duplicate name for oid %s" % oid)
            names.append(settings['name'])

            kwargs = {"oid": oid}
            if 'translator' in settings:
                # pylint: disable=eval-used
                kwargs['translator'] = eval(settings['translator'])
            if 'doc' in settings:
                kwargs['doc'] = settings['doc']

            setattr(Hub, settings['name'], snmp.Attribute(**kwargs))
        except Exception:
            warnings.warn("Problem with OID %s" % oid)
            raise 
Example #8
Source File: quoted_strings.py    From yamllint with GNU General Public License v3.0 5 votes vote down vote up
def _quotes_are_needed(string):
    loader = yaml.BaseLoader('key: ' + string)
    # Remove the 5 first tokens corresponding to 'key: ' (StreamStartToken,
    # BlockMappingStartToken, KeyToken, ScalarToken(value=key), ValueToken)
    for _ in range(5):
        loader.get_token()
    try:
        a, b = loader.get_token(), loader.get_token()
        if (isinstance(a, yaml.ScalarToken) and a.style is None and
                isinstance(b, yaml.BlockEndToken)):
            return False
        return True
    except yaml.scanner.ScannerError:
        return True 
Example #9
Source File: linter.py    From yamllint with GNU General Public License v3.0 5 votes vote down vote up
def get_syntax_error(buffer):
    try:
        list(yaml.parse(buffer, Loader=yaml.BaseLoader))
    except yaml.error.MarkedYAMLError as e:
        problem = LintProblem(e.problem_mark.line + 1,
                              e.problem_mark.column + 1,
                              'syntax error: ' + e.problem + ' (syntax)')
        problem.level = 'error'
        return problem 
Example #10
Source File: main.py    From professional-services with Apache License 2.0 5 votes vote down vote up
def __init__(self, config_file):
        with open(config_file, 'r') as config_yaml:
            config_dict = yaml.load(config_yaml, Loader=yaml.BaseLoader)
            for pname in config_dict:
                setattr(self, pname.lower(), config_dict[pname]) 
Example #11
Source File: scpi.py    From basil with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def init(self):
        super(scpi, self).init()
        self._scpi_commands = _scpi_ieee_488_2.copy()
        device_desciption = os.path.join(os.path.dirname(__file__), self._init['device'].lower().replace(" ", "_") + '.yaml')
        try:
            with open(device_desciption, 'r') as in_file:
                self._scpi_commands.update(load(in_file, Loader=BaseLoader))
        except scanner.ScannerError:
            raise RuntimeError('Parsing error for ' + self._init['device'] + ' device description in file ' + device_desciption)
        except IOError:
            raise RuntimeError('Cannot find a device description for ' + self._init['device'] + '. Consider adding it!')
        if 'identifier' in self._scpi_commands and self._scpi_commands['identifier']:
            name = self.get_name()
            if self._scpi_commands['identifier'] not in name:
                raise RuntimeError('Wrong device description (' + self._init['device'] + ') loaded for ' + name) 
Example #12
Source File: test_configuration.py    From vitrage with Apache License 2.0 5 votes vote down vote up
def _load_yaml_file(path):
        with open(path, 'r') as stream:
            return yaml.load(stream, Loader=yaml.BaseLoader) 
Example #13
Source File: file.py    From vitrage with Apache License 2.0 5 votes vote down vote up
def load_yaml_file(full_path, with_exception=False):
    if not os.path.isfile(full_path):
        LOG.error("File doesn't exist: %s." % full_path)
        return None

    with open(full_path, 'r') as stream:
        try:
            return yaml.load(stream, Loader=yaml.BaseLoader)
        except Exception as e:
            if with_exception:
                raise
            else:
                LOG.error("Fails to parse file: %s. %s" % (full_path, e))
                return None 
Example #14
Source File: utils.py    From conda-concourse-ci with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def load_yaml_config_dir(platforms_dir, platform_filters):
    platforms = []
    for f in os.listdir(platforms_dir):
        if f.endswith('.yml') and any(fnmatch.fnmatch(f, pat) for pat in platform_filters):
            with open(os.path.join(platforms_dir, f)) as buff:
                platforms.append(yaml.load(buff, Loader=yaml.BaseLoader))
    return platforms 
Example #15
Source File: file_utils.py    From orca with Apache License 2.0 5 votes vote down vote up
def load_yaml(path):
    data = None
    with open(path, 'r') as stream:
        data = yaml.load(stream, Loader=yaml.BaseLoader)
    return data 
Example #16
Source File: full_pipeline_stream.py    From Turku-neural-parser-pipeline with Apache License 2.0 5 votes vote down vote up
def read_pipelines(fname):
    absdir=os.path.dirname(os.path.abspath(fname))
    with open(fname) as f:
        pipelines=yaml.load(f, Loader=yaml.BaseLoader)
    for pipeline_name,component_list in pipelines.items():
        new_component_list=[c.format(thisdir=absdir) for c in component_list]
        pipelines[pipeline_name]=new_component_list
    return pipelines 
Example #17
Source File: cassandra_utils_test.py    From cassandra-medusa with Apache License 2.0 5 votes vote down vote up
def test_yaml_token_enforcement_with_tokens(self):
        with open('tests/resources/yaml/original/cassandra_with_tokens.yaml', 'r') as f:
            shutil.copyfile('tests/resources/yaml/original/cassandra_with_tokens.yaml',
                            'tests/resources/yaml/work/cassandra_with_tokens.yaml')
        config = configparser.ConfigParser(interpolation=None)
        config['cassandra'] = {
            'config_file': os.path.join(os.path.dirname(__file__), 'resources/yaml/work/cassandra_with_tokens.yaml'),
            'start_cmd': '/etc/init.d/cassandra start',
            'stop_cmd': '/etc/init.d/cassandra stop',
            'is_ccm': '1'
        }

        medusa_config = MedusaConfig(
            storage=None,
            monitoring=None,
            cassandra=_namedtuple_from_dict(CassandraConfig, config['cassandra']),
            ssh=None,
            checks=None,
            logging=None
        )

        cassandra = Cassandra(medusa_config.cassandra)
        tokens = ['1', '2', '3']
        cassandra.replaceTokensInCassandraYamlAndDisableBootstrap(tokens)

        with open('tests/resources/yaml/work/cassandra_with_tokens.yaml', 'r') as f:
            modified_yaml = yaml.load(f, Loader=yaml.BaseLoader)
            self.assertEqual(modified_yaml.get('num_tokens'), '3')
            self.assertEqual(modified_yaml.get('initial_token'), '1,2,3')
            self.assertEqual(modified_yaml.get('auto_bootstrap'), 'false') 
Example #18
Source File: cassandra_utils_test.py    From cassandra-medusa with Apache License 2.0 5 votes vote down vote up
def test_yaml_token_enforcement_no_tokens(self):
        with open('tests/resources/yaml/original/cassandra_no_tokens.yaml', 'r') as f:
            shutil.copyfile('tests/resources/yaml/original/cassandra_no_tokens.yaml',
                            'tests/resources/yaml/work/cassandra_no_tokens.yaml')
        config = configparser.ConfigParser(interpolation=None)
        config['cassandra'] = {
            'config_file': os.path.join(os.path.dirname(__file__), 'resources/yaml/work/cassandra_no_tokens.yaml'),
            'start_cmd': '/etc/init.d/cassandra start',
            'stop_cmd': '/etc/init.d/cassandra stop',
            'is_ccm': '1'
        }

        medusa_config = MedusaConfig(
            storage=None,
            monitoring=None,
            cassandra=_namedtuple_from_dict(CassandraConfig, config['cassandra']),
            ssh=None,
            checks=None,
            logging=None
        )

        cassandra = Cassandra(medusa_config.cassandra)
        tokens = ['1', '2', '3']
        cassandra.replaceTokensInCassandraYamlAndDisableBootstrap(tokens)

        with open('tests/resources/yaml/work/cassandra_no_tokens.yaml', 'r') as f:
            modified_yaml = yaml.load(f, Loader=yaml.BaseLoader)
            self.assertEqual(modified_yaml.get('num_tokens'), '3')
            self.assertEqual(modified_yaml.get('initial_token'), '1,2,3')
            self.assertEqual(modified_yaml.get('auto_bootstrap'), 'false') 
Example #19
Source File: cassandra_utils.py    From cassandra-medusa with Apache License 2.0 5 votes vote down vote up
def __init__(self, cassandra_config=None):
        config_file = pathlib.Path(cassandra_config or self.DEFAULT_CASSANDRA_CONFIG)
        if not config_file.is_file():
            raise RuntimeError('{} is not a file'.format(config_file))
        self._config = yaml.load(config_file.open(), Loader=yaml.BaseLoader) 
Example #20
Source File: utils.py    From attacut with MIT License 5 votes vote down vote up
def load_training_params(path: str) -> ModelParams:
    with open("%s/params.yml" % path, "r") as f:
        params = yaml.load(f, Loader=yaml.BaseLoader)
        return ModelParams(**params) 
Example #21
Source File: base.py    From DenseMatchingBenchmark with MIT License 5 votes vote down vote up
def annLoader(self):
        data_list = []
        with open(file=self.annFile, mode='r') as fp:
            data_list.extend(yaml.load(fp, Loader=yaml.BaseLoader))
        return data_list 
Example #22
Source File: catalog.py    From ideascube with GNU Affero General Public License v3.0 5 votes vote down vote up
def install(self, download_path, install_dir):
        super().install(download_path, install_dir)
        print('Adding medias to mediacenter database.')
        root = self.get_root_dir(install_dir)
        manifestfile = Path(root, 'manifest.yml')

        try:
            with manifestfile.open('r') as m:
                manifest = yaml.load(m.read(), Loader=BaseYAMLLoader)

        except FileNotFoundError:
            raise InvalidPackageContent('Missing manifest file in {}'.format(
                self.id))

        os.makedirs(settings.MEDIA_ROOT, exist_ok=True)
        catalog_path = os.path.join(settings.MEDIA_ROOT, "catalog")
        try:
            os.symlink(install_dir, catalog_path)
        except FileExistsError:
            if not os.path.islink(catalog_path):
                printerr("Cannot install package {}. {} must not exist "
                         "or being a symlink.".format(self.id, catalog_path))
                return

        pseudo_install_dir = os.path.join(catalog_path, self.id)
        for media in manifest['medias']:
            try:
                self._install_media(media, pseudo_install_dir)
            except Exception as e:
                # This can lead to installed package with uninstall media.
                # We sould handle this somehow.
                printerr("Cannot install media {} from package {} : {}".format(
                    media['title'], self.id, e))
                continue 
Example #23
Source File: catalog.py    From ideascube with GNU Affero General Public License v3.0 5 votes vote down vote up
def load_from_yml_file(path):
    with open(path, 'r', encoding='utf-8') as f:
        return yaml.load(f.read(), Loader=BaseYAMLLoader) 
Example #24
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 #25
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 #26
Source File: tools.py    From Chai with GNU General Public License v3.0 5 votes vote down vote up
def loadFromPackage(path, withNumbers=True):
    """
    功能:从模块包中加载 YAML 数据库
    输入:路径 path
    输出:yaml 解析器加载后的数据
    """
    return yaml.load(pkgutil.get_data(__package__, path).decode(), Loader=yaml.SafeLoader if withNumbers else yaml.BaseLoader) 
Example #27
Source File: tools.py    From Chai with GNU General Public License v3.0 5 votes vote down vote up
def load(path, withNumbers=True):
    """
    功能:从当前工作目录中加载 YAML 数据库
    输入:路径 path
    输出:yaml 解析器加载后的数据
    """
    return yaml.load(open(path, encoding='utf-8'), Loader=yaml.SafeLoader if withNumbers else yaml.BaseLoader) 
Example #28
Source File: tools.py    From Chai with GNU General Public License v3.0 5 votes vote down vote up
def loadFromPackage(path, withNumbers=True):
    """
    功能:从模块包中加载 YAML 数据库
    输入:路径 path
    输出:yaml 解析器加载后的数据
    """
    return yaml.load(pkgutil.get_data(__package__, path).decode(), Loader=yaml.SafeLoader if withNumbers else yaml.BaseLoader) 
Example #29
Source File: tools.py    From Chai with GNU General Public License v3.0 5 votes vote down vote up
def load(path, withNumbers=True):
    """
    功能:从当前工作目录中加载 YAML 数据库
    输入:路径 path
    输出:yaml 解析器加载后的数据
    """
    return yaml.load(open(path, encoding='utf-8'), Loader=yaml.SafeLoader if withNumbers else yaml.BaseLoader)