def __new__(mcs, name, bases, attrs): definitions = {} schema = attrs.get('schema', S.object()) # make sure this class inherits definitions and schemas for base in bases: if hasattr(base, "definitions") and base.definitions is not None: definitions = merge(deepcopy(base.definitions), definitions) if hasattr(base, "schema") and base.schema is not None: schema = merge(deepcopy(base.schema), schema) if hasattr(base, "__doc__"): docstring = base.__doc__ # merge definitions from this class into the definition list. if "definitions" in attrs: merge(attrs['definitions'], definitions) else: attrs['definitions'] = definitions # set the title of the schema and class if 'title' not in attrs or (attrs['title'] is None): if 'title' in schema: attrs['title'] = schema['title'] else: attrs['title'] = split_camelcase(name) # use the modified schema attrs['schema'] = schema return super().__new__(mcs, name, bases, attrs)
def __init__(self, suite, name=None): """Create a new instance of the application. Args: suite (sondra.suite.Suite): The suite with which to register the application. name (str): If supplied, this is the name of the app. Otherwise, the name is the same as the classname The application slug is the slugified version of the name. """ self.suite = suite self.name = name or self.__class__.__name__ self.title = utils.split_camelcase(self.name) self.slug = utils.camelcase_slugify(self.name) if suite.db_prefix: self.db = suite.db_prefix + utils.convert_camelcase(self.name) else: self.db = utils.convert_camelcase(self.name) self._collections = {} self._url = '/'.join((self.suite.url, self.slug)) self.log = logging.getLogger(self.name) self.application = self signals.pre_registration.send(self.__class__, instance=self) self.log.info("Registering application {0}".format(self.slug)) suite.register_application(self) signals.post_registration.send(self.__class__, instance=self) signals.pre_init.send(self.__class__, instance=self) for collection_class in self.collections: name = collection_class.slug self.log.info("Creating collection for {0}/{1}".format(self.slug, collection_class.slug)) if name in self._collections: raise ApplicationException(name + " already exists in " + self.name) coll = collection_class(self) # inherit definitions from this suite if self.suite.definitions and 'definitions' not in coll.schema: coll.schema['definitions'] = {} for k, v in self.suite.definitions.items(): if k not in coll.schema['definitions']: coll.schema['definitions'][k] = v # inherit definitions from this application if self.definitions and 'definitions' not in coll.schema: coll.schema['definitions'] = {} for k, v in self.definitions.items(): if k not in coll.schema['definitions']: coll.schema['definitions'][k] = v coll.document_class.specials = SchemaParser(coll.schema, coll.schema['definitions'])() self._collections[name] = coll signals.post_init.send(self.__class__, instance=self)
def __init__(cls, name, bases, nmspc): super(CollectionMetaclass, cls).__init__(name, bases, nmspc) cls.exposed_methods = {} cls.title = cls.title or cls.__name__ cls.name = utils.convert_camelcase(cls.__name__) for base in bases: if hasattr(base, 'exposed_methods'): cls.exposed_methods.update(base.exposed_methods) for name, method in (n for n in nmspc.items() if hasattr(n[1], 'exposed')): cls.exposed_methods[name] = method if cls.document_class and (cls.document_class is not Document): cls.abstract = False cls.schema = deepcopy(cls.document_class.schema) if cls.primary_key == 'id': cls.schema['properties']['id'] = {"type": "string", "description": "The primary key.", "title": "ID"} cls.document_class.schema['properties']['id'] = {"type": "string", "description": "The primary key.", "title": "ID"} cls.schema["methods"] = {} for m in cls.exposed_methods.values(): cls.schema['methods'][m.slug] = method_schema(False, m) cls.schema['documentMethods'] = {} for m in cls.document_class.exposed_methods.values(): cls.schema["documentMethods"][m.slug] = method_schema(None, m) cls.schema['primary_key'] = 'id' if not cls.primary_key else cls.primary_key if not 'description' in cls.schema: cls.schema['description'] = cls.document_class.__doc__ or 'No Description Provided.' if not 'title' in cls.schema: cls.schema['title'] = split_camelcase(cls.document_class.__name__) _validator.check_schema(cls.schema) cls.slug = utils.camelcase_slugify(cls.__name__) else: cls.abstract = True