1. Arnaud Grausem
  2. adama

Commits

Arnaud Grausem  committed b1c112a

Some refactoring stuff and adding the possibility to create the order module with an empty __init__ file

  • Participants
  • Parent commits 687b599
  • Branches default

Comments (0)

Files changed (4)

File src/adama/commandment.py

View file
                 .format(self.module))
         else:
             if not self.__orders:
-                print package
                 for name in find_orders(package.__path__[0]):
                     subpackage = '{0}.{1}'.format(app_orders, name)
                     if subpackage not in sys.modules:

File src/adama/orders/__init__.py

View file
 import os
 import sys
 
+from ..commandment import OrderError
+
+
 def get_template(name):
     """
     """
     with open(template_path) as template:
         return template.read()
 
-def add_to_pythonpath(path):
-    """
+
+def get_module(module_name, path):
+    """Adds a path to a pythonpath and imports and returns a module
     """
     if path and path not in sys.path:
         sys.path.append(path)
+    try:
+        module = __import__(module_name)
+    except ImportError as e:
+        raise OrderError(str(e))
+    return module
 
+
+def touch(filename, times=None):
+    """Creates an empty file
+    """
+    with file(filename, 'a'):
+        os.utime(filename, times)
+

File src/adama/orders/create_order.py

View file
 import os
 from optparse import make_option
 
-from adama.commandment import BaseOrder, OrderError
-from adama.orders import get_template, add_to_pythonpath
+from ..commandment import BaseOrder, OrderError
+from . import get_template, get_module, touch
 
 
 class Order(BaseOrder):
         if len(args) != 3:
             raise OrderError('The create_program has 3 required arguments', self.usage())
 
+        # adds a path to pythonpath if options has been selected
+        # and if it is not already there and returns a module
+        module = get_module(args[1], options.pythonpath)
         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:
-            raise OrderError(str(e))
-
         # Constructs, searches and creates the orders path
         orders_path = os.path.join(module.__path__[0], 'orders')
         if not os.path.isdir(orders_path):

File src/adama/orders/create_program.py

View file
 import os
 from optparse import make_option
 
-from adama.commandment import BaseOrder, OrderError
-from adama.orders import get_template
+from ..commandment import BaseOrder, OrderError
+from . import get_module, get_template
 
 class Order(BaseOrder):
     """Creates a command line program for your application that will look for orders
             raise OrderError('The create_program has one required argument',
                 self.usage())
 
-        module = args[1]
+        module = get_module(args[1], options.pythonpath)
 
         # Checks if entered path exists and create it
         if not os.path.isdir(options.path):
 
         # Defines command's name if user doesn't
         if not options.name:
-            options.name = module.split('.')[-1]
+            options.name = module.__name__.split('.')[-1]
 
         # Path to save the command
         file_path = os.path.join(options.path, options.name)
         # Writes data coming from template to file
         with open(file_path, "w") as program:
             template = get_template('program')
-            program.write(template.format(module))
+            program.write(template.format(module.__name__))
 
         return 0