Skip to content

Commit

Permalink
Merge pull request #1 from yoshiya0503/development
Browse files Browse the repository at this point in the history
[WIP] add base scaffold
  • Loading branch information
yoshiya0503 authored May 29, 2018
2 parents 428d983 + 1b55ec3 commit da31f0e
Show file tree
Hide file tree
Showing 23 changed files with 1,345 additions and 164 deletions.
45 changes: 45 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
version: 2
jobs:
pypi:
docker:
- image: circleci/python:3.6.1
working_directory: ~/repo

steps:
- checkout

# Download and cache dependencies
- restore_cache:
keys:
- v1-dependencies-{{ checksum "requirements.txt" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-

- run:
name: install dependencies
command: |
python3 -m venv venv
. venv/bin/activate
pip install wheel twine
- save_cache:
paths:
- ./venv
key: v1-dependencies-{{ checksum "requirements.txt" }}

- run:
name: run build
command: |
echo 'dry run'
#. venv/bin/activate
#python setup.py sdist bdist_wheel
#twine upload --repository pypi dist/*
workflows:
version: 2
release:
jobs:
- pypi:
filters:
branches:
only: master
149 changes: 145 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,146 @@
# Flask-Best-Practices
このリポジトリはFlaskのベストプラクティス、実施的なテクニックを紹介するリポジトリです。
以下の URL で色々確認できます。
# Hermetica

https://github.com/yoshiya0503/Flask-Best-Practices/wiki

[![CircleCI](https://circleci.com/gh/yoshiya0503/Hermetica.svg?style=shield&circle-token=4614abf3b106e5f31f9726ebaedfcebc5c7fa859)](https://circleci.com/gh/yoshiya0503/Hermetica)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](CONTRIBUTING.md#pull-requests)
[![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](LICENSE)

---

THIS IS NOT WEB FRAMEWORK TO REPLACE FLASK.

Hermetica is scaffold tools, and wiki to implement better flask applications.

When we try to build web applications by using flask web framework, there are to many patterns and practices.
This diversity make it difficult to implement apps that have simple architecture.
In other words, because of too many manners, options, and patters, to implement more bigger applications is not so easy.
(but, to implement small app by using flask is extreamly easy)

Therefor, we try to implement the scaffold tools head for better architecture applications as mach as possible
based on our many experiences.

* better and common directory structure
* scaffold to create tipycal API, model.
* select powerful packages(like SQLAlchemy Nose)

# Installation

We dare to support only python 3.x, because python 2.x will eventually deprecated almost all systems, and we have to get used to python 3.x quickly.

```
pip install hermetica
```

# Usage

### Overview the usage

hermetica has some subcommands, to create scaffold api, decorator, model.

* api (url and routing method base or class base or flask-restful)
* model (database models, sqlalchemy or mongoengine)
* decorator (you can insert some code before enter the api, like a 'authentication')

```
→ hermetica --help
Usage: hermetica [OPTIONS] COMMAND [ARGS]...
Options:
--help Show this message and exit.
Commands:
api create api
decorator create decorator
init initialize your flask app
model create model
```

### initialize your flask project.

```
→ hermetica init --help
Usage: hermetica init [OPTIONS]
initialize your flask app
Options:
--api [restful|decorator|class]
Flask-Restful or Flask decorator or
methodview
--db [sqlalchemy|mongoengine] SQLAlchemy or Mongoengine or None
--decorator create decorator or None
--redis using Redis or None
--docker using container
--help Show this message and exit.
```

After create project scaffold, you will check `Pipfile` contents, if there are shortages in list of packages, you can
add other packages into `Pipfile`, and lock your package.
(We recommend you to use `pipenv` https://github.com/pypa/pipenv)

Hermetica support docker. you can see `Dockerfile` and `docker-compose.yml` at your root of project.
We recommend you to use docker-compose, it will helpful to separate from other projects.

```
pipenv lock
# if you set docker option, you can up the app container
docker-compose build
docker-compose up
```

### add api to your flask project.

```
→ hermetica api --help
Usage: hermetica api [OPTIONS] NAME
create api
Options:
--api [restful|decorator|class]
Flask-Restful or Flask decorator or
methodview
--version TEXT API version
--help Show this message and exit.
```

### add model to your flask project.

```
→ hermetica model --help
Usage: hermetica model [OPTIONS] NAME
create model
Options:
--db [sqlalchemy|mongoengine] SQLAlchemy or Mongoengine or None
--help Show this message and exit.
```

### add decorator to your flask project.

```
→ hermetica decorator --help
Usage: hermetica decorator [OPTIONS] NAME
create decorator
Options:
--help Show this message and exit.
```

# Development

This repos is too young, so we provide few useful features yet.
we will grad if you send PRs...

# See Before Wiki (Flask Best Practices)

Why we apply broken change? Because, before repo source code is slightly trivial,
and we believe this change will not cause any negative impact to others.

To create scaffold tools for flask will cause good affect the world rather than remain trivial code.
But there are no warries. Flask-best-Practices contents (wiki docs) remain here (but only for japanese).

https://github.com/yoshiya0503/Hermetica/wiki
File renamed without changes.
128 changes: 128 additions & 0 deletions hermetica/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#! /usr/bin/env python3
# -*- encoding: utf-8 -*-
"""
main.py
hermetica main script
"""
__author__ = 'Yoshiya Ito <myon53@gmail.com>'
__version__ = '1.0.0'
__date__ = '2018-04-24'
import os
import click
from hermetica.scaffold.app import App
from hermetica.scaffold.config import Config
from hermetica.scaffold.wsgi import WSGI
from hermetica.scaffold.api import API
from hermetica.scaffold.model import Model
from hermetica.scaffold.test import Test
from hermetica.scaffold.docker import Docker
from hermetica.scaffold.extension import Extension
from hermetica.scaffold.pipfile import Pipfile
from hermetica.scaffold.decorator import Decorator

@click.group()
def main():
pass

@main.command()
@click.option('--api', default='restful', type=click.Choice(['restful', 'decorator', 'class']), help='Flask-Restful or Flask decorator or methodview')
@click.option('--db', default=None, type=click.Choice(['sqlalchemy', 'mongoengine']), help='SQLAlchemy or Mongoengine or None')
@click.option('--decorator', default=False, is_flag=True, help='create decorator or None')
@click.option('--redis', default=False, is_flag=True, flag_value='redis', help='using Redis or None')
@click.option('--docker', default=False, is_flag=True, flag_value='docker', help='using container')
def init(api, db, decorator, redis, docker):
""" initialize your flask app
"""
dirs = ['./app/', './test/', './config/', './app/api/v1/', './app/models/']
for dir in dirs:
if os.path.exists(os.path.dirname(dir)):
click.echo('[WARNING] directory {} is already exists, skip to create this directory'.format(dir))
continue
os.makedirs(os.path.dirname(dir))

app = App(db=db, redis=redis, api=api)
pipfile = Pipfile(db=db, redis=redis)
wsgi = WSGI(db=db)
config = Config(db=db, redis=redis)
extension = Extension(db=db, redis=redis)
test = Test(db=db, name='root')
api = API(api=api, name='root')
decorator = Decorator(name='root')

with open('./Pipfile', 'w') as f:
f.write(pipfile.create_pipfile())
with open('wsgi.py', 'w') as f:
f.write(wsgi.create_wsgi())

with open('app/__init__.py', 'w') as f:
f.write(app.create_app__init__())
with open('app/extensions.py', 'w') as f:
f.write(extension.create_extensions())
if decorator:
with open('app/decorators.py', 'w') as f:
f.write(decorator.create_decorators())

with open('config/__init__.py', 'w') as f:
f.write(config.create_config(name='config', env='test'))
with open('config/development.py', 'w') as f:
f.write(config.create_config(name='development', env='development'))
with open('config/production.py', 'w') as f:
f.write(config.create_config(name='production', env='production'))

if docker:
docker = Docker(db=db, redis=redis)
with open('Dockerfile', 'w') as f:
f.write(docker.create_dockerfile())
with open('docker-compose.yml', 'w') as f:
f.write(docker.create_docker_compose_yml())

with open('test/__init__.py', 'w') as f:
f.write(test.create__init__())
with open('nose.cfg', 'w') as f:
f.write(test.create_nose_cfg())

with open('app/api/__init__.py', 'w') as f:
f.write(api.create__init__())
with open('app/api/v1/__init__.py', 'w') as f:
pass
with open('app/api/v1/root.py', 'w') as f:
f.write(api.create_api())

if db:
model = Model(db=db, name='root')
with open('app/models/__init__.py', 'w') as f:
f.write(model.create__init__())
with open('app/models/root.py', 'w') as f:
f.write(model.create_model())

@main.command()
@click.argument('name', type=str, required=True)
@click.option('--api', default='restful', type=click.Choice(['restful', 'decorator', 'class']), help='Flask-Restful or Flask decorator or methodview')
@click.option('--version', default='v1', help='API version')
def api(name, api, version):
""" create api
"""
path = 'app/api/{}/{}.py'.format(version, name)
api = API(api=api, name=name)
with open(path, 'w') as f:
f.write(api.create_api())

@main.command()
@click.argument('name', type=str, required=True)
@click.option('--db', default='sqlalchemy', type=click.Choice(['sqlalchemy', 'mongoengine']), help='SQLAlchemy or Mongoengine or None')
def model(name, db):
""" create model
"""
path = 'app/models/{}.py'.format(name)
model = Model(db=db, name=name)
with open(path, 'w') as f:
f.write(model.create_model())

@main.command()
@click.argument('name', type=str, required=True)
def decorator(name):
""" create decorator
"""
decorator = Decorator(name=name)
with open('app/decorators.py', 'a') as f:
f.write(decorator.create_decorator())
18 changes: 18 additions & 0 deletions hermetica/scaffold/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#! /usr/bin/env python3
# -*- encoding: utf-8 -*-
"""
__init__.py
Scaffold Abstract
"""
__author__ = 'Yoshiya Ito <myon53@gmail.com>'
__version__ = '1.0.0'
__date__ = '2018-04-27'


class Scaffold(object):
def __init__(self, filepath):
self.filepath = filepath

def write(self, source_code):
with open(self.filepath, 'w') as py:
py.write(source_code)
Loading

0 comments on commit da31f0e

Please sign in to comment.