Skip to content

Commit

Permalink
delete sql, add --sql/config
Browse files Browse the repository at this point in the history
  • Loading branch information
misakar committed Oct 30, 2015
1 parent d08823b commit efb4a42
Show file tree
Hide file tree
Showing 44 changed files with 212 additions and 78 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.pyo
venv
-1
mana.egg-info
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ ex:
2015-1029: add mana deploy to deploy flask app on wsgi server
----------------------------------------------------
2015-1030: fix ISSUE #3 && happy🍺agenerate flask project
----------------------------------------------------
2015-1030: delete sql and add --sql --config to init



Expand Down
13 changes: 0 additions & 13 deletions examples/GoodIdea/app/book/__init__.py

This file was deleted.

13 changes: 0 additions & 13 deletions examples/GoodIdea/app/test/__init__.py

This file was deleted.

7 changes: 0 additions & 7 deletions examples/GoodIdea/wsgi.py

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
# coding: utf-8
"""
~~~~~~~
~~~~~~~~
"""

from flask import Flask
from config import config


app = Flask(__name__)


app.config.from_object(config['default'])


from . import views, models, forms
app.register_blueprint('book')
app.register_blueprint('test', url_prefix='/test')
File renamed without changes.
File renamed without changes.
47 changes: 47 additions & 0 deletions examples/init_cfg_project/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# coding: utf-8
"""
config.py
~~~~~~~~~
配置文件
"""

import os
basedir = os.path.abspath(os.path.dirname(__file__))


class Config:
"""基本配置类"""
SECRET_KEY = os.environ.get('SECRET_KEY') or 'hard to guess string'

@staticmethod
def init_app(app):
pass


class DevelopmentConfig(Config):
"""开发环境配置类"""
DEBUG = True


class TestingConfig(Config):
"""测试环境配置类"""
TESTING = True
WTF_CSRF_ENABLED = False


class ProductionConfig(Config):
"""生产环境配置类"""

@classmethod
def init_app(cls, app):
Config.init_app(app)


config = {
'development': DevelopmentConfig,
'testing': TestingConfig,
'production': ProductionConfig,

'default': DevelopmentConfig
}
File renamed without changes.
File renamed without changes.
15 changes: 15 additions & 0 deletions examples/init_project/app/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# coding: utf-8
"""
~~~~~~~
"""

from flask import Flask


app = Flask(__name__)
app.config["SECRET_KEY"] = "I love mana!" # you can change it :)


from . import views, forms
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
23 changes: 23 additions & 0 deletions examples/init_sql_config/app/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# coding: utf-8
"""
~~~~~~~
"""

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from config import config


app = Flask(__name__)


app.config.from_object(config['default'])


db = SQLAlchemy(app)


from . import views, models, forms
app.register_blueprint('share', url_prefix='/share')
Binary file added examples/init_sql_config/app/__init__.pyc
Binary file not shown.
File renamed without changes.
Binary file added examples/init_sql_config/app/forms.pyc
Binary file not shown.
File renamed without changes.
Binary file added examples/init_sql_config/app/models.pyc
Binary file not shown.
File renamed without changes.
Empty file.
Empty file.
Empty file.
Binary file added examples/init_sql_config/app/views.pyc
Binary file not shown.
File renamed without changes.
Binary file added examples/init_sql_config/config.pyc
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def make_shell_context():


# 后台数据库管理界面
admin.add_view(ModelView([models], db.session))
# admin.add_view(ModelView([models], db.session))


@manager.command
Expand Down
Empty file.
Empty file added examples/project/README.md
Empty file.
15 changes: 15 additions & 0 deletions examples/project/app/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# coding: utf-8
"""
~~~~~~~
"""

from flask import Flask


app = Flask(__name__)
app.config["SECRET_KEY"] = "I love mana!" # you can change it :)


from . import views, forms
Binary file added examples/project/app/__init__.pyc
Binary file not shown.
Empty file added examples/project/app/forms.py
Empty file.
Binary file added examples/project/app/forms.pyc
Binary file not shown.
Empty file added examples/project/app/views.py
Empty file.
Binary file added examples/project/app/views.pyc
Binary file not shown.
47 changes: 47 additions & 0 deletions examples/project/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# coding: utf-8

import sys
from flask.ext.script import Manager, Shell
from flask.ext.migrate import Migrate, MigrateCommand
from flask.ext.admin import Admin
from flask.ext.admin.contrib.sqla import ModelView
from app import app, db


# 编码设置
reload(sys)
sys.setdefaultencoding('utf-8')


manager = Manager(app)
migrate = Migrate(app, db)
admin = Admin(app, name="")


def make_shell_context():
"""自动加载环境"""
return dict(
app = app,
db = db
)


manager.add_command("shell", Shell(make_context=make_shell_context))
manager.add_command('db', MigrateCommand)


# 后台数据库管理界面
# admin.add_view(ModelView([models], db.session))


@manager.command
def test():
"""运行测试"""
import unittest
tests = unittest.TestLoader().discover('test')
unittest.TextTestRunner(verbosity=2).run(tests)


