Arnaud Grausem avatar Arnaud Grausem committed c8a4183

Making work the create_program command

Comments (0)

Files changed (4)

src/adama/commandment.py

     """
 
     arg = ''
-    help = ''
     options = ()
 
     def __init__(self, command, module):
         """
         options, args = self.decrypt()
         try:
-            result = self.run(*args, **options.__dict__)
+            result = self.run(args, options)
         except OrderError, e:
             sys.stderr.write(str(e))
             sys.exit(1)
     def __getitem__(self, key):
         return self.get_order(key)
 
-    def run(self, *args, **kwargs):
+    def run(self, args, options):
         """Bad use of command so we print usage
         """
         return self.explanations()
     """
 
     args = ''
-    help = ''
     options = ()
+    examples = ""
 
     def __init__(self, command, module):
         super(BaseOrder, self).__init__(command, module)
         """Usage of a command
         """
         return '%prog {0.name} [options] {0.args}'.format(self)
+
+    @property
+    def decrypter(self):
+        decrypter = super(BaseOrder, self).decrypter
+        decrypter.epilog = self.examples
+        return decrypter

src/adama/orders/create_program.py

 from adama.orders import get_template
 
 class Order(BaseOrder):
+    """Creates a program script that can launch orders from a python module
 
-    options = BaseOrder.options
+Argument:
+  module      Python module that contains or will contain the orders module
+    """
 
-    help = __doc__
-    args = "project_path name"
+    options = BaseOrder.options + (
+        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"
+    examples = ""
 
     def __init__(self, commander, module):
         super(Order, self).__init__(commander, module)
 
-    def run(self, *args, **options):
-        if len(args) != 3:
-            raise OrderError('The create_program needs two arguments')
+    def run(self, args, options):
+        if len(args) != 2:
+            raise OrderError('The create_program has one required argument',
+                self.usage())
 
-        project_path = args[1]
-        prog_name = args[2]
+        module = args[1]
 
-        if not os.path.isdir(project_path):
-            raise OrderError(
-                'The project {0} does not exist'.format(project_path)
-            )
+        # Checks if entered path exists and create it
+        if not os.path.isdir(options.path):
+            os.makedirs(options.path)
 
-        bin_path = os.path.join(project_path, 'bin')
-        if not os.path.isdir(bin_path):
-            os.mkdir(bin_path)
+        # Defines command's name if user doesn't
+        if not options.name:
+            options.name = module.split('.')[-1]
 
-        script_path = os.path.join(bin_path, prog_name)
+        # Path to save the command
+        file_path = os.path.join(options.path, options.name)
 
-        with open(script_path, "w") as program:
-            program.write(get_template('program'))
+        # Writes data coming from template to file
+        with open(file_path, "w") as program:
+            template = get_template('program')
+            program.write(template.format(module))
 
         return 0

src/adama/templates/program.template

 from adama import sir_yes_sir
 
 if __name__ == '__main__':
-    sys.exit(sir_yes_sir())
+    sys.exit(sir_yes_sir('{0}'))

src/adama/utils.py

     """
 
     def format_epilog(self, formatter):
-        """Initial format_epilog function stris newline
+        """Initial format_epilog function strips newline
         """
         return self.epilog
 
+    def format_description(self, formatter):
+        """Initial format_description function strips newline
+        """
+        return self.description
+
 
 VERSION = '0.2'
 
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.