pyramid-basemodel 0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyramid_basemodel-0.1/._MANIFEST.in +0 -0
- pyramid_basemodel-0.1/._README.md +0 -0
- pyramid_basemodel-0.1/._UNLICENSE +0 -0
- pyramid_basemodel-0.1/._setup.py +0 -0
- pyramid_basemodel-0.1/MANIFEST.in +2 -0
- pyramid_basemodel-0.1/PKG-INFO +25 -0
- pyramid_basemodel-0.1/README.md +5 -0
- pyramid_basemodel-0.1/UNLICENSE +24 -0
- pyramid_basemodel-0.1/setup.cfg +5 -0
- pyramid_basemodel-0.1/setup.py +50 -0
- pyramid_basemodel-0.1/src/pyramid_basemodel/.___init__.py +0 -0
- pyramid_basemodel-0.1/src/pyramid_basemodel/__init__.py +153 -0
- pyramid_basemodel-0.1/src/pyramid_basemodel.egg-info/PKG-INFO +25 -0
- pyramid_basemodel-0.1/src/pyramid_basemodel.egg-info/SOURCES.txt +11 -0
- pyramid_basemodel-0.1/src/pyramid_basemodel.egg-info/dependency_links.txt +1 -0
- pyramid_basemodel-0.1/src/pyramid_basemodel.egg-info/not-zip-safe +1 -0
- pyramid_basemodel-0.1/src/pyramid_basemodel.egg-info/requires.txt +4 -0
- pyramid_basemodel-0.1/src/pyramid_basemodel.egg-info/top_level.txt +1 -0
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
Metadata-Version: 1.0
|
|
2
|
+
Name: pyramid_basemodel
|
|
3
|
+
Version: 0.1
|
|
4
|
+
Summary: Global base classes for Pyramid SQLAlchemy applications.
|
|
5
|
+
Home-page: http://github.com/thruflo/pyramid_basemodel
|
|
6
|
+
Author: James Arthur
|
|
7
|
+
Author-email: username: thruflo, domain: gmail.com
|
|
8
|
+
License: This is free and unencumbered software released into the public domain.
|
|
9
|
+
Description: UNKNOWN
|
|
10
|
+
Platform: UNKNOWN
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Environment :: Web Environment
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: Public Domain
|
|
15
|
+
Classifier: Natural Language :: English
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python
|
|
18
|
+
Classifier: Programming Language :: Python :: 2.6
|
|
19
|
+
Classifier: Programming Language :: Python :: 2.7
|
|
20
|
+
Classifier: Programming Language :: Python :: 3
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.2
|
|
22
|
+
Classifier: Framework :: Pylons
|
|
23
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
24
|
+
Classifier: Topic :: Internet :: WWW/HTTP :: WSGI
|
|
25
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
[pyramid_basemodel][] is a thin, low level package that provides an SQLAlchemy
|
|
2
|
+
declarative `Base` and a thread local scoped `Session` that can be used by
|
|
3
|
+
different packages whilst only needing to be bound to a db engine once.
|
|
4
|
+
|
|
5
|
+
[pyramid_basemodel]: http://github.com/thruflo/pyramid_basemodel
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
This is free and unencumbered software released into the public domain.
|
|
2
|
+
|
|
3
|
+
Anyone is free to copy, modify, publish, use, compile, sell, or
|
|
4
|
+
distribute this software, either in source code form or as a compiled
|
|
5
|
+
binary, for any purpose, commercial or non-commercial, and by any
|
|
6
|
+
means.
|
|
7
|
+
|
|
8
|
+
In jurisdictions that recognize copyright laws, the author or authors
|
|
9
|
+
of this software dedicate any and all copyright interest in the
|
|
10
|
+
software to the public domain. We make this dedication for the benefit
|
|
11
|
+
of the public at large and to the detriment of our heirs and
|
|
12
|
+
successors. We intend this dedication to be an overt act of
|
|
13
|
+
relinquishment in perpetuity of all present and future rights to this
|
|
14
|
+
software under copyright law.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
19
|
+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
20
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
21
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+
|
|
24
|
+
For more information, please refer to <http://unlicense.org/>
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
|
|
4
|
+
from os.path import dirname, join as join_path
|
|
5
|
+
from setuptools import setup, find_packages
|
|
6
|
+
|
|
7
|
+
def _read(file_name):
|
|
8
|
+
sock = open(file_name)
|
|
9
|
+
text = sock.read()
|
|
10
|
+
sock.close()
|
|
11
|
+
return text
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
setup(
|
|
16
|
+
name = 'pyramid_basemodel',
|
|
17
|
+
version = '0.1',
|
|
18
|
+
description = 'Global base classes for Pyramid SQLAlchemy applications.',
|
|
19
|
+
author = 'James Arthur',
|
|
20
|
+
author_email = 'username: thruflo, domain: gmail.com',
|
|
21
|
+
url = 'http://github.com/thruflo/pyramid_basemodel',
|
|
22
|
+
classifiers = [
|
|
23
|
+
'Development Status :: 3 - Alpha',
|
|
24
|
+
'Environment :: Web Environment',
|
|
25
|
+
'Intended Audience :: Developers',
|
|
26
|
+
'License :: Public Domain',
|
|
27
|
+
'Natural Language :: English',
|
|
28
|
+
'Operating System :: OS Independent',
|
|
29
|
+
'Programming Language :: Python',
|
|
30
|
+
'Programming Language :: Python :: 2.6',
|
|
31
|
+
'Programming Language :: Python :: 2.7',
|
|
32
|
+
'Programming Language :: Python :: 3',
|
|
33
|
+
'Programming Language :: Python :: 3.2',
|
|
34
|
+
'Framework :: Pylons',
|
|
35
|
+
'Topic :: Internet :: WWW/HTTP',
|
|
36
|
+
'Topic :: Internet :: WWW/HTTP :: WSGI',
|
|
37
|
+
'Topic :: Software Development :: Libraries :: Python Modules',
|
|
38
|
+
],
|
|
39
|
+
license = _read('UNLICENSE').split('\n')[0],
|
|
40
|
+
packages = find_packages('src'),
|
|
41
|
+
package_dir = {'': 'src'},
|
|
42
|
+
include_package_data = True,
|
|
43
|
+
zip_safe = False,
|
|
44
|
+
install_requires=[
|
|
45
|
+
'pyramid_tm',
|
|
46
|
+
'pyramid',
|
|
47
|
+
'zope.sqlalchemy',
|
|
48
|
+
'SQLAlchemy'
|
|
49
|
+
]
|
|
50
|
+
)
|
|
Binary file
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
"""Provides global scoped ``Session`` and declarative ``Base``, ``BaseMixin``
|
|
4
|
+
class and ``bind_engine`` function.
|
|
5
|
+
|
|
6
|
+
To use, import and, e.g.: inherit from the base classes::
|
|
7
|
+
|
|
8
|
+
>>> class MyModel(Base, BaseMixin):
|
|
9
|
+
... __tablename__ = 'my_model'
|
|
10
|
+
...
|
|
11
|
+
>>> instance = MyModel()
|
|
12
|
+
>>> Session.add(instance)
|
|
13
|
+
>>> # etc.
|
|
14
|
+
|
|
15
|
+
To automatically bind the base metadata and session to your db engine, just
|
|
16
|
+
include the package::
|
|
17
|
+
|
|
18
|
+
config.include('pyramid_basemodel')
|
|
19
|
+
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
__all__ = [
|
|
23
|
+
'Base',
|
|
24
|
+
'BaseMixin',
|
|
25
|
+
'Session',
|
|
26
|
+
'bind_engine',
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
from datetime import datetime
|
|
30
|
+
|
|
31
|
+
from sqlalchemy import engine_from_config
|
|
32
|
+
from sqlalchemy import Column, DateTime, Integer
|
|
33
|
+
from sqlalchemy.ext.declarative import declarative_base
|
|
34
|
+
from sqlalchemy.orm import scoped_session, sessionmaker
|
|
35
|
+
from zope.sqlalchemy import ZopeTransactionExtension
|
|
36
|
+
|
|
37
|
+
Session = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
|
|
38
|
+
Base = declarative_base()
|
|
39
|
+
|
|
40
|
+
class BaseMixin(object):
|
|
41
|
+
"""Provides an int ``id`` as primary key, ``version``, ``created`` and
|
|
42
|
+
``modified`` columns and a scoped ``self.query`` property.
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
id = Column(Integer, primary_key=True)
|
|
46
|
+
|
|
47
|
+
version = Column('v', Integer, default=1)
|
|
48
|
+
created = Column('c', DateTime, default=datetime.utcnow)
|
|
49
|
+
modified = Column('m', DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
|
50
|
+
|
|
51
|
+
query = Session.query_property()
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def save(instance_or_instances, session=Session):
|
|
55
|
+
"""Save model instance(s) to the db.
|
|
56
|
+
|
|
57
|
+
Setup::
|
|
58
|
+
|
|
59
|
+
>>> from mock import Mock
|
|
60
|
+
>>> mock_session = Mock()
|
|
61
|
+
|
|
62
|
+
A single instance is added to the session::
|
|
63
|
+
|
|
64
|
+
>>> save('a', session=mock_session)
|
|
65
|
+
>>> mock_session.add.assert_called_with('a')
|
|
66
|
+
|
|
67
|
+
Multiple instances are all added at the same time::
|
|
68
|
+
|
|
69
|
+
>>> save(['a', 'b'], session=mock_session)
|
|
70
|
+
>>> mock_session.add_all.assert_called_with(['a', 'b'])
|
|
71
|
+
|
|
72
|
+
"""
|
|
73
|
+
|
|
74
|
+
v = instance_or_instances
|
|
75
|
+
if isinstance(v, list) or isinstance(v, tuple):
|
|
76
|
+
session.add_all(v)
|
|
77
|
+
else:
|
|
78
|
+
session.add(v)
|
|
79
|
+
|
|
80
|
+
def bind_engine(engine, session=Session, base=Base, should_drop=False):
|
|
81
|
+
"""Bind the ``session`` and ``base`` to the ``engine``.
|
|
82
|
+
|
|
83
|
+
Setup::
|
|
84
|
+
|
|
85
|
+
>>> from mock import Mock
|
|
86
|
+
>>> mock_session = Mock()
|
|
87
|
+
>>> mock_base = Mock()
|
|
88
|
+
>>> mock_engine = Mock()
|
|
89
|
+
|
|
90
|
+
Binds the session::
|
|
91
|
+
|
|
92
|
+
>>> bind_engine(mock_engine, session=mock_session, base=mock_base)
|
|
93
|
+
>>> mock_session.configure.assert_called_with(bind=mock_engine)
|
|
94
|
+
|
|
95
|
+
Binds the base metadata::
|
|
96
|
+
|
|
97
|
+
>>> mock_base.metadata.bind == mock_engine
|
|
98
|
+
True
|
|
99
|
+
|
|
100
|
+
Creates tables if they don't exist::
|
|
101
|
+
|
|
102
|
+
>>> mock_base.metadata.create_all.assert_called_with(mock_engine)
|
|
103
|
+
|
|
104
|
+
Drops tables if ``should_drop`` is ``True``::
|
|
105
|
+
|
|
106
|
+
>>> mock_base.metadata.drop_all.called
|
|
107
|
+
False
|
|
108
|
+
>>> bind_engine(mock_engine, session=mock_session, base=mock_base,
|
|
109
|
+
... should_drop=True)
|
|
110
|
+
>>> mock_base.metadata.drop_all.assert_called_with(mock_engine)
|
|
111
|
+
|
|
112
|
+
"""
|
|
113
|
+
|
|
114
|
+
session.configure(bind=engine)
|
|
115
|
+
base.metadata.bind = engine
|
|
116
|
+
if should_drop:
|
|
117
|
+
base.metadata.drop_all(engine)
|
|
118
|
+
base.metadata.create_all(engine)
|
|
119
|
+
|
|
120
|
+
def includeme(config):
|
|
121
|
+
"""Bind to the db engine specifed in ``config.registry.settings``.
|
|
122
|
+
|
|
123
|
+
Setup::
|
|
124
|
+
|
|
125
|
+
>>> from mock import Mock
|
|
126
|
+
>>> import pyramid_basemodel
|
|
127
|
+
>>> _engine_from_config = pyramid_basemodel.engine_from_config
|
|
128
|
+
>>> _bind_engine = pyramid_basemodel.bind_engine
|
|
129
|
+
>>> pyramid_basemodel.engine_from_config = Mock()
|
|
130
|
+
>>> pyramid_basemodel.engine_from_config.return_value = 'engine'
|
|
131
|
+
>>> pyramid_basemodel.bind_engine = Mock()
|
|
132
|
+
>>> mock_config = Mock()
|
|
133
|
+
>>> mock_config.registry.settings = {}
|
|
134
|
+
|
|
135
|
+
Calls ``bind_engine`` with the configured ``engine``::
|
|
136
|
+
|
|
137
|
+
>>> includeme(mock_config)
|
|
138
|
+
>>> pyramid_basemodel.bind_engine.assert_called_with('engine',
|
|
139
|
+
... should_drop=False)
|
|
140
|
+
|
|
141
|
+
Teardown::
|
|
142
|
+
|
|
143
|
+
>>> pyramid_basemodel.engine_from_config = _engine_from_config
|
|
144
|
+
>>> pyramid_basemodel.bind_engine = _bind_engine
|
|
145
|
+
|
|
146
|
+
"""
|
|
147
|
+
|
|
148
|
+
# Bind the engine.
|
|
149
|
+
settings = config.registry.settings
|
|
150
|
+
engine = engine_from_config(settings, 'sqlalchemy.')
|
|
151
|
+
should_drop = settings.get('basemodel.should_drop_all', False)
|
|
152
|
+
bind_engine(engine, should_drop=should_drop)
|
|
153
|
+
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
Metadata-Version: 1.0
|
|
2
|
+
Name: pyramid-basemodel
|
|
3
|
+
Version: 0.1
|
|
4
|
+
Summary: Global base classes for Pyramid SQLAlchemy applications.
|
|
5
|
+
Home-page: http://github.com/thruflo/pyramid_basemodel
|
|
6
|
+
Author: James Arthur
|
|
7
|
+
Author-email: username: thruflo, domain: gmail.com
|
|
8
|
+
License: This is free and unencumbered software released into the public domain.
|
|
9
|
+
Description: UNKNOWN
|
|
10
|
+
Platform: UNKNOWN
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Environment :: Web Environment
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: Public Domain
|
|
15
|
+
Classifier: Natural Language :: English
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python
|
|
18
|
+
Classifier: Programming Language :: Python :: 2.6
|
|
19
|
+
Classifier: Programming Language :: Python :: 2.7
|
|
20
|
+
Classifier: Programming Language :: Python :: 3
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.2
|
|
22
|
+
Classifier: Framework :: Pylons
|
|
23
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
24
|
+
Classifier: Topic :: Internet :: WWW/HTTP :: WSGI
|
|
25
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
MANIFEST.in
|
|
2
|
+
README.md
|
|
3
|
+
UNLICENSE
|
|
4
|
+
setup.py
|
|
5
|
+
src/pyramid_basemodel/__init__.py
|
|
6
|
+
src/pyramid_basemodel.egg-info/PKG-INFO
|
|
7
|
+
src/pyramid_basemodel.egg-info/SOURCES.txt
|
|
8
|
+
src/pyramid_basemodel.egg-info/dependency_links.txt
|
|
9
|
+
src/pyramid_basemodel.egg-info/not-zip-safe
|
|
10
|
+
src/pyramid_basemodel.egg-info/requires.txt
|
|
11
|
+
src/pyramid_basemodel.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pyramid_basemodel
|