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 Note: Returns string for non-color and unicode for colored text 30 31 @param xml_text: XML/RDF 32 @type xml_text: string 33 34 @rtype: unicode or string 35 @return: DOAP in Notation 3 36 ''' 37 store = ConjunctiveGraph() 38 graph = store.parse(StringIO(xml_text), publicID=None, format="xml") 39 notation3 = graph.serialize(format="n3") 40 41 if color: 42 #pygments plugin fools pylint 43 # pylint: disable-msg=E0611 44 try: 45 from pygments import highlight 46 from doapfiend.lexers import Notation3Lexer 47 from pygments.formatters import TerminalFormatter 48 except ImportError: 49 return notation3 50 return highlight(notation3, 51 Notation3Lexer(), 52 TerminalFormatter(full=False)) 53 else: 54 return notation3
55
56 -class OutputPlugin(Plugin):
57 58 """Class for formatting DOAP output""" 59 60 #This will be the opt_parser option (--n3) 61 name = "n3" 62 enabled = False 63 enable_opt = None 64
65 - def __init__(self):
66 '''Setup N3 OutputPlugin class''' 67 super(OutputPlugin, self).__init__() 68 self.options = None
69
70 - def serialize(self, doap_xml, color=False):
71 ''' 72 Serialize RDF/XML DOAP as N3 syntax 73 74 @param doap_xml: DOAP in RDF/XML serialization 75 @type doap_xml: string 76 77 @rtype: unicode 78 @return: DOAP in Notation 3 79 ''' 80 if hasattr(self, 'options') and hasattr(self.options, 'no_color'): 81 color = not self.options.no_color 82 return get_n3(doap_xml, color)
83
84 - def add_options(self, parser, output, search):
85 """Add plugin's options to doapfiend's opt parser""" 86 output.add_option('-n', '--%s' % self.name, 87 action='store_true', 88 dest=self.enable_opt, 89 help='Output DOAP as Notation 3') 90 return parser, output, search
91