Package doapfiend :: Module doaplib
[hide private]
[frames] | no frames]

Source Code for Module doapfiend.doaplib

  1  #!/usr/bin/env python 
  2  #pylint: disable-msg=C0103 
  3   
  4  """ 
  5   
  6  Library for parsing, displaying, querying and serializing DOAP 
  7   
  8  """ 
  9   
 10  import sys 
 11  import xmlrpclib 
 12  from cStringIO import StringIO 
 13   
 14  from rdfalchemy import rdfSubject 
 15  from rdflib import ConjunctiveGraph 
 16   
 17  from doapfiend.utils import fetch_file 
 18  from doapfiend.model import Project 
 19  from doapfiend.plugins import load_plugins 
 20   
 21  XMLRPC_SERVER = xmlrpclib.ServerProxy('http://doapspace.org/xmlrpc/') 
 22   
 23   
24 -def load_graph(doap, format="xml"):
25 ''' 26 Load a DOAP profile into a RDFAlchemy/rdflib graph 27 28 Supports any serialization format rdflib can parse (xml, n3, etc.) 29 30 @param doap: DOAP 31 @type doap: string 32 33 @param format: Serialization format we're parsing 34 @type format: string 35 36 @rtype: Project 37 @returns: a Project{rdfSubject} 38 39 ''' 40 rdfSubject.db = ConjunctiveGraph() 41 rdfSubject.db.parse(StringIO(doap), format) 42 return Project.ClassInstances().next()
43 44
45 -def get_by_pkg_index(index, project_name, proxy=None):
46 ''' 47 Get DOAP for a package index project name 48 49 Builtin indexes: 50 51 - 'sf' SourceForge 52 - 'fm' Freshmeat 53 - 'py' Python Package Index 54 55 Note there can be other package indexes available by 56 third party plugins. 57 58 @param index: Package index two letter abbreviation 59 @type index: string 60 61 @param project_name: project name 62 @type project_name: string 63 64 @param proxy: Optional HTTP proxy URL 65 @type proxy: string 66 67 @rtype: string 68 @return: text of file retrieved 69 70 ''' 71 for plugin_obj in list(load_plugins()): 72 plugin = plugin_obj() 73 if hasattr(plugin, 'prefix'): 74 if plugin.prefix == index: 75 plugin.query = project_name 76 return plugin.search(proxy)
77 78
79 -def query_by_homepage(url):
80 ''' 81 Get list of URL's for DOAP given a project's homepage. 82 The list can contain zero or multiple URLs. 83 84 The return format is: 85 [(source, URL), (source, URL)...] 86 87 'source' is the two letter package index abbreviation or 'ex' for external. 88 'external' meaning the DOAP was spidered on the web. 89 Possible package indexes: 90 91 Current indexes: 92 93 - 'sf' SourceForge 94 - 'fm' Freshmeat 95 - 'py' Python Package Index 96 97 @param url: URL of homepage of a project 98 @type url: string 99 100 @rtype: list 101 @return: A list of tuples containing URLs for DOAP found by homepage 102 103 ''' 104 #Should check for env variable for alternate xmplrpc server for testing? 105 return XMLRPC_SERVER.query_by_homepage(url)
106 107 146 147
148 -def get_serializer(format):
149 ''' 150 Return a serializer instance given its name 151 152 @param format: Name of serializer 153 @type format: string 154 155 @rtype: function 156 @returns: Instance of a serializer 157 ''' 158 #Get all plugins with a `serialize` method 159 for plugin_obj in get_plugin('serialize'): 160 plugin = plugin_obj() 161 if plugin.name == format: 162 return plugin.serialize
163 164
165 -def get_plugin(method):
166 """ 167 Return plugin object if `method` exists 168 169 @param method: name of plugin's method we're calling 170 @type method: string 171 172 @returns: list of plugins with `method` 173 174 """ 175 all_plugins = [] 176 for plugin in load_plugins(): 177 #plugin().configure(None, None) 178 if not hasattr(plugin, method): 179 plugin = None 180 else: 181 all_plugins.append(plugin) 182 return all_plugins
183 184
185 -def fetch_doap(url, proxy=None):
186 ''' 187 Fetch DOAP by its URL or filename 188 189 @param url: URL of DOAP profile in RDF/XML serialization 190 @type url: string 191 192 @rtype: text 193 @return: DOAP 194 ''' 195 return fetch_file(url, proxy)
196