doapfiend.plugins.xml

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

# Source Code for Module doapfiend.plugins.xml

     1  #!/usr/bin/env python
     2
     3  # pylint: disable-msg=W0221,R0201
     4
     5  """
     6
     7  Serialize DOAP as XML/RDF
     8  =========================
     9
    10  This plugin outputs DOAP in RDF/XML
    11  It basically does nothing because all DOAP today is in RDF/XML.
    12  In the future this may take N3, Turtle, RDFa etc. and convert it to RDF/XML.
    13
    14  """
    15
    16  __docformat__ = 'epytext'
    17
    18  from elementtree import ElementTree
    19
    20  from doapfiend.plugins.base import Plugin
    21
    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 = "xml"
    29      enabled = False
    30      enable_opt = None
    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          output.add_option('-x', '--%s' % self.name,
    40                  action='store_true',
    41                  dest=self.enable_opt,
    42                  help='Output DOAP as RDF/XML')
    43          return parser, output, search
    44
    45 -    def serialize(self, doap_xml, color=False):
    46          '''
    47          Serialize RDF/XML DOAP as N3 syntax
    48
    49          Since the only input we currently have is XML, all this really does
    50          is parse the XML and raise an exception if it's invalid.
    51          When we do content negotiation/accept N3 etc., this will serialize.
    52
    53          @param doap_xml: DOAP in RDF/XML serialization
    54          @type doap_xml: string
    55
    56          @rtype: unicode
    57          @returns: DOAP
    58          '''
    59          #This will raise ExpatError if we have invalid XML
    60          #(from xml.parsers.expat import ExpatError)
    61          #We could format/pretty print here but we just return exactly what
    62          #was fetched.
    63          ElementTree.fromstring(doap_xml)
    64          if hasattr(self.options, 'no_color'):
    65              color = not self.options.no_color
    66          if color:
    67              #pygments plugin fools pylint
    68              # pylint: disable-msg=E0611
    69              try:
    70                  from pygments import highlight
    71                  from pygments.lexers import XmlLexer
    72                  from pygments.formatters import TerminalFormatter
    73              except ImportError:
    74                  return doap_xml
    75              return highlight(doap_xml,
    76                      XmlLexer(),
    77                      TerminalFormatter(full=False))
    78          else:
    79              return doap_xml
    80