if __name__ == '__main__':
app.debug = True
manager.run()
Empty file.
63 changes: 35 additions & 28 deletions mana/mana.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
:version 1.0
mana init project_name # init your project
--config # create config.py for dev test product environment
--sql # integrate with flask-sqlalchemy
mana install # install your flask extensions
--venv # with virtualenv
mana sql project_name # integrate with flask-sqlalchemy
mana manage project_name # create manage.py to manage the project
:version 2.0
Expand All @@ -28,7 +29,8 @@
import click
import os
# import code templates
from templates._base import _init_head_py, _init_middle_py, _init_tail_py, _init_blue_py, _init_sql_py
# from templates._base import _init_head_py, _init_middle_py, _init_tail_py, _init_blue_py, _init_sql_py
from templates._base import _init_py, _init_sql_py, _init_config_py
from templates._config import _config_sql_py, _config_py
from templates._sql import _sql_py
from templates._management import _management_py
Expand Down Expand Up @@ -102,7 +104,9 @@ def cli():

@click.command()
@click.argument('project_name')
def init(project_name):
@click.option('--sql', default=False, help="integrate with flask-sqlalchemy")
@click.option('--config', default=False, help="create config.py for dev product test environment")
def init(project_name, sql, config):
"""
init your project
"""
Expand All @@ -114,23 +118,39 @@ def init(project_name):

# 在python中执行shell命令
os.system("mkdir %s" % project_name)
os.system("touch %s/README.md %s/config.py %s/requirement.txt" \
% make_tuple(project_name, 3))

if config:
os.system("touch %s/config.py" % project_name)
os.system("touch %s/README.md %s/requirement.txt" \
% make_tuple(project_name, 2))
os.system("mkdir %s/app/ %s/test/" \
% make_tuple(project_name, 2))
os.system("touch %s/app/__init__.py %s/app/models.py %s/app/views.py %s/app/forms.py" % \
make_tuple(project_name, 4))

if sql:
os.system("touch %s/app/models.py" % project_name)
os.system("touch %s/app/__init__.py %s/app/views.py %s/app/forms.py" % \
make_tuple(project_name, 3))
os.system("mkdir %s/app/templates/ %s/app/static/" % \
make_tuple(project_name, 2))
os.system("cd ..")

# happy coding
# 调用 fill_file 函数
# 初始化的时候调用模版预填代码
fill_file_w(project_name, 'config.py', _config_py)
fill_file_w(project_name, 'app/__init__.py', (_init_head_py + _init_middle_py + _init_blue_py))
if config:
fill_file_w(project_name, 'app/__init__.py', _init_config_py)
fill_file_w(project_name, 'config.py', _config_py)

elif sql:
fill_file_w(project_name, 'app/__init__.py', _init_sql_py)
fill_file_w(project_name, 'config.py', _config_sql_py)
fill_file_w(project_name, 'app/models.py', _sql_py)
# 调用mana命令
os.system("mana manage %s" % project_name)
else:
fill_file_w(project_name, 'app/__init__.py', _init_py)

click.echo("init ... done!")
click.echo("init ... done!🍺 ")


@click.command()
Expand Down Expand Up @@ -158,21 +178,7 @@ def install(venv):
click.echo("install extensions")
# use sudo
os.system("sudo pip install -r requirement.txt")
click.echo("install ... done!")


@click.command()
@click.argument('project_name')
def sql(project_name):
"""
integrate flask-sqlalchemy
"""
# 自动集成flask-sqlalchemy扩展
# :param project_name 项目的名称
fill_file_w(project_name, 'config.py', _config_sql_py)
fill_file_w(project_name, 'app/__init__.py', _init_sql_py)
fill_file_w(project_name, 'app/models.py', _sql_py)
click.echo("integrate flask-sqlalchemy ... done!")
click.echo("install ... done!🍺 ")


@click.command()
Expand All @@ -184,7 +190,7 @@ def manage(project_name):
# 创建 manage.py 文件
# 调用 fill_file 函数
fill_file_w(project_name, 'manage.py', _management_py)
click.echo("create ... done!")
click.echo("manage... done! 🍺 ")


""":version 2.0"""
Expand Down Expand Up @@ -224,7 +230,7 @@ def blue(project_name, blueprint_name, prefix):
# open:app::__init__.py 直接在蓝图注册区写入
fill_file_r(project_name, 'app/__init__.py', blue_code)
# ...done !
click.echo("create ... done!")
click.echo("blueprint... done!🍺 ")


""":version 2.1"""
Expand All @@ -238,12 +244,13 @@ def deploy(project_name, host, port):
os.system("cd %s && touch wsgi.py" % project_name)
fill_file_w(project_name, 'wsgi.py', _wsgi_py % (host, port))

click.echo("deploy wsgi...done!🍺 ")


###########################
# mana command set
cli.add_command(init)
cli.add_command(install)
cli.add_command(sql)
cli.add_command(manage)
cli.add_command(blue)
cli.add_command(deploy)
Expand Down
Loading

0 comments on commit efb4a42

Please sign in to comment.