Commits

Arnaud Grausem committed e91ab74

Pylint check

Comments (0)

Files changed (13)

src/adama/__init__.py

 # -*- coding: utf-8 -*-
 
 """
+adama
+=====
+
+Defines the base logic of the library to launch orders from command line or
+directly in Python scripts
 """
 
 import sys
 
 
 def get_version(command=''):
+    """Human readable version
+    """
     version = '.'.join(element for element in VERSION[:3])
     return '{0} {1}'.format(command, version) if command else version
 

src/adama/commandment.py

 # -*- coding: utf-8 -*-
 
+"""
+adama.commandment
+=================
+
+    Core API which contains base classes for commands and orders
+"""
+
 import sys
 
 from .utils import find_orders, AdamaOptionParser
         options, args = self.decrypt(sysargs)
         try:
             result = self.execute(*args, **options.__dict__)
-        except OrderError as e:
-            return e()
+        except OrderError as order_error:
+            result = order_error()
         return result
 
 class Commander(QG):
     """Program class
     """
 
-
     def __init__(self, module, command='', doc=''):
         super(Commander, self).__init__(module, command=command)
         self.doc = doc if doc else __doc__
         if not self.__orders:
             app_orders = '{0}.orders'.format(self.module)
             try:
-                package = __import__(app_orders) if app_orders not in sys.modules \
+                package = __import__(app_orders) \
+                if app_orders not in sys.modules \
                     else sys.modules[self.module]
-            except ImportError as e:
+            except ImportError:
                 pass
             else:
                 for name in find_orders(package.__path__[0]):
 {1}{2}
 
 """
-        create_help = "\n\nType 'adama create_order [options] {0} <order_name>' to create one"\
-                .format(self.module)
+        create_help = "\n\nType 'adama create_order [options] {0} <order_name>'\
+ to create one".format(self.module)
         # Formats the epilog
         decrypter.epilog = epilog.format(
-            self.command, self.available_orders, create_help if not self.orders else ''
+            self.command, self.available_orders,
+            create_help if not self.orders else ''
         )
 
         return decrypter
 
     @property
     def available_orders(self):
-        """
+        """Human readable string to show available orders for a command
         """
         available = "Available orders:\n{0}"
         if self.orders:
             max_ordername_len = max((len(name) for name in self.orders))
             # Pretty output of available orders
             available_orders = '\n'.join(
-                '  {0:{2}}\t{1}'.format(name, order.description, max_ordername_len)
-                for name, order in self.orders.items())
+                '  {0:{2}}\t{1}'.format(
+                    name, order.description, max_ordername_len
+                ) for name, order in self.orders.items())
         else:
             # Help output when no orders can be found under package
             available_orders = "No orders available."
         return available.format(available_orders)
 
 
+    def execute(self):
+        return self.usage()
+
     def __getitem__(self, key):
         try:
             return self.get_order(key)
         decrypter.epilog = self.examples
         return decrypter
 
+    def execute(self, *args, **kwargs):
+        raise NotImplementedError
+
     def __str__(self):
         return self.usage()
 

src/adama/exceptions.py

 # -*- coding: utf-8 -*-
 
-"""Adama's exceptions declaration
+"""
+adama.exception
+===============
+
+    Exceptions based on a abstract base exception class to catch missing
+    orders or errors during execution of orders
 """
 
 
     """
 
     def __init__(self, msg, number=1):
+        super(AdamaError, self).__init__()
         self.message = msg
         self.number = number
 
 
 
 class UnknownOrderError(AdamaError):
-    """
+    """An order called doesn't exist
     """
 
     def __init__(self, msg, commander):
 
 
 class OrderError(AdamaError):
-    """
+    """An error during the excecution of an order
     """
 
     def __init__(self, msg, order):

src/adama/orders/__init__.py

 # -*- coding: utf-8
 
+"""
+adama.orders
+============
+
+    Helper functions for adama defined orders
+"""
+
 import os
 import sys
 
-from ..commandment import OrderError
-
 
 def get_template(name):
-    """
+    """Gets a template based on name of order
     """
     template_path = os.path.join(
         os.path.dirname(__path__[0]), 'templates', '{0}.template'.format(name)

src/adama/orders/create_order.py

 """
 
 import os
-from optparse import make_option
 
+from ..exceptions import OrderError
 from ..commandment import BaseOrder
-from ..exceptions import OrderError
 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
+    """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
 
     def execute(self, *args, **options):
         if len(args) != 2:
-            raise OrderError('The create_order order has two required arguments', self)
+            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 e:
-            raise OrderError(str(e), self)
+        except ImportError as bad_import:
+            raise OrderError(str(bad_import), self)
         name = args[1]
 
         # Constructs, searches and creates the orders path

src/adama/orders/create_program.py

 """Create Program Order
 """
 
-import sys
 import os
 from optparse import make_option
 
+from ..exceptions import OrderError
 from ..commandment import BaseOrder
-from ..exceptions import OrderError
 from . import get_module, get_template, get_command
 
 class Order(BaseOrder):
-    """Creates a command line program for your application that will look for orders
+    """Creates a command line program for your application that will look for
+orders
 
 Argument:
   module      Python module that contains or will contain the orders module
 
     def execute(self, *args, **options):
         if len(args) != 1:
-            raise OrderError('The create_program order has one required argument',
-                self)
+            raise OrderError(
+                'The create_program order has one required argument', self)
 
         try:
             module = get_module(args[0], options['pythonpath'])
-        except ImportError as e:
-            raise OrderError(str(e), self)
+        except ImportError as bad_import:
+            raise OrderError(str(bad_import), self)
 
         # Checks if entered path exists and create it
         if not os.path.isdir(options['path']):

src/adama/tests/__init__.py

 # -*- coding: utf-8 -*-
 
-"""Test getting orders
+"""
+adama.tests
+===========
+
+    Unit tests for the adama library.
 """
 
 import unittest
         sys.path.remove(os.path.dirname(self.base_path))
 
 
-def no_shell_printing(f):
+def no_shell_printing(func):
     """No shell printing for help when lauching tests
     """
     print_functions = (QG.explanations, AdamaError.__call__)
     def wrap(**kwargs):
+        """Wrapper for the printing function"""
         for function in print_functions:
             function.im_func.func_defaults = (False, )
-        result = f(**kwargs)
+        result = func(**kwargs)
         for function in print_functions:
             function.im_func.func_defaults = (True, )
         return result
     """Creates orders, adds path to sys path before launching test
     and removes path and modules
     """
-    def wrap(f):
+    def wrap(func):
+        """Wrapper for calling a function in a package that needs to be in
+        sys.path"""
         def wrapped_f(instance, *args, **kwargs):
+            """Adding in sys.path, launching func and remove from sys.path"""
             instance._create_orders(*orders)
             instance._add_to_syspath()
-            f(instance, *args, **kwargs)
+            func(instance, *args, **kwargs)
             instance._remove_from_syspath()
         return wrapped_f
     return wrap

src/adama/tests/test_commandment.py

 # -*- coding utf-8 -*-
 
 """
+adama.tests.test_commandment
+============================
+
+    Testing the commandment API
 """
 
 import unittest
-import sys
 
 from adama.tests import TestBaseOrders
 from adama.commandment import Commander
         super(TestCommander, self).tearDown()
 
     def test_usage(self):
-        assert self.commander.usage() == 'Usage: {0} order [options] [args]'.format(self.command)
+        """Tests the usage of a command
+        """
+        assert self.commander.usage() == 'Usage: {0} order [options] [args]'\
+            .format(self.command)
 
     def test_run_not_implemented(self):
-        self.assertRaises(NotImplementedError, self.commander, [])
+        """Tests implemented the execute method on commander
+        """
+        assert self.commander([]) == self.commander.usage()
 
 if __name__ == '__main__':
     unittest.main()

src/adama/tests/test_exceptions.py

 # -*- coding utf-8 -*-
 
 """
+adama.tests.test_exceptions
+===========================
+
+    Testing messages and raised exceptions in adama library
 """
 
 import unittest
-import sys
 
 from adama import call_order
-from adama.tests import TestBaseOrders, no_shell_printing
+from adama.tests import no_shell_printing
 from adama.exceptions import AdamaError, OrderError
 
 
     """Test the implementation of adama's base exception
     """
 
