Package doapfiend :: Package plugins :: Module text
[hide private]
[frames] | no frames]

Source Code for Module doapfiend.plugins.text

  1  #!/usr/bin/env python 
  2   
  3  # pylint: disable-msg=W0221,R0201 
  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   
32 -class OutputPlugin(Plugin):
33 34 """Class for formatting DOAP output""" 35 36 #This will be the opt_parser option (--text) 37 name = "text" 38 enabled = False 39 enable_opt = None 40
41 - def __init__(self):
42 '''Setup Plain Text OutputPlugin class''' 43 super(OutputPlugin, self).__init__() 44 self.options = None
45
46 - def add_options(self, parser, output, search):
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
54 - def serialize(self, doap_xml, color=False):
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
74 -class DoapPrinter(object):
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
85 - def write(self, text):
86 ''' 87 Write to DOAP output file object 88 ''' 89 self.text.write(text.encode('utf-8') + '\n')
90
91 - def print_doap(self):
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
109 - def print_misc(self):
110 '''Prints basic DOAP metadata''' 111 #Tuples with boolean to indicate single or multiple values 112 #and line wrap or not 113 #(name, multiple, wrap) 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
133 - def print_repos(self):
134 '''Prints DOAP repository metadata''' 135 if hasattr(self.doap.cvs_repository, 'module') and \ 136 self.doap.cvs_repository.module is not None: 137 self.write(misc_field('CVS Module:', 138 self.doap.cvs_repository.module)) 139 self.write(misc_field('CVS Anon:', 140 self.doap.cvs_repository.anon_root)) 141 self.write(misc_field('CVS Browse:', 142 self.doap.cvs_repository.cvs_browse.resUri)) 143 if hasattr(self.doap.svn_repository, 'location') and \ 144 self.doap.svn_repository.location is not None: 145 self.write(misc_field('SVN Location:', 146 self.doap.svn_repository.location.resUri)) 147 self.write(misc_field('SVN Browse:', 148 self.doap.svn_repository.svn_browse.resUri))
149
150 - def print_releases(self):
151 '''Print DOAP package release metadata''' 152 if hasattr(self.doap, 'releases'): 153 for release in self.doap.releases: 154 self.write(COLOR['bold'] + COLOR['cyan'] + release.name + \ 155 COLOR['normal']) 156 self.write(COLOR['cyan'] + ' ' + release.revision + ' ' + \ 157 COLOR['normal'] + release.created) 158 for frel in release.file_releases: 159 self.write(' %s' % frel.resUri)
160
161 - def print_people(self):
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 #label = label.ljust(13) 184 self.write(misc_field(label, 185 ", ".join([p for p in peeps])))
186
187 - def print_field(self, field):
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 #Can have multiple values per attribute 208 text = "" 209 for thing in getattr(self.doap, name): 210 if isinstance(thing, rdfSubject): 211 text += thing.resUri + "\n" 212 else: 213 #unicode object 214 thing = thing.strip() 215 text += thing + "\n" 216 #text.rstrip() 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
232 -def pretty_name(field):
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
253 -def misc_field(label, text):
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