1
2
3
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
43
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
57
58 """Class for formatting DOAP output"""
59
60
61 name = "n3"
62 enabled = False
63 enable_opt = None
64
69
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
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