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 #We should be able to get this from model.py automatically, 112 #but this lets us print in the order we like. 113 #Maybe move this to that model.py so we don't forget to sync 114 #when the DOAP schema changes. 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
129 - def print_repos(self):
130 '''Prints DOAP repository metadata''' 131 if hasattr(self.doap.cvs_repository, 'module') and \ 132 self.doap.cvs_repository.module is not None: 133 self.write(misc_field('CVS Module:', 134 self.doap.cvs_repository.module)) 135 self.write(misc_field('CVS Anon:', 136 self.doap.cvs_repository.anon_root)) 137 self.write(misc_field('CVS Browse:', 138 self.doap.cvs_repository.cvs_browse.resUri)) 139 140 if hasattr(self.doap.svn_repository, 'location') and \ 141 self.doap.svn_repository.location is not None: 142 self.write(misc_field('SVN Location:', 143 self.doap.svn_repository.location.resUri)) 144 145 if hasattr(self.doap.svn_repository, 'browse') and \ 146 self.doap.svn_repository.browse is not None: 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') and len(self.doap.releases) != 0: 153 self.write(COLOR['bold'] + "Releases:" + COLOR['normal']) 154 for release in self.doap.releases: 155 if release.name: 156 self.write(COLOR['bold'] + COLOR['cyan'] + release.name + \ 157 COLOR['normal']) 158 self.write(COLOR['cyan'] + ' ' + release.revision + ' ' + \ 159 COLOR['normal'] + release.created) 160 if hasattr(release, 'changelog'): 161 if release.changelog: 162 self.write(COLOR['yellow'] + \ 163 release.changelog + 164 COLOR['normal'] 165 ) 166 for frel in release.file_releases: 167 self.write(' %s' % frel.resUri)
168
169 - def print_people(self):
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 #label = label.ljust(13) 192 self.write(misc_field(label, 193 ", ".join([p for p in peeps])))
194
195 - def print_field(self, name):
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 #Can have multiple values per attribute 216 text = "" 217 for thing in getattr(self.doap, name): 218 if isinstance(thing, rdfSubject): 219 text += thing.resUri + "\n" 220 else: 221 #unicode object 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
236 -def pretty_name(field):
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
258 -def misc_field(label, text):
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