dp-tornado¶
MVC Web Application Framework with Tornado, Python 2 and 3
Environment Setup¶
Virtualenv:
$ pip install virtualenv
$ virtualenv ./venv
$ . ./venv/bin/activate
Install dp-tornado:
$ pip install dp-tornado
Run¶
Startup:
$ dp4p init app_dir
$ dp4p run app_dir
or:
$ dp4p init --path app_dir --template=bbs
$ dp4p run --path app_dir --template=bbs
or:
$ mkdir app_dir
$ cd app_dir
$ dp4p init
$ dp4p run
with Docker:
$ docker pull why2pac/dp4p:latest-py34 # py27, py34, py35, pypy27
$ docker run --name "dp4p-example" -d -p 8080:52848 -v "$(pwd)/app_dir:/data/app" why2pac/dp4p:latest-py34
Documentation¶
MVC¶
Warning
This package is not documented yet.
engine.model
— Model of MVC¶
Warning
This package is not documented yet.
The model directly manages the data, logic, and rules of the application. <Wikipedia>
Here is a foo_bar model example:
from dp_tornado.engine.model import Model as dpModel
class FooBarModel(dpModel):
def func1(self):
"""
assert self.helper.foo.func1(10, 20) == None
"""
return None
def func2(self, a):
"""
assert self.helper.foo.func2(10) == 10
"""
return a
def func3(self, a, b):
"""
assert self.helper.foo.func3(10, 20) == 30
"""
return a + b
File/Class Invoke rules¶
- /model/__init__.py, DO NOT IMPLEMENT ANY CODE IN THIS FILE
- /model/blog/__init__.py,
BlogModel
> model.blog - /model/blog/admin/__init__.py,
AdminModel
> model.blog.admin - /model/blog/post.py,
PostModel
> model.blog.post - /model/blog/view.py,
ViewModel
> model.blog.view - /model/foo_bar.py,
FooBarModel
> model.foo_bar
Method Invoke rules¶
- /model/foo.py,
def func1(self)
: model.foo.func1() - /model/foo.py,
def func2(self, a)
: model.foo.func2(a) - /model/foo.py,
def func3(self, a, b)
: model.foo.func3(a, b)
-
class
dp_tornado.engine.model.
InValueModelConfig
(driver=None, database=None, host=None, user=None, password=None, port=None, path=None, pure=None)[source]¶ Bases:
object
-
class
dp_tornado.engine.model.
Model
(category=None, path_prefix=None)[source]¶ Bases:
dp_tornado.engine.engine.Engine
,dp_tornado.engine.loader.Loader
-
class
dp_tornado.engine.model.
ModelProxy
(connection, transaction)[source]¶ Bases:
object
-
connection
¶
-
transaction
¶
-
engine.view
— View of MVC¶
Warning
This package is not documented yet.
A view can be any output representation of information, such as a chart or a diagram. <Wikipedia>
Here is a view example:
from dp_tornado.engine.controller import Controller
class FooBarController(Controller):
def get(self):
self.render('index.html')
engine.controller
— Controller of MVC¶
Warning
This package is not documented yet.
Controller Accepts input and converts it to commands for the model or view. <Wikipedia>
Here is a foo_bar controller example:
from dp_tornado.engine.controller import Controller
class FooBarController(Controller):
def get(self):
self.finish('done')
Directory/File and URL Mapping rules¶
- /controller/__init__.py,
StarterController
> / - /controller/blog/__init__.py,
BlogController
> /blog - /controller/blog/admin/__init__.py,
AdminController
> /blog/admin - /controller/blog/post.py,
PostController
> /blog/post - /controller/blog/view.py,
ViewController
> /blog/view - /controller/foo_bar.py,
FooBarController
> /foo_bar
Class/Method and URL Mapping rules¶
- /controller/foo.py,
def get(self)
: GET /foo - /controller/foo.py,
def get(self, a)
: GET /foo/{path} - /controller/foo.py,
def get(self, a, b)
: GET /foo/{path}/{path} - /controller/foo.py,
def get(self, a, b=None)
: GET /foo/{path}/{path} and GET /foo/{path} - /controller/foo.py,
def post(self)
: POST /foo - /controller/foo.py,
def put(self)
: PUT /foo - /controller/foo.py,
def delete(self)
: DELETE /foo - /controller/foo.py,
def head(self)
: HEAD /foo - /controller/foo.py,
def patch(self)
: PATCH /foo
INI¶
Warning
This package is not documented yet.
Config¶
Warning
This package is not documented yet.
Here is a foo_bar config example:
from dp_tornado.engine.config import Config
class FooBarConfig(Config):
def index(self):
self.conf.value_str = 'string'
self.conf.value_int = 10
self.conf.value_dict = {
'foo': 100,
'bar': {
'baz': 200,
'baf': 300
}
}
self.conf.databases = {
'drv_sqlite_name': {
'driver': 'sqlite'
},
'drv_mysql_name': {
'driver': 'mysql+cymysql',
'database': '',
'host': '127.0.0.1',
'port': 3306,
'user': 'root',
'password': '',
'pool_size': 1,
'charset': 'utf8'
}
}
self.conf.caches = {
'drv_sqlite_name': {
'driver': 'memory',
'identifier': 'dp_test_sqlite'
},
'drv_redis_name': {
'driver': 'redis',
'host': '127.0.0.1',
'port': 6379,
'password': None,
'maxconn': 256
}
}
File/Class Invoke rules¶
- /config/__init__.py, DO NOT IMPLEMENT ANY CODE IN THIS FILE
- /config/blog/__init__.py,
BlogConfig
> config.blog - /config/blog/admin/__init__.py,
AdminConfig
> config.blog.admin - /config/blog/post.py,
PostConfig
> config.blog.post - /config/blog/view.py,
ViewConfig
> config.blog.view - /config/foo_bar.py,
FooBarConfig
> config.foo_bar
Config Invoke rules¶
- /config/foo_bar.py,
self.conf.value_str
: config.foo_bar.value_str == ‘string’ - /config/foo_bar.py,
self.conf.value_int
: config.foo_bar.value_int == 10 - /config/foo_bar.py,
self.conf.value_dict
: config.foo_bar.value_dict.foo == 100 - /config/foo_bar.py,
self.conf.value_dict
: config.foo_bar.value_dict.bar.baz == 200 - /config/foo_bar.py,
self.conf.value_dict
: config.foo_bar.value_dict.bar.baf == 300 - /config/foo_bar.py,
self.conf.databases
: ‘foo_bar/drv_sqlite_name’ - /config/foo_bar.py,
self.conf.databases
: ‘foo_bar/drv_mysql_name’ - /config/foo_bar.py,
self.conf.databases
: ‘foo_bar/drv_sqlite_name’ - /config/foo_bar.py,
self.conf.databases
: ‘foo_bar/drv_redis_name’
Helper¶
Here is a foo_bar helper example:
from dp_tornado.engine.helper import Helper as dpHelper
class FooHelper(dpHelper):
def func1(self):
"""
assert self.helper.foo.func1(10, 20) == None
"""
return None
def func2(self, a):
"""
assert self.helper.foo.func2(10) == 10
"""
return a
def func3(self, a, b):
"""
assert self.helper.foo.func3(10, 20) == 30
"""
return a + b
File/Class Invoke rules¶
- /helper/__init__.py, DO NOT IMPLEMENT ANY CODE IN THIS FILE
- /helper/blog/__init__.py,
BlogHelper
> helper.blog - /helper/blog/admin/__init__.py,
AdminHelper
> helper.blog.admin - /helper/blog/post.py,
PostHelper
> helper.blog.post - /helper/blog/view.py,
ViewHelper
> helper.blog.view - /helper/foo_bar.py,
FooBarHelper
> helper.foo_bar
Method Invoke rules¶
- /helper/foo.py,
def func1(self)
: helper.foo.func1() - /helper/foo.py,
def func2(self, a)
: helper.foo.func2(a) - /helper/foo.py,
def func3(self, a, b)
: helper.foo.func3(a, b)
helper.datetime
— Manipulate datetime¶
Warning
This package is not documented yet.
-
class
dp_tornado.helper.datetime.
DatetimeHelper
(category=None, path_prefix=None)[source]¶ Bases:
dp_tornado.engine.helper.Helper
helper.datetime.date
— Manipulate date¶
Warning
This package is not documented yet.
-
class
dp_tornado.helper.datetime.date.
DateHelper
(category=None, path_prefix=None)[source]¶ Bases:
dp_tornado.engine.helper.Helper
-
convert
(auto=None, datetime=None, timezone=None, timestamp=None, yyyymmdd=None, yyyymmddhhiiss=None, ms=False)[source]¶
-
helper.datetime.time
— Manipulate time¶
Warning
This package is not documented yet.
helper.datetime.timestamp
— Manipulate timestamp¶
Warning
This package is not documented yet.
-
class
dp_tornado.helper.datetime.timestamp.
TimestampHelper
(category=None, path_prefix=None)[source]¶ Bases:
dp_tornado.engine.helper.Helper
-
convert
(auto=None, datetime=None, timezone=None, timestamp=None, yyyymmdd=None, yyyymmddhhiiss=None, ms=False)[source]¶
-
helper.io
— IO¶
Warning
This package is not documented yet.
-
class
dp_tornado.helper.io.
IoHelper
(category=None, path_prefix=None)[source]¶ Bases:
dp_tornado.engine.helper.Helper
helper.io.file
— File¶
Warning
This package is not documented yet.
-
class
dp_tornado.helper.io.file.
FileHelper
(category=None, path_prefix=None)[source]¶ Bases:
dp_tornado.engine.helper.Helper
helper.io.file.zip
— ZIP¶
Warning
This package is not documented yet.
helper.io.path
— Path¶
Warning
This package is not documented yet.
helper.locale
— Localization¶
Warning
This package is not documented yet.
helper.misc
— Miscellaneous functions¶
Warning
This package is not documented yet.
-
class
dp_tornado.helper.misc.
MiscHelper
(category=None, path_prefix=None)[source]¶ Bases:
dp_tornado.engine.helper.Helper
helper.misc.performance
— Performance¶
Warning
This package is not documented yet.
helper.misc.system
— System¶
Warning
This package is not documented yet.
helper.misc.type
— Type¶
Warning
This package is not documented yet.
helper.numeric
— Manipulate numeric¶
-
class
dp_tornado.helper.numeric.
NumericHelper
(category=None, path_prefix=None)[source]¶ Bases:
dp_tornado.engine.helper.Helper
-
xxx
¶
-
helper.numeric.cast
— Type Cast¶
Warning
This package is not documented yet.
helper.security
— Security¶
Warning
This package is not documented yet.
-
class
dp_tornado.helper.security.
SecurityHelper
(category=None, path_prefix=None)[source]¶ Bases:
dp_tornado.engine.helper.Helper
helper.security.crypto
— Crypto¶
Warning
This package is not documented yet.
helper.serialization
— Serialization¶
Warning
This package is not documented yet.
-
class
dp_tornado.helper.serialization.
SerializationHelper
(category=None, path_prefix=None)[source]¶ Bases:
dp_tornado.engine.helper.Helper
helper.serialization.json
— JSON¶
Warning
This package is not documented yet.
helper.string
— Manipulate string¶
Warning
This package is not documented yet.
-
class
dp_tornado.helper.string.
StringHelper
(category=None, path_prefix=None)[source]¶ Bases:
dp_tornado.engine.helper.Helper
-
ascii_letters
¶
-
ascii_lowercase
¶
-
ascii_uppercase
¶
-
digits
¶
-
printable
¶
-
punctuation
¶
-
whitespace
¶
-
helper.string.cast
— Type Cast¶
Warning
This package is not documented yet.
helper.string.check
— Check¶
Warning
This package is not documented yet.
helper.string.random
— Random¶
Warning
This package is not documented yet.
helper.string.serialization
— Serialization¶
Warning
This package is not documented yet.
-
class
dp_tornado.helper.string.serialization.
SerializationHelper
(category=None, path_prefix=None)[source]¶ Bases:
dp_tornado.engine.helper.Helper
helper.string.serialization.json
— JSON¶
Warning
This package is not documented yet.
helper.validator
— Validators¶
Warning
This package is not documented yet.
helper.web
— Web¶
Warning
This package is not documented yet.
-
class
dp_tornado.helper.web.
WebHelper
(category=None, path_prefix=None)[source]¶ Bases:
dp_tornado.engine.helper.Helper
helper.web.aws
— AWS¶
Warning
This package is not documented yet.
-
class
dp_tornado.helper.web.aws.
AwsHelper
(category=None, path_prefix=None)[source]¶ Bases:
dp_tornado.engine.helper.Helper
helper.web.aws.dynamodb
— AWS DynamoDB¶
Warning
This package is not documented yet.
-
class
dp_tornado.helper.web.aws.dynamodb.
DynamodbHelper
(category=None, path_prefix=None)[source]¶ Bases:
dp_tornado.engine.helper.Helper
helper.web.aws.dynamodb.table
— AWS DynamoDB Table¶Warning
This package is not documented yet.
-
class
dp_tornado.helper.web.aws.dynamodb.table.
TableHelper
(category=None, path_prefix=None)[source]¶ Bases:
dp_tornado.engine.helper.Helper
-
create
(access_key_id, secret_access_key, region_name, table_name, index_columns, read_capacity_units=1, write_capacity_units=1, wait_until_exists=True, **kwargs)[source]¶
-
describe
(access_key_id, secret_access_key, region_name, table_name, wait_until_exists=True)[source]¶
-
helper.web.aws.dynamodb.table.column
— AWS DynamoDB Table - Column¶Warning
This package is not documented yet.
helper.web.aws.dynamodb.item
— AWS DynamoDB Item¶Warning
This package is not documented yet.
-
class
dp_tornado.helper.web.aws.dynamodb.item.
ItemHelper
(category=None, path_prefix=None)[source]¶ Bases:
dp_tornado.engine.helper.Helper
-
put
(access_key_id, secret_access_key, region_name, table_name, items, overwrite_by_pkeys=None)[source]¶
-
helper.web.aws.s3
— AWS S3¶
Warning
This package is not documented yet.
-
class
dp_tornado.helper.web.aws.s3.
S3Helper
(category=None, path_prefix=None)[source]¶ Bases:
dp_tornado.engine.helper.Helper
-
copy
(access_key_id, secret_access_key, region_name, src_bucket_name, src_key, dest_bucket_name, dest_key, copied_check=True)[source]¶
-
generate_presigned_post
(access_key_id, secret_access_key, region_name, bucket_name, key, success_action_redirect=None, content_length_range=None, max_content_length=None, expires_in=6000, acl=None)[source]¶
-
helper.web.email
— E-Mail¶
Warning
This package is not documented yet.
helper.web.form
— Form¶
Warning
This package is not documented yet.
helper.web.html
— HTML¶
Warning
This package is not documented yet.
helper.web.http
— HTTP¶
Warning
This package is not documented yet.
-
class
dp_tornado.helper.web.http.
HttpHelper
(category=None, path_prefix=None)[source]¶ Bases:
dp_tornado.engine.helper.Helper
-
request
(req_type, res_type, url, data=None, json=None, raise_exception=False, session=None, **kwargs)[source]¶
-
helper.web.http.get
— HTTP - Get¶
Warning
This package is not documented yet.
helper.web.http.post
— HTTP - Post¶
Warning
This package is not documented yet.
helper.web.http.put
— HTTP - Put¶
Warning
This package is not documented yet.
helper.web.http.delete
— HTTP - Delete¶
Warning
This package is not documented yet.
helper.web.http.patch
— HTTP - Patch¶
Warning
This package is not documented yet.
Scheduler¶
Warning
This package is not documented yet.
Schema¶
Schema module provides auto-generate relational database model as object based.
File/Class Invoke rules¶
- /schema/__init__.py, DO NOT IMPLEMENT ANY CODE IN THIS FILE
- /schema/blog/__init__.py,
BlogSchema
> schema.blog - /schema/blog/articles.py,
ArticlesSchema
> schema.blog.articles - /schema/blog/comments.py,
CommentsSchema
> schema.blog.comments
Table migration(create or alter) will be executed by invoking .migrate()
method manually.
For example, you can migrate comments
table with invoking schema.blog.comments.migrate()
method.
There are 2 types of schema. One is __dsn__ specifier and another one is table describer.
__init__.py
file of each directory must be __dsn__ specifier.
and child .py
files are must be table describer.
Here is a __dsn__ specifier example (/schema/blog/__init__.py):
from dp_tornado.engine.schema import Schema as dpSchema
class BlogSchema(dpSchema):
__dsn__ = 'tests.model_test/drv_mysql_test'
and Here is a table describer example (/schema/blog/comments.py):
from dp_tornado.engine.schema import Table as dpTable
from dp_tornado.engine.schema import Schema as dpSchema
from dp_tornado.engine.schema import Attribute as dpAttribute
class CommentsSchema(dpSchema):
__table_name__ = 'comments'
__engine__ = 'InnoDB'
__charset__ = 'utf8'
comment_id = dpAttribute.field(dpAttribute.DataType.BIGINT, ai=True, pk=True, nn=True, un=True, comment='Comment ID')
article_id = dpAttribute.field(dpAttribute.DataType.BIGINT, nn=True, un=True, comment='Parent Article ID')
author = dpAttribute.field(dpAttribute.DataType.VARCHAR(32), nn=True, comment='Author')
comment = dpAttribute.field(dpAttribute.DataType.VARCHAR(128), nn=True, comment='Comment')
primary_key = dpAttribute.index(dpAttribute.IndexType.PRIMARY, 'comment_id')
idx_articles_author_and_comment = dpAttribute.index(dpAttribute.IndexType.INDEX, ('author', 'comment'))
idx_articles_comment = dpAttribute.index(dpAttribute.IndexType.INDEX, 'comment')
fk_comments_article_id = dpAttribute.foreign_key(('article_id', dpSchema.field().blog.articles, 'article_id'))
Cache¶
Warning
This package is not documented yet.
M17n¶
Warning
This package is not documented yet.
Vars¶
Warning
This package is not documented yet.
Logging¶
Warning
This package is not documented yet.
Testing¶
Testing module provides unittest by pre-defined testing syntax. You can define testing syntax as method docstring, `.. test::` indicates the beginning of testing syntaxes.
Run Test:
$ dp4p test --path=./example
Available syntax:
- expect(criteria)
- expect(priority, rules)
- !expect(criteria)
- !expect(priority, rules)
rules:
controller:
code, text, json, args(arguments separated by /), params(query string, form values)
model, helper:
int, long, bool, str, json, args (arguments by list), kwargs (arguments by dict)
from dp_tornado.engine.controller import Controller as dpController
from dp_tornado.engine.model import Model as dpModel
from dp_tornado.engine.helper import Helper as dpHelper
class FooController(dpController):
def get(self):
"""
.. test::
expect(
1,
code=200,
text='foo==bar',
params={'foo': 'bar'})
"""
foo = self.get_argument('foo')
if foo == 'bar':
return self.finish('foo==bar')
self.finish('done')
def post(self):
"""
.. test::
expect(code=400, params={'foo': 'bar'})
"""
foo = self.get_argument('foo')
if foo == 'bar':
return self.finish_with_error(400)
self.finish('done')
class FooController(dpController):
def get(self):
"""
Test with params and expect json value.
.. test::
expect(json="{'foo':'bar'}", params={'foo': 'bar'})
"""
foo = self.get_argument('foo')
return self.finish({'foo': bar)
class FooController(dpController):
def get(self):
"""
Test with params and expect json value.
.. test::
expect(json={'foo':'bar'}, params={'foo': 'bar'})
"""
foo = self.get_argument('foo')
return self.finish({'foo': bar)
class FooModel(dpModel):
def foobar(self, foo):
"""
Test with kwargs arguments.
.. test::
expect(int=100, kwargs={'foo': 'bar'})
"""
if foo == 'bar':
return int(100)
return 0
class FooModel(dpModel):
def foobar(self, foo):
"""
Test with kwargs arguments.
.. test::
expect(long=100, kwargs={'foo': 'bar'})
expect(long=0, kwargs={'foo': 'foo'})
!expect(long=0, kwargs={'foo': 'bar'})
"""
if foo == 'bar':
if self.e.helper.misc.system.py_version <= 2:
return long(100)
else:
return int(100)
if self.e.helper.misc.system.py_version <= 2:
return long(0)
else:
return int(0)
class FooHelper(dpHelper):
def foobar(self, foo, bar):
"""
Test with args arguments.
.. test::
expect(bool=True, args={'foo': 'bar'})
!expect(bool=False, args={'foo': 'bar'})
"""
foo = self.get_argument('foo')
if foo == 'bar':
return True
return False