doapfiend.plugins.fields

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

# Source Code for Module doapfiend.plugins.fields

      1  #!/usr/bin/env python
      2
      3  # pylint: disable-msg=W0221,R0201
      4  """
      5
      6  Plain text serializer
      7  =====================
      8
      9  This plugin outputs DOAP in human-readable plain text
     10
     11  """
     12
     13  __docformat__ = 'epytext'
     14
     15  import logging
     16
     17  from rdflib import Namespace
     18  from rdfalchemy import rdfSubject
     19
     20  from doapfiend.plugins.base import Plugin
     21  from doapfiend.utils import COLOR
     22  from doapfiend.doaplib import load_graph
     23
     24
     25  FOAF = Namespace("http://xmlns.com/foaf/0.1/")
     26
     27  LOG = logging.getLogger('doapfiend')
     28
     29
     30 -class OutputPlugin(Plugin):
     31
     32      """Class for formatting DOAP output"""
     33
     34      #This will be the opt_parser option (--fields)
     35      name = "fields"
     36      enabled = False
     37      enable_opt = name
     38
     39 -    def __init__(self):
     40          '''Setup Plain Text OutputPlugin class'''
     41          super(OutputPlugin, self).__init__()
     42          self.options = None
     43
     44 -    def add_options(self, parser, output, search):
     45          """Add plugin's options to doapfiend's opt parser"""
     46          output.add_option('--%s' % self.name,
     47                  action='store',
     48                  dest=self.enable_opt,
     49                  help='Output specific DOAP fields as plain text')
     50          return parser, output, search
     51
     52 -    def serialize(self, doap_xml, color=False):
     53          '''
     54          Serialize RDF/XML DOAP as N3 syntax
     55
     56          @param doap_xml: DOAP in RDF/XML serialization
     57          @type doap_xml: string
     58
     59          @rtype: unicode
     60          @return: DOAP in plain text
     61          '''
     62          if hasattr(self.options, 'no_color'):
     63              color = not self.options.no_color
     64          if not color:
     65              #This has already been done if we're called from cli.py
     66              #Fix me: Need to think on this.
     67              for this in COLOR:
     68                  COLOR[this] = '\x1b[0m'
     69
     70          if hasattr(self.options, 'quiet'):
     71              brief = self.options.quiet
     72          else:
     73              brief = False
     74
     75          doap = load_graph(doap_xml)
     76          fields = self.options.fields.split(',')
     77          #print fields
     78          out = ''
     79          for field in fields:
     80              if '-' in field:
     81                  field = field.replace('-', '_')
     82              field = field.strip()
     83              if '.' in field:
     84                  repo, field = field.split('.')
     85                  text = print_repos(doap, repo, field)
     86              elif field == 'releases':
     87                  text = get_releases(doap, brief)
     88              if field in ['maintainer', 'developer', 'documenter', 'helper',
     89                      'tester', 'translator']:
     90                  text = get_people(doap, field)
     91              else:
     92                  try:
     93                      text = getattr(doap, field)
     94                  except AttributeError:
     95                      LOG.warn("No such attribute: %s" % field)
     96                      text = None
     97                  if not text:
     98                      continue
     99                  if isinstance(text, list):
    100                      text = print_list(doap, field)
    101                  else:
    102                      text = print_field(doap, field)
    103              out += text + '\n'
    104          return out.rstrip()
    105
    106 -def print_list(doap, field):
    107      '''
    108      Print list of DOAP attributes
    109
    110      @param doap: DOAP in RDF/XML
    111      @type doap: text
    112
    113      @param field: DOAP attribute to be printed
    114      @type field: text
    115
    116      @rtype: text
    117      @returns: Field to be printed
    118      '''
    119      #Can have multiple values per attribute
    120      text = ""
    121      for thing in getattr(doap, field):
    122          if isinstance(thing, rdfSubject):
    123              text += thing.resUri
    124          else:
    125              #unicode object
    126              thing = thing.strip()
    127              text += thing
    128      return text
    129
    130 -def print_field(doap, field):
    131      '''
    132      Print single field
    133
    134      @param doap: DOAP in RDF/XML
    135      @type doap: text
    136
    137      @param field: DOAP attribute to be printed
    138      @type field: text
    139
    140      @rtype: text
    141      @returns: Field to be printed
    142      '''
    143      text = getattr(doap, field)
    144      if isinstance(text, rdfSubject):
    145          return text.resUri.strip()
    146      else:
    147          return text.strip()
    148
    149 -def print_repos(doap, repo, field):
    150      '''Prints DOAP repository metadata'''
    151      if repo == 'cvs':
    152          if hasattr(doap.cvs_repository, field):
    153              return getattr(doap.cvs_repository, field)
    154
    155      if repo == 'svn':
    156          if field == 'browse':
    157              field = 'svn_browse'
    158          if hasattr(doap.svn_repository, field):
    159              text = getattr(doap.svn_repository, field)
    160              if text:
    161                  if isinstance(text, rdfSubject):
    162                      return text.resUri
    163                  else:
    164                      return text.strip()
    165      return ''
    166
    167 -def get_people(doap, job):
    168      '''Print people for a particular job '''
    169      out = ''
    170      if hasattr(doap, job):
    171          attribs = getattr(doap, job)
    172          if len(attribs) > 0:
    173              peeps = []
    174              for attr in attribs:
    175                  if attr[FOAF.mbox] is None:
    176                      person = "%s" % attr[FOAF.name]
    177                  else:
    178                      mbox = attr[FOAF.mbox].resUri
    179                      if mbox.startswith('mailto:'):
    180                          mbox = mbox[7:]
    181                          person = "%s <%s>" % (attr[FOAF.name], mbox)
    182                      else:
    183                          LOG.debug("mbox is invalid: %s" % mbox)
    184                          person = "%s" % attr[FOAF.name]
    185                  peeps.append(person)
    186              out += ", ".join([p for p in peeps])
    187      return out
    188
    189
    190 -def get_releases(doap, brief=False):
    191      '''Print DOAP package release metadata'''
    192      out = ''
    193      if hasattr(doap, 'releases') and len(doap.releases) != 0:
    194          if not brief:
    195              out += COLOR['bold'] + "Releases:" + COLOR['normal'] + '\n'
    196          for release in doap.releases:
    197              if release.name:
    198                  out += COLOR['bold'] + COLOR['cyan'] + release.name + \
    199                      COLOR['normal'] + '\n'
    200              if hasattr(release, 'created') and release.created is not None:
    201                  created = release.created
    202              else:
    203                  created = ''
    204              out += COLOR['cyan'] + '  ' + release.revision + ' ' + \
    205                      COLOR['normal'] + created + '\n'
    206              if not brief:
    207                  if hasattr(release, 'changelog'):
    208                      if release.changelog:
    209                          out += COLOR['yellow'] + release.changelog + \
    210                                  COLOR['normal'] + '\n'
    211
    212              for frel in release.file_releases:
    213                  out += '   %s' % frel.resUri + '\n'
    214      return out
    215