doapfiend.doaplib

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, get_n3, DoapPrinter
     18  from doapfiend.model import Project
     19
     20  XMLRPC_SERVER = xmlrpclib.ServerProxy('http://doapspace.org/xmlrpc/')
     21  PKG_INDEX_URI = 'http://doapspace.org/doap'
     22
     23
     24 -def load(doap, format="xml"):
     25      '''
     26      Load a DOAP profile into a 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      Current indexes:
     50
     51         - 'sf' SourceForge
     52         - 'fm' Freshmeat
     53         - 'py' Python Package Index
     54
     55      Raises doaplib.utils.NotFound exception on HTTP 404 error
     56
     57      @param index: Package index two letter abbreviation
     58      @type index: string
     59
     60      @param project_name: project name
     61      @type project_name: string
     62
     63      @param proxy: Optional HTTP proxy URL
     64      @type proxy: string
     65
     66      @rtype: string
     67      @return: text of file retrieved
     68
     69      '''
     70      url = '%s/%s/%s' % (PKG_INDEX_URI, index, project_name)
     71      return fetch_file(url, proxy)
     72
     73
     74 -def query_by_homepage(url):
     75      '''
     76      Get list of URL's for DOAP given a project's homepage.
     77      The list can contain zero or multiple URLs.
     78
     79      The return format is:
     80      [(source, URL), (source, URL)...]
     81
     82      'source' is the two letter package index abbreviation or 'ex' for external.
     83      'external' meaning the DOAP was spidered on the web.
     84      Possible package indexes:
     85
     86      Current indexes:
     87
     88         - 'sf' SourceForge
     89         - 'fm' Freshmeat
     90         - 'py' Python Package Index
     91
     92      @param url: URL of homepage of a project
     93      @type url: string
     94
     95      @rtype: list
     96      @return: A list of tuples containing URLs for DOAP found by homepage
     97
     98      '''
     99      #Should check for env variable for alternate xmplrpc server for testing?
    100      return XMLRPC_SERVER.query_by_homepage(url)
    101
    102
    103 -def display_doap(doap_xml, serialize='text', brief=False):
    104      '''
    105      Print DOAP as text, xml, or n3
    106
    107      @param doap_xml: DOAP profile in RDF/XML
    108      @type doap_xml: string
    109
    110      @param serialize: Serialization syntax
    111      @type serialize: string
    112
    113      @param brief: Only show brief info when serializing as plain text
    114      @type brief: boolean
    115
    116      @return: None on success, 2 on invalid serialization request
    117
    118      '''
    119      if serialize == 'text':
    120          printer = DoapPrinter(load(doap_xml), brief)
    121          printer.print_doap()
    122      elif serialize == 'xml':
    123          print doap_xml
    124      elif serialize == 'n3':
    125          print get_n3(doap_xml)
    126      else:
    127          sys.stderr.write('Unknown serialization requested: %s' % serialize)
    128          return 2
    129
    130
    131 -def fetch_doap(url, proxy=None):
    132      '''
    133      Fetch DOAP by its URL or filename
    134
    135      @param url: URL of DOAP profile in RDF/XML serialization
    136      @type url: string
    137
    138      @rtype: text
    139      @return: DOAP
    140      '''
    141      return fetch_file(url, proxy)
    142