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.utils import color, NotFoundError
     25  from doapfiend.__init__ import __version__ as VERSION
     26  from doapfiend.doaplib import (get_by_pkg_index, query_by_homepage,
     27          display_doap, fetch_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
     41 -    def by_homepage(self, url):
     42          '''
     43          Print DOAP given a project's homepage
     44
     45          @param url: URL of homepage of a project
     46          @type url: string
     47
     48          @rtype: None
     49          @return: Print a DOAP profile
     50
     51          '''
     52          resp = query_by_homepage(url)
     53          self.log.debug(resp)
     54          if len(resp) == 0:
     55              self.log.error("Not found: %s" % url)
     56              return 1
     57          elif len(resp) == 1:
     58              url = resp[0][1]
     59          else:
     60              #Multiple, send warning and use first 'external' if any
     61              self.log.error("Warning: Multiple DOAP found.")
     62              url = None
     63              for this in resp:
     64                  self.log.error(this)
     65                  if not url:
     66                      #Keep first one if there is no external DOAP
     67                      url = this[1]
     68                  if this[0] == 'ex':
     69                      url = this[1]
     70              self.log.error("Using %s" % url)
     71          doap_text = fetch_doap(url, self.options.proxy)
     72          self.log.debug(doap_text)
     73          if doap_text:
     74              self.print_doap(doap_text)
     75
     76 -    def set_log_level(self):
     77          '''Set log level according to command-line options'''
     78          if self.options.debug:
     79              self.log.setLevel(logging.DEBUG)
     80
     81 -    def doapfiend_version(self):
     82          '''Print doapfiend version'''
     83          #pylint: disable-msg=R0201
     84          print VERSION
     85
     86 -    def by_pkg_index(self, index, project_name):
     87          '''
     88          Display DOAP for a package index project name
     89          index examples: sf - SourceForge, fm - Freshmeat
     90
     91          @param index: Package index two letter abbreviation
     92          @type index: string
     93
     94          @param project_name: project name
     95          @type project_name: string
     96
     97          @rtype: None
     98          @return: Nothing, just prints DOAP
     99
    100          '''
    101          try:
    102              doap = get_by_pkg_index(index, project_name, self.options.proxy)
    103          except NotFoundError:
    104              self.log.error("Not found: %s" % project_name)
    105              return 1
    106          self.print_doap(doap)
    107
    108 -    def show_doap(self, url):
    109          '''
    110          Display DOAP given its URL
    111
    112          @param url: URL of DOAP profile in RDF/XML serialization
    113          @type url: string
    114
    115          @rtype: None
    116          @return: Just displays DOAP
    117          '''
    118          try:
    119              doap_xml = fetch_doap(url, self.options.proxy)
    120          except NotFoundError:
    121              self.log.error("Not found: %s" % url)
    122              return 1
    123          self.print_doap(doap_xml)
    124
    125 -    def print_doap(self, doap_xml):
    126          '''
    127          Print doap in one of two serializations or plain text
    128
    129          @param doap_xml: DOAP in RDF/XML serialization
    130          @type doap_xml: text
    131
    132          @rtype: None
    133          @return: Just displays DOAP
    134
    135          '''
    136          if self.options.xml:
    137              serialize = 'xml'
    138          elif self.options.n3:
    139              serialize = 'n3'
    140          else:
    141              serialize = 'text'
    142          display_doap(doap_xml, serialize, self.options.brief)
    143
    144 -    def run(self):
    145          '''Run doapfiend command'''
    146          # Too many return statements:
    147          # pylint: disable-msg=R0911
    148
    149          opt_parser = setup_opt_parser()
    150          (self.options, remaining_args) = opt_parser.parse_args()
    151          if remaining_args:
    152              opt_parser.print_help()
    153          self.set_log_level()
    154
    155          if self.options.no_color:
    156              #Set it to normal code instead of '' for justification
    157              for this in color:
    158                  color[this] = '\x1b[0m'
    159
    160          if self.options.by_homepage:
    161              return self.by_homepage(url=self.options.by_homepage)
    162          elif self.options.display_doap:
    163              return self.show_doap(url=self.options.display_doap)
    164          elif self.options.doapfiend_version:
    165              return self.doapfiend_version()
    166          elif self.options.sourceforge:
    167              return self.by_pkg_index(index="sf",
    168                      project_name=self.options.sourceforge)
    169          elif self.options.freshmeat:
    170              return self.by_pkg_index(index="fm",
    171                      project_name=self.options.freshmeat)
    172          elif self.options.pypi:
    173              return self.by_pkg_index(index="py",
    174                      project_name=self.options.pypi)
    175          else:
    176              opt_parser.print_help()
    177              return 1
    178
    179
    180 -def setup_opt_parser():
    181      '''
    182      Setup the optparser
    183
    184      @rtype: opt_parser.OptionParser
    185      @return: Option parser
    186
    187      '''
    188
    189      usage = 'usage: %prog [options]'
    190      opt_parser = optparse.OptionParser(usage=usage)
    191      group_search = optparse.OptionGroup(opt_parser,
    192              'Search options',
    193              'Options for searching for DOAP')
    194
    195      opt_parser.add_option('--version', action='store_true', dest=
    196                            'doapfiend_version', default=False, help=
    197                            'Show doapfiend version and exit.')
    198
    199      opt_parser.add_option('-P', '--http-proxy', action='store', dest=
    200                            'proxy', default=False, help=
    201                            'Specify http proxy URL if you use one.')
    202
    203      group_search.add_option('-d', '--display-doap', action='store', dest=
    204                            'display_doap', default=False, help=
    205                            'Display DOAP you know the URL of or filename.',
    206                            metavar='URL')
    207
    208      group_search.add_option('-o', '--homepage', action='store', dest=
    209                            'by_homepage', default=False, help=
    210                            "Get DOAP by the project's homepage URL.",
    211                            metavar='HOMEPAGE_URL')
    212
    213      group_search.add_option('-f', '--freshmeat', action='store', dest=
    214                            'freshmeat', default=False, help=
    215                            'Display DOAP by its Freshmeat project name.',
    216                            metavar='PROJECT_NAME')
    217
    218      group_search.add_option('-s', '--sourceforge', action='store', dest=
    219                            'sourceforge', default=False, help=
    220                            'Display DOAP by its SourceForge project name.',
    221                            metavar='PROJECT_NAME')
    222
    223      group_search.add_option('-p', '--pypi', action='store', dest=
    224                            'pypi', default=False, help=
    225                            'Display DOAP by its Python Package Index pkg name.',
    226                            metavar='PROJECT_NAME')
    227
    228      group_display = optparse.OptionGroup(opt_parser,
    229              'Display options',
    230              'Choose these options to change default display behavior')
    231
    232      group_display.add_option('--debug', action='store_true', dest=
    233                            'debug', default=False, help=
    234                            'Show debugging information')
    235
    236      group_display.add_option('-b', '--brief', action='store_true', dest=
    237                            'brief', default=False, help=
    238                            'Show less output.')
    239
    240      group_display.add_option('-C', '--no-color', action='store_true', dest=
    241                            'no_color', default=False, help=
    242                            "Don't use color in output")
    243
    244      group_display.add_option('-n', '--n3', action='store_true',
    245                            dest='n3', default=False, help=
    246                            'Display DOAP in Notation Three syntax')
    247
    248      group_display.add_option('-x', '--xml', action='store_true', dest=
    249                            'xml', default=False, help=
    250                            'Display DOAP as RDF/XML')
    251
    252      opt_parser.add_option_group(group_search)
    253      opt_parser.add_option_group(group_display)
    254      return opt_parser
    255
    256
    257 -def main():
    258      '''Let's do it.'''
    259      my_doapfiend = DoapFiend()
    260      return my_doapfiend.run()
    261
    262
    263  if __name__ == '__main__':
    264      sys.exit(main())
    265