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
17 from rdflib import Namespace
18 from rdfalchemy import rdfSubject
19
20 from doapfiend.plugins.base import Plugin
21 from doapfiend.utils import COLOR
22 from doapfiend.doaplib import load_graph
23
24
25 FOAF = Namespace("http://xmlns.com/foaf/0.1/")
26
27 LOG = logging.getLogger('doapfiend')
28
29
31
32 """Class for formatting DOAP output"""
33
34
35 name = "fields"
36 enabled = False
37 enable_opt = name
38
40 '''Setup Plain Text OutputPlugin class'''
41 super(OutputPlugin, self).__init__()
42 self.options = None
43
45 """Add plugin's options to doapfiend's opt parser"""
46 output.add_option('--%s' % self.name,
47 action='store',
48 dest=self.enable_opt,
49 help='Output specific DOAP fields as plain text')
50 return parser, output, search
51
53 '''
54 Serialize RDF/XML DOAP as N3 syntax
55
56 @param doap_xml: DOAP in RDF/XML serialization
57 @type doap_xml: string
58
59 @rtype: unicode
60 @return: DOAP in plain text
61 '''
62 if hasattr(self.options, 'no_color'):
63 color = not self.options.no_color
64 if not color:
65
66
67 for this in COLOR:
68 COLOR[this] = '\x1b[0m'
69
70 if hasattr(self.options, 'quiet'):
71 brief = self.options.quiet
72 else:
73 brief = False
74
75 doap = load_graph(doap_xml)
76 fields = self.options.fields.split(',')
77
78 out = ''
79 for field in fields:
80 field = field.strip()
81 if '.' in field:
82 repo, field = field.split('.')
83 text = print_repos(doap, repo, field)
84 elif field == 'releases':
85 text = get_releases(doap, brief)
86 if field in ['maintainer', 'developer', 'documenter', 'helper',
87 'tester', 'translator']:
88 text = get_people(doap, field)
89 else:
90 try:
91 text = getattr(doap, field)
92 except AttributeError:
93 LOG.warn("No such attribute: %s" % field)
94 text = None
95 if not text:
96 continue
97 if isinstance(text, list):
98 text = print_list(doap, field)
99 else:
100 text = print_field(doap, field)
101 out += text + '\n'
102 return out.rstrip()
103
105 '''
106 Print list of DOAP attributes
107
108 @param doap: DOAP in RDF/XML
109 @type doap: text
110
111 @param field: DOAP attribute to be printed
112 @type field: text
113
114 @rtype: text
115 @returns: Field to be printed
116 '''
117
118 text = ""
119 for thing in getattr(doap, field):
120 if isinstance(thing, rdfSubject):
121 text += thing.resUri
122 else:
123
124 thing = thing.strip()
125 text += thing
126 return text
127
129 '''
130 Print single field
131
132 @param doap: DOAP in RDF/XML
133 @type doap: text
134
135 @param field: DOAP attribute to be printed
136 @type field: text
137
138 @rtype: text
139 @returns: Field to be printed
140 '''
141 text = getattr(doap, field)
142 if isinstance(text, rdfSubject):
143 return text.resUri.strip()
144 else:
145 return text.strip()
146
148 '''Prints DOAP repository metadata'''
149 if repo == 'cvs':
150 if hasattr(doap.cvs_repository, field):
151 return getattr(doap.cvs_repository, field)
152
153 if repo == 'svn':
154 if field == 'browse':
155 field = 'svn_browse'
156 if hasattr(doap.svn_repository, field):
157 text = getattr(doap.svn_repository, field)
158 if text:
159 if isinstance(text, rdfSubject):
160 return text.resUri
161 else:
162 return text.strip()
163 return ''
164
166 '''Print people for a particular job '''
167 out = ''
168 if hasattr(doap, job):
169 attribs = getattr(doap, job)
170 if len(attribs) > 0:
171 peeps = []
172 for attr in attribs:
173 if attr[FOAF.mbox] is None:
174 person = "%s" % attr[FOAF.name]
175 else:
176 mbox = attr[FOAF.mbox].resUri
177 if mbox.startswith('mailto:'):
178 mbox = mbox[7:]
179 person = "%s <%s>" % (attr[FOAF.name], mbox)
180 else:
181 LOG.debug("mbox is invalid: %s" % mbox)
182 person = "%s" % attr[FOAF.name]
183 peeps.append(person)
184 out += ", ".join([p for p in peeps])
185 return out
186
187
189 '''Print DOAP package release metadata'''
190 out = ''
191 if hasattr(doap, 'releases') and len(doap.releases) != 0:
192 if not brief:
193 out += COLOR['bold'] + "Releases:" + COLOR['normal'] + '\n'
194 for release in doap.releases:
195 if release.name:
196 out += COLOR['bold'] + COLOR['cyan'] + release.name + \
197 COLOR['normal'] + '\n'
198 if hasattr(release, 'created') and release.created is not None:
199 created = release.created
200 else:
201 created = ''
202 out += COLOR['cyan'] + ' ' + release.revision + ' ' + \
203 COLOR['normal'] + created + '\n'
204 if not brief:
205 if hasattr(release, 'changelog'):
206 if release.changelog:
207 out += COLOR['yellow'] + release.changelog + \
208 COLOR['normal'] + '\n'
209
210 for frel in release.file_releases:
211 out += ' %s' % frel.resUri + '\n'
212 return out
213