<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Management script."""
from __future__ import unicode_literals

import os
from glob import glob
from subprocess import call

from flask_migrate import MigrateCommand
from flask_script import Command, Manager, Option, Server, Shell
from flask_script.commands import Clean, ShowUrls

from p101stat.app import create_app
from p101stat.database import db
from p101stat.idol.models import Idol
from p101stat.settings import DevConfig, ProdConfig
from p101stat.utils import fetch_idol

CONFIG = ProdConfig if os.environ.get('P101STAT_ENV') == 'prod' else DevConfig
HERE = os.path.abspath(os.path.dirname(__file__))
TEST_PATH = os.path.join(HERE, 'tests')

app = create_app(CONFIG)
manager = Manager(app)


def _make_context():
    """Return context dict for a shell session so you can access app, db, and the Idol model by default."""
    return {'app': app, 'db': db, 'Idol': Idol}


@manager.command
def test():
    """Run the tests."""
    import pytest
    exit_code = pytest.main([TEST_PATH, '--verbose'])
    return exit_code


@manager.command
def update_idols():
    """Update db entries for all p101 idols."""
    for i in range(1, 102):
        idol_data = fetch_idol(i)
        if idol_data:
            idol = Idol.query.filter_by(idx=i).first()
            if idol:
                idol.vote_percentage = float(idol_data['per'])
            else:
                idol = Idol(idx=i, name_kr=idol_data['name'], age=int(idol_data['age']),
                            agency=idol_data['agency'], comment=idol_data['comment'],
                            vote_percentage=float(idol_data['per']))
                db.session.add(idol)
            db.session.commit()
    print 'Successfully fetched p101 idol data.'


class Lint(Command):
    """Lint and check code style with flake8 and isort."""

    def get_options(self):
        """Command line options."""
        return (
            Option('-f', '--fix-imports', action='store_true', dest='fix_imports', default=False,
                   help='Fix imports using isort, before linting'),
        )

    def run(self, fix_imports):
        """Run command."""
        skip = ['requirements']
        root_files = glob('*.py')
        root_directories = [name for name in next(os.walk('.'))[1] if not name.startswith('.')]<commit_msg>use crud functions for models<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Management script."""
from __future__ import unicode_literals

import os
from glob import glob
from subprocess import call

from flask_migrate import MigrateCommand
from flask_script import Command, Manager, Option, Server, Shell
from flask_script.commands import Clean, ShowUrls

from p101stat.app import create_app
from p101stat.database import db
from p101stat.idol.models import Idol
from p101stat.settings import DevConfig, ProdConfig
from p101stat.utils import fetch_idol

CONFIG = ProdConfig if os.environ.get('P101STAT_ENV') == 'prod' else DevConfig
HERE = os.path.abspath(os.path.dirname(__file__))
TEST_PATH = os.path.join(HERE, 'tests')

app = create_app(CONFIG)
manager = Manager(app)


def _make_context():
    """Return context dict for a shell session so you can access app, db, and the Idol model by default."""
    return {'app': app, 'db': db, 'Idol': Idol}


@manager.command
def test():
    """Run the tests."""
    import pytest
    exit_code = pytest.main([TEST_PATH, '--verbose'])
    return exit_code


@manager.command
def update_idols():
    """Update db entries for all p101 idols."""
    for i in range(1, 102):
        idol_data = fetch_idol(i)
        if idol_data:
            idol = Idol.query.filter_by(idx=i).first()
            if idol:
                idol.update(vote_percentage=float(idol_data['per']))
            else:
                Idol.create(idx=i, name_kr=idol_data['name'], age=int(idol_data['age']),
                            agency=idol_data['agency'], comment=idol_data['comment'],
                            vote_percentage=float(idol_data['per']))
    print 'Successfully fetched p101 idol data.'


class Lint(Command):
    """Lint and check code style with flake8 and isort."""

    def get_options(self):
        """Command line options."""
        return (
            Option('-f', '--fix-imports', action='store_true', dest='fix_imports', default=False,
                   help='Fix imports using isort, before linting'),
        )

    def run(self, fix_imports):
        """Run command."""
        skip = ['requirements']
        root_files = glob('*.py')
        root_directories = [name for name in next(os.walk('.'))[1] if not name.startswith('.')]
{'fraction_non_alphanumeric': 0.1267454350161117, 'fraction_numerical': 0.013319011815252416, 'mean_word_length': 3.048695652173913, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '279279', 'n_tokens_mistral': 1484, 'n_tokens_neox': 1419, 'n_words': 403}