root / elixir / trunk / elixir / options.py @ 119

Revision 119, 7.4 kB (checked in by ged, 7 years ago)

- Made EntityMeta public, so that people can actually define their own base

class (I thought I already made this possible but it seems like not).

- Removed all all declarations from subpackages as they are IMHO useless

since we can't get to them from outside of the lib, consequently replaced
all the "from xyz import *" lines in init.py by explicit imports.

Line 
1'''
2Option statements for Elixir entities
3
4=======
5Options
6=======
7
8This module provides DSL statements for defining options on your Elixir
9entities.  There are three different kinds of options that can be set
10up, and for this there are three different statements: using_options_,
11using_table_options_ and using_mapper_options_.
12Alternatively, options can be set on all Elixir entities by modifying the
13`options_defaults` dictionary before defining any entity.
14
15`using_options`
16---------------
17The 'using_options' DSL statement allows you to set up some additional
18behaviors on your model objects, including table names, ordering, and
19more.  To specify an option, simply supply the option as a keyword
20argument onto the statement, as follows:
21
22::
23
24    class Person(Entity):
25        has_field('name', Unicode(64))
26
27        using_options(shortnames=True, order_by='name')
28
29The list of supported arguments are as follows:
30
31+---------------------+-------------------------------------------------------+
32| Option Name         | Description                                           |
33+=====================+=======================================================+
34| ``inheritance``     | Specify the type of inheritance this entity must use. |
35|                     | It can be one of ``single``, ``concrete`` or          |
36|                     | ``multi``.                                            |
37|                     | **For now, only the single type is implemented.**     |
38+---------------------+-------------------------------------------------------+
39| ``metadata``        | Specify a custom MetaData                             |
40+---------------------+-------------------------------------------------------+
41| ``autoload``        | Automatically load column definitions from the        |
42|                     | existing database table.                              |
43|                     | Using autoloaded tables implies setting               |
44|                     | ``delay_setup`` to ``True`` before defining your      |
45|                     | entities.                                             |
46+---------------------+-------------------------------------------------------+
47| ``tablename``       | Specify a custom tablename. You can either provide a  |
48|                     | plain string or a callable. The callable will be      |
49|                     | given the entity (ie class) as argument and must      |
50|                     | return a string representing the name of the table    |
51|                     | for that entity.                                      |
52+---------------------+-------------------------------------------------------+
53| ``shortnames``      | Usually tablenames include the full module-path       |
54|                     | to the entity, but lower-cased and separated by       |
55|                     | underscores ("_"), eg.: "project1_model_myentity"     |
56|                     | for an entity named "MyEntity" in the module          |
57|                     | "project1.model".  If shortnames is True, the         |
58|                     | tablename will just be the entity's classname         |
59|                     | lower-cased, ie. "myentity".                          |
60+---------------------+-------------------------------------------------------+
61| ``auto_primarykey`` | If given as string, it will represent the             |
62|                     | auto-primary-key's column name.  If this option       |
63|                     | is True, it will allow auto-creation of a primary     |
64|                     | key if there's no primary key defined for the         |
65|                     | corresponding entity.  If this option is False,       |
66|                     | it will disallow auto-creation of a primary key.      |
67+---------------------+-------------------------------------------------------+
68| ``version_id_col``  | If this option is True, it will create a version      |
69|                     | column automatically using the default name. If given |
70|                     | as string, it will create the column using that name. |
71|                     | This can be used to prevent concurrent modifications  |
72|                     | to the entity's table rows (i.e. it will raise an     |
73|                     | exception if it happens).                             |
74+---------------------+-------------------------------------------------------+
75| ``order_by``        | How to order select results. Either a string or a     |
76|                     | list of strings, composed of the field name,          |
77|                     | optionally lead by a minus (descending order).        |
78+---------------------+-------------------------------------------------------+
79
80For examples, please refer to the examples and unit tests.
81
82`using_table_options`
83---------------------
84The 'using_table_options' DSL statement allows you to set up some
85additional options on your entity table. It is meant only to handle the
86options which are not supported directly by the 'using_options' statement.
87By opposition to the 'using_options' statement, these options are passed
88directly to the underlying SQLAlchemy Table object (both non-keyword arguments
89and keyword arguments) without any processing.
90
91For further information, please refer to the `SQLAlchemy table's documentation
92<http://www.sqlalchemy.org/docs/docstrings.myt
93#docstrings_sqlalchemy.schema_Table>`_.
94
95You might also be interested in the section about `constraints
96<http://www.sqlalchemy.org/docs/metadata.myt#metadata_constraints>`_.
97
98`using_mapper_options`
99----------------------
100The 'using_mapper_options' DSL statement allows you to set up some
101additional options on your entity mapper. It is meant only to handle the
102options which are not supported directly by the 'using_options' statement.
103By opposition to the 'using_options' statement, these options are passed
104directly to the underlying SQLAlchemy mapper (as keyword arguments)
105without any processing.
106
107For further information, please refer to the `SQLAlchemy mapper
108function's documentation <http://www.sqlalchemy.org/docs/adv_datamapping.myt
109#advdatamapping_mapperoptions>`_.
110'''
111
112from elixir.statements import Statement
113
114__pudge_all__ = ['options_defaults']
115
116options_defaults = dict(
117    inheritance='single',
118    autoload=None,
119    shortnames=False,
120    tablename=None,
121    auto_primarykey=True,
122    version_id_col=False,
123    mapper_options=dict(),
124    table_options=dict(),
125)
126
127class UsingOptions(object):   
128    valid_options = (
129        'inheritance',
130        'metadata',
131        'autoload',
132        'tablename',
133        'shortnames',
134        'auto_primarykey',
135        'version_id_col',
136        'order_by',
137    )
138   
139    def __init__(self, entity, *args, **kwargs):
140        desc = entity._descriptor
141       
142        for kwarg in kwargs:
143            if kwarg in UsingOptions.valid_options:
144                setattr(desc, kwarg, kwargs[kwarg])
145            else:
146                raise Exception("'%s' is not a valid option for Elixir "
147                                "entities." % kwarg)
148
149
150class UsingTableOptions(object):
151
152    def __init__(self, entity, *args, **kwargs):
153        entity._descriptor.table_args = list(args)
154        entity._descriptor.table_options.update(kwargs)
155
156
157class UsingMapperOptions(object):
158
159    def __init__(self, entity, *args, **kwargs):
160        entity._descriptor.mapper_options.update(kwargs)
161
162
163using_options = Statement(UsingOptions)
164using_table_options = Statement(UsingTableOptions)
165using_mapper_options = Statement(UsingMapperOptions)
Note: See TracBrowser for help on using the browser.