def _CreateEntityInstances(yaml_filename): parsed_yaml = instance_parser.parse_yaml( path.join(_INSTANCES_PATH, yaml_filename)) entities = {} for entity_name, entity_yaml in parsed_yaml.items(): entities[entity_name] = entity_instance.EntityInstance(entity_yaml) return entities
def testValidateMultipleCompliantTranslationWithNamespaceOther(self): parsed = instance_parser.parse_yaml( os.path.join(_TESTCASE_PATH, 'GOOD', 'good_translation.yaml')) parsed = dict(parsed) entity_name_lighting = list(parsed.keys())[0] entity_lighting = dict(parsed[entity_name_lighting]) instance_lighting = entity_instance.EntityInstance( entity_lighting, self.universe, parsed.keys()) if not instance_lighting.IsValidEntityInstance(): self.fail('exception incorrectly raised')
def testValidateStates(self): parsed = instance_parser.parse_yaml( os.path.join(_TESTCASE_PATH, 'GOOD', 'good_translation_states.yaml')) parsed = dict(parsed) for raw_entity in list(parsed.keys()): entity_parsed = dict(parsed[raw_entity]) entity = entity_instance.EntityInstance(entity_parsed, self.universe, parsed.keys()) if not entity.IsValidEntityInstance(): self.fail('exception incorrectly raised')
def testValidateBadEntityNamespace(self): parsed = instance_parser.parse_yaml( os.path.join(_TESTCASE_PATH, 'BAD', 'bad_building_type_namespace.yaml')) parsed = dict(parsed) entity_name = list(parsed.keys())[0] entity = dict(parsed[entity_name]) instance = entity_instance.EntityInstance(entity, self.universe, parsed.keys()) if instance.IsValidEntityInstance(): self.fail('exception not raised')
def testValidateRejectsUseOfAbstractType(self): parsed = instance_parser.parse_yaml( os.path.join(_TESTCASE_PATH, 'BAD', 'bad_abstract_type.yaml')) parsed = dict(parsed) entity_name = list(parsed.keys())[0] entity = dict(parsed[entity_name]) instance = entity_instance.EntityInstance(entity, self.universe, parsed.keys()) if instance.IsValidEntityInstance(): self.fail('exception not raised')
def testValidateGoodExample(self): parsed = instance_parser.parse_yaml( os.path.join(_TESTCASE_PATH, 'GOOD', 'good_building_type.yaml')) parsed = dict(parsed) entity_name = list(parsed.keys())[0] entity = dict(parsed[entity_name]) instance = entity_instance.EntityInstance(entity, self.universe, parsed.keys()) if not instance.IsValidEntityInstance(): self.fail('exception incorrectly raised')
def testValidateTranslationUnitsAndStates(self): parsed = instance_parser.parse_yaml( os.path.join(_TESTCASE_PATH, 'GOOD', 'good_translation_units_and_states.yaml')) parsed = dict(parsed) entity_name = list(parsed.keys())[0] entity = dict(parsed[entity_name]) instance = entity_instance.EntityInstance(entity, self.universe, parsed.keys()) if not instance.IsValidEntityInstance(): self.fail('exception incorrectly raised')
def testValidateMultipleCompliantTranslationWithExtraField(self): parsed = instance_parser.parse_yaml( os.path.join(_TESTCASE_PATH, 'BAD', 'bad_translation_with_extra_field.yaml')) parsed = dict(parsed) entity_name = list(parsed.keys())[0] entity = dict(parsed[entity_name]) instance = entity_instance.EntityInstance(entity, self.universe, parsed.keys()) if instance.IsValidEntityInstance(): self.fail('exception not raised')
def testValidateMultipleCompliantTranslationWithFields(self): parsed = instance_parser.parse_yaml( os.path.join(_TESTCASE_PATH, 'GOOD', 'good_building_translation_fields.yaml')) parsed = dict(parsed) entity_name = list(parsed.keys())[0] entity = dict(parsed[entity_name]) instance = entity_instance.EntityInstance(entity, self.universe, parsed.keys()) if not instance.IsValidEntityInstance(): self.fail('exception incorrectly raised')
def testValidateBadTranslationUnitValues(self): parsed = instance_parser.parse_yaml( os.path.join(_TESTCASE_PATH, 'BAD', 'bad_translation_unit_values.yaml')) parsed = dict(parsed) entity_name = list(parsed.keys())[0] entity = dict(parsed[entity_name]) instance = entity_instance.EntityInstance(entity, self.universe, parsed.keys()) if instance.IsValidEntityInstance(): self.fail('exception not raised')
def testValidateBadEntityTypeFormat(self): parsed = instance_parser.parse_yaml( os.path.join(_TESTCASE_PATH, 'BAD', 'bad_building_type.yaml')) parsed = dict(parsed) entity_name = list(parsed.keys())[0] entity = dict(parsed[entity_name]) try: entity_instance.EntityInstance(entity, self.universe, parsed.keys()) except TypeError as e: self.assertEqual(type(e), TypeError) else: self.fail('{0} was not raised'.format(TypeError))
def testValidateBadLinkWrongField(self): parsed = instance_parser.parse_yaml( os.path.join(_TESTCASE_PATH, 'BAD', 'bad_links_wrong_link.yaml')) entity_instances = {} parsed = dict(parsed) for raw_entity in list(parsed.keys()): entity_parsed = dict(parsed[raw_entity]) entity = entity_instance.EntityInstance(entity_parsed, self.universe, parsed.keys()) entity_instances[raw_entity] = entity if entity_instances.get('ENTITY-NAME')\ .IsValidEntityInstance(entity_instances): self.fail('exception not raised')
def testValidateGoodLinkEntityName(self): parsed = instance_parser.parse_yaml( os.path.join(_TESTCASE_PATH, 'GOOD', 'good_links.yaml')) entity_instances = {} parsed = dict(parsed) for raw_entity in list(parsed.keys()): entity_parsed = dict(parsed[raw_entity]) entity = entity_instance.EntityInstance(entity_parsed, self.universe, parsed.keys()) entity_instances[raw_entity] = entity for _, instance in entity_instances.items(): if not instance.IsValidEntityInstance(entity_instances): self.fail('exception incorrectly raised')
def testGoodConnections(self): parsed = instance_parser.parse_yaml( os.path.join(_TESTCASE_PATH, 'GOOD', 'good_building_connections.yaml')) parsed = dict(parsed) entity_name = list(parsed.keys())[0] entity = dict(parsed[entity_name]) if 'connections' not in entity.keys(): self.fail('entity does not have connections when expected') if self.universe.connections_universe is None: self.fail('universe does not valid connections universe') instance = entity_instance.EntityInstance(entity, self.universe, parsed.keys()) if not instance.IsValidEntityInstance(): self.fail('exception incorrectly raised')
def deserialize(yaml_files): """Parses a yaml configuration file and deserialize it. Args: yaml_files: list of building configuration file. Returns: entity_instances: all the deserialized instances. """ parsed_yaml = {} for yaml_file in yaml_files: print('Parsing file: {0}, please wait ...'.format(yaml_file)) parsed_yaml.update(instance_parser.parse_yaml(yaml_file)) print('Syntax checks passed for file: {0}'.format(yaml_file)) entity_instances = {} for entity_name, entity_yaml in parsed_yaml.items(): entity_instances[entity_name] = entity_instance.EntityInstance( entity_yaml) return entity_instances
def deserialize(yaml_file, universe): """Parses a yaml configuration file and deserialize it. Args: yaml_file: the building configuration file. universe: the generated ontology universe. :returns entity_instances: all the deserialized instances. parsed: the raw parsed entities """ raw_parse = instance_parser.parse_yaml(yaml_file) print('Passed syntax checks!') print('Serializing Passed syntax checks!') parsed = dict(raw_parse) entity_instances = {} entity_names = list(parsed.keys()) # first build all the entity instances for entity_name in entity_names: entity = dict(parsed[entity_name]) instance = entity_instance.EntityInstance(entity, universe, set(entity_names)) entity_instances[entity_name] = instance return entity_instances, parsed
def testInstanceValidatorDetectImproperTabbing(self): with self.assertRaises(SystemExit): parse = instance_parser.parse_yaml( os.path.join(_TESTCASE_PATH, 'BAD', 'bad_tabbing.yaml')) self.assertIsNone(parse)
def testInstanceValidatorDetectMissingColon(self): with self.assertRaises(SystemExit): parse = instance_parser.parse_yaml( os.path.join(_TESTCASE_PATH, 'BAD', 'bad_missing_colon.yaml')) self.assertIsNone(parse)
def testInstanceValidatorDetectDuplicateKeys(self): with self.assertRaises(SystemExit): parse = instance_parser.parse_yaml( os.path.join(_TESTCASE_PATH, 'BAD', 'bad_duplicate_keys.yaml')) self.assertIsNone(parse)
help='Filepath to YAML building configuration', metavar='FILE') parser.add_argument('-m', '--modified-ontology-types', dest='modified_types_filepath', required=False, help='Filepath to modified type filepaths', metavar='MODIFIED_TYPE_FILEPATHS') arg = parser.parse_args() # SYNTAX VALIDATION print('\nValidator starting ...\n') filename = arg.filename # prints for syntax errors and exits gracefully raw_parse = instance_parser.parse_yaml(filename) print('Passed syntax checks!') modified_types_filepath = arg.modified_types_filepath print('Generating universe ...') universe = generate_universe.BuildUniverse(modified_types_filepath) if universe is None: print('\nError generating universe') sys.exit(0) print('Universe generated successfully') parsed = dict(raw_parse)
with open( os.path.join(_TELEMETRY_PATH, 'telemetry_invalid_number_boolean.json')) as file: _MESSAGE_INVALID_NUMBER_BOOLEAN = FakeMessage(_MESSAGE_ATTRIBUTES_1, file.read()) with open(os.path.join(_TELEMETRY_PATH, 'telemetry_multiple_errors.json')) as file: _MESSAGE_MULTIPLE_ERRORS = FakeMessage(_MESSAGE_ATTRIBUTES_1, file.read()) # TODO: fix inconsistency between telemetry parser expecting a string, # but instance parser expecting a file # A single test entity with numeric fields. _ENTITIES_1 = dict( instance_parser.parse_yaml( os.path.join(_INSTANCES_PATH, 'good_translation_units.yaml'))) _ENTITY_NAME_1 = 'CHWS_WDT-17' # A single test entity with multistate fields. _ENTITIES_2 = dict( instance_parser.parse_yaml( os.path.join(_INSTANCES_PATH, 'good_translation_states.yaml'))) _ENTITY_NAME_2 = 'DMP_EDM-17' # A set of two test entities with identical points. _ENTITIES_3_4 = dict( instance_parser.parse_yaml( os.path.join(_INSTANCES_PATH, 'good_translation_identical.yaml'))) _ENTITY_NAME_3 = 'SDC_EXT-17' _ENTITY_NAME_4 = 'SDC_EXT-18'
def testInstanceValidatorParseProperConnections(self): parse = instance_parser.parse_yaml( os.path.join(_TESTCASE_PATH, 'GOOD', 'good_building_connections.yaml')) self.assertIsNotNone(parse)
def testInstanceValidatorDetectImproperTranslationCompliance(self): with self.assertRaises(SystemExit): parse = instance_parser.parse_yaml( path.join(_TESTCASE_PATH, 'BAD', 'bad_translation_compliant.yaml')) self.assertIsNone(parse)
def testInstanceValidatorDetectImproperUnitsKeys(self): with self.assertRaises(SystemExit): parse = instance_parser.parse_yaml( os.path.join(_TESTCASE_PATH, 'BAD', 'bad_translation_units_format.yaml')) self.assertIsNone(parse)
def testInstanceValidatorParseProperFormat(self): parse = instance_parser.parse_yaml( path.join(_TESTCASE_PATH, 'GOOD', 'good_building_type.yaml')) self.assertIsNotNone(parse)