doapfiend.cli

Package doapfiend :: Module cli |
[hide private]
[frames] | no frames]

# Source Code for Module doapfiend.cli

      1
      2  # pylint: disable-msg=C0103
      3  '''
      4
      5  cli.py
      6  ======
      7
      8  Command-line tool for querying, serializing and displaying DOAP
      9
     10  Author: Rob Cakebread <rob@doapspace.org>
     11
     12  License : BSD-2
     13
     14  '''
     15
     16  __docformat__ = 'epytext'
     17  __revision__ = '$Revision:  $'[11:-1].strip()
     18
     19
     20  import sys
     21  import logging
     22  import optparse
     23
     24  from doapfiend.plugins import load_plugins
     25  from doapfiend.utils import COLOR
     26  from doapfiend.__init__ import __version__ as VERSION
     27  from doapfiend.doaplib import print_doap
     28
     29
     30 -class DoapFiend(object):
     31
     32      '''`DoapFiend` class'''
     33
     34 -    def __init__(self):
     35          '''Initialize attributes, set logger'''
     36          self.doap = None
     37          self.options = None
     38          self.log = logging.getLogger('doapfiend')
     39          self.log.addHandler(logging.StreamHandler())
     40          #Cache list of all plugins
     41          self.plugins = list(load_plugins(others=True))
     42
     43 -    def get_plugin(self, method):
     44          """
     45          Return plugin object if CLI option is activated and method exists
     46
     47          @param method: name of plugin's method we're calling
     48          @type method: string
     49
     50          @returns: list of plugins with `method`
     51
     52          """
     53          all_plugins = []
     54          for plugin_obj in self.plugins:
     55              plugin = plugin_obj()
     56              plugin.configure(self.options, None)
     57              if plugin.enabled:
     58                  if not hasattr(plugin, method):
     59                      plugin = None
     60                  else:
     61                      all_plugins.append(plugin)
     62          return all_plugins
     63
     64 -    def set_log_level(self):
     65          '''Set log level according to command-line options'''
     66          if self.options.debug:
     67              self.log.setLevel(logging.DEBUG)
     68
     69 -    def print_doap(self, doap_xml):
     70          '''
     71          Print doap as n3, rdf/xml, plain text or using serialization plugin
     72
     73          @param doap_xml: DOAP in RDF/XML serialization
     74          @type doap_xml: text
     75
     76          @rtype: None
     77          @return: Just displays DOAP
     78
     79          '''
     80          #Find all plugins that are enabled on the command-line and have a
     81          #`serialize` method. If none are enabled, default to plain text
     82          plugins = self.get_plugin('serialize')
     83          if len(plugins) == 0:
     84              serializer = None
     85          else:
     86              #Choose first serializer in case they try more than one
     87              serializer = plugins[0].serialize
     88          if self.options.write:
     89              filename = self.options.write
     90          else:
     91              filename = None
     92          print_doap(doap_xml, serializer=serializer, filename=filename,
     93                  color=not self.options.no_color)
     94
     95 -    def get_search_plugin(self):
     96          '''
     97          Return active search plugin callable
     98
     99          @rtype: callable
    100          @returns: A callable object that fetches for DOAP
    101          '''
    102          plugins = self.get_plugin('search')
    103          if len(plugins) == 1:
    104              return plugins[0].search
    105
    106 -    def run(self):
    107          '''
    108          Run doapfiend command
    109
    110          Find the active plugin that has a 'search' method and run it,
    111          then output the DOAP with print_doap, using the active plugin
    112          with a 'serializer' method.
    113
    114
    115          @rtype: int
    116          @returns: 0 success or 1 failure
    117
    118          '''
    119          opt_parser = self.setup_opt_parser()
    120          (self.options, remaining_args) = opt_parser.parse_args()
    121          if remaining_args:
    122              opt_parser.print_help()
    123          self.set_log_level()
    124
    125          if self.options.doapfiend_version:
    126              return doapfiend_version()
    127
    128          if self.options.no_color:
    129              for this in COLOR:
    130                  COLOR[this] = '\x1b[0m'
    131          search_func = self.get_search_plugin()
    132          if search_func:
    133              doap = search_func()
    134              if doap:
    135                  return self.print_doap(doap)
    136          else:
    137              opt_parser.print_help()
    138          return 1
    139
    140 -    def setup_opt_parser(self):
    141          '''
    142          Setup the optparser
    143
    144          @rtype: opt_parser.OptionParser
    145          @return: Option parser
    146
    147          '''
    148          usage = 'usage: %prog [options]'
    149          opt_parser = optparse.OptionParser(usage=usage)
    150          group_search = optparse.OptionGroup(opt_parser,
    151                  'Search options',
    152                  'Options for searching for DOAP')
    153
    154          opt_parser.add_option('--version', action='store_true', dest=
    155                                'doapfiend_version', default=False, help=
    156                                'Show doapfiend version and exit.')
    157
    158          opt_parser.add_option('-P', '--http-proxy', action='store', dest=
    159                                'proxy', default=False, help=
    160                                'Specify http proxy URL if you use one.')
    161
    162          group_output = optparse.OptionGroup(opt_parser,
    163                  'Output options',
    164                  'Choose these options to change default output behavior')
    165
    166          group_output.add_option('--debug', action='store_true', dest=
    167                                'debug', default=False, help=
    168                                'Show debugging information')
    169
    170          group_output.add_option('-w', '--write', action='store', dest=
    171                                'write', default=False, help=
    172                                'Write DOAP to a file instead of displaying it.',
    173                                metavar='FILENAME')
    174
    175          group_output.add_option('-b', '--brief', action='store_true', dest=
    176                                'brief', default=False, help=
    177                                'Show less output.')
    178
    179          group_output.add_option('-C', '--no-color', action='store_true', dest=
    180                                'no_color', default=False, help=
    181                                "Don't use color in output")
    182
    183          # add opts from plugins
    184          for plugcls in self.plugins:
    185              plug = plugcls()
    186              plug.add_options(opt_parser, group_output, group_search)
    187          opt_parser.add_option_group(group_search)
    188          opt_parser.add_option_group(group_output)
    189          return opt_parser
    190
    191
    192 -def doapfiend_version():
    193      '''Print doapfiend version'''
    194      print VERSION
    195
    196
    197 -def main():
    198      '''Let's do it.'''
    199      my_doapfiend = DoapFiend()
    200      return my_doapfiend.run()
    201
    202
    203  if __name__ == '__main__':
    204      sys.exit(main())
    205