-    def runTest(self):
+    def test_printer_implented(self):
+        """Tests that calling printer and return value of base exception
+        """
         base_exception = AdamaError('Base exception')
+        # print_error should be implemented
         self.assertRaises(NotImplementedError, base_exception.print_error)
         assert no_shell_printing(base_exception)() == 1
 
     """Tests errors catched in execution of orders
     """
 
-    def runTest(self):
+    def test_raising_order_error(self):
+        """Tests that an error during an order execution raised an OrderError
+        exception and checks the returned value, message and representation of
+    an instance
+        """
         with self.assertRaises(OrderError) as order_error:
             call_order('adama', 'create_program')
         exception = order_error.exception
-        assert exception.print_error() == 'The create_program order has one required argument\nUsage: adama create_program [options] module\n'
+        # error printed on shell
+        assert exception.print_error() == 'The create_program order has one \
+required argument\nUsage: adama create_program [options] module\n'
+        #  representation
         assert repr(exception) == '<OrderError: create_program>'
+        # returned value
         assert no_shell_printing(exception)() == 12
 
 

src/adama/tests/test_orders.py

 # -*- coding utf-8 -*-
 
 """
+adama.tests.test_orders
+=======================
+
+    Tests the adama orders : create_program and create_order
 """
 
 import unittest
-import sys
 import os
 import shutil
 
 from adama import call_order
-from adama.tests import TestBaseOrders, no_shell_printing
-from adama.exceptions import AdamaError, OrderError
+from adama.tests import TestBaseOrders
+from adama.exceptions import OrderError
 
 
 class TestCreateProgram(TestBaseOrders):
         super(TestCreateProgram, self).tearDown()
 
     def _isfile(self, filename):
+        """Checks if a file has been created
+        """
         return os.path.isfile(os.path.join(self.destination, filename))
 
     def test_no_arg(self):
         with self.assertRaises(OrderError) as order_error:
             call_order('adama', 'create_program')
         exception = order_error.exception
-        assert exception.message == 'The create_program order has one required argument'
+        assert exception.message == 'The create_program order has one required \
+argument'
 
     def test_command_name(self):
         """Tests creating a command with a name defined
         """
-        call_order('adama', 'create_program', self.module, name=self.command, pythonpath='/tmp', path=self.destination)
+        call_order('adama', 'create_program', self.module, name=self.command,
+            pythonpath='/tmp', path=self.destination)
         self.assertTrue(self._isfile(self.command))
         self._remove_from_syspath()
 
     def test_no_command_name(self):
         """Tests creating a command with no name defined
         """
-        call_order('adama', 'create_program', self.module, name='', pythonpath='/tmp', path=self.destination)
+        call_order('adama', 'create_program', self.module, pythonpath='/tmp',
+            path=self.destination, name='')
         self.assertTrue(self._isfile(self.module))
         self._remove_from_syspath()
 
         """Tests searching module with no pythonpath add and module in syspath
         """
         self._add_to_syspath()
-        call_order('adama', 'create_program', self.module, name=self.command, pythonpath='', path=self.destination)
+        call_order('adama', 'create_program', self.module, name=self.command,
+            pythonpath='', path=self.destination)
         self.assertTrue(self._isfile(self.command))
         self._remove_from_syspath()
 
     def test_not_in_syspath(self):
-        """Tests searching module with no pythonpath add and module not in syspath
+        """Tests searching module with no pythonpath add and module not in
+        syspath
         """
-        self.assertRaises(OrderError, call_order, 'adama', 'create_program', self.module, name=self.command, pythonpath='', path=self.destination)
+        self.assertRaises(
+            OrderError, call_order, 'adama', 'create_program', self.module,
+            name=self.command, pythonpath='', path=self.destination
+        )
 
 
 class TestCreateOrder(TestBaseOrders):
     """
 
     def  _isfile(self, order_file):
+        """Checks if a file has been created
+        """
         return os.path.isfile(os.path.join(self.orders_path, order_file))
 
     def test_no_arg(self):
         with self.assertRaises(OrderError) as order_error:
             call_order('adama', 'create_order')
         exception = order_error.exception
