def __new__(meta, name, bases, class_dict): cls = type.__new__(meta, name, bases, class_dict) # Return early for error classes which should be unregistered duplicates # of the base class if getattr(cls, '_is_error_class', False): return cls # Determine subtype subtype = None if cls._schema_subtype_key is not None: subtype = getattr( cls, cls._schema_subtype_key, getattr(cls, '_' + cls._schema_subtype_key, None)) # Inherit new schema properties if cls._schema_inherit and (cls.__name__ != 'ComponentBase'): assert (isinstance(cls._schema_inherit, bool)) inherit_from = None for x in bases: if hasattr(x, '_schema_properties'): inherit_from = x break if inherit_from is None: # pragma: debug raise RuntimeError(("Class %s dosn't have a component " "parent class.") % cls) # Dont inherit if the base is ComponentBase (empty schema) if inherit_from.__name__ != ComponentBase: cls._schema_properties, cls._schema_required = inherit_schema( inherit_from, new_properties=cls._schema_properties, new_required=cls._schema_required, remove_keys=cls._schema_excluded_from_inherit) # Do non-registration things if not (name.endswith('Base') or (subtype is None) or cls._dont_register): # Perform class specific actions in preparation for registration cls.before_registration(cls) # assert(cls.name is not None) # Add parameter descriptions from docs for x in cls.__mro__[::-1]: args_dict = docs2args(x.__doc__) for k, v in cls._schema_properties.items(): if k in args_dict: v.setdefault('description', args_dict[k]['description']) # Determine base class global _registry_base_classes if cls._schema_base_class is None: if cls._schema_type in _registry_base_classes: cls._schema_base_class = _registry_base_classes[ cls._schema_type] else: base_comp = cls.__name__ for i, x in enumerate(cls.__mro__): if x._schema_type != cls._schema_type: break base_comp = x.__name__ else: # pragma: debug raise RuntimeError( ("Could not determine base class for %s " "from %s.") % (cls, bases)) cls._schema_base_class = base_comp # Register global _registry global _registry_defaults global _registry_class2subtype yaml_typ = cls._schema_type default_subtype = cls._schema_properties.get( cls._schema_subtype_key, {}).get('default', cls._schema_subtype_default) if yaml_typ not in _registry: _registry[yaml_typ] = OrderedDict() _registry_defaults[yaml_typ] = default_subtype _registry_base_classes[yaml_typ] = cls._schema_base_class _registry_class2subtype[yaml_typ] = {} elif default_subtype is not None: assert (_registry_defaults[yaml_typ] == default_subtype) if cls.__name__ not in _registry[yaml_typ]: _registry[yaml_typ][cls.__name__] = cls _registry_class2subtype[yaml_typ][subtype] = cls.__name__ if not (os.environ.get('YGG_RUNNING_YGGSCHEMA', 'None').lower() in ['true', '1']): cls.after_registration(cls) cls.finalize_registration(cls) return cls
def __new__(meta, name, bases, class_dict): cls = type.__new__(meta, name, bases, class_dict) # Determine subtype subtype = None if cls._schema_subtype_key is not None: subtype = getattr( cls, cls._schema_subtype_key, getattr(cls, '_' + cls._schema_subtype_key, None)) # Inherit new schema properties if cls._schema_inherit and (cls.__name__ != 'ComponentBase'): assert (isinstance(cls._schema_inherit, bool)) inherit_from = None for x in bases: if hasattr(x, '_schema_properties'): inherit_from = x break if inherit_from is None: # pragma: debug raise RuntimeError(("Class %s dosn't have a component " "parent class.") % cls) # Dont inherit if the base is ComponentBase (empty schema) if inherit_from.__name__ != ComponentBase: cls._schema_properties, cls._schema_required = inherit_schema( inherit_from, new_properties=cls._schema_properties, new_required=cls._schema_required, remove_keys=cls._schema_excluded_from_inherit) # Do non-registration things if not (name.endswith('Base') or (subtype is None) or cls._dont_register): # Perform class specific actions in preparation for registration cls.before_registration(cls) # assert(cls.name is not None) # Add parameter descriptions from docs for x in cls.__mro__[::-1]: args_dict = docs2args(x.__doc__) for k, v in cls._schema_properties.items(): if k in args_dict: v.setdefault('description', args_dict[k]['description']) # Determine base class if cls._schema_base_class is None: reg = get_registry() if cls._schema_type in reg: cls._schema_base_class = reg[cls._schema_type]['base'] else: base_comp = cls.__name__ for i, x in enumerate(cls.__mro__): if x._schema_type != cls._schema_type: break base_comp = x.__name__ else: # pragma: debug raise RuntimeError( f"Could not determine base class for {cls} " f"from {bases}.") cls._schema_base_class = base_comp # Register global _registry yaml_typ = cls._schema_type default_subtype = cls._schema_properties.get( cls._schema_subtype_key, {}).get('default', cls._schema_subtype_default) if yaml_typ not in _registry: _registry[yaml_typ] = OrderedDict([ ("classes", OrderedDict()), ("module", '.'.join(cls.__module__.split('.')[:-1])), ("default", default_subtype), ("base", cls._schema_base_class), ("key", cls._schema_subtype_key), ("subtypes", {}) ]) elif default_subtype is not None: assert (_registry[yaml_typ]["default"] == default_subtype) if cls.__name__ not in _registry[yaml_typ]["classes"]: _registry[yaml_typ]["classes"][cls.__name__] = cls _registry[yaml_typ]["subtypes"][subtype] = cls.__name__ if not registration_in_progress(): cls.after_registration(cls) cls.finalize_registration(cls) return cls