1
2
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
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
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
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
105 return XMLRPC_SERVER.query_by_homepage(url)
106
107
108 -def print_doap(doap_xml, color=None, format='text', serializer=None,
109 filename=None):
110 '''
111 Print DOAP as text, xml, or n3 etc. or to stdout or a file
112 A callable serializer object may be passed or a name of a serializer
113 plugin.
114
115 @param doap_xml: DOAP profile in RDF/XML
116 @type doap_xml: string
117
118 @param format: Serialization syntax formatter name
119 @type format: string
120
121 @param serializer: Instance of a serializer
122 @type serializer: callable
123
124 @param filename: Optional filename to write to
125 @type filename: string
126
127 @return: `serializer` or 1 if invalid serialization request
128
129 '''
130
131
132 if not serializer:
133 serializer = get_serializer(format)
134 if not serializer:
135 sys.stderr.write('Unknown serialization requested: %s\n' % format)
136 return 1
137
138 doap = serializer(doap_xml, color)
139 if filename:
140 try:
141 open(filename, 'w').write(doap.encode('utf-8'))
142 except UnicodeDecodeError:
143 open(filename, 'w').write(doap)
144 else:
145 print doap
146
147
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
159 for plugin_obj in get_plugin('serialize'):
160 plugin = plugin_obj()
161 if plugin.name == format:
162 return plugin.serialize
163
164
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
178 if not hasattr(plugin, method):
179 plugin = None
180 else:
181 all_plugins.append(plugin)
182 return all_plugins
183
184
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