-        assert exception.message == 'The create_order order has two required arguments'
+        assert exception.message == 'The create_order order has two required \
+arguments'
 
     def test_no_orders_module(self):
         """The orders module is absent
         """
         shutil.rmtree(self.orders_path)
-        call_order('adama', 'create_order', self.module, 'no_module', pythonpath='/tmp', name=self.command)
+        call_order('adama', 'create_order', self.module, 'no_module',
+            pythonpath='/tmp', name=self.command)
         self.assertTrue(self._isfile('no_module.py'))
         self._remove_from_syspath()
 
         """Tests creating orders in a package that is present in syspath
         """
         self._add_to_syspath()
-        call_order('adama', 'create_order', self.module, 'test', pythonpath='', name=self.command)
+        call_order('adama', 'create_order', self.module, 'test', pythonpath='',
+            name=self.command)
         self.assertTrue(self._isfile('test.py'))
         self._remove_from_syspath()
 
     def test_not_in_syspath(self):
         """Tests creating orders in a package that is not present in syspath
         """
-        self.assertRaises(OrderError, call_order, 'adama', 'create_order', self.module, 'test', pythonpath='', name=self.command)
+        self.assertRaises(OrderError, call_order, 'adama', 'create_order',
+            self.module, 'test', pythonpath='', name=self.command)
 
 
 if __name__ == '__main__':

src/adama/tests/test_parsing_args.py

 # -*- coding utf-8 -*-
 
 """
+adama.tests.test_parsing_args
+=============================
+
+    Testing the capability of the library to parse and deal with arguments
+    and options : showing help, direct launch, launching through command line,
+    dealing with problem and exception
+
 """
 
 import unittest
-import sys
 
-from adama.tests import TestBaseOrders, no_shell_printing, encapsulate_test_with_syspath
+from adama.tests import TestBaseOrders, no_shell_printing, \
+    encapsulate_test_with_syspath
 from adama import sir_yes_sir, call_order
 from adama.exceptions import UnknownOrderError
 
 
-sir_yes_sir = no_shell_printing(sir_yes_sir)
+# disables the standard output printing
+_sir_yes_sir = no_shell_printing(sir_yes_sir)
 
 
 class TestShellHelp(TestBaseOrders):
     """Tests the help message and the way they are displayed
     """
 
+
     def test_no_args(self):
+        """No argument shell return
         """
-        """
-        assert sir_yes_sir(module=self.module, argv=[self.command]) == 1
+        assert _sir_yes_sir(module=self.module, argv=[self.command]) == 1
 
     def test_global_help(self):
+        """Global help shell return
         """
-        """
-        assert sir_yes_sir(module=self.module, argv=[self.command, 'help']) == 1
+        assert _sir_yes_sir(module=self.module, argv=[self.command, 'help']) \
+            == 1
 
     @encapsulate_test_with_syspath('add.py')
     def test_order_help(self):
+        """Order help shell return
         """
-        """
-        assert sir_yes_sir(module=self.module, argv=[self.command, 'help', 'add']) == 1
+        assert _sir_yes_sir(module=self.module, argv=[self.command, 'help',
+            'add']) == 1
 
 
 class TestShellNoOrder(TestBaseOrders):
     """Tests executing an order that doesn't exist
     """
 
-    def runTest(self):
-        assert sir_yes_sir(module=self.module, argv=[self.command, 'clear']) == 11
+    # disables the standard output printing
+    _sir_yes_sir = no_shell_printing(_sir_yes_sir)
+
+    def test_no_order_found(self):
+        """Order launched not found shell return
+        """
+        assert _sir_yes_sir(module=self.module, argv=[self.command, 'clear']) \
+            == 11
 
 
 class TestShellExcuteOrder(TestBaseOrders):
     """
 
     @encapsulate_test_with_syspath('add.py')
-    def runTest(self):
-        assert sir_yes_sir(module=self.module, argv=[self.command, 'add']) == 0
+    def test_order_execution(self):
+        """Order executed shell return
+        """
+        assert _sir_yes_sir(module=self.module, argv=[self.command, 'add']) == 0
 
 
 class TestDirectLaunchOrder(TestBaseOrders):
     """
 
     def test_no_order(self):
-        """No order created
+        """No order found raises an exception
         """
         self.assertRaises(UnknownOrderError, call_order, self.module, 'add')
 
     @encapsulate_test_with_syspath('add.py')
     def test_executing_order(self):
