Advertisement
ylSiew

cliUtils

Aug 10th, 2015
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.01 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3.  
  4. """ Module cliUtils: This module provides useful methods for dealing
  5. with the Command Line interface.
  6. """
  7.  
  8. import logging
  9. from argparse import ArgumentParser
  10.  
  11.  
  12. class ParseArgs(object):
  13.     """
  14.    This class contains methods for parsing arguments from the command line.
  15.    Can be overloaded to provide custom command line arguments.
  16.    """
  17.  
  18.     def __init__(self, description=None, *args):
  19.         """
  20.        Reads in the command line arguments passed by the user.
  21.  
  22.        :param description: ``str`` containing help about the module being run.
  23.        :param args: ``str`` arguments that indicate what additional arguments are
  24.                    to be read in.
  25.                    Currently supports:
  26.                    - *mode*
  27.                    - *files*
  28.                    - *output*
  29.                    - *overwriteExisting*
  30.  
  31.        :return: ``None``
  32.        """
  33.  
  34.         # Read in command-line arguments
  35.         self.parser = ArgumentParser(description=description)
  36.  
  37.         self.addArguments(*args)
  38.  
  39.         self.args = self.parser.parse_args()
  40.  
  41.  
  42.     def addArguments(self, *args):
  43.         """
  44.        This method adds/configures the available command line arguments.
  45.        Can be overloaded for adding custom arguments.
  46.  
  47.        :return: ``None``
  48.        """
  49.  
  50.         # Add common arguments
  51.         self.parser.add_argument('-m', '--mode',
  52.                                  help='Determines the mode in which the program '
  53.                                     'should be run in.',
  54.                                  type=str,
  55.                                  action='store',
  56.                                  dest='mode'
  57.  
  58.          )
  59.  
  60.         if 'files' in args:
  61.             self.parser.add_argument('-f', '--files',
  62.                                      help='Provide a semicolon-separated list of filepaths.',
  63.                                      type=str,
  64.                                      action='store',
  65.                                      dest='files'
  66.             )
  67.  
  68.         if 'output' in args:
  69.             self.parser.add_argument('-o', '--output',
  70.                                      help='Provide an output path.',
  71.                                      type=str,
  72.                                      action='store',
  73.                                      dest='files'
  74.             )
  75.  
  76.         if 'overwrite' in args:
  77.             self.parser.add_argument('-O', '--overwriteExisting',
  78.                                      help='Determines if existing files in '
  79.                                           'output path are to be overwritten.',
  80.                                      type=bool,
  81.                                      action='store',
  82.                                      dest='overwriteExisting'
  83.             )
  84.  
  85.  
  86.     def getLoggingLevel(self):
  87.         """
  88.        This retrieves the desired logging level. By default is set to INFO level.
  89.  
  90.        :return: ``int`` corresponding to chosen logging level
  91.        :rtype: ``int``
  92.        """
  93.  
  94.         try:
  95.             loggingLevel = self.args.mode
  96.  
  97.             if loggingLevel == 'critical' or loggingLevel == 'CRITICAL':
  98.                 loggingLevel = logging.CRITICAL
  99.  
  100.             elif loggingLevel == 'error' or loggingLevel == 'ERROR':
  101.                 loggingLevel = logging.ERROR
  102.  
  103.             elif loggingLevel == 'warning' or loggingLevel == 'WARNING':
  104.                 loggingLevel = logging.WARNING
  105.  
  106.             elif loggingLevel == 'info' or loggingLevel == 'INFO':
  107.                 loggingLevel = logging.INFO
  108.  
  109.             elif loggingLevel == 'debug' or loggingLevel == 'DEBUG':
  110.                 loggingLevel = logging.DEBUG
  111.  
  112.             else:
  113.                 loggingLevel = logging.INFO
  114.  
  115.         except AttributeError:
  116.             loggingLevel=logging.INFO
  117.  
  118.         return loggingLevel
  119.  
  120.  
  121.     def getFiles(self):
  122.         """
  123.        This method retrieves a semicolon-separated list of files passed in as arguments.
  124.        Returns a ``list`` of the file paths.
  125.  
  126.        :return: ``list`` of file paths
  127.        :rtype: ``list`
  128.        """
  129.  
  130.         try:
  131.             files = self.args.files
  132.             files = files.split(';')
  133.  
  134.         except AttributeError:
  135.             files = None
  136.  
  137.         return files
  138.  
  139.  
  140.     def getOutputPath(self):
  141.         """
  142.        This method returns the output path that is passed in as an argument.
  143.  
  144.        :return: ``str`` to file path
  145.        :rtype: ``str``
  146.        """
  147.  
  148.         try: outputPath = self.args.output
  149.         except AttributeError: outputPath = None
  150.  
  151.         return outputPath
  152.  
  153.  
  154.     def getOverwriteExisting(self):
  155.         """
  156.        This method returns the value that was passed for choosing to
  157.        overwrite existing files in place.
  158.  
  159.        Defaults to ``True``.
  160.  
  161.        :return: ``bool``
  162.        :rtype: ``bool``
  163.        """
  164.  
  165.         try: overwriteExisting = self.args.overwriteExisting
  166.         except AttributeError: overwriteExisting = True
  167.  
  168.         return overwriteExisting
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement