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 plain text
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 if not color:
67
68
69 for this in COLOR:
70 COLOR[this] = '\x1b[0m'
71 if hasattr(self.options, 'quiet'):
72 brief = self.options.quiet
73 else:
74 brief = False
75
76 printer = DoapPrinter(load_graph(doap_xml, get_list=True), brief, color)
77 return printer.print_doap()
78
79
81
82 '''Prints DOAP in human readable text'''
83
84 - def __init__(self, doap, brief=False, color=False):
85 '''Initialize attributes'''
86 self.brief = brief
87 self.doap_list = doap
88 self.doap = None
89 self.text = StringIO()
90 self.color = color
91
93 '''
94 Write to DOAP output file object
95 '''
96 self.text.write(text.encode('utf-8') + '\n')
97
99 '''
100 Serialize DOAP in human readable text, optionally colorized
101
102 @rtype: unicode
103 @return: DOAP as plain text
104 '''
105 for doap in self.doap_list:
106 self.doap = doap
107 self.print_misc()
108 if self.brief:
109 return
110 self.print_people()
111 self.print_repos()
112 self.print_releases()
113 doap = self.text.getvalue()
114 self.text.close()
115 return doap
116
118 '''Prints basic DOAP metadata'''
119
120
121
122
123 fields = ('name', 'shortname', 'homepage', 'shortdesc',
124 'description', 'old_homepage', 'created',
125 'download_mirror')
126
127 fields_verbose = ('license', 'programming_language',
128 'bug_database', 'screenshots', 'oper_sys',
129 'wiki', 'download_page', 'mailing_list')
130
131 for fld in fields:
132 self.print_field(fld)
133 if not self.brief:
134 for fld in fields_verbose:
135 self.print_field(fld)
136
157
180
182 '''Print all people involved in the project'''
183 people = ['maintainer', 'developer', 'documenter', 'helper',
184 'tester', 'translator']
185 for job in people:
186 if hasattr(self.doap, job):
187 attribs = getattr(self.doap, job)
188 if len(attribs) > 0:
189 peeps = []
190 for attr in attribs:
191 if attr[FOAF.mbox] is None:
192 person = "%s" % attr[FOAF.name]
193 else:
194 mbox = attr[FOAF.mbox].resUri
195 if mbox.startswith('mailto:'):
196 mbox = mbox[7:]
197 person = "%s <%s>" % (attr[FOAF.name], mbox)
198 else:
199 LOG.debug("mbox is invalid: %s" % mbox)
200 person = "%s" % attr[FOAF.name]
201 peeps.append(person)
202 label = job.capitalize() + "s:"
203
204 self.write(misc_field(label,
205 ", ".join([p for p in peeps])))
206
208 '''
209 Print a DOAP element
210
211 @param name: A misc DOAP element
212 @type name: string, list or RDFSubject
213
214 @rtype: None
215 @return: Nothing
216 '''
217 if not hasattr(self.doap, name):
218 return
219 attr = getattr(self.doap, name)
220 if attr is [] or attr is None:
221 return
222
223 label = '%s' % COLOR['bold'] + pretty_name(name) + \
224 COLOR['normal'] + ':'
225 label = label.ljust(21)
226 if isinstance(attr, list):
227
228 text = ""
229 for thing in getattr(self.doap, name):
230 if isinstance(thing, rdfSubject):
231 text += thing.resUri + "\n"
232 else:
233
234 thing = thing.strip()
235 text += thing + "\n"
236 else:
237 text = getattr(self.doap, name)
238 if isinstance(text, rdfSubject):
239 text = text.resUri
240 else:
241 text = text.strip()
242 if text:
243 if text.startswith('http://'):
244 self.write('%s %s' % (label, text.strip()))
245 else:
246 self.write(textwrap.fill('%s %s' % (label, text),
247 initial_indent='',
248 subsequent_indent = ' '))
249
250
252 """
253 Convert DOAP element name to pretty printable label
254 Shorten some labels for formatting purposes
255
256 @param field: Text to be formatted
257 @type field: C{string}
258
259 @return: formatted string
260 @rtype: string
261 """
262 if field == 'programming_language':
263 field = 'Prog. Lang.'
264 elif field == 'created':
265 field = 'DOAP Created'
266 else:
267 field = field.capitalize()
268 field = field.replace('_', ' ')
269 field = field.replace('-', ' ')
270 return field
271
272
274 '''
275 Print colorized and justified single label value pair
276
277 @param label: A label
278 @type label: string
279
280 @param text: Text to print
281 @type text: string
282
283 @rtype: string
284 @return: Colorized, left-justified text with label
285 '''
286 label = label.ljust(13)
287 label = COLOR['bold'] + label + COLOR['normal']
288 return '%s %s' % (label, text)
289