doapfiend.plugins.homepage

Package doapfiend :: Package plugins :: Module homepage |
[hide private]
[frames] | no frames]

# Source Code for Module doapfiend.plugins.homepage

     1  #!/usr/bin/env python
     2
     3  # pylint: disable-msg=W0221,R0201
     4
     5  """
     6
     7  homepage
     8  ========
     9
    10  Fetches DOAP by searching doapspace.org by a project's homepage.
    11
    12  """
    13
    14  __docformat__ = 'epytext'
    15
    16  import logging
    17
    18  from doapfiend.plugins.base import Plugin
    19  from doapfiend.doaplib import fetch_doap, query_by_homepage
    20
    21  LOG = logging.getLogger("doapfiend")
    22
    23 -class OutputPlugin(Plugin):
    24
    25      """Class for formatting DOAP output"""
    26
    27      #This will be the opt_parser option (--xml) in the output group
    28      name = "homepage"
    29      enabled = False
    30      enable_opt = name
    31
    32 -    def __init__(self):
    33          '''Setup RDF/XML OutputPlugin class'''
    34          super(OutputPlugin, self).__init__()
    35          self.options = None
    36
    37 -    def add_options(self, parser, output, search):
    38          """Add plugin's options to doapfiend's opt parser"""
    39          search.add_option('-o', '--%s' % self.name,
    40                  action='store',
    41                  dest=self.enable_opt,
    42                  help="Search for DOAP by a project's homepage",
    43                  metavar='HOMEPAGE_URL')
    44          return parser, output, search
    45
    46 -    def search(self):
    47          '''
    48          Get DOAP given a project's homepage
    49
    50          @rtype: unicode
    51          @return: DOAP
    52          '''
    53          return do_search(self.options.homepage)
    54
    55 -def do_search(homepage):
    56      '''
    57      Get DOAP given a project's homepage
    58
    59      @param homepage: Project homepage URL
    60
    61      @rtype: unicode
    62      @return: DOAP
    63      '''
    64      resp = query_by_homepage(homepage)
    65      LOG.debug(resp)
    66      if len(resp) == 0:
    67          LOG.error("Not found: %s" % homepage)
    68          return
    69      elif len(resp) == 1:
    70          url = resp[0][1]
    71      else:
    72          #Multiple, send warning and use first 'external' if any
    73          LOG.warn("Warning: Multiple DOAP found.")
    74          url = None
    75          for this in resp:
    76              LOG.warn(this)
    77              if not url:
    78                  #Keep first one if there is no external DOAP
    79                  url = this[1]
    80              if this[0] == 'ex':
    81                  url = this[1]
    82          LOG.warn("Using %s" % url)
    83      return fetch_doap(url)
    84