Exemplo n.º 1
0
def blueprint(ctx, name, mode):
    """Generate an application component (blueprint)."""
    # Convert the name to CamelCase for use with class names
    name_camelcase = camelcase(name)

    # Generation of items can only run in a valid project directory
    if not valid_project_directory():
        ctx.fail(
            'You can only run the generate command from a valid project '
            'directory'
        )

    click.echo()
    click.echo('Generating new blueprint named %s:' % name)
    click.echo()
    scaffold = Scaffold(
        source_root=[
            os.path.join(TEMPLATE_DIR, 'asset'),
            os.path.join(TEMPLATE_DIR, 'blueprint'),
        ],
        target_root=os.getcwd(),
        variables={'name': name, 'name_camelcase': name_camelcase},
        ignored_dirs=IGNORED_DIRS, ignored_files=IGNORED_FILES,
        overwrite_target_root=True, existing_policy=mode
    )
    scaffold.render_structure()
    click.echo()
    click.echo('Steps required to activate the new blueprint:')
    click.echo()
    click.echo('  Add the blueprint import to app/__init__.py in the '
               'configure_blueprints function')
    click.echo()
    click.echo('  from .views import %s_view' % name)
    click.echo('  app.register_blueprint(%s_view.mod)' % name)
    click.echo()
Exemplo n.º 2
0
def new(ctx, project_name, mode):
    """Create a new Flaskage project."""
    # Unpack the project directory and name
    name, directory = project_name

    # Convert the name to CamelCase for use with class names
    name_camelcase = camelcase(name)

    # Generation of a new project can only run outside a valid project
    # directory
    if valid_project_directory(os.path.dirname(directory)):
        ctx.fail('You cannot create a new project inside a project directory')

    click.echo()
    click.echo('Generating new project %s:' % name)
    click.echo()
    scaffold = Scaffold(
        source_root=os.path.join(TEMPLATE_DIR, 'project'),
        target_root=directory,
        variables={'name': name, 'name_camelcase': name_camelcase},
        ignored_dirs=IGNORED_DIRS, ignored_files=IGNORED_FILES,
        overwrite_target_root=True, existing_policy=mode
    )
    scaffold.render_structure()
    click.echo()
    click.echo('Getting started with your project:')
    click.echo()
    click.echo('  1. Change into the new project directory')
    click.echo('     cd %s' % directory)
    click.echo()
    click.echo('  2. Install all client-side components using Bower')
    click.echo('     bower install')
    click.echo()
    click.echo('  3. Install all server-side dependencies using pip')
    click.echo('     pip install -r requirements/development.txt')
    click.echo()
    click.echo('  4. Start up the development web server')
    click.echo('     ./manage.py server')
    click.echo()
Exemplo n.º 3
0
def library(ctx, name, mode):
    """Generate an application-agnostic library."""
    # Convert the name to CamelCase for use with class names
    name_camelcase = camelcase(name)

    # Generation of items can only run in a valid project directory
    if not valid_project_directory():
        ctx.fail(
            'You can only run the generate command from a valid project '
            'directory'
        )

    click.echo()
    click.echo('Generating new library named %s:' % name)
    click.echo()
    scaffold = Scaffold(
        source_root=os.path.join(TEMPLATE_DIR, 'lib'),
        target_root=os.getcwd(),
        variables={'name': name, 'name_camelcase': name_camelcase},
        ignored_dirs=IGNORED_DIRS, ignored_files=IGNORED_FILES,
        overwrite_target_root=True, existing_policy=mode
    )
    scaffold.render_structure()
    click.echo()
Exemplo n.º 4
0
def model(ctx, name, columns, mode):
    """Generate a database model using a given name. You may also specify the
    columns you need following the model name using the format:

    <name>[:<type>[,<length>][:<modifier>,<modifier>...]]

    e.g.

    flaskage g model user email:string:primary name:string,80:index:required

    The following types are listed below along with their corresponding
    SQLAlchemy mapping:

    \b
    Numeric Types:
    - integer (or int): Integer
    - decimal: Numeric
    - float: Float

    \b
    Text Types:
    - string (or str): String
    - text: Text

    \b
    Date & Time Types:
    - date: Date
    - time: Time
    - datetime: DateTime

    \b
    Other Types:
    - binary (or bin): LargeBinary
    - boolean (or bool): Boolean

    The string, text and binary types also accept an optional length.

    \b
    The column modifiers available are:
    - index
    - primary
    - required
    - unique

    If no primary key is specified, a primary key integer column named id
    will be created for you.
    """
    # Convert the name to CamelCase for use with class names
    name_camelcase = camelcase(name)

    # Generation of items can only run in a valid project directory
    if not valid_project_directory():
        ctx.fail(
            'You can only run the generate command from a valid project '
            'directory'
        )

    # Generate the Python code required for each column (this is too
    # tedious to do in templates)
    primary_key_provided = False
    column_model_definitions = []
    column_factory_definitions = []

    for column_name, type, length, modifiers in columns:
        # Generate the type and its size (if applicable)
        model_definition = 'db.%s' % COLUMN_TYPE_MAPPING[type]
        if length:
            model_definition += '(%i)' % length

        # Generate modifiers (primary key, index .etc)
        for modifier in modifiers:
            model_definition += ', %s' % COLUMN_MODIFIER_MAPPING[modifier]
            if modifier == COLUMN_MODIFIER_PRIMARY_KEY:
                primary_key_provided = True

        # Add the model column definition to our list
        column_model_definitions.append((column_name, model_definition))

        # Generate the model factory fakers
        factory_definition = None
        if type in COLUMN_FACTORY_MAPPING:
            if column_name in COLUMN_FACTORY_MAPPING[type]:
                factory_definition = COLUMN_FACTORY_MAPPING[type][column_name]
            elif '*' in COLUMN_FACTORY_MAPPING[type]:
                factory_definition = COLUMN_FACTORY_MAPPING[type]['*']

        # Add the factory column definition to our list
        if factory_definition:
            column_factory_definitions.append(
                (column_name, factory_definition)
            )

    click.echo()
    click.echo('Generating new model named %s:' % name)
    click.echo()
    scaffold = Scaffold(
        source_root=os.path.join(TEMPLATE_DIR, 'model'),
        target_root=os.getcwd(),
        variables={
            'name': name, 'name_camelcase': name_camelcase,
            'column_model_definitions': column_model_definitions,
            'primary_key_provided': primary_key_provided,
            'column_factory_definitions': column_factory_definitions
        },
        ignored_dirs=IGNORED_DIRS, ignored_files=IGNORED_FILES,
        overwrite_target_root=True, existing_policy=mode
    )
    scaffold.render_structure()
    click.echo()
    click.echo('Steps required to activate the new model:')
    click.echo()
    click.echo('  1. Add the model import to app/models/__init__.py')
    click.echo('     from .%s import %s  # noqa' % (name, name_camelcase))
    click.echo()
    click.echo('  2. Add the factory import to test/factories/__init__.py')
    click.echo('     from .%s_factory import %sFactory  # noqa' %
               (name, name_camelcase))
    click.echo()
    click.echo('  3. Generate a migration to add the new model to your '
               'database')
    click.echo('     ./manage.py db migrate')
    click.echo()
    click.echo('  4. Apply the migration')
    click.echo('     ./manage.py db upgrade')
    click.echo()
Exemplo n.º 5
0
def test_camelcase_leading_underscore():
    assert camelcase('_leading_underscore') == '_LeadingUnderscore'
Exemplo n.º 6
0
def test_camelcase():
    assert camelcase('hello_there_mate') == 'HelloThereMate'