Arnaud Grausem avatar Arnaud Grausem committed f6ec0e5

Feature : launching an order inside a python script using the API

Comments (0)

Files changed (7)

     make_option('--pythonpath', action='store', type='string', dest='pythonpath',
         metavar='PYTHONPATH',
         help='A directory to add to the Python path, e.g. "/path/to/myproject"'),
+    make_option('-n', '--name', action='store', type='string', dest='name',
+        metavar='NAME', help='name of the command [Default: module\'s name]'),
 )
 
 if __name__ == '__main__':

src/adama/__init__.py

 
 VERSION = ('0', '3', '1', 'beta')
 
+
 def get_version(command=''):
     version = '.'.join(element for element in VERSION[:3])
     return '{0} {1}'.format(command, version) if command else version
 
 
 def sir_yes_sir(module='', doc='', options=(), version='', argv=None):
-    """
+    """Launches the right order or displaying the help for a command or an order
+    directly from command line.
     """
     argv = argv if argv else sys.argv[:]
     command = os.path.basename(argv[0])
     module = module if module else command
-    commander = Commander(command, module, doc)
+    commander = Commander(module, doc=doc, command=command)
 
     # global options and app version made available for the orders
     QG.options = options
                 return order.explanations()
             else:
                 return order(argv[2:])
+
+
+def call_order(module_name, order_name, *args, **kwargs):
+    """Calls an order from another python script directly
+    """
+    commander = Commander(module_name)
+    order = commander[order_name]
+    return order.execute(*args, **kwargs)

src/adama/commandment.py

     examples = ''
     version = ''
 
-    def __init__(self, command, module):
+    def __init__(self, module, command=''):
         self.command = command
         self.module = module
 
 
     __orders = {}
 
-    def __init__(self, command, module, doc):
-        super(Commander, self).__init__(command, module)
+    def __init__(self, module, command='', doc=''):
+        super(Commander, self).__init__(module, command=command)
         self.doc = doc if doc else __doc__
 
     @property
                     subpackage = '{0}.{1}'.format(app_orders, name)
                     if subpackage not in sys.modules:
                         __import__(subpackage)
-                    self.__orders[name] = sys.modules[subpackage].Order(
-                        self.command, self.module)
+                    self.__orders[name] = sys.modules[subpackage].Order()
 
         return self.__orders
 
     to define a command
     """
 
-    def __init__(self, command, module):
-        super(BaseOrder, self).__init__(command, module)
+    def __init__(self, module, command=''):
+        super(BaseOrder, self).__init__(module, command='')
         self.name = self.__class__.__module__.split('.')[-1] \
 
     def usage(self):

src/adama/orders/__init__.py

     return module
 
 
+def get_command(name, module):
+    """Defines command's name if user doesn't
+    """
+    if not name:
+        return module.__name__
+    return name
+
+
 def touch(filename, times=None):
     """Creates an empty file
     """

src/adama/orders/create_order.py

 from optparse import make_option
 
 from ..commandment import BaseOrder, OrderError
-from . import get_template, get_module, touch
+from . import get_template, get_module, get_command, touch
 
 
 class Order(BaseOrder):
     """
 
     options = BaseOrder.options + ()
+
     description = __doc__.split('\n')[0].lower()
     args = "module name"
 
-    def __init__(self, commander, module):
-        super(Order, self).__init__(commander, module)
+    def __init__(self):
+        super(Order, self).__init__('adama', command='adama')
 
     def execute(self, *args, **options):
         if len(args) != 2:
             # 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)
 
         # Writes in file
         with open(order_path, "w") as order:
-            order.write(get_template('order'))
+            order.write(get_template('order')\
+                .format(module.__name__, command_name))
 
         return 0

src/adama/orders/create_program.py

 from optparse import make_option
 
 from ..commandment import BaseOrder, OrderError
-from . import get_module, get_template
+from . import get_module, get_template, get_command
 
 class Order(BaseOrder):
     """Creates a command line program for your application that will look for orders
         make_option('-d', '--directory', action='store', type='string',
             dest='path', metavar='PATH', default=os.getcwd(),
             help='directory to save the command [Default: %default]'),
-        make_option('-n', '--name', action='store', type='string', dest='name',
-            metavar='NAME', help='name of the command [Default: module\'s name]'),
     )
 
     args = "module"
     description = __doc__.split('\n')[0].lower()
     examples = ""
 
-    def __init__(self, commander, module):
-        super(Order, self).__init__(commander, module)
+    def __init__(self):
+        super(Order, self).__init__('adama', command='adama')
 
     def execute(self, *args, **options):
         if len(args) != 1:
         if not os.path.isdir(options['path']):
             os.makedirs(options['path'])
 
-        # Defines command's name if user doesn't
-        if not options['name']:
-            options['name'] = module.__name__.split('.')[-1]
+        # Defines the command name
+        command_name = get_command(options['name'], module)
 
         # Path to save the command
-        file_path = os.path.join(options['path'], options['name'])
+        file_path = os.path.join(options['path'], command_name)
 
         # Writes data coming from template to file
         with open(file_path, "w") as program:

src/adama/templates/order.template

     description = 'help for the order (ex: __doc__)'
     examples = 'put examples here'
 
-    def __init__(self, command, module):
-        super(Order, self).__init__(command, module)
+    def __init__(self):
+        super(Order, self).__init__('{0}', command='{1}')
 
     def execute(self, *args, **options):
         # the logic of the order comes here
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.