Ejemplo n.º 1
0
def finish(db, schema_module=None):
    """Unlock the schema import mutex and return the schema definition."""
    if schema_module is None:
        import_lock.release()
        return
    schema_def = schevo.namespace.SCHEMADEF
    # Reset the global namespace SCHEMADEF.
    schevo.namespace.SCHEMADEF = None
    schevo.namespace.SCHEMADB = None
    schevo.namespace.EVOLVING = False
    # Remove this class now that the schema has been processed.
    del schema_def.E.Entity
    # Force all Entity field classes to readonly.
    for entity_name in schema_def.E:
        EntityClass = schema_def.E[entity_name]
        for FieldClass in EntityClass._field_spec.itervalues():
            FieldClass.readonly = True
    # Decorate all Transaction classes with the current database.
    for transaction_name in schema_def.T:
        TransactionClass = schema_def.T[transaction_name]
        TransactionClass._db = db
    # Decorate all Entity transaction classes with the current database.
    for entity_name in schema_def.E:
        EntityClass = schema_def.E[entity_name]
        for v in EntityClass.__dict__.itervalues():
            if (inspect.isclass(v)
                and issubclass(v, schevo.transaction.Transaction)):
                v._db = db
    # Decorate all View transaction classes with the current database.
    for view_name in schema_def.V:
        ViewClass = schema_def.V[view_name]
        for v in ViewClass.__dict__.itervalues():
            if (inspect.isclass(v)
                and issubclass(v, schevo.transaction.Transaction)):
                v._db = db
    # Add relationship metadata to each Entity class.
    for parent, spec in schema_def.relationships.iteritems():
        E = schema_def.E
        # Catch non-existence errors.
        parentClass = getattr(E, parent, None)
        if parentClass is None:
            raise schevo.error.ExtentDoesNotExist(parent)
        # Make sure spec is sorted in field definition order.
        other_map = {}
        for other_extent_name, other_field_name in spec:
            other_extent_field_set = other_map.setdefault(
                other_extent_name, set())
            other_extent_field_set.add(other_field_name)
        spec = []
        for other_extent_name, other_extent_field_set in other_map.items():
            other_class = getattr(E, other_extent_name)
            spec.extend(
                (other_extent_name, other_field_name)
                for other_field_name
                in other_class._field_spec
                if other_field_name in other_extent_field_set
                )
        # Record final spec.
        parentClass._relationships = spec
    del schema_def.relationships
    # Create database-level query function namespace.
    q = schema_def.q
    for name, func in schema_module.__dict__.iteritems():
        if isinstance(func, FunctionType) and name.startswith('q_'):
            # Strip q_ prefix.
            name = name[2:]
            # Give it a label if necessary.
            if getattr(func, '_label', None) is None:
                func._label = schevo.label.label_from_name(name)
            q._set(name, func)
    # Create database-level transaction function namespace.
    t = schema_def.t
    for name, func in schema_module.__dict__.iteritems():
        if isinstance(func, FunctionType) and name.startswith('t_'):
            # Strip t_ prefix.
            name = name[2:]
            # Give it a label if necessary.
            if getattr(func, '_label', None) is None:
                func._label = schevo.label.label_from_name(name)
            t._set(name, func)
    # Get rid of the temporary null database.
    del schema_module.db
    # Optimize the module.
    optimize.bind_all(schema_module)
    # Install the actual database.
    schema_module.db = db
    # Release import lock.
    import_lock.release()
    return schema_def
Ejemplo n.º 2
0

def open(filename, backend_name=None, backend_args=None):
    """Open an existing database and return it.

    - `filename`: Name of the file containing the database.
    - `backend_name`: (optional) Name of the backend to use if
      auto-detection is not desired.
    - `backend_args`: (optional) Arguments to pass to the backend.
    """
    backend = new_backend(filename, backend_name, backend_args)
    # Make sure the database already exists.
    root = backend.get_root()
    if 'SCHEVO' not in root:
        backend.close()
        raise DatabaseDoesNotExist(filename)
    # Determine the version of the database.
    schevo = root['SCHEVO']
    format = schevo['format']
    # Determine database class based on format number.
    Database = format_dbclass[format]
    # Create the Database instance.
    db = Database(backend)
    db._sync()
    # Install icon support and finalize opening of database.
    icon.install(db)
    return db


optimize.bind_all(sys.modules[__name__])  # Last line of module.
Ejemplo n.º 3
0
    def update(self, other, reorder=False):
        """Update values in this odict based on the `other` odict."""
        if not isinstance(other, odict):
            raise ValueError('other must be an odict')
        if other is self:
            raise ValueError('other cannot be the same odict')
        dict.update(self, other)
        keys = self._keys
        if not reorder:
            for key in other:
                if key not in keys:
                    keys.append(key)
        else:
            for key in other:
                if key in keys:
                    keys.remove(key)
                keys.append(key)

    def values(self):
        return [self[key] for key in self._keys]


optimize.bind_all(odict)


## # Bind the docstrings from dict to odict.
## for k, v in odict.__dict__.iteritems():
##     if k != '_keys' and hasattr(v, '__doc__') and v.__doc__ is None:
##         v.__doc__ = getattr(dict, v.__name__).__doc__