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

Source Code for Module doapfiend.plugins.n3

 1  #!/usr/bin/env python 
 2   
 3  # pylint: disable-msg=W0221,R0201 
 4   
 5  """ 
 6   
 7  Serializer for N3 (Notation 3) 
 8  ============================== 
 9   
10  This is a plugin for formatting DOAP output as N3 (Notation 3) syntax. 
11   
12  """ 
13   
14  __docformat__ = 'epytext' 
15   
16  import logging 
17  from cStringIO import StringIO 
18   
19  from rdflib import ConjunctiveGraph 
20   
21  from doapfiend.plugins.base import Plugin 
22   
23  LOG = logging.getLogger(__name__) 
24   
25   
26 -def get_n3(xml_text, color=False):
27 ''' 28 Return N3 (Notation 3) text 29 30 @param xml_text: XML/RDF 31 @type xml_text: string 32 33 @rtype: unicode 34 @return: DOAP in Notation 3 35 ''' 36 store = ConjunctiveGraph() 37 graph = store.parse(StringIO(xml_text), publicID=None, format="xml") 38 notation3 = graph.serialize(format="n3") 39 40 if color: 41 #pygments plugin fools pylint 42 # pylint: disable-msg=E0611 43 try: 44 from pygments import highlight 45 from doapfiend.lexers import Notation3Lexer 46 from pygments.formatters import TerminalFormatter 47 except ImportError: 48 return notation3 49 return highlight(notation3, 50 Notation3Lexer(), 51 TerminalFormatter(full=False)) 52 else: 53 return notation3
54
55 -class OutputPlugin(Plugin):
56 57 """Class for formatting DOAP output""" 58 59 #This will be the opt_parser option (--n3) 60 name = "n3" 61 enabled = False 62 enable_opt = None 63
64 - def __init__(self):
65 '''Setup N3 OutputPlugin class''' 66 super(OutputPlugin, self).__init__() 67 self.options = None
68
69 - def serialize(self, doap_xml, color=False):
70 ''' 71 Serialize RDF/XML DOAP as N3 syntax 72 73 @param doap_xml: DOAP in RDF/XML serialization 74 @type doap_xml: string 75 76 @rtype: unicode 77 @return: DOAP in Notation 3 78 ''' 79 if hasattr(self, 'options') and hasattr(self.options, 'no_color'): 80 color = not self.options.no_color 81 return get_n3(doap_xml, color)
82
83 - def add_options(self, parser, output, search):
84 """Add plugin's options to doapfiend's opt parser""" 85 output.add_option('-n', '--%s' % self.name, 86 action='store_true', 87 dest=self.enable_opt, 88 help='Output DOAP as Notation 3') 89 return parser, output, search
90