Exemplo n.º 1
0
    def process_slot_usages(self, cls: ClassDefinition) -> None:
        """
        Connect any slot usage items

        :param cls: class to process
        :return: usage item
        """
        for slotname, slot_usage in cls.slot_usage.items():
            # Construct a new slot
            # Follow the ancestry of the class to get the most proximal parent
            parent_slot = self.slot_definition_for(slotname, cls)
            if not parent_slot and slotname in self.schema.slots:
                parent_slot = self.schema.slots[slotname]

            # If parent slot is still not defined, it means that we introduced a NEW slot in the slot usages
            if not parent_slot:
                self.logger.warning(
                    f'class "{cls.name}" slot "{slotname}" does not reference an existing slot.  '
                    f'New slot was created.')
                child_name = slotname
                slot_alias = None
            else:
                child_name = slot_usage_name(slotname, cls)
                slot_alias = slotname
            new_slot = SlotDefinition(name=child_name,
                                      alias=slot_alias,
                                      domain=cls.name,
                                      is_usage_slot=Bool(True),
                                      owner=cls.name)
            self.schema.slots[child_name] = new_slot
            merge_slots(new_slot, slot_usage)

            # Copy the parent definition.  If there is no parent definition, the slot is being defined
            # locally as a slot_usage
            if parent_slot is not None:
                new_slot.is_a = parent_slot.name
                merge_slots(new_slot, parent_slot)
                # This situation occurs when we are doing chained overrides.  Kludgy, but it works...
                if parent_slot.name in cls.slots:
                    if child_name in cls.slots:
                        del cls.slots[cls.slots.index(child_name)]
                    cls.slots[cls.slots.index(parent_slot.name)] = child_name
Exemplo n.º 2
0
 def test_bool(self):
     self.assertTrue(Bool(True))
     self.assertTrue(Bool("True"))
     self.assertTrue(Bool("true"))
     self.assertTrue(Bool(1))
     self.assertTrue(Bool("1"))
     self.assertTrue(Bool(Bool(True)))
     self.assertFalse(Bool(False))
     self.assertFalse(Bool("False"))
     self.assertFalse(Bool("false"))
     self.assertFalse(Bool(0))
     self.assertFalse(Bool("0"))
     self.assertFalse(Bool(Bool(0)))
     # Strict mode
     with self.assertRaises(ValueError):
         x = Bool(17)
     with self.assertRaises(ValueError):
         x = Bool("a")
     lax()
     x = Bool(17)
     self.assertFalse(Bool.is_valid(17))
     x = Bool("a")
     self.assertFalse(Bool.is_valid("a"))
     self.assertTrue(Bool.is_valid(True))
     self.assertTrue(Bool.is_valid(Bool(True)))
Exemplo n.º 3
0
    def process_slot_usages(self, cls: ClassDefinition) -> None:
        """
        Connect any slot usage items

        :param cls: class to process
        :return: usage item
        """
        for slotname, slot_usage in cls.slot_usage.items():
            if slot_usage.alias:
                self.raise_value_error(f'Class: "{cls.name}" - alias not permitted in slot_usage slot:'
                                       f' {slot_usage.alias}')
            # Construct a new slot
            # If we've already assigned a parent, use it

            parent_slot = self.schema.slots.get(slot_usage.is_a)
            # Follow the ancestry of the class to get the most proximal parent
            if not parent_slot:
                parent_slot = self.slot_definition_for(slotname, cls)
            if not parent_slot and slotname in self.schema.slots:
                parent_slot = self.schema.slots[slotname]

            if not parent_slot:
                # This test is here because it is really easy to break things in the slot merge utilities.  It should
                # stay
                self.logger.error(f'class "{cls.name}" slot "{slotname}" -- error occurred. This should not happen')
            else:
                child_name = slot_usage_name(slotname, cls)
                slot_alias = parent_slot.alias if parent_slot.alias else slotname
            new_slot = SlotDefinition(name=child_name, alias=slot_alias, domain=cls.name, is_usage_slot=Bool(True),
                                      usage_slot_name=slotname, owner=cls.name, domain_of=[cls.name],
                                      imported_from=cls.imported_from)
            self.schema.slots[child_name] = new_slot
            merge_slots(new_slot, slot_usage, inheriting=False, skip=['name', 'alias', 'domain', 'is_usage_slot',
                                                                      'usage_slot_name', 'owner', 'domain_of'])

            # Copy the parent definition.  If there is no parent definition, the slot is being defined
            # locally as a slot_usage
            if parent_slot is not None:
                new_slot.is_a = parent_slot.name
                merge_slots(new_slot, parent_slot)
                # This situation occurs when we are doing chained overrides.  Kludgy, but it works...
                if parent_slot.name in cls.slots:
                    if child_name in cls.slots:
                        del cls.slots[cls.slots.index(child_name)]
                    cls.slots[cls.slots.index(parent_slot.name)] = child_name
                elif child_name not in cls.slots:
                    cls.slots.append(child_name)
            elif not new_slot.range:
                new_slot.range = self.schema.default_range
Exemplo n.º 4
0
 def test_bool(self):
     self.assertTrue(Bool(True))
     self.assertTrue(Bool("True"))
     self.assertTrue(Bool("true"))
     self.assertTrue(Bool(1))
     self.assertTrue(Bool("1"))
     self.assertTrue(Bool(Bool(True)))
     self.assertFalse(Bool(False))
     self.assertFalse(Bool("False"))
     self.assertFalse(Bool("false"))
     self.assertFalse(Bool(0))
     self.assertFalse(Bool("0"))
     self.assertFalse(Bool(Bool(0)))
     with self.assertRaises(ValueError):
         x = Bool(17)
     with self.assertRaises(ValueError):
         x = Bool("a")