root / elixir / tags / 0.3.0 / elixir / __init__.py

Revision 89, 3.4 kB (checked in by ged, 8 years ago)

- Added version
- Fixed inheritance unit test to pass SQLAlchemy type check on relations

(introduced in SA 0.3.6)

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 *
26from elixir.relationships           import *
27
28try:
29    set
30except NameError:
31    from sets import Set as set
32
33__version__ = '0.3.0'
34
35__all__ = ['Entity', 'Field', 'has_field', 'with_fields',
36           'belongs_to', 'has_one', 'has_many', 'has_and_belongs_to_many',
37           'using_options', 'using_table_options', 'using_mapper_options',
38           'options_defaults', 'metadata', 'objectstore',
39           'create_all', 'drop_all', 'setup_all', 'cleanup_all',
40           'delay_setup'] + \
41          sqlalchemy.types.__all__
42
43__pudge_all__ = ['create_all', 'drop_all', 'setup_all', 'cleanup_all',
44                 'metadata', 'objectstore', 'delay_setup']
45
46# connect
47metadata = sqlalchemy.DynamicMetaData('elixir', threadlocal=False)
48
49try:
50    # this only happens when the threadlocal extension is used
51    objectstore = sqlalchemy.objectstore
52except AttributeError:
53    # thread local SessionContext
54    class Objectstore(object):
55
56        def __init__(self, *args, **kwargs):
57            self.context = SessionContext(*args, **kwargs)
58
59        def __getattr__(self, name):
60            return getattr(self.context.current, name)
61        session = property(lambda s:s.context.current)
62
63    objectstore = Objectstore(sqlalchemy.create_session)
64
65metadatas = set()
66
67
68def create_all():
69    'Create all necessary tables for all declared entities'
70    for md in metadatas:
71        md.create_all()
72
73
74def drop_all():
75    'Drop all tables for all declared entities'
76    for md in metadatas:
77        md.drop_all()
78
79delayed_entities = set()
80delay_setup = False
81
82
83def setup_all():
84    '''Setup the table and mapper for all entities which have been delayed.
85
86    This should be used in conjunction with setting ``delay_setup`` to ``True``
87    before defining your entities.
88    '''
89    for entity in delayed_entities:
90        entity.setup_table()
91    for entity in delayed_entities:
92        entity.setup_mapper()
93
94    # setup all relationships
95    for entity in delayed_entities:
96        for rel in entity.relationships.itervalues():
97            rel.setup()
98
99    delayed_entities.clear()
100
101    # issue the "CREATE" SQL statements
102    create_all()
103
104
105def cleanup_all():
106    '''Drop table and clear mapper for all entities, and clear all metadatas.
107    '''
108    drop_all()
109    for md in metadatas:
110        md.clear()
111    metadatas.clear()
112    EntityDescriptor.uninitialized_rels.clear()
113
114    objectstore.clear()
115    sqlalchemy.clear_mappers()
Note: See TracBrowser for help on using the browser.