1
2
3
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 import textwrap
17 from cStringIO import StringIO
18
19 from rdflib import Namespace
20 from rdfalchemy import rdfSubject
21
22 from doapfiend.plugins.base import Plugin
23 from doapfiend.utils import COLOR
24 from doapfiend.doaplib import load_graph
25
26
27 FOAF = Namespace("http://xmlns.com/foaf/0.1/")
28
29 LOG = logging.getLogger(__name__)
30
31
33
34 """Class for formatting DOAP output"""
35
36
37 name = "text"
38 enabled = False
39 enable_opt = None
40
42 '''Setup Plain Text OutputPlugin class'''
43 super(OutputPlugin, self).__init__()
44 self.options = None
45
47 """Add plugin's options to doapfiend's opt parser"""
48 output.add_option('--%s' % self.name,
49 action='store_true',
50 dest=self.enable_opt,
51 help='Output DOAP as plain text (Default)')
52 return parser, output, search
53
55 '''
56 Serialize RDF/XML DOAP as N3 syntax
57
58 @param doap_xml: DOAP in RDF/XML serialization
59 @type doap_xml: string
60
61 @rtype: unicode
62 @return: DOAP in plain text
63 '''
64 if hasattr(self.options, 'no_color'):
65 color = not self.options.no_color
66 brief = self.options.brief
67 else:
68 brief = False
69
70 printer = DoapPrinter(load_graph(doap_xml), brief, color)
71 return printer.print_doap()
72
73
75
76 '''Prints DOAP in human readable text'''
77
78 - def __init__(self, doap, brief=False, color=False):
79 '''Initialize attributes'''
80 self.brief = brief
81 self.doap = doap
82 self.text = StringIO()
83 self.color = color
84
86 '''
87 Write to DOAP output file object
88 '''
89 self.text.write(text.encode('utf-8') + '\n')
90
92 '''
93 Serialize DOAP in human readable text, optionally colorized
94
95 @rtype: unicode
96 @return: DOAP as plain text
97 '''
98
99 self.print_misc()
100 if self.brief:
101 return
102 self.print_people()
103 self.print_repos()
104 self.print_releases()
105 doap = self.text.getvalue()
106 self.text.close()
107 return doap
108
110 '''Prints basic DOAP metadata'''
111
112
113
114 fields = (('name', False, True), ('shortname', False, True),
115 ('homepage', False, False), ('shortdesc', True, True),
116 ('description', True, True),
117 ('old_homepage', True, False), ('created', False, True),
118 ('download_mirror', False, False))
119
120 fields_verbose = (('license', True, True),
121 ('programming_language', True, True),
122 ('bug_database', False, False),
123 ('screenshots', True, False), ('oper_sys', True, True),
124 ('wiki', True, False), ('download_page', False, False),
125 ('mailing_list', True, False))
126
127 for fld in fields:
128 self.print_field(fld)
129 if not self.brief:
130 for fld in fields_verbose:
131 self.print_field(fld)
132
149
160
162 '''Print all people involved in the project'''
163 people = ['maintainer', 'developer', 'documenter', 'helper',
164 'tester', 'translator']
165 for job in people:
166 if hasattr(self.doap, job):
167 attribs = getattr(self.doap, job)
168 if len(attribs) > 0:
169 peeps = []
170 for attr in attribs:
171 if attr[FOAF.mbox] is None:
172 person = "%s" % attr[FOAF.name]
173 else:
174 mbox = attr[FOAF.mbox].resUri
175 if mbox.startswith('mailto:'):
176 mbox = mbox[7:]
177 person = "%s <%s>" % (attr[FOAF.name], mbox)
178 else:
179 LOG.debug("mbox is invalid: %s" % mbox)
180 person = "%s" % attr[FOAF.name]
181 peeps.append(person)
182 label = job.capitalize() + "s:"
183
184 self.write(misc_field(label,
185 ", ".join([p for p in peeps])))
186
188 '''
189 Print a DOAP element
190
191 @param field: A misc DOAP element
192 @type field: string
193
194 @rtype: None
195 @return: Nothing
196 '''
197 name, multi, wrap = field
198 if not hasattr(self.doap, name):
199 return
200 attr = getattr(self.doap, name)
201 if attr == [] or attr is None:
202 return
203 label = '%s' % COLOR['bold'] + pretty_name(name) + \
204 COLOR['normal'] + ':'
205 label = label.ljust(21)
206 if multi:
207
208 text = ""
209 for thing in getattr(self.doap, name):
210 if isinstance(thing, rdfSubject):
211 text += thing.resUri + "\n"
212 else:
213
214 thing = thing.strip()
215 text += thing + "\n"
216
217 else:
218 text = getattr(self.doap, name)
219 if isinstance(text, rdfSubject):
220 text = text.resUri
221 else:
222 text = text.strip()
223 if wrap:
224 self.write(textwrap.fill('%s %s' % (label, text),
225 initial_indent='',
226 subsequent_indent = ' '))
227
228 else:
229 self.write('%s %s' % (label, text))
230
231
233 """
234 Convert DOAP element name to pretty printable label
235 Shorten some labels for formatting purposes
236
237 @param field: Text to be formatted
238 @type field: C{string}
239
240 @return: formatted string
241 @rtype: string
242 """
243 if field == 'programming_language':
244 field = 'Prog. Lang.'
245 if field == 'created':
246 field = 'DOAP Created'
247 field = field.capitalize()
248 field = field.replace('_', ' ')
249 field = field.replace('-', ' ')
250 return field
251
252
254 '''
255 Print colorized and justified single label value pair
256
257 @param label: A label
258 @type label: string
259
260 @param text: Text to print
261 @type text: string
262
263 @rtype: string
264 @return: Colorized, left-justified text with label
265 '''
266 label = label.ljust(13)
267 label = COLOR['bold'] + label + COLOR['normal']
268 return '%s %s' % (label, text)
269