adama / src / adama / orders / create_order.py

# -*- coding: utf-8 -*-

"""Create Order order
"""

import os

from ..exceptions import OrderError
from ..commandment import BaseOrder
from . import get_template, get_module, get_command, touch


class Order(BaseOrder):
    """Creates an order for your application that will be launch with a command
as a subcommand

Arguments:
  module    python module that contains or will contain the orders module
  name      name of the order
    """

    options = BaseOrder.options + ()

    description = __doc__.split('\n')[0].lower()
    args = "module name"

    def __init__(self):
        super(Order, self).__init__('adama', command='adama')

    def execute(self, *args, **options):
        if len(args) != 2:
            raise OrderError(
                'The create_order order has two required arguments', self)

        # adds a path to pythonpath if options has been selected
        # and if it is not already there and returns a module
        try:
            module = get_module(args[0], options['pythonpath'])
        except ImportError as bad_import:
            raise OrderError(str(bad_import), self)
        name = args[1]

        # Constructs, searches and creates the orders path
        orders_path = os.path.join(module.__path__[0], 'orders')
        if not os.path.isdir(orders_path):
            os.mkdir(orders_path)
            # Makes order a python module
            touch(os.path.join(orders_path, '__init__.py'))

        # Defines the command name
        command_name = get_command(options['name'], module)

        # Defines the order filename
        name = name if os.path.splitext(name)[1] == '.py' \
            else '{0}.py'.format(name)
        order_path = os.path.join(orders_path, name)

        # Writes in file
        with open(order_path, "w") as order:
            order.write(get_template('order')\
                .format(module.__name__, command_name))

        return 0
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.