-        """
+        """Order executed function return
         """
         assert call_order(self.module, 'add') == 0
 
+
 if __name__ == '__main__':
     unittest.main()

src/adama/tests/test_searching_orders.py

 # -*- coding: utf-8 -*-
 
-"""Test getting orders
+"""
+adama.tests.test_searching_orders
+=================================
+
+    Test finding and getting orders from filesystem ans sys.path
 """
 
 import unittest
-import sys
 import os
 import shutil
 
     """
 
     def test_is_a_package(self):
+        """Tests if a file defines a python package
+        """
         my_package = '__init__.py'
         self.assertTrue(is_package(my_package))
 
     def test_is_not_a_package(self):
+        """Tests if a file doesn't define a python package
+        """
         my_module = 'my_module.py'
         self.assertFalse(is_package(my_module))
 
     def test_is_a_module(self):
-        my_module = ('module', '.py')
-        self.assertTrue(is_module(*my_module))
+        """Tests if a file is a python module
+        """
+        self.assertTrue(is_module('module', '.py'))
 
     def test_is_not_a_module(self):
-        my_file = ('file', '.txt')
-        self.assertFalse(is_module(*my_file))
-        my_package = ('__init__', '.py')
-        self.assertFalse(is_module(*my_package))
+        """Tests of a file is not a python module
+        """
+        self.assertFalse(is_module('file', '.txt'))
+        self.assertFalse(is_module('__init__', '.py'))
 
     def test_is_a_file(self):
+        """Tests if element on filesystem is a file
+        """
         with open('/tmp/my_module.py', 'w') as module:
             module.write('I\'m a Python module')
         self.assertTrue(is_file('/tmp', 'my_module.py'))
         os.remove('/tmp/my_module.py')
 
     def test_is_not_a_file(self):
+        """Tests if an element on filesystem is not a file
+        """
         self.assertFalse(is_file('/tmp', 'my_other_module.py'))
         os.mkdir('/tmp/my_directory')
         self.assertFalse(is_file('/tmp', 'my_directory'))
         assert repr(self.commander['clear']) == '<Order: clear>'
 
     def test_get_not_existing_order(self):
-        """
+        """Test if an error is raised when trying to get an order that doesn't
+        exist
         """
         with self.assertRaises(UnknownOrderError) as error:
             clear = self.commander['clear']
+            clear()
         the_error = error.exception
         self.assertEqual(the_error.number, 11)
-        self.assertRegexpMatches(str(the_error), 'The order "clear" doesn\'t exist')
-        self.assertEqual(repr(the_error), '<UnknownOrderError: {0}>'.format(self.command))
+        self.assertRegexpMatches(str(the_error),
+            'The order "clear" doesn\'t exist')
+        self.assertEqual(repr(the_error), '<UnknownOrderError: {0}>'\
+            .format(self.command))
 
 
 class TestNoOrderPackage(TestBaseOrders):
         self._remove_from_syspath()
         super(TestNoOrderPackage, self).tearDown()
 
-    def runTest(self):
+    def test_no_order_package(self):
+        """No order package is already created
+        """
         commander = Commander(self.module)
         self.assertTrue(not commander.orders)
 

src/adama/utils.py

 # -*- coding: utf-8 -*-
 
-"""Some utils functions and classes
+"""
+adama.utils
+===========
+
+    The adama option parser that inherits OptionParser and utils function
+    to check python package module and package and to find orders on filesystem
 """
 
 import os
 
 
 def find_orders(path):
+    """Finds orders in filesystem given a path
     """
-    """
-    DEFAULT_ORDER_DIRECTORY = 'orders'
+    default_order_directory = 'orders'
     # returns only python modules name found in orders path
     try:
-        orders_path = os.path.join(path, DEFAULT_ORDER_DIRECTORY)
+        orders_path = os.path.join(path, default_order_directory)
         splitted_files = [os.path.splitext(element) for element
             in os.listdir(orders_path) if is_file(orders_path, element)]
         return [filename for filename, extension in splitted_files