root / elixir / trunk / elixir / __init__.py @ 48

Revision 48, 3.2 kB (checked in by ged, 6 years ago)

documented cleanup_all

Line 
1'''
2Elixir package
3   
4A declarative layer on top of SQLAlchemy, which is intended to replace the
5ActiveMapper SQLAlchemy extension, and the TurboEntity project.  Elixir is a
6fairly thin wrapper around SQLAlchemy, which provides the ability to define
7model objects following the Active Record design pattern, and using a DSL
8syntax similar to that of the Ruby on Rails ActiveRecord system.
9
10Elixir does not intend to replace SQLAlchemy's core features, but instead
11focuses on providing a simpler syntax for defining model objects when you do
12not need the full expressiveness of SQLAlchemy's manual mapper definitions.
13
14For an example of how to use Elixir, please refer to the examples directory
15and the unit tests. The examples directory includes a TurboGears application
16with full identity support called 'videostore'.
17'''
18
19import sqlalchemy
20
21from sqlalchemy.ext.sessioncontext  import SessionContext
22from sqlalchemy.types               import *
23from elixir.options                 import *
24from elixir.entity                  import Entity, EntityDescriptor
25from elixir.fields                  import Field, has_field, with_fields
26from elixir.relationships           import belongs_to, has_one, has_many, \
27                                           has_and_belongs_to_many
28
29__all__ = ['Entity', 'Field', 'has_field', 'with_fields', 
30           'belongs_to', 'has_one', 'has_many', 'has_and_belongs_to_many', 
31           'using_options', 'using_table_options', 'using_mapper_options',
32           'options_defaults', 'metadata', 'objectstore',
33           'create_all', 'drop_all', 'setup_all', 'cleanup_all', 
34           'delay_setup'] + \
35          sqlalchemy.types.__all__
36
37__pudge_all__ = ['create_all', 'drop_all', 'setup_all', 'cleanup_all',
38                 'metadata', 'objectstore', 'delay_setup']
39
40# connect
41metadata = sqlalchemy.DynamicMetaData('elixir')
42
43try:
44    objectstore = sqlalchemy.objectstore
45except AttributeError:
46    # thread local SessionContext
47    class Objectstore(object):
48        def __init__(self, *args, **kwargs):
49            self.context = SessionContext(*args, **kwargs)
50        def __getattr__(self, name):
51            return getattr(self.context.current, name)
52        session = property(lambda s:s.context.current)
53   
54    objectstore = Objectstore(sqlalchemy.create_session)
55
56metadatas = set()
57
58def create_all():
59    'Create all necessary tables for all declared entities'
60    for md in metadatas:
61        md.create_all()
62
63def drop_all():
64    'Drop all tables for all declared entities'
65    for md in metadatas:
66        md.drop_all()
67
68delayed_entities = set()
69delay_setup = False
70
71def setup_all():
72    '''Setup the table and mapper for all entities which have been delayed.
73   
74    This should be used in conjunction with setting ``delay_setup`` to ``True``
75    before defining your entities.
76    '''
77    global delay_setup
78    delay_setup = False
79    for entity in delayed_entities:
80        entity.setup()
81    delayed_entities.clear()
82    create_all()
83
84def cleanup_all():
85    '''Drop table and clear mapper for all entities, and clear all metadatas.
86    '''
87    drop_all()
88    for md in metadatas:
89        md.clear()
90    metadatas.clear()
91    EntityDescriptor.uninitialized_rels.clear()
92
93    objectstore.clear()
94    sqlalchemy.clear_mappers()
Note: See TracBrowser for help on using the browser.