def ArtifactsFromYaml(self, yaml_content): """Get a list of Artifacts from yaml.""" raw_list = yaml.ParseMany(yaml_content) # TODO(hanuszczak): I am very sceptical about that "doing the right thing" # below. What are the real use cases? # Try to do the right thing with json/yaml formatted as a list. if (isinstance(raw_list, list) and len(raw_list) == 1 and isinstance(raw_list[0], list)): raw_list = raw_list[0] # Convert json into artifact and validate. valid_artifacts = [] for artifact_dict in raw_list: # In this case we are feeding parameters directly from potentially # untrusted yaml/json to our RDFValue class. However, safe_load ensures # these are all primitive types as long as there is no other # deserialization involved, and we are passing these into protobuf # primitive types. try: artifact_value = rdf_artifacts.Artifact(**artifact_dict) valid_artifacts.append(artifact_value) except (TypeError, AttributeError, type_info.TypeValueError) as e: name = artifact_dict.get("name") raise rdf_artifacts.ArtifactDefinitionError( name, "invalid definition", cause=e) return valid_artifacts
def ArtifactsFromYaml(self, yaml_content): """Get a list of Artifacts from yaml.""" raw_list = yaml.ParseMany(yaml_content) # TODO(hanuszczak): I am very sceptical about that "doing the right thing" # below. What are the real use cases? # Try to do the right thing with json/yaml formatted as a list. if (isinstance(raw_list, list) and len(raw_list) == 1 and isinstance(raw_list[0], list)): raw_list = raw_list[0] # Convert json into artifact and validate. valid_artifacts = [] for artifact_dict in raw_list: # Old artifacts might still use deprecated fields, so we have to ignore # such. Here, we simply delete keys from the dictionary as otherwise the # RDF value constructor would raise on unknown fields. for field in DEPRECATED_ARTIFACT_FIELDS: artifact_dict.pop(field, None) # In this case we are feeding parameters directly from potentially # untrusted yaml/json to our RDFValue class. However, safe_load ensures # these are all primitive types as long as there is no other # deserialization involved, and we are passing these into protobuf # primitive types. try: artifact_value = rdf_artifacts.Artifact(**artifact_dict) valid_artifacts.append(artifact_value) except (TypeError, AttributeError, type_info.TypeValueError) as e: name = artifact_dict.get("name") raise rdf_artifacts.ArtifactDefinitionError( name, "invalid definition", cause=e) return valid_artifacts
def CreateAuthorizations(self, yaml_data, auth_class): try: raw_list = yaml.ParseMany(yaml_data) except (ValueError, pyyaml.YAMLError) as e: raise InvalidAuthorization("Invalid YAML: %s" % e) logging.debug("Adding %s authorizations", len(raw_list)) for auth in raw_list: auth_object = auth_class(**auth) if auth_object.key in self.auth_objects: raise InvalidAuthorization("Duplicate authorizations for %s" % auth_object.key) self.auth_objects[auth_object.key] = auth_object
def testUnicode(self): parsed = yaml.ParseMany(""" gąszcz: żuk --- gęstwina: chrabąszcz """) expected = [ { "gąszcz": "żuk" }, { "gęstwina": "chrabąszcz" }, ] self.assertEqual(parsed, expected)
def ParseYAMLAuthorizationsList(yaml_data): """Parses YAML data into a list of APIAuthorization objects.""" try: raw_list = yaml.ParseMany(yaml_data) except (ValueError, pyyaml.YAMLError) as e: raise InvalidAPIAuthorization("Invalid YAML: %s" % e) result = [] for auth_src in raw_list: auth = APIAuthorization() auth.router_cls = _GetRouterClass(auth_src["router"]) auth.users = auth_src.get("users", []) auth.groups = auth_src.get("groups", []) auth.router_params = auth_src.get("router_params", {}) result.append(auth) return result
def testMultipleDicts(self): parsed = yaml.ParseMany(""" foo: 42 bar: 108 --- quux: norf thud: blargh """) expected = [ { "foo": 42, "bar": 108, }, { "quux": "norf", "thud": "blargh", }, ] self.assertEqual(parsed, expected)