Python yaml.safe_load() Examples
The following are 30
code examples of yaml.safe_load().
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 |
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 query-exporter with GNU General Public License v3.0 | 8 votes |
def _validate_config(config: Dict[str, Any]): schema_path = PACKAGE.get_resource_filename( # type: ignore None, "query_exporter/schemas/config.yaml" ) with open(schema_path) as fd: schema = yaml.safe_load(fd) try: jsonschema.validate(config, schema) except jsonschema.ValidationError as e: path = "/".join(str(item) for item in e.absolute_path) raise ConfigError(f"Invalid config at {path}: {e.message}")
Example #3
Source File: test_schema_validation.py From drydock with Apache License 2.0 | 7 votes |
def _test_validate(self, schema, expect_failure, input_files, input): """validates input yaml against schema. :param schema: schema yaml file :param expect_failure: should the validation pass or fail. :param input_files: pytest fixture used to access the test input files :param input: test input yaml doc filename""" schema_dir = pkg_resources.resource_filename('drydock_provisioner', 'schemas') schema_filename = os.path.join(schema_dir, schema) schema_file = open(schema_filename, 'r') schema = yaml.safe_load(schema_file) input_file = input_files.join(input) instance_file = open(str(input_file), 'r') instance = yaml.safe_load(instance_file) if expect_failure: with pytest.raises(ValidationError): jsonschema.validate(instance['spec'], schema['data']) else: jsonschema.validate(instance['spec'], schema['data'])
Example #4
Source File: config_generator.py From rift-python with Apache License 2.0 | 6 votes |
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 #5
Source File: dataset.py From PolarSeg with BSD 3-Clause "New" or "Revised" License | 6 votes |
def __init__(self, data_path, imageset = 'train', return_ref = False): self.return_ref = return_ref with open("semantic-kitti.yaml", 'r') as stream: semkittiyaml = yaml.safe_load(stream) self.learning_map = semkittiyaml['learning_map'] self.imageset = imageset if imageset == 'train': split = semkittiyaml['split']['train'] elif imageset == 'val': split = semkittiyaml['split']['valid'] elif imageset == 'test': split = semkittiyaml['split']['test'] else: raise Exception('Split must be train/val/test') self.im_idx = [] for i_folder in split: self.im_idx += absoluteFilePaths('/'.join([data_path,str(i_folder).zfill(2),'velodyne']))
Example #6
Source File: cookiejar_client.py From sawtooth-cookiejar with Apache License 2.0 | 6 votes |
def _wait_for_status(self, batch_id, wait, result): '''Wait until transaction status is not PENDING (COMMITTED or error). 'wait' is time to wait for status, in seconds. ''' if wait and wait > 0: waited = 0 start_time = time.time() while waited < wait: result = self._send_to_rest_api("batch_statuses?id={}&wait={}" .format(batch_id, wait)) status = yaml.safe_load(result)['data'][0]['status'] waited = time.time() - start_time if status != 'PENDING': return result return "Transaction timed out after waiting {} seconds." \ .format(wait) else: return result
Example #7
Source File: bootaction.py From drydock with Apache License 2.0 | 6 votes |
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 #8
Source File: collate.py From escpos-printer-db with Creative Commons Attribution 4.0 International | 6 votes |
def load_profiles(): """ Load in all the profiles from the profile folder (file-per-profile) """ profiles_raw = {} profiles_dir = os.path.dirname(__file__) + "/../data/profile/" for profile_fn in os.listdir(profiles_dir): if not profile_fn[-3:] == 'yml': continue profile_dict = yaml.safe_load(open(profiles_dir + profile_fn).read()) # One item per file assert len(profile_dict) == 1, "{}: expected one entry, got {}" \ .format(profile_fn, len(profile_dict)) # Item must match filename profile_name, profile_val = profile_dict.popitem() assert profile_name + ".yml" == profile_fn, \ "{}: Expected to find profile named the same as file, got {}" \ .format(profile_fn, profile_name) profiles_raw[profile_name] = profile_val return profiles_raw
Example #9
Source File: validators.py From schema.data.gouv.fr with MIT License | 6 votes |
def validate(self): super(XsdSchemaValidator, self).validate() self.check_file_exists("schemas.yml") try: with open(self.filepath("schemas.yml"), "r") as f: config = yaml.safe_load(f) self.schemas_config = config["schemas"] self.title = config["title"] self.description = config["description"] self.homepage = config["homepage"] for schema in config["schemas"]: self.check_schema(schema["path"], schema["title"]) except Exception as e: message = "`schemas.yml` has not the required format: " + repr(e) raise exceptions.InvalidSchemaException(self.repo, message)
Example #10
Source File: util.py From ftw with Apache License 2.0 | 6 votes |
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 #11
Source File: common_test.py From loaner with Apache License 2.0 | 6 votes |
def test_write(self): expected_dict = { 'project_id': 'test_project', 'client_id': 'test_client_id', 'client_secret': 'test_client_secret', 'bucket': 'test_project.appspot.com', } self.fs.CreateFile('/this/config/file.yaml', contents=_EMPTY_CONFIG) test_config = common.ProjectConfig( 'asdf', 'test_project', 'test_client_id', 'test_client_secret', None, '/this/config/file.yaml', ) test_config.write() with open('/this/config/file.yaml') as config_file: config = yaml.safe_load(config_file) self.assertEqual(config['asdf'], expected_dict)
Example #12
Source File: device.py From Paradrop with Apache License 2.0 | 6 votes |
def reconfigure(ctx): """ Reconfigure the chute without rebuilding. """ url = ctx.obj['chute_url'] + "/config" if not os.path.exists("paradrop.yaml"): raise Exception("No paradrop.yaml file found in working directory.") with open("paradrop.yaml", "r") as source: data = yaml.safe_load(source) config = data.get('config', {}) res = router_request("PUT", url, json=config) data = res.json() ctx.invoke(watch, change_id=data['change_id'])
Example #13
Source File: store.py From Paradrop with Apache License 2.0 | 6 votes |
def create_version(ctx): """ Push a new version of the chute to the store. """ if not os.path.exists("paradrop.yaml"): raise Exception("No paradrop.yaml file found in working directory.") with open('paradrop.yaml', 'r') as source: chute = yaml.safe_load(source) name = chute_find_field(chute, 'name') source = chute_find_field(chute, 'source') config = chute.get('config', {}) chute_resolve_source(source, config) client = ControllerClient() result = client.find_chute(name) if result is None: raise Exception("Could not find ID for chute {} - is it registered?".format(name)) result = client.create_version(name, config) click.echo(util.format_result(result)) return result
Example #14
Source File: buildinfo_test.py From glazier with Apache License 2.0 | 6 votes |
def testSerialize(self): mock_b = mock.Mock(spec_set=self.buildinfo) mock_b._chooser_responses = { 'USER_choice_one': 'value1', 'USER_choice_two': 'value2' } mock_b._timers.GetAll.return_value = { 'timer_1': datetime.datetime.utcnow() } mock_b.Serialize = buildinfo.BuildInfo.Serialize.__get__(mock_b) mock_b.Serialize('/build_info.yaml') parsed = yaml.safe_load(buildinfo.open('/build_info.yaml')) self.assertIn('branch', parsed['BUILD']) self.assertIn('Model', parsed['BUILD']) self.assertIn('SerialNumber', parsed['BUILD']) self.assertIn('USER_choice_two', parsed['BUILD']) self.assertIn('TIMER_timer_1', parsed['BUILD']) self.assertEqual(parsed['BUILD']['USER_choice_two'], 'value2')
Example #15
Source File: buildinfo_test.py From glazier with Apache License 2.0 | 6 votes |
def testReleasePath(self, comp_os, read): read.return_value = yaml.safe_load(_VERSION_INFO) comp_os.return_value = 'windows-7-stable' expected = 'https://glazier-server.example.com/stable/' self.assertEqual(self.buildinfo.ReleasePath(), expected) self.buildinfo.ComputerOs.cache_clear() comp_os.return_value = 'windows-10-unstable' expected = 'https://glazier-server.example.com/unstable/' self.assertEqual(self.buildinfo.ReleasePath(), expected) self.buildinfo.ComputerOs.cache_clear() # no os comp_os.return_value = None self.assertRaises(buildinfo.Error, self.buildinfo.ReleasePath) self.buildinfo.ComputerOs.cache_clear() # invalid os comp_os.return_value = 'invalid-os-string' self.assertRaises(buildinfo.Error, self.buildinfo.ReleasePath)
Example #16
Source File: files.py From glazier with Apache License 2.0 | 6 votes |
def _YamlReader(path: Text) -> Text: """Read a configuration file and return the contents. Can be overloaded to read configs from different sources. Args: path: The config file name (eg build.yaml). Returns: The parsed content of the yaml file. """ try: with open(path, 'r') as yaml_file: yaml_config = yaml.safe_load(yaml_file) except IOError as e: raise Error('Could not read yaml file %s: %s' % (path, str(e))) return yaml_config
Example #17
Source File: identify.py From operator-courier with Apache License 2.0 | 6 votes |
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 #18
Source File: flatten.py From operator-courier with Apache License 2.0 | 6 votes |
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 #19
Source File: general_inputter.py From knowledge-net with MIT License | 5 votes |
def initialize(self, metadata, asset_dir=None, asset_prefix=""): config_file = metadata['config_file'] # read config file self.input_features = [] self.has = defaultdict(bool) with open(config_file, "r") as f: config = yaml.safe_load(f) for fi in config["features"]: f = Feature(fi[0], fi[1], fi[2]) self.has[f.where] = True self.input_features.append(f) print(self.input_features)
Example #20
Source File: common.py From loaner with Apache License 2.0 | 5 votes |
def from_yaml(cls, key, config_file_path): """Load project config data from the config yaml file. Args: key: str, the top-level key in the config file for this project. config_file_path: str, the name or path to the config file. Returns: An instance of ProjectConfig with the project specific configurations. Raises: ConfigError: when the configuration is missing required values. """ logging.debug('Loading the configuration from the provided config file.') path = _get_config_file_path(config_file_path) with open(path) as config_file: config = yaml.safe_load(config_file)[key] for field in REQUIRED_FIELDS: if config[field] is None: raise ConfigError( 'the field {field!r} is required and is not configured, please ' 'ensure there is a value set for all of the following fields ' '{fields}'.format(field=field, fields=REQUIRED_FIELDS)) return cls( key=key, project=config['project_id'], client_id=config['client_id'], client_secret=config['client_secret'], bucket=config['bucket'], path=path, )
Example #21
Source File: utils.py From ffplayout-engine with GNU General Public License v3.0 | 5 votes |
def read_config(path): with open(path, 'r') as config_file: return yaml.safe_load(config_file)
Example #22
Source File: test_config.py From esmlab with Apache License 2.0 | 5 votes |
def test_ensure_file(tmpdir): a = {'x': 1, 'y': {'a': 1}} b = {'x': 123} source = os.path.join(str(tmpdir), 'source.yaml') dest = os.path.join(str(tmpdir), 'dest') destination = os.path.join(dest, 'source.yaml') with open(source, 'w') as f: yaml.dump(a, f) ensure_file(source=source, destination=dest, comment=False) with open(destination) as f: result = yaml.safe_load(f) assert result == a # don't overwrite old config files with open(source, 'w') as f: yaml.dump(b, f) ensure_file(source=source, destination=dest, comment=False) with open(destination) as f: result = yaml.safe_load(f) assert result == a os.remove(destination) # Write again, now with comments ensure_file(source=source, destination=dest, comment=True) with open(destination) as f: text = f.read() assert '123' in text with open(destination) as f: result = yaml.safe_load(f) assert not result
Example #23
Source File: buildinfo_test.py From glazier with Apache License 2.0 | 5 votes |
def testSupportedModels(self, unused_rel_path, fread): fread.return_value = yaml.safe_load(_RELEASE_INFO) results = self.buildinfo.SupportedModels() self.assertIn('tier1', results) self.assertIn('tier2', results) for model in results['tier1'] + results['tier2']: self.assertEqual(type(model), str) self.assertIn('windows tier 1 device', results['tier1']) self.assertIn('windows tier 2 device', results['tier2'])
Example #24
Source File: common.py From loaner with Apache License 2.0 | 5 votes |
def write(self): """Writes the project config for the provided key to disk.""" with open(self.path) as config_file: configs = yaml.safe_load(config_file) config = configs.get(self.key) or {} config['project_id'] = self.project config['client_id'] = self.client_id config['client_secret'] = self.client_secret config['bucket'] = self.bucket configs[self.key] = config with open(self.path, 'w') as config_file: yaml.dump(configs, config_file, default_flow_style=False)
Example #25
Source File: utils.py From loaner with Apache License 2.0 | 5 votes |
def load_config_from_yaml(): """Opens the config_defaults yaml file. Returns: A dictionary of the default data from the yaml file. """ yaml_path = constants.CONFIG_DEFAULTS_PATH with open(yaml_path) as data: return yaml.safe_load(data)
Example #26
Source File: installer.py From glazier with Apache License 2.0 | 5 votes |
def Run(self): path = os.path.join(constants.SYS_CACHE, 'build_info.yaml') if os.path.exists(path): with open(path) as handle: input_config = yaml.safe_load(handle) self._WriteRegistry(input_config['BUILD']) os.remove(path) else: logging.debug('%s does not exist - skipped processing.', path)
Example #27
Source File: identifier.py From glazier with Apache License 2.0 | 5 votes |
def _check_file() -> Text: """Call set_id if image identifier is not set and in WinPE. Returns: Image identifier as a string if already set. Raises: Error: Could not locate build info file. Error: Could not determine image identifier from file. """ # First boot into host needs to grab image_id from buildinfo file. # It has not been written to registry yet. path = os.path.join(constants.SYS_CACHE, 'build_info.yaml') if os.path.exists(path): with open(path) as handle: try: input_config = yaml.safe_load(handle) image_id = input_config['BUILD']['image_id'] registry.set_value('image_id', image_id) return image_id except registry.Error as e: raise Error(str(e)) except KeyError as e: raise Error('Could not determine %s from file: %s.' % (e, path)) else: raise Error('Could not locate build info file.')
Example #28
Source File: validators.py From schema.data.gouv.fr with MIT License | 5 votes |
def consolidation_data(self, slug): with open(os.path.join(self.static_dir, self.CONSOLIDATION_FILENAME)) as f: return yaml.safe_load(f).get(slug, None)
Example #29
Source File: format.py From operator-courier with Apache License 2.0 | 5 votes |
def unformat_bundle(formattedBundle): """ Converts a push-ready bundle into a structured object by changing stringified yaml of 'customResourceDefinitions', 'clusterServiceVersions', and 'packages' into lists of objects. Undoing the format helps simplify bundle validation. :param formattedBundle: A push-ready bundle """ bundle = BuildCmd()._get_empty_bundle() if 'data' not in formattedBundle: return bundle if 'customResourceDefinitions' in formattedBundle['data']: customResourceDefinitions = yaml.safe_load( formattedBundle['data']['customResourceDefinitions']) if customResourceDefinitions: bundle['data']['customResourceDefinitions'] = customResourceDefinitions if 'clusterServiceVersions' in formattedBundle['data']: clusterServiceVersions = yaml.safe_load( formattedBundle['data']['clusterServiceVersions']) if clusterServiceVersions: bundle['data']['clusterServiceVersions'] = clusterServiceVersions if 'packages' in formattedBundle['data']: packages = yaml.safe_load(formattedBundle['data']['packages']) if packages: bundle['data']['packages'] = packages return bundle
Example #30
Source File: config.py From pywren-ibm-cloud with Apache License 2.0 | 5 votes |
def load_yaml_config(config_filename): import yaml with open(config_filename, 'r') as config_file: data = yaml.safe_load(config_file) return data