#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import absolute_import, print_function

import os

import click
from flask_migrate import Migrate, MigrateCommand
from flask.cli import FlaskGroup
from livereload import Server
import yaml

from flask_toolbox.crawler.celery import celery_app
from flask_toolbox.crawler.worker import update_pypi_info, update_github_info
from flask_toolbox.app import create_app
from flask_toolbox.configs import ProductionConfig, DevelopmentConfig
from flask_toolbox.extensions import db
from flask_toolbox.models import Category, Package


CONFIG = (ProductionConfig if os.environ.get('FLASK_APP_ENV') == 'production'
          else DevelopmentConfig)
app = create_app(CONFIG)
app.app_context().push()
migrate = Migrate(app, db)


@app.cli.command()
def live():
    """"Set up a liveload server."""
    server = Server(app.wsgi_app)
    server.serve(port=5000)


@app.cli.command()
def init_db():
    """Initialize the database."""
    with app.app_context():
        db.drop_all()
        db.create_all()
    print('Init the database.')


@app.cli.command()
def init_data():
    """Seed the database with packages.yml"""
    with open('packages.yml') as f:
        data = yaml.load(f)

    for category_name, category_info in data['categories'].items():
        new_category = Category(
            name=category_name,
            description=category_info['description']
        )
        db.session.add(new_category)
        db.session.commit()

        for package in category_info['packages']:
            package_info = data['packages'][package]
            new_package = Package(
                category_id=new_category.id,
                name=package,
                description=package_info['description'],
                pypi_url=package_info['pypi_url'],
                documentation_url=package_info['documentation_url'],
                source_code_url=package_info['source_code_url'],
                bug_tracker_url=package_info['bug_tracker_url'],
            )
            db.session.add(new_package)
            db.session.commit()


@app.cli.command()
def update_data():
    """Crawl package's github and PyPI info."""
    print('Update PyPI info...')
    update_pypi_info()
    print('Update Github info...')
    update_github_info()
    print('Done.')

@@ -1508,16 +1508,21 @@
  package
+_name
  in cate
@@ -1595,16 +1595,21 @@
 %5Bpackage
+_name
 %5D%0A      
@@ -1710,16 +1710,21 @@
 =package
+_name
 ,%0A      
@@ -2078,16 +2078,16 @@
 ackage)%0A
-
         
@@ -2108,24 +2108,1520 @@
 .commit()%0A%0A%0A
+@app.cli.command()%0Adef sync_data():%0A    %22%22%22Sync the database with packages.yml%22%22%22%0A    with open('packages.yml') as f:%0A        data = yaml.load(f)%0A%0A    for category_name, category_info in data%5B'categories'%5D.items():%0A        category = Category.query.filter_by(name=category_name).first()%0A        if category:%0A            category.description = category_info%5B'description'%5D%0A        else:%0A            category = Category(%0A                name=category_name,%0A                description=category_info%5B'description'%5D%0A            )%0A        db.session.add(category)%0A%0A        for package_name in category_info%5B'packages'%5D:%0A            package_info = data%5B'packages'%5D%5Bpackage_name%5D%0A            package = Package.query.filter_by(name=package_name).first()%0A            if package:%0A                package_info.update(%7B'name': package_name, 'category_id': category.id%7D)%0A                db.session.query(Package).filter(Package.id==package.id).update(package_info)%0A            else:%0A                package = Package(%0A                    category_id=category.id,%0A                    name=package_name,%0A                    description=package_info%5B'description'%5D,%0A                    pypi_url=package_info%5B'pypi_url'%5D,%0A                    documentation_url=package_info%5B'documentation_url'%5D,%0A                    source_code_url=package_info%5B'source_code_url'%5D,%0A                    bug_tracker_url=package_info%5B'bug_tracker_url'%5D,%0A                )%0A            db.session.add(package)%0A    db.session.commit()%0A%0A%0A
 @app.cli.com

Add a simple function to sync data, close #31