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
115 fields = ('name', 'shortname', 'homepage', 'shortdesc',
116 'description', 'old_homepage', 'created',
117 'download_mirror')
118
119 fields_verbose = ('license', 'programming_language',
120 'bug_database', 'screenshots', 'oper_sys',
121 'wiki', 'download_page', 'mailing_list')
122
123 for fld in fields:
124 self.print_field(fld)
125 if not self.brief:
126 for fld in fields_verbose:
127 self.print_field(fld)
128
149
168
170 '''Print all people involved in the project'''
171 people = ['maintainer', 'developer', 'documenter', 'helper',
172 'tester', 'translator']
173 for job in people:
174 if hasattr(self.doap, job):
175 attribs = getattr(self.doap, job)
176 if len(attribs) > 0:
177 peeps = []
178 for attr in attribs:
179 if attr[FOAF.mbox] is None:
180 person = "%s" % attr[FOAF.name]
181 else:
182 mbox = attr[FOAF.mbox].resUri
183 if mbox.startswith('mailto:'):
184 mbox = mbox[7:]
185 person = "%s <%s>" % (attr[FOAF.name], mbox)
186 else:
187 LOG.debug("mbox is invalid: %s" % mbox)
188 person = "%s" % attr[FOAF.name]
189 peeps.append(person)
190 label = job.capitalize() + "s:"
191
192 self.write(misc_field(label,
193 ", ".join([p for p in peeps])))
194
196 '''
197 Print a DOAP element
198
199 @param name: A misc DOAP element
200 @type name: string, list or RDFSubject
201
202 @rtype: None
203 @return: Nothing
204 '''
205 if not hasattr(self.doap, name):
206 return
207 attr = getattr(self.doap, name)
208 if attr is [] or attr is None:
209 return
210
211 label = '%s' % COLOR['bold'] + pretty_name(name) + \
212 COLOR['normal'] + ':'
213 label = label.ljust(21)
214 if isinstance(attr, list):
215
216 text = ""
217 for thing in getattr(self.doap, name):
218 if isinstance(thing, rdfSubject):
219 text += thing.resUri + "\n"
220 else:
221
222 thing = thing.strip()
223 text += thing + "\n"
224 else:
225 text = getattr(self.doap, name)
226 if isinstance(text, rdfSubject):
227 text = text.resUri
228 else:
229 text = text.strip()
230 if text:
231 self.write(textwrap.fill('%s %s' % (label, text),
232 initial_indent='',
233 subsequent_indent = ' '))
234
235
237 """
238 Convert DOAP element name to pretty printable label
239 Shorten some labels for formatting purposes
240
241 @param field: Text to be formatted
242 @type field: C{string}
243
244 @return: formatted string
245 @rtype: string
246 """
247 if field == 'programming_language':
248 field = 'Prog. Lang.'
249 elif field == 'created':
250 field = 'DOAP Created'
251 else:
252 field = field.capitalize()
253 field = field.replace('_', ' ')
254 field = field.replace('-', ' ')
255 return field
256
257
259 '''
260 Print colorized and justified single label value pair
261
262 @param label: A label
263 @type label: string
264
265 @param text: Text to print
266 @type text: string
267
268 @rtype: string
269 @return: Colorized, left-justified text with label
270 '''
271 label = label.ljust(13)
272 label = COLOR['bold'] + label + COLOR['normal']
273 return '%s %s' % (label, text)
274