Commits

Arnaud Grausem committed da46094

Adding global_options possibility for adama and adding the pythonpath global option for the adama script

  • Participants
  • Parent commits 78394b0
  • Tags 0.2

Comments (0)

Files changed (6)

 """
 
 import sys
+from optparse import make_option
+
 from adama import sir_yes_sir
 
+global_options = (
+    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"'),
+)
 
 if __name__ == '__main__':
-	sys.exit(sir_yes_sir(doc=__doc__))
+	sys.exit(sir_yes_sir(doc=__doc__, options=global_options))

src/adama/__init__.py

 import sys
 import os
 
-from .commandment import Commander, OrderError
+from .commandment import Commander, OrderError, QG
 
 
-def sir_yes_sir(module='', doc='', argv=None):
+def sir_yes_sir(module='', doc='', options=(), argv=None):
     """
     """
     argv = argv if argv else sys.argv[:]
     module = module if module else command
     commander = Commander(command, module, doc)
 
+    QG.options = options
+
     no_arg = len(argv) == 1
     needs_help = not no_arg and argv[1] == 'help'
     global_help = needs_help and len(argv) == 2

src/adama/commandment.py

 
     arg = ''
     options = ()
+    examples = ''
 
     def __init__(self, command, module):
         self.command = command
     to define a command
     """
 
-    args = ''
-    options = ()
-    examples = ""
-
     def __init__(self, command, module):
         super(BaseOrder, self).__init__(command, module)
         self.name = self.__class__.__module__.split('.')[-1] \

src/adama/orders/__init__.py

 # -*- coding: utf-8
 
 import os
+import sys
 
 def get_template(name):
+    """
+    """
     template_path = os.path.join(
         os.path.dirname(__path__[0]), 'templates', '{0}.template'.format(name)
     )
     with open(template_path) as template:
         return template.read()
+
+def add_to_pythonpath(path):
+    """
+    """
+    if path and path not in sys.path:
+        sys.path.append(path)
+

src/adama/orders/create_order.py

 from optparse import make_option
 
 from adama.commandment import BaseOrder, OrderError
-from adama.orders import get_template
+from adama.orders import get_template, add_to_pythonpath
 
 
 class Order(BaseOrder):
   name      name of the order
     """
 
-    options = ()
+    options = BaseOrder.options + ()
     description = __doc__.split('\n')[0].lower()
     args = "module name"
 
 
         name = args[2]
 
+        # adds a path to pythonpath if options has been selected
+        # and if it is not already there
+        add_to_pythonpath(options.pythonpath)
+
         try:
             module = __import__(args[1])
         except ImportError as e:

src/adama/templates/program.template

 """
 
 import sys
+from optparse import make_option
+
 from adama import sir_yes_sir
 
+global_options = (
+    # options for command come here
+    # use make_option
+    # see http://docs.python.org/library/optparse.html#populating-the-parser
+)
+
 if __name__ == '__main__':
-    sys.exit(sir_yes_sir(module='{0}', doc=__doc__))
+    sys.exit(sir_yes_sir(module='{0}', doc=__doc__, options=global_options))