def post_new_spec(request, dbp, obj, resource):
    require_permission(request.req, resource, dbp.env, operation="CREATE")

    if obj is Entity:  # instantiating Entity (i.e., creating a spec)
        pass
    elif obj is Instance or isinstance(
            obj, Entity):  # instantiating an existing spec
        return post_new_artifact(request.req, dbp, obj, resource)
    else:
        raise Exception(
            "Trying to instantiate something that can't be instantiated '%s'" %
            (obj, ))

    name = request.req.args.get('name')
    parent_name = request.req.args.get('parent')

    attributes = [
        Attribute(n, m, t) for n, t, m in _group_spec_attributes(request.req)
    ]

    if parent_name:
        dbp.load_spec(parent_name)
        bases = (dbp.pool.get_item(parent_name), )
    else:
        bases = tuple()
    brand_new_inst = Entity(name=name, attributes=attributes, bases=bases)

    dbp.pool.add(brand_new_inst)
    dbp.save(get_reporter_id(request.req), 'comment', request.req.remote_addr)
    add_notice(request.req, 'Your changes have been saved.')
    url = request.req.href.customartifacts('spec',
                                           brand_new_inst.get_name(),
                                           action='view')
    request.req.redirect(url)
    def setUp(self):
        self.env = EnvironmentStub(enable=['trac.*', 'AdaptiveArtifacts.*', 'AdaptiveArtifacts.persistence.db.*'])
        Setup(self.env).upgrade_environment(self.env.get_db_cnx())

        self.Vehicle = Entity(name="Vehicle")
        self.Car = Entity(name="Car", bases=(self.Vehicle,),
                attributes=[Attribute(name="Wheels", multiplicity=4, atype=str)]
            )
        self.lightningMcQueen = self.Car(
                values={"Wheels": ['front left wheel', 'front right wheel', 'rear left wheel', 'front right wheel']}
            )

        pool = InstancePool()
        pool.add(self.Vehicle)
        pool.add(self.Car)
        pool.add(self.lightningMcQueen)
        dbp = DBPool(self.env, pool)
        dbp.save('anonymous',"","120.0.0.1")
Пример #3
0
    def load_spec(self, spec_name, db=None):

        # Ignore requests to load the top-most classes of the instantiation chain (Entity and Instance),
        # as these will not be persisted to the database and will be always available from the pool.
        if spec_name in (Entity.get_name(), Instance.get_name()):
            return

        if not db:
            db = self.env.get_read_db()
        version = self._get_latest_spec_version(spec_name, db)
        if version is None:
            raise ValueError("No version found for spec with name '%s'" % (spec_name,))

        # get the baseclass
        base_class = None
        cursor = db.cursor()
        rows = cursor.execute("""
                SELECT base_class
                FROM asa_spec
                WHERE name='%s' AND version_id='%d'
                GROUP BY name""" % (spec_name, version))
        base_class_name = rows.fetchone()
        if not base_class_name is None and len(base_class_name) > 0:
            base_class_name = base_class_name[0]
            # load base classes (recursively until the root is reached)
            if base_class_name == Instance.get_name():
                base_class = Instance
            else:
                self.load_spec(base_class_name, db)
                base_class = self.pool.get_item(base_class_name)
        bases = (base_class,) if not base_class is None else tuple()

        # get the attributes
        attributes = []
        cursor = db.cursor()
        rows = cursor.execute("""
                SELECT name, multplicity_low, multplicity_high, type, uiorder
                FROM asa_spec_attribute
                WHERE spec_name='%s' AND version_id='%d'""" % (spec_name, version))
        for row in rows.fetchall():
            attributes.append(Attribute(name=row[0], multiplicity=(row[1], row[2]), atype=row[3], order=row[4]))

        # create the entity
        spec = Entity(name=spec_name, bases=bases, version=version, persisted=True, attributes=attributes)

        if self.pool.get_item(spec.get_name()) is None:
            self.pool.add